86 lines
2.4 KiB
OCaml
86 lines
2.4 KiB
OCaml
%{
|
|
open Syntax
|
|
|
|
let here () = Location.span_of_lexing (Parsing.symbol_start_pos ()) (Parsing.symbol_end_pos ())
|
|
%}
|
|
|
|
%token <int> INT
|
|
%token <bool> BOOL
|
|
%token <string> STRING
|
|
%token <string> IDENT
|
|
%token LET IN FUN IF THEN ELSE
|
|
%token ARROW PIPE AND OR
|
|
%token EQ NE LT LE GT GE
|
|
%token PLUS MINUS STAR SLASH
|
|
%token LPAREN RPAREN COMMA DOT LBRACE RBRACE COLON SEMI
|
|
%token EOF
|
|
|
|
%start expr
|
|
%type <Syntax.expr> expr
|
|
|
|
%%
|
|
|
|
expr:
|
|
| pipeline { $1 }
|
|
|
|
pipeline:
|
|
| disjunction { $1 }
|
|
| pipeline PIPE disjunction { make (EApp ($3, $1)) (here ()) }
|
|
|
|
disjunction:
|
|
| conjunction { $1 }
|
|
| disjunction OR conjunction { make (EBinop (Or, $1, $3)) (here ()) }
|
|
|
|
conjunction:
|
|
| comparison { $1 }
|
|
| conjunction AND comparison { make (EBinop (And, $1, $3)) (here ()) }
|
|
|
|
comparison:
|
|
| sum { $1 }
|
|
| sum EQ sum { make (EBinop (Eq, $1, $3)) (here ()) }
|
|
| sum NE sum { make (EBinop (Ne, $1, $3)) (here ()) }
|
|
| sum LT sum { make (EBinop (Lt, $1, $3)) (here ()) }
|
|
| sum LE sum { make (EBinop (Le, $1, $3)) (here ()) }
|
|
| sum GT sum { make (EBinop (Gt, $1, $3)) (here ()) }
|
|
| sum GE sum { make (EBinop (Ge, $1, $3)) (here ()) }
|
|
|
|
sum:
|
|
| product { $1 }
|
|
| sum PLUS product { make (EBinop (Add, $1, $3)) (here ()) }
|
|
| sum MINUS product { make (EBinop (Sub, $1, $3)) (here ()) }
|
|
|
|
product:
|
|
| unary { $1 }
|
|
| product STAR unary { make (EBinop (Mul, $1, $3)) (here ()) }
|
|
| product SLASH unary { make (EBinop (Div, $1, $3)) (here ()) }
|
|
|
|
unary:
|
|
| application { $1 }
|
|
| MINUS unary { make (ENeg $2) (here ()) }
|
|
|
|
application:
|
|
| atom { $1 }
|
|
| application atom { make (EApp ($1, $2)) (here ()) }
|
|
|
|
atom:
|
|
| INT { make (EInt $1) (here ()) }
|
|
| BOOL { make (EBool $1) (here ()) }
|
|
| STRING { make (EString $1) (here ()) }
|
|
| IDENT { make (EVar $1) (here ()) }
|
|
| LPAREN RPAREN { make EUnit (here ()) }
|
|
| LPAREN expr RPAREN { $2 }
|
|
| LPAREN expr COMMA expr_list RPAREN { make (ETuple ($2 :: $4)) (here ()) }
|
|
| LBRACE record_fields RBRACE { make (ERecord $2) (here ()) }
|
|
| atom DOT IDENT { make (EField ($1, $3)) (here ()) }
|
|
| LET IDENT EQ expr IN expr { make (ELet ($2, $4, $6)) (here ()) }
|
|
| FUN IDENT ARROW expr { make (ELambda ($2, $4)) (here ()) }
|
|
| IF expr THEN expr ELSE expr { make (EIf ($2, $4, $6)) (here ()) }
|
|
|
|
expr_list:
|
|
| expr { [ $1 ] }
|
|
| expr COMMA expr_list { $1 :: $3 }
|
|
|
|
record_fields:
|
|
| IDENT EQ expr { [ ($1, $3) ] }
|
|
| IDENT EQ expr SEMI record_fields { ($1, $3) :: $5 }
|