2022-12-07 16:45:55 +01:00
|
|
|
%{
|
2022-12-09 14:26:28 +01:00
|
|
|
exception MyParsingError of string
|
|
|
|
|
2022-12-07 16:45:55 +01:00
|
|
|
let current_location () = symbol_start_pos (), symbol_end_pos ()
|
2022-12-09 14:26:28 +01:00
|
|
|
|
|
|
|
let defined_nodes : (Ast.ident, Ast.t_node) Hashtbl.t = Hashtbl.create 100
|
|
|
|
|
|
|
|
let defined_vars : (Ast.ident, Ast.t_var) Hashtbl.t = Hashtbl.create 100
|
|
|
|
|
|
|
|
let fetch_node (n: Ast.ident) =
|
|
|
|
match Hashtbl.find_opt defined_nodes n with
|
|
|
|
| None ->
|
|
|
|
raise (MyParsingError
|
|
|
|
("The node %s does not exist."))
|
|
|
|
| Some node -> node
|
|
|
|
|
|
|
|
let fetch_var (n: Ast.ident) =
|
|
|
|
match Hashtbl.find_opt defined_vars n with
|
|
|
|
| None ->
|
|
|
|
raise (MyParsingError
|
|
|
|
("The var %s does not exist."))
|
|
|
|
| Some var -> var
|
|
|
|
|
2022-12-07 16:45:55 +01:00
|
|
|
%}
|
|
|
|
|
|
|
|
%token EOF
|
|
|
|
%token<string> IDENT
|
|
|
|
%token LPAREN
|
|
|
|
%token RPAREN
|
|
|
|
%token RETURNS
|
|
|
|
%token SEMICOL
|
|
|
|
%token COLON
|
|
|
|
%token BOOL
|
|
|
|
%token INT
|
|
|
|
%token LET
|
|
|
|
%token TEL
|
|
|
|
%token NODE
|
|
|
|
%token VAR
|
|
|
|
%token EQUAL
|
|
|
|
%token COMMA
|
2022-12-09 14:26:28 +01:00
|
|
|
%token<Ast.base_ty> TYP
|
2022-12-07 21:56:38 +01:00
|
|
|
|
|
|
|
%token MO_not
|
2022-12-09 14:26:28 +01:00
|
|
|
%token MO_pre
|
|
|
|
%token PLUS
|
|
|
|
%token MINUS
|
2022-12-07 21:56:38 +01:00
|
|
|
%token BO_and
|
|
|
|
%token BO_or
|
|
|
|
%token BO_mul
|
|
|
|
%token BO_div
|
2022-12-09 14:26:28 +01:00
|
|
|
%token BO_mod
|
|
|
|
%token BO_arrow
|
|
|
|
%token CMP_le
|
|
|
|
%token CMP_lt
|
|
|
|
%token CMP_ge
|
|
|
|
%token CMP_gt
|
|
|
|
%token CMP_neq
|
|
|
|
%token TO_merge
|
2022-12-07 21:56:38 +01:00
|
|
|
|
2022-12-09 14:26:28 +01:00
|
|
|
%token WHEN
|
2022-12-07 21:56:38 +01:00
|
|
|
|
|
|
|
%token IF
|
|
|
|
%token THEN
|
|
|
|
%token ELSE
|
|
|
|
|
|
|
|
%token<int> CONST_INT
|
|
|
|
%token<bool> CONST_BOOL
|
2022-12-09 14:26:28 +01:00
|
|
|
%token<Ast.real> CONST_REAL
|
2022-12-07 16:45:55 +01:00
|
|
|
|
|
|
|
/* The Entry Point */
|
|
|
|
%start main
|
2022-12-09 14:26:28 +01:00
|
|
|
%type <Ast.t_nodelist> main
|
2022-12-07 16:45:55 +01:00
|
|
|
|
|
|
|
%%
|
|
|
|
|
|
|
|
main: nodes EOF { $1 };
|
|
|
|
|
|
|
|
nodes:
|
|
|
|
| /* empty */ { [] }
|
|
|
|
| node nodes { $1 :: $2 };
|
|
|
|
|
|
|
|
node:
|
2022-12-09 14:26:28 +01:00
|
|
|
NODE node_content { (* Flush known variables *) Hashtbl.clear defined_vars; $2 }
|
|
|
|
|
|
|
|
node_content:
|
|
|
|
IDENT LPAREN in_params RPAREN
|
2022-12-07 16:45:55 +01:00
|
|
|
RETURNS LPAREN out_params RPAREN SEMICOL
|
|
|
|
local_params
|
|
|
|
LET equations TEL
|
2022-12-09 14:26:28 +01:00
|
|
|
{ { n_name = $1;
|
|
|
|
n_inputs = $3;
|
|
|
|
n_outputs = $7;
|
|
|
|
n_local_vars = $10;
|
|
|
|
n_equations = $12; }
|
2022-12-07 16:45:55 +01:00
|
|
|
} ;
|
|
|
|
|
|
|
|
in_params:
|
|
|
|
| /* empty */ { [] }
|
|
|
|
| param_list { $1 }
|
2022-12-09 14:26:28 +01:00
|
|
|
;
|
2022-12-07 16:45:55 +01:00
|
|
|
|
|
|
|
out_params: param_list { $1 } ;
|
|
|
|
|
|
|
|
local_params:
|
|
|
|
| /* empty */ { [] }
|
|
|
|
| VAR param_list_semicol { $2 }
|
|
|
|
;
|
|
|
|
|
|
|
|
param_list_semicol:
|
2022-12-09 14:26:28 +01:00
|
|
|
| param_list SEMICOL { $1 }
|
2022-12-07 16:45:55 +01:00
|
|
|
|
|
|
|
param_list:
|
|
|
|
| param { $1 }
|
|
|
|
| param SEMICOL param_list { $1 @ $3 }
|
|
|
|
;
|
|
|
|
|
|
|
|
param:
|
2022-12-09 14:26:28 +01:00
|
|
|
ident_comma_list COLON TYP
|
|
|
|
{ let typ = $3 in
|
|
|
|
let idents = $1 in
|
|
|
|
Ast.(
|
|
|
|
match typ with
|
|
|
|
| TBool ->
|
|
|
|
List.map (fun s -> Hashtbl.add defined_vars s (BVar s); BVar s) idents
|
|
|
|
| TReal ->
|
|
|
|
List.map (fun s -> Hashtbl.add defined_vars s (RVar s); RVar s) idents
|
|
|
|
| TInt ->
|
|
|
|
List.map (fun s -> Hashtbl.add defined_vars s (IVar s); IVar s) idents) }
|
2022-12-07 16:45:55 +01:00
|
|
|
;
|
|
|
|
|
2022-12-09 14:26:28 +01:00
|
|
|
ident_comma_list:
|
|
|
|
| IDENT { [$1] }
|
|
|
|
| IDENT COMMA ident_comma_list { $1 :: $3 }
|
|
|
|
|
2022-12-07 16:45:55 +01:00
|
|
|
equations:
|
|
|
|
| /* empty */ { [] }
|
|
|
|
| equation SEMICOL equations
|
|
|
|
{ $1 :: $3 }
|
|
|
|
;
|
|
|
|
|
|
|
|
equation:
|
|
|
|
pattern EQUAL expr
|
2022-12-09 14:26:28 +01:00
|
|
|
{ ($1, $3) }
|
2022-12-07 16:45:55 +01:00
|
|
|
;
|
|
|
|
|
2022-12-07 21:56:38 +01:00
|
|
|
pattern:
|
2022-12-09 14:26:28 +01:00
|
|
|
| IDENT { [fetch_var $1] }
|
|
|
|
| LPAREN ident_comma_list_patt RPAREN { $2 };
|
2022-12-07 21:56:38 +01:00
|
|
|
|
2022-12-09 14:26:28 +01:00
|
|
|
ident_comma_list_patt:
|
|
|
|
| IDENT { [fetch_var $1] }
|
|
|
|
| IDENT COMMA ident_comma_list_patt { (fetch_var $1) :: $3 }
|
2022-12-07 21:56:38 +01:00
|
|
|
|
|
|
|
expr:
|
2022-12-09 14:26:28 +01:00
|
|
|
/* Note: EQUAL does not follow the nomenclature CMP_, ... */
|
2022-12-07 21:56:38 +01:00
|
|
|
| LPAREN expr RPAREN { $2 }
|
2022-12-09 14:26:28 +01:00
|
|
|
| IDENT { EVar (fetch_var $1) }
|
|
|
|
/* Unary operators */
|
|
|
|
| MO_not expr { EMonOp (MOp_not, $2) }
|
|
|
|
| MO_pre expr { EMonOp (MOp_pre, $2) }
|
|
|
|
| MINUS expr { EMonOp (MOp_minus, $2) }
|
|
|
|
| PLUS expr { $2 }
|
|
|
|
/* Binary operators */
|
|
|
|
| expr PLUS expr { EBinOp (BOp_add, $1, $3) }
|
|
|
|
| expr MINUS expr { EBinOp (BOp_sub, $1, $3) }
|
|
|
|
| expr BO_mul expr { EBinOp (BOp_mul, $1, $3) }
|
|
|
|
| expr BO_div expr { EBinOp (BOp_div, $1, $3) }
|
|
|
|
| expr BO_mod expr { EBinOp (BOp_mod, $1, $3) }
|
|
|
|
| expr BO_and expr { EBinOp (BOp_and, $1, $3) }
|
|
|
|
| expr BO_or expr { EBinOp (BOp_or, $1, $3) }
|
|
|
|
| expr BO_arrow expr { EBinOp (BOp_arrow, $1, $3) }
|
|
|
|
/* Comparison operators */
|
|
|
|
| expr EQUAL expr { EComp (COp_eq, $1, $3) }
|
|
|
|
| expr CMP_neq expr { EComp (COp_neq, $1, $3) }
|
|
|
|
| expr CMP_le expr { EComp (COp_le, $1, $3) }
|
|
|
|
| expr CMP_lt expr { EComp (COp_lt, $1, $3) }
|
|
|
|
| expr CMP_ge expr { EComp (COp_ge, $1, $3) }
|
|
|
|
| expr CMP_gt expr { EComp (COp_gt, $1, $3) }
|
|
|
|
/* Tertiary operators */
|
|
|
|
| IF expr THEN expr ELSE expr { ETriOp (TOp_if, $2, $4, $6) }
|
|
|
|
| TO_merge expr expr expr { ETriOp (TOp_merge, $2, $3, $4) }
|
|
|
|
/* When is neither a binop (a * 'a -> 'a) or a comp ('a * 'a -> bool) */
|
|
|
|
| WHEN expr expr { EWhen ($2, $3) }
|
|
|
|
/* Constants */
|
|
|
|
| CONST_INT { EConst (CInt $1) }
|
|
|
|
| CONST_BOOL { EConst (CBool $1) }
|
|
|
|
| CONST_REAL { EConst (CReal $1) }
|
|
|
|
/* Tuples */
|
|
|
|
| LPAREN expr_comma_list RPAREN { $2 }
|
|
|
|
/* Applications */
|
|
|
|
| IDENT LPAREN expr_comma_list RPAREN
|
|
|
|
{ let name = $1 in
|
|
|
|
let args = $3 in
|
|
|
|
EApp (fetch_node name, args) }
|
2022-12-07 21:56:38 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
expr_comma_list:
|
2022-12-09 14:26:28 +01:00
|
|
|
| expr
|
|
|
|
{ let e = $1 in
|
|
|
|
match e with
|
|
|
|
| ETuple _ -> e
|
|
|
|
| _ -> ETuple [e] }
|
|
|
|
| expr COMMA expr_comma_list
|
|
|
|
{ let e = $1 in
|
|
|
|
let le = $3 in
|
|
|
|
match e, le with
|
|
|
|
| ETuple t, ETuple t' -> ETuple (t @ t')
|
|
|
|
| _, ETuple t' -> ETuple (e :: t')
|
|
|
|
| _, _ -> raise (MyParsingError "This exception should not have been \
|
|
|
|
raised.") }
|
2022-12-07 16:45:55 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
ident_comma_list:
|
|
|
|
| IDENT { [$1] }
|
|
|
|
| IDENT COMMA ident_comma_list { $1 :: $3 }
|
|
|
|
;
|
|
|
|
|