v5 Parsing: modules, imports, and member access
Lesson, slides, and applied problem sets.
View SlidesLesson
v5 Parsing: modules, imports, and member access
New grammar
program -> moduleDecl* | stmts (implicit main)
moduleDecl -> "module" IDENT "{" moduleItem* "}"
moduleItem -> importStmt | exportDecl | stmt
importStmt -> "import" IDENT ("as" IDENT)? ";"
exportDecl -> "export" (fnDecl | varDecl)
postfix -> primary (call | index | member)*
member -> "." IDENT
Design notes
- If the file has no
modulekeyword, wrap all statements intomodule main. exportis only allowed at module top level.- Member access is parsed like calls and indexing so chaining works:
m.add(1)[0].
AST additions
Program.Modules []*ModuleModule{Name, Imports, Stmts}Import{Name, Alias}ExprMemberfora.b(Left + member name)StmtgainsExported bool