Program Parser
Program Parser
Parse a token stream into an AST for the expanded language (v2).
You will receive a token slice (including EOF). Your job is to build a Program that contains top-level declarations and statements.
Function signature
func Parse(tokens []Token) (*Program, error)
Grammar
program -> decl* EOF
decl -> funDecl | stmt
funDecl -> "fn" IDENT "(" params? ")" block
params -> IDENT ("," IDENT)*
stmt -> varDecl
| ifStmt
| whileStmt
| returnStmt
| block
| assignStmt
| exprStmt
varDecl -> ("let" | "var" | "const") IDENT ("=" expr)? ";"
assignStmt -> IDENT "=" expr ";"
exprStmt -> expr ";"
block -> "{" stmt* "}"
ifStmt -> "if" "(" expr ")" block ("else" block)?
whileStmt -> "while" "(" expr ")" block
returnStmt -> "return" expr? ";"
Expression grammar is the same as earlier modules:
expr -> equality
equality -> comparison ( ("==" | "!=") comparison )*
comparison -> term ( ("<" | "<=" | ">" | ">=") term )*
term -> factor ( ("+" | "-") factor )*
factor -> unary ( ("*" | "/") unary )*
unary -> ("!" | "-") unary | primary
primary -> NUMBER | STRING | TRUE | FALSE | NIL | IDENT | "(" expr ")" | call
call -> IDENT "(" args? ")"
args -> expr ( "," expr )*
AST shape
Use the provided structs:
ProgramwithStmts []*StmtStmtwith aKindenum and fields:Name,Expr,Body,Else,Params,VarKindExprwith aKindenum and fields:Value,Left,Right,Args
Rules
- Function declarations are top-level only (not inside blocks).
- Assignment statements are only of the form
IDENT = expr;.
Errors
Return a non-nil error if:
- tokens do not match the grammar
- parentheses or braces are unbalanced
- extra tokens remain after parsing
Notes
- Keep the parser readable. A simple recursive descent is perfect.
Run tests to see results
No issues detected