From 45d18f7b1203d2a5fd9c434e01178e633efae48b Mon Sep 17 00:00:00 2001 From: Christian Rinderknecht Date: Thu, 28 Feb 2019 15:46:34 +0100 Subject: [PATCH] Added instruction 'fail'. I changed the grammar and AST for local functions and removed global mutable variables. --- AST.ml | 142 ++++++++++++++++++++++-------------- AST.mli | 52 +++++++++----- EvalOpt.ml | 40 ++++++----- LexToken.mli | 55 +++++++------- LexToken.mll | 5 ++ Lexer.mll | 20 +++--- ParToken.mly | 129 ++++++++++++++++----------------- Parser.mly | 184 +++++++++++++++++++++++++---------------------- Tests/a.li | 18 ++--- Version.ml | 2 +- ligo-parser.opam | 32 ++++----- 11 files changed, 381 insertions(+), 298 deletions(-) diff --git a/AST.ml b/AST.ml index 5d6e71542..db0465839 100644 --- a/AST.ml +++ b/AST.ml @@ -36,6 +36,7 @@ let sepseq_to_region to_region = function type kwd_begin = Region.t type kwd_const = Region.t type kwd_down = Region.t +type kwd_fail = Region.t type kwd_if = Region.t type kwd_in = Region.t type kwd_is = Region.t @@ -137,6 +138,7 @@ type 'a braces = (lbrace * 'a * rbrace) reg type t = < types : type_decl list; + constants : const_decl reg list; parameter : parameter_decl; storage : storage_decl; operations : operations_decl; @@ -185,48 +187,58 @@ and lambda_decl = and fun_decl = < kwd_function : kwd_function; - var : variable; + name : variable; param : parameters; colon : colon; ret_type : type_expr; kwd_is : kwd_is; - body : block reg; + local_decls : local_decl list; + block : block reg; kwd_with : kwd_with; return : expr > and proc_decl = < kwd_procedure : kwd_procedure; - var : variable; + name : variable; param : parameters; kwd_is : kwd_is; - body : block reg + local_decls : local_decl list; + block : block reg > and parameters = (param_decl, semi) nsepseq par -and param_decl = (var_kind * variable * colon * type_expr) reg - -and var_kind = - Mutable of kwd_var -| Const of kwd_const +and param_decl = + ParamConst of (kwd_const * variable * colon * type_expr) reg +| ParamVar of (kwd_var * variable * colon * type_expr) reg and block = < - decls : value_decls; opening : kwd_begin; instr : instructions; close : kwd_end > -and value_decls = (var_decl reg, semi) sepseq reg +and local_decl = + LocalLam of lambda_decl +| LocalConst of const_decl reg +| LocalVar of var_decl reg +and const_decl = < + kwd_const : kwd_const; + name : variable; + colon : colon; + vtype : type_expr; + equal : equal; + init : expr +> and var_decl = < - kind : var_kind; - var : variable; - colon : colon; - vtype : type_expr; - setter : Region.t; (* "=" or ":=" *) - init : expr + kwd_var : kwd_var; + name : variable; + colon : colon; + vtype : type_expr; + asgnmnt : asgnmnt; + init : expr > and instructions = (instruction, semi) nsepseq reg @@ -242,6 +254,7 @@ and single_instr = | Loop of loop | ProcCall of fun_call | Null of kwd_null +| Fail of (kwd_fail * expr) reg and conditional = < kwd_if : kwd_if; @@ -428,10 +441,6 @@ let expr_to_region = function | MapLookUp {region; _} | ParExpr {region; _} -> region -let var_kind_to_region = function - Mutable region -| Const region -> region - let instr_to_region = function Single Cond {region;_} | Single Match {region; _} @@ -441,6 +450,7 @@ let instr_to_region = function | Single Loop For ForCollect {region; _} | Single ProcCall {region; _} | Single Null region +| Single Fail {region; _} | Block {region; _} -> region let core_pattern_to_region = function @@ -458,6 +468,12 @@ let core_pattern_to_region = function | PList Raw {region; _} | PTuple {region; _} -> region +let local_decl_to_region = function + LocalLam FunDecl {region; _} +| LocalLam ProcDecl {region; _} +| LocalConst {region; _} +| LocalVar {region; _} -> region + (* Printing the tokens with their source regions *) let printf = Printf.printf @@ -589,22 +605,24 @@ and print_lambda_decl = function | ProcDecl proc_decl -> print_proc_decl proc_decl and print_fun_decl {value=node; _} = - print_token node#kwd_function "function"; - print_var node#var; - print_parameters node#param; - print_token node#colon ":"; - print_type_expr node#ret_type; - print_token node#kwd_is "is"; - print_block node#body; - print_token node#kwd_with "with"; - print_expr node#return + print_token node#kwd_function "function"; + print_var node#name; + print_parameters node#param; + print_token node#colon ":"; + print_type_expr node#ret_type; + print_token node#kwd_is "is"; + print_local_decls node#local_decls; + print_block node#block; + print_token node#kwd_with "with"; + print_expr node#return and print_proc_decl {value=node; _} = - print_token node#kwd_procedure "procedure"; - print_var node#var; - print_parameters node#param; - print_token node#kwd_is "is"; - print_block node#body + print_token node#kwd_procedure "procedure"; + print_var node#name; + print_parameters node#param; + print_token node#kwd_is "is"; + print_local_decls node#local_decls; + print_block node#block and print_parameters {value=node; _} = let lpar, sequence, rpar = node in @@ -612,36 +630,51 @@ and print_parameters {value=node; _} = print_nsepseq ";" print_param_decl sequence; print_token rpar ")" -and print_param_decl {value=node; _} = - let var_kind, variable, colon, type_expr = node in - print_var_kind var_kind; +and print_param_decl = function + ParamConst param_const -> print_param_const param_const +| ParamVar param_var -> print_param_var param_var + +and print_param_const {value=node; _} = + let kwd_const, variable, colon, type_expr = node in + print_token kwd_const "const"; print_var variable; print_token colon ":"; print_type_expr type_expr -and print_var_kind = function - Mutable kwd_var -> print_token kwd_var "var" -| Const kwd_const -> print_token kwd_const "const" +and print_param_var {value=node; _} = + let kwd_var, variable, colon, type_expr = node in + print_token kwd_var "var"; + print_var variable; + print_token colon ":"; + print_type_expr type_expr and print_block {value=node; _} = - print_value_decls node#decls; print_token node#opening "begin"; print_instructions node#instr; print_token node#close "end" -and print_value_decls {value=sequence; _} = - print_sepseq ";" print_var_decl sequence +and print_local_decls sequence = + List.iter print_local_decl sequence -and print_var_decl {value=node; _} = - let setter = - match node#kind with - Mutable _ -> ":=" - | Const _ -> "=" in - print_var_kind node#kind; - print_var node#var; +and print_local_decl = function + LocalLam decl -> print_lambda_decl decl +| LocalConst decl -> print_const_decl decl +| LocalVar decl -> print_var_decl decl + +and print_const_decl {value=node; _} = + print_token node#kwd_const "const"; + print_var node#name; print_token node#colon ":"; print_type_expr node#vtype; - print_token node#setter setter; + print_token node#equal "="; + print_expr node#init + +and print_var_decl {value=node; _} = + print_token node#kwd_var "var"; + print_var node#name; + print_token node#colon ":"; + print_type_expr node#vtype; + print_token node#asgnmnt ":="; print_expr node#init and print_instructions {value=sequence; _} = @@ -658,6 +691,11 @@ and print_single_instr = function | Loop loop -> print_loop loop | ProcCall fun_call -> print_fun_call fun_call | Null kwd_null -> print_token kwd_null "null" +| Fail {value; _} -> print_fail value + +and print_fail (kwd_fail, expr) = + print_token kwd_fail "fail"; + print_expr expr and print_conditional node = print_token node#kwd_if "if"; diff --git a/AST.mli b/AST.mli index 3d6c2e361..2c2a8cb3f 100644 --- a/AST.mli +++ b/AST.mli @@ -24,6 +24,7 @@ val sepseq_to_region : ('a -> Region.t) -> ('a,'sep) sepseq -> Region.t type kwd_begin = Region.t type kwd_const = Region.t type kwd_down = Region.t +type kwd_fail = Region.t type kwd_if = Region.t type kwd_in = Region.t type kwd_is = Region.t @@ -125,6 +126,7 @@ type 'a braces = (lbrace * 'a * rbrace) reg type t = < types : type_decl list; + constants : const_decl reg list; parameter : parameter_decl; storage : storage_decl; operations : operations_decl; @@ -173,48 +175,59 @@ and lambda_decl = and fun_decl = < kwd_function : kwd_function; - var : variable; + name : variable; param : parameters; colon : colon; ret_type : type_expr; kwd_is : kwd_is; - body : block reg; + local_decls : local_decl list; + block : block reg; kwd_with : kwd_with; return : expr > and proc_decl = < kwd_procedure : kwd_procedure; - var : variable; + name : variable; param : parameters; kwd_is : kwd_is; - body : block reg + local_decls : local_decl list; + block : block reg > and parameters = (param_decl, semi) nsepseq par -and param_decl = (var_kind * variable * colon * type_expr) reg - -and var_kind = - Mutable of kwd_var -| Const of kwd_const +and param_decl = + ParamConst of (kwd_const * variable * colon * type_expr) reg +| ParamVar of (kwd_var * variable * colon * type_expr) reg and block = < - decls : value_decls; opening : kwd_begin; instr : instructions; close : kwd_end > -and value_decls = (var_decl reg, semi) sepseq reg +and local_decl = + LocalLam of lambda_decl +| LocalConst of const_decl reg +| LocalVar of var_decl reg + +and const_decl = < + kwd_const : kwd_const; + name : variable; + colon : colon; + vtype : type_expr; + equal : equal; + init : expr +> and var_decl = < - kind : var_kind; - var : variable; - colon : colon; - vtype : type_expr; - setter : Region.t; (* "=" or ":=" *) - init : expr + kwd_var : kwd_var; + name : variable; + colon : colon; + vtype : type_expr; + asgnmnt : asgnmnt; + init : expr > and instructions = (instruction, semi) nsepseq reg @@ -230,6 +243,7 @@ and single_instr = | Loop of loop | ProcCall of fun_call | Null of kwd_null +| Fail of (kwd_fail * expr) reg and conditional = < kwd_if : kwd_if; @@ -373,12 +387,12 @@ val type_expr_to_region : type_expr -> Region.t val expr_to_region : expr -> Region.t -val var_kind_to_region : var_kind -> Region.t - val instr_to_region : instruction -> Region.t val core_pattern_to_region : core_pattern -> Region.t +val local_decl_to_region : local_decl -> Region.t + (* Printing *) val print_tokens : t -> unit diff --git a/EvalOpt.ml b/EvalOpt.ml index 904020479..8407efc39 100644 --- a/EvalOpt.ml +++ b/EvalOpt.ml @@ -14,16 +14,21 @@ let help () = printf "Usage: %s [