50 lines
865 B
OCaml
50 lines
865 B
OCaml
%{
|
|
open Ast
|
|
%}
|
|
|
|
%token EOF
|
|
|
|
%token KWD_EXTERN
|
|
%token KWD_FN
|
|
%token KWD_RETURN
|
|
|
|
%token PUNCT_COLON
|
|
%token PUNCT_LBRACE
|
|
%token PUNCT_LPAREN
|
|
%token PUNCT_RARROW
|
|
%token PUNCT_RBRACE
|
|
%token PUNCT_RPAREN
|
|
%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 }
|
|
| extern=extern_func { DeclExternFunc extern }
|
|
|
|
func:
|
|
| KWD_FN name=IDENT PUNCT_LPAREN PUNCT_RPAREN body=body { { name; body } }
|
|
|
|
extern_func:
|
|
| KWD_EXTERN KWD_FN name=IDENT PUNCT_LPAREN PUNCT_RPAREN PUNCT_SEMI { { name } }
|
|
|
|
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) }
|
|
|
|
(* vim: set ft=ocamlyacc *)
|