e0/lib/parser.mly

41 lines
673 B
OCaml
Raw Normal View History

2022-03-08 06:16:27 +00:00
%{
open Ast
%}
2022-03-08 08:52:20 +00:00
%token EOF
2022-03-08 06:16:27 +00:00
%token KWD_FN
2022-03-08 08:52:20 +00:00
%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
2022-03-08 06:16:27 +00:00
%token <string> IDENT
%start <program> program
%%
program:
2022-03-08 08:52:20 +00:00
| decls=decl* EOF { decls }
2022-03-08 06:16:27 +00:00
decl:
| func=func { DeclFunc func }
func:
2022-03-08 08:52:20 +00:00
| 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) }