e0/lib/parser.mly
Michael Zhang e66b515826
Parsing
2022-03-08 02:52:20 -06:00

40 lines
673 B
OCaml

%{
open Ast
%}
%token EOF
%token KWD_FN
%token KWD_RETURN
%token PUNCT_LPAREN
%token PUNCT_RPAREN
%token PUNCT_RARROW
%token PUNCT_LBRACE
%token PUNCT_RBRACE
%token PUNCT_COLON
%token PUNCT_SEMI
%token <int> LIT_NEG_INT
%token <int> LIT_INT
%token <string> IDENT
%start <program> program
%%
program:
| decls=decl* EOF { decls }
decl:
| func=func { DeclFunc func }
func:
| KWD_FN name=IDENT PUNCT_LPAREN PUNCT_RPAREN body=body { { name; body } }
body:
| PUNCT_LBRACE stmts=stmt* PUNCT_RBRACE { { stmts; ret=ExprUnit } }
stmt:
| KWD_RETURN expr=expr PUNCT_SEMI { StmtReturn expr }
expr:
| i=LIT_NEG_INT { ExprLit (LitNegInt i) }
| i=LIT_INT { ExprLit (LitInt i) }