From 847a3a42b62b0a2ec7ae5023e2c6d469f4e5e408 Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Tue, 26 Mar 2019 15:51:28 +0100 Subject: [PATCH] Refactoring for tuple expressions. --- AST.ml | 31 ++++++++++++++++++++----------- AST.mli | 9 ++++++--- Parser.mly | 19 +++++++++++-------- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/AST.ml b/AST.ml index 71eb036ee..e34624bc7 100644 --- a/AST.ml +++ b/AST.ml @@ -510,7 +510,7 @@ and expr = | ECall of fun_call | EBytes of (Lexer.lexeme * Hex.t) reg | EUnit of c_Unit -| ETuple of tuple +| ETuple of tuple_expr | EPar of expr par reg and set_expr = @@ -634,7 +634,10 @@ and record_projection = { field_path : (field_name, dot) nsepseq } -and tuple = (expr, comma) nsepseq par reg +and tuple_expr = + TupleInj of tuple_injection + +and tuple_injection = (expr, comma) nsepseq par reg and none_expr = typed_none_expr par @@ -646,7 +649,7 @@ and typed_none_expr = { and fun_call = (fun_name * arguments) reg -and arguments = tuple +and arguments = tuple_injection (* Patterns *) @@ -690,13 +693,16 @@ let rec expr_to_region = function | EConstr e -> constr_expr_to_region e | ERecord e -> record_expr_to_region e | EMap e -> map_expr_to_region e +| ETuple e -> tuple_expr_to_region e | EVar {region; _} | ECall {region; _} | EBytes {region; _} | EUnit region -| ETuple {region; _} | EPar {region; _} -> region +and tuple_expr_to_region = function + TupleInj {region; _} -> region + and map_expr_to_region = function MapLookUp {region; _} | MapInj {region; _} -> region @@ -1214,7 +1220,7 @@ and print_expr = function | ECall e -> print_fun_call e | EBytes b -> print_bytes b | EUnit r -> print_token r "Unit" -| ETuple e -> print_tuple e +| ETuple e -> print_tuple_expr e | EPar e -> print_par_expr e and print_map_expr = function @@ -1394,7 +1400,10 @@ and print_binding {value; _} = print_token arrow "->"; print_expr image -and print_tuple {value; _} = +and print_tuple_expr = function + TupleInj inj -> print_tuple_inj inj + +and print_tuple_inj {value; _} = let {lpar; inside; rpar} = value in print_token lpar "("; print_nsepseq "," print_expr inside; @@ -1427,18 +1436,18 @@ and print_none_expr {value; _} = and print_fun_call {value; _} = let fun_name, arguments = value in - print_var fun_name; - print_tuple arguments + print_var fun_name; + print_tuple_inj arguments and print_constr_app {value; _} = let constr, arguments = value in - print_constr constr; - print_tuple arguments + print_constr constr; + print_tuple_inj arguments and print_some_app {value; _} = let c_Some, arguments = value in print_token c_Some "Some"; - print_tuple arguments + print_tuple_inj arguments and print_par_expr {value; _} = let {lpar; inside; rpar} = value in diff --git a/AST.mli b/AST.mli index 56fd67639..02efd31ef 100644 --- a/AST.mli +++ b/AST.mli @@ -494,7 +494,7 @@ and expr = | ECall of fun_call | EBytes of (Lexer.lexeme * Hex.t) reg | EUnit of c_Unit -| ETuple of tuple +| ETuple of tuple_expr | EPar of expr par reg and set_expr = @@ -618,7 +618,10 @@ and record_projection = { field_path : (field_name, dot) nsepseq } -and tuple = (expr, comma) nsepseq par reg +and tuple_expr = + TupleInj of tuple_injection + +and tuple_injection = (expr, comma) nsepseq par reg and none_expr = typed_none_expr par @@ -630,7 +633,7 @@ and typed_none_expr = { and fun_call = (fun_name * arguments) reg -and arguments = tuple +and arguments = tuple_injection (* Patterns *) diff --git a/Parser.mly b/Parser.mly index 80daa0229..35c5dceb1 100644 --- a/Parser.mly +++ b/Parser.mly @@ -64,7 +64,7 @@ par(X): brackets(X): LBRACKET X RBRACKET { let region = cover $1 $3 - and value = { + and value = { lbracket = $1; inside = $2; rbracket = $3} @@ -923,13 +923,13 @@ unary_expr: core_expr: Int { EArith (Int $1) } -| var { EVar $1 } (* TODO: Path *) +| 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 } +| tuple_expr { ETuple $1 } | list_expr { EList (List $1) } | nil { EList (Nil $1) } | none_expr { EConstr (NoneExpr $1) } @@ -955,9 +955,7 @@ path: map_lookup: path brackets(expr) { let region = cover (path_to_region $1) $2.region in - let value = { - path = $1; - index = $2} + let value = {path=$1; index=$2} in {region; value} } @@ -1004,11 +1002,16 @@ fun_call: in {region; value = $1,$2} } -tuple: +tuple_expr: + tuple_inj { + TupleInj $1 + } + +tuple_inj: par(nsepseq(expr,COMMA)) { $1 } arguments: - tuple { $1 } + tuple_inj { $1 } list_expr: List series(expr,End) {