Program Parser v4

hard · compilers, parsing, ast, control flow

Program Parser v4

Parse tokens into an AST for the v4 language (for loops, break/continue, && / ||).

Function signature

func Parse(tokens []Token) (*Program, error)

Grammar

program     -> stmt* EOF

stmt        -> varDecl
            | ifStmt
            | whileStmt
            | forStmt
            | breakStmt
            | continueStmt
            | returnStmt
            | block
            | fnDecl
            | assignOrExpr

fnDecl      -> "fn" IDENT "(" params? ")" block
params      -> IDENT ("," IDENT)*

varDecl     -> ("let" | "var" | "const") IDENT ("=" expr)? ";"
assignOrExpr-> expr ("=" expr)? ";"
block       -> "{" stmt* "}"

ifStmt      -> "if" "(" expr ")" block ("else" block)?
whileStmt   -> "while" "(" expr ")" block
forStmt     -> "for" "(" forInit? ";" expr? ";" forPost? ")" block
forInit     -> varDeclNoSemi | assignOrExprNoSemi
forPost     -> assignOrExprNoSemi
breakStmt   -> "break" ";"
continueStmt-> "continue" ";"
returnStmt  -> "return" expr? ";"

expr        -> or
or          -> and ("||" and)*
and         -> equality ("&&" equality)*

equality    -> comparison ( ("==" | "!=") comparison )*
comparison  -> term ( ("<" | "<=" | ">" | ">=") term )*
term        -> factor ( ("+" | "-") factor )*
factor      -> unary ( ("*" | "/") unary )*

unary       -> ("!" | "-") unary | postfix
postfix     -> primary ("(" args? ")" | "[" expr "]")*
args        -> expr ("," expr)*

primary     -> NUMBER | STRING | TRUE | FALSE | NIL | IDENT
            | "(" expr ")"
            | array
            | map

array       -> "[" (expr ("," expr)*)? "]"
map         -> "{" (pair ("," pair)*)? "}"
pair        -> expr ":" expr

AST shape

Use the provided structs:

  • Program with Stmts []*Stmt
  • Stmt with fields: Kind, VarKind, Name, Expr, Target, Body, Else, Params, Init, Post, Cond
  • Expr with fields: Kind, Value, Left, Right, Args, Elems, Pairs
  • MapPair with Key, Value

Rules

  • Functions are statements and can appear in any block.
  • An assignment statement must have a valid target: identifier or index expression.

Errors

Return a non-nil error if:

  • tokens do not match the grammar
  • parentheses or braces are unbalanced
  • an assignment target is not valid
  • extra tokens remain after parsing

Notes

  • Keep the parser readable. A simple recursive descent is perfect.
Run tests to see results
No issues detected