From abbebbf0f6fd69c9a86488469bed78689422be80 Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Wed, 20 Mar 2019 21:36:12 +0100 Subject: [PATCH] Refactoring of the AST. --- AST.ml | 126 ++++++++++++++++++++++++++--------------------------- AST.mli | 44 +++++++++---------- Parser.mly | 92 +++++++++++++++++++------------------- 3 files changed, 131 insertions(+), 131 deletions(-) diff --git a/AST.ml b/AST.ml index 7eefad490..9ee644047 100644 --- a/AST.ml +++ b/AST.ml @@ -155,9 +155,9 @@ type t = { and ast = t and declaration = - TypeDecl of type_decl reg -| ConstDecl of const_decl reg -| LambdaDecl of lambda_decl + TypeDecl of type_decl reg +| ConstDecl of const_decl reg +| LambdaDecl of lambda_decl and const_decl = { kwd_const : kwd_const; @@ -180,11 +180,11 @@ and type_decl = { } and type_expr = - Prod of cartesian -| Sum of (variant reg, vbar) nsepseq reg -| Record of record_type reg -| TypeApp of (type_name * type_tuple) reg -| ParType of type_expr par reg + TProd of cartesian +| TSum of (variant reg, vbar) nsepseq reg +| TRecord of record_type reg +| TApp of (type_name * type_tuple) reg +| TPar of type_expr par reg | TAlias of variable and cartesian = (type_expr, times) nsepseq reg @@ -443,20 +443,20 @@ and for_collect = { (* Expressions *) and expr = - LogicExpr of logic_expr -| ArithExpr of arith_expr -| StringExpr of string_expr -| ListExpr of list_expr -| SetExpr of set_expr -| ConstrExpr of constr_expr -| RecordExpr of record_expr -| MapExpr of map_expr -| Var of Lexer.lexeme reg -| FunCall of fun_call -| Bytes of (Lexer.lexeme * Hex.t) reg -| Unit of c_Unit -| Tuple of tuple -| ParExpr of expr par reg + ELogic of logic_expr +| EArith of arith_expr +| EString of string_expr +| EList of list_expr +| ESet of set_expr +| EConstr of constr_expr +| ERecord of record_expr +| EMap of map_expr +| EVar of Lexer.lexeme reg +| ECall of fun_call +| EBytes of (Lexer.lexeme * Hex.t) reg +| EUnit of c_Unit +| ETuple of tuple +| EPar of expr par reg and map_expr = MapLookUp of map_lookup reg @@ -609,28 +609,28 @@ and list_pattern = open! Region let type_expr_to_region = function - Prod {region; _} -| Sum {region; _} -| Record {region; _} -| TypeApp {region; _} -| ParType {region; _} + TProd {region; _} +| TSum {region; _} +| TRecord {region; _} +| TApp {region; _} +| TPar {region; _} | TAlias {region; _} -> region let rec expr_to_region = function - LogicExpr e -> logic_expr_to_region e -| ArithExpr e -> arith_expr_to_region e -| StringExpr e -> string_expr_to_region e -| ListExpr e -> list_expr_to_region e -| SetExpr e -> set_expr_to_region e -| ConstrExpr e -> constr_expr_to_region e -| RecordExpr e -> record_expr_to_region e -| MapExpr e -> map_expr_to_region e -| Var {region; _} -| FunCall {region; _} -| Bytes {region; _} -| Unit region -| Tuple {region; _} -| ParExpr {region; _} -> region + ELogic e -> logic_expr_to_region e +| EArith e -> arith_expr_to_region e +| EString e -> string_expr_to_region e +| EList e -> list_expr_to_region e +| ESet e -> set_expr_to_region e +| EConstr e -> constr_expr_to_region e +| ERecord e -> record_expr_to_region e +| EMap e -> map_expr_to_region e +| EVar {region; _} +| ECall {region; _} +| EBytes {region; _} +| EUnit region +| ETuple {region; _} +| EPar {region; _} -> region and map_expr_to_region = function MapLookUp {region; _} @@ -788,9 +788,9 @@ let rec print_tokens ast = print_token eof "EOF" and print_decl = function - TypeDecl decl -> print_type_decl decl -| ConstDecl decl -> print_const_decl decl -| LambdaDecl decl -> print_lambda_decl decl + TypeDecl decl -> print_type_decl decl +| ConstDecl decl -> print_const_decl decl +| LambdaDecl decl -> print_lambda_decl decl and print_const_decl {value; _} = let {kwd_const; name; colon; const_type; @@ -813,11 +813,11 @@ and print_type_decl {value; _} = print_terminator terminator and print_type_expr = function - Prod cartesian -> print_cartesian cartesian -| Sum sum_type -> print_sum_type sum_type -| Record record_type -> print_record_type record_type -| TypeApp type_app -> print_type_app type_app -| ParType par_type -> print_par_type par_type + TProd cartesian -> print_cartesian cartesian +| TSum sum_type -> print_sum_type sum_type +| TRecord record_type -> print_record_type record_type +| TApp type_app -> print_type_app type_app +| TPar par_type -> print_par_type par_type | TAlias type_alias -> print_var type_alias and print_cartesian {value; _} = @@ -1106,20 +1106,20 @@ and print_bind_to = function | None -> () and print_expr = function - LogicExpr e -> print_logic_expr e -| ArithExpr e -> print_arith_expr e -| StringExpr e -> print_string_expr e -| ListExpr e -> print_list_expr e -| SetExpr e -> print_set_expr e -| ConstrExpr e -> print_constr_expr e -| RecordExpr e -> print_record_expr e -| MapExpr e -> print_map_expr e -| Var var -> print_var var -| FunCall e -> print_fun_call e -| Bytes b -> print_bytes b -| Unit region -> print_token region "Unit" -| Tuple e -> print_tuple e -| ParExpr e -> print_par_expr e + ELogic e -> print_logic_expr e +| EArith e -> print_arith_expr e +| EString e -> print_string_expr e +| EList e -> print_list_expr e +| ESet e -> print_set_expr e +| EConstr e -> print_constr_expr e +| ERecord e -> print_record_expr e +| EMap e -> print_map_expr e +| EVar v -> print_var v +| ECall e -> print_fun_call e +| EBytes b -> print_bytes b +| EUnit r -> print_token r "Unit" +| ETuple e -> print_tuple e +| EPar e -> print_par_expr e and print_map_expr = function MapLookUp {value; _} -> print_map_lookup value diff --git a/AST.mli b/AST.mli index d1b02e081..5a7777499 100644 --- a/AST.mli +++ b/AST.mli @@ -139,9 +139,9 @@ type t = { and ast = t and declaration = - TypeDecl of type_decl reg -| ConstDecl of const_decl reg -| LambdaDecl of lambda_decl + TypeDecl of type_decl reg +| ConstDecl of const_decl reg +| LambdaDecl of lambda_decl and const_decl = { kwd_const : kwd_const; @@ -164,11 +164,11 @@ and type_decl = { } and type_expr = - Prod of cartesian -| Sum of (variant reg, vbar) nsepseq reg -| Record of record_type reg -| TypeApp of (type_name * type_tuple) reg -| ParType of type_expr par reg + TProd of cartesian +| TSum of (variant reg, vbar) nsepseq reg +| TRecord of record_type reg +| TApp of (type_name * type_tuple) reg +| TPar of type_expr par reg | TAlias of variable and cartesian = (type_expr, times) nsepseq reg @@ -427,20 +427,20 @@ and for_collect = { (* Expressions *) and expr = - LogicExpr of logic_expr -| ArithExpr of arith_expr -| StringExpr of string_expr -| ListExpr of list_expr -| SetExpr of set_expr -| ConstrExpr of constr_expr -| RecordExpr of record_expr -| MapExpr of map_expr -| Var of Lexer.lexeme reg -| FunCall of fun_call -| Bytes of (Lexer.lexeme * Hex.t) reg -| Unit of c_Unit -| Tuple of tuple -| ParExpr of expr par reg + ELogic of logic_expr +| EArith of arith_expr +| EString of string_expr +| EList of list_expr +| ESet of set_expr +| EConstr of constr_expr +| ERecord of record_expr +| EMap of map_expr +| EVar of Lexer.lexeme reg +| ECall of fun_call +| EBytes of (Lexer.lexeme * Hex.t) reg +| EUnit of c_Unit +| ETuple of tuple +| EPar of expr par reg and map_expr = MapLookUp of map_lookup reg diff --git a/Parser.mly b/Parser.mly index 0a08a89a6..4d42d57e9 100644 --- a/Parser.mly +++ b/Parser.mly @@ -157,9 +157,9 @@ type_decl: in {region; value}} type_expr: - cartesian { Prod $1 } -| sum_type { Sum $1 } -| record_type { Record $1 } + cartesian { TProd $1 } +| sum_type { TSum $1 } +| record_type { TRecord $1 } cartesian: nsepseq(core_type,TIMES) { @@ -173,15 +173,15 @@ core_type: } | type_name type_tuple { let region = cover $1.region $2.region - in TypeApp {region; value = $1,$2} + in TApp {region; value = $1,$2} } | Map type_tuple { let region = cover $1 $2.region in let value = {value="map"; region=$1} - in TypeApp {region; value = value, $2} + in TApp {region; value = value, $2} } | par(type_expr) { - ParType $1 + TPar $1 } type_tuple: @@ -377,7 +377,7 @@ unqualified_decl(OP): lpar = Region.ghost; inside = value; rpar = Region.ghost} in - ListExpr (EmptyList {region; value}) + EList (EmptyList {region; value}) | `ENone region -> let value = { lpar = Region.ghost; @@ -386,9 +386,9 @@ unqualified_decl(OP): colon = Region.ghost; opt_type = $3}; rpar = Region.ghost} - in ConstrExpr (NoneExpr {region; value}) + in EConstr (NoneExpr {region; value}) | `EMap inj -> - MapExpr (MapInj inj) + EMap (MapInj inj) in $1, $2, $3, $4, init, $6, stop } @@ -621,7 +621,7 @@ expr: and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} in - LogicExpr (BoolExpr (Or {region; value})) + ELogic (BoolExpr (Or {region; value})) } | conj_expr { $1 } @@ -630,8 +630,8 @@ conj_expr: let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop - and value = {arg1 = $1; op = $2; arg2 = $3} in - LogicExpr (BoolExpr (And {region; value})) + and value = {arg1 = $1; op = $2; arg2 = $3} + in ELogic (BoolExpr (And {region; value})) } | comp_expr { $1 } @@ -640,43 +640,43 @@ comp_expr: let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop - and value = {arg1 = $1; op = $2; arg2 = $3} in - LogicExpr (CompExpr (Lt {region; value})) + and value = {arg1 = $1; op = $2; arg2 = $3} + in ELogic (CompExpr (Lt {region; value})) } | comp_expr LEQ cat_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in LogicExpr (CompExpr (Leq {region; value})) + in ELogic (CompExpr (Leq {region; value})) } | comp_expr GT cat_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in LogicExpr (CompExpr (Gt {region; value})) + in ELogic (CompExpr (Gt {region; value})) } | comp_expr GEQ cat_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in LogicExpr (CompExpr (Geq {region; value})) + in ELogic (CompExpr (Geq {region; value})) } | comp_expr EQUAL cat_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in LogicExpr (CompExpr (Equal {region; value})) + in ELogic (CompExpr (Equal {region; value})) } | comp_expr NEQ cat_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in LogicExpr (CompExpr (Neq {region; value})) + in ELogic (CompExpr (Neq {region; value})) } | cat_expr { $1 } @@ -686,7 +686,7 @@ cat_expr: and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in StringExpr (Cat {region; value}) + in EString (Cat {region; value}) } | cons_expr { $1 } @@ -696,7 +696,7 @@ cons_expr: and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in ListExpr (Cons {region; value}) + in EList (Cons {region; value}) } | add_expr { $1 } @@ -706,14 +706,14 @@ add_expr: and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in ArithExpr (Add {region; value}) + in EArith (Add {region; value}) } | add_expr MINUS mult_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in ArithExpr (Sub {region; value}) + in EArith (Sub {region; value}) } | mult_expr { $1 } @@ -723,21 +723,21 @@ mult_expr: and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in ArithExpr (Mult {region; value}) + in EArith (Mult {region; value}) } | mult_expr SLASH unary_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in ArithExpr (Div {region; value}) + in EArith (Div {region; value}) } | mult_expr Mod unary_expr { let start = expr_to_region $1 and stop = expr_to_region $3 in let region = cover start stop and value = {arg1 = $1; op = $2; arg2 = $3} - in ArithExpr (Mod {region; value}) + in EArith (Mod {region; value}) } | unary_expr { $1 } @@ -746,40 +746,40 @@ unary_expr: let stop = expr_to_region $2 in let region = cover $1 stop and value = {op = $1; arg = $2} - in ArithExpr (Neg {region; value}) + in EArith (Neg {region; value}) } | Not core_expr { let stop = expr_to_region $2 in let region = cover $1 stop and value = {op = $1; arg = $2} in - LogicExpr (BoolExpr (Not {region; value})) + ELogic (BoolExpr (Not {region; value})) } | core_expr { $1 } core_expr: - Int { ArithExpr (Int $1) } -| var { Var $1 } -| String { StringExpr (String $1) } -| Bytes { Bytes $1 } -| C_False { LogicExpr (BoolExpr (False $1)) } -| C_True { LogicExpr (BoolExpr (True $1)) } -| C_Unit { Unit $1 } -| tuple { Tuple $1 } -| list_expr { ListExpr (List $1) } -| empty_list { ListExpr (EmptyList $1) } -| set_expr { SetExpr (Set $1) } -| empty_set { SetExpr (EmptySet $1) } -| none_expr { ConstrExpr (NoneExpr $1) } -| fun_call { FunCall $1 } -| map_expr { MapExpr $1 } -| record_expr { RecordExpr $1 } + Int { EArith (Int $1) } +| var { EVar $1 } +| String { EString (String $1) } +| Bytes { EBytes $1 } +| C_False { ELogic (BoolExpr (False $1)) } +| C_True { ELogic (BoolExpr (True $1)) } +| C_Unit { EUnit $1 } +| tuple { ETuple $1 } +| list_expr { EList (List $1) } +| empty_list { EList (EmptyList $1) } +| set_expr { ESet (Set $1) } +| empty_set { ESet (EmptySet $1) } +| none_expr { EConstr (NoneExpr $1) } +| fun_call { ECall $1 } +| map_expr { EMap $1 } +| record_expr { ERecord $1 } | Constr arguments { let region = cover $1.region $2.region in - ConstrExpr (ConstrApp {region; value = $1,$2}) + EConstr (ConstrApp {region; value = $1,$2}) } | C_Some arguments { let region = cover $1 $2.region in - ConstrExpr (SomeApp {region; value = $1,$2}) + EConstr (SomeApp {region; value = $1,$2}) } map_expr: