v5 Parsing: modules, imports, and member access

Lesson, slides, and applied problem sets.

View Slides

Lesson

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 module keyword, wrap all statements into module main.
  • export is 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 []*Module
  • Module{Name, Imports, Stmts}
  • Import{Name, Alias}
  • ExprMember for a.b (Left + member name)
  • Stmt gains Exported bool

Module Items