diff --git a/src/passes/2-concrete_to_imperative/cameligo.ml b/src/passes/2-concrete_to_imperative/cameligo.ml index 371e0c784..0fea68765 100644 --- a/src/passes/2-concrete_to_imperative/cameligo.ml +++ b/src/passes/2-concrete_to_imperative/cameligo.ml @@ -294,7 +294,7 @@ let rec compile_expression : | Component index -> Z.to_string (snd index.value) in List.map aux @@ npseq_to_list path in - return @@ List.fold_left (e_accessor ~loc ) var path' + return @@ List.fold_left (e_record_accessor ~loc ) var path' in let compile_path : Raw.path -> string * label list = fun p -> match p with @@ -319,7 +319,7 @@ let rec compile_expression : let record = match path with | [] -> e_variable (Var.of_name name) | _ -> - let aux expr (Label l) = e_accessor expr l in + let aux expr (Label l) = e_record_accessor expr l in List.fold_left aux (e_variable (Var.of_name name)) path in let updates = u.updates.value.ne_elements in let%bind updates' = @@ -333,10 +333,10 @@ let rec compile_expression : let aux ur (path, expr) = let rec aux record = function | [] -> failwith "error in parsing" - | hd :: [] -> ok @@ e_update ~loc record hd expr + | hd :: [] -> ok @@ e_record_update ~loc record hd expr | hd :: tl -> - let%bind expr = (aux (e_accessor ~loc record hd) tl) in - ok @@ e_update ~loc record hd expr + let%bind expr = (aux (e_record_accessor ~loc record hd) tl) in + ok @@ e_record_update ~loc record hd expr in aux ur path in bind_fold_list aux record updates' @@ -384,11 +384,11 @@ let rec compile_expression : | hd :: [] -> if (List.length prep_vars = 1) then e_let_in hd inline rhs_b_expr body - else e_let_in hd inline (e_accessor rhs_b_expr (string_of_int ((List.length prep_vars) - 1))) body + else e_let_in hd inline (e_record_accessor rhs_b_expr (string_of_int ((List.length prep_vars) - 1))) body | hd :: tl -> e_let_in hd inline - (e_accessor rhs_b_expr (string_of_int ((List.length prep_vars) - (List.length tl) - 1))) + (e_record_accessor rhs_b_expr (string_of_int ((List.length prep_vars) - (List.length tl) - 1))) (chain_let_in tl body) | [] -> body (* Precluded by corner case assertion above *) in diff --git a/src/passes/2-concrete_to_imperative/pascaligo.ml b/src/passes/2-concrete_to_imperative/pascaligo.ml index 2eb055351..8a224f69b 100644 --- a/src/passes/2-concrete_to_imperative/pascaligo.ml +++ b/src/passes/2-concrete_to_imperative/pascaligo.ml @@ -220,7 +220,7 @@ let compile_projection : Raw.projection Region.reg -> _ = fun p -> | Component index -> (Z.to_string (snd index.value)) in List.map aux @@ npseq_to_list path in - ok @@ List.fold_left (e_accessor ~loc) var path' + ok @@ List.fold_left (e_record_accessor ~loc) var path' let rec compile_expression (t:Raw.expr) : expr result = @@ -423,10 +423,10 @@ and compile_update = fun (u:Raw.update Region.reg) -> let aux ur (path, expr) = let rec aux record = function | [] -> failwith "error in parsing" - | hd :: [] -> ok @@ e_update ~loc record hd expr + | hd :: [] -> ok @@ e_record_update ~loc record hd expr | hd :: tl -> - let%bind expr = (aux (e_accessor ~loc record hd) tl) in - ok @@ e_update ~loc record hd expr + let%bind expr = (aux (e_record_accessor ~loc record hd) tl) in + ok @@ e_record_update ~loc record hd expr in aux ur path in bind_fold_list aux record updates' @@ -614,7 +614,7 @@ and compile_fun_decl : let%bind tpl_declarations = let aux = fun i (param, type_expr) -> let expr = - e_accessor (e_variable arguments_name) (string_of_int i) in + e_record_accessor (e_variable arguments_name) (string_of_int i) in let type_variable = Some type_expr in let ass = return_let_in (Var.of_name param , type_variable) inline expr in ass @@ -677,7 +677,7 @@ and compile_fun_expression : (arguments_name , type_expression) in let%bind tpl_declarations = let aux = fun i (param, param_type) -> - let expr = e_accessor (e_variable arguments_name) (string_of_int i) in + let expr = e_record_accessor (e_variable arguments_name) (string_of_int i) in let type_variable = Some param_type in let ass = return_let_in (Var.of_name param , type_variable) false expr in ass diff --git a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml index 4a69e6c67..7d0a531b7 100644 --- a/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml +++ b/src/passes/4-imperative_to_sugar/imperative_to_sugar.ml @@ -37,7 +37,7 @@ let repair_mutable_variable_in_matching (match_body : O.expression) (element_nam ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs let_result) else( let free_var = if (List.mem name free_var) then free_var else name::free_var in - let expr = O.e_let_in (env,None) false false (O.e_record_update (O.e_variable env) (Var.to_name name) (O.e_variable name)) let_result in + let expr = O.e_let_in (env,None) false false (O.e_record_update (O.e_variable env) (O.Label (Var.to_name name)) (O.e_variable name)) let_result in ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs expr) ) | E_constant {cons_name=C_MAP_FOLD;arguments= _} @@ -70,8 +70,8 @@ and repair_mutable_variable_in_loops (for_body : O.expression) (element_names : else( let free_var = if (List.mem name free_var) then free_var else name::free_var in let expr = O.e_let_in (env,None) false false ( - O.e_record_update (O.e_variable env) ("0") - (O.e_record_update (O.e_record_accessor (O.e_variable env) "0") (Var.to_name name) (O.e_variable name)) + O.e_record_update (O.e_variable env) (Label "0") + (O.e_record_update (O.e_record_accessor (O.e_variable env) (Label "0")) (Label (Var.to_name name)) (O.e_variable name)) ) let_result in ok (true,(decl_var, free_var), O.e_let_in let_binder false false rhs expr) @@ -90,12 +90,12 @@ and store_mutable_variable (free_vars : I.expression_variable list) = if (List.length free_vars == 0) then O.e_unit () else - let aux var = (Var.to_name var, O.e_variable var) in - O.e_record_ez (List.map aux free_vars) + let aux var = (O.Label (Var.to_name var), O.e_variable var) in + O.e_record @@ O.LMap.of_list (List.map aux free_vars) and restore_mutable_variable (expr : O.expression->O.expression_content) (free_vars : O.expression_variable list) (env : O.expression_variable) = let aux (f: O.expression -> O.expression) (ev: O.expression_variable) = - fun expr -> f (O.e_let_in (ev,None) true false (O.e_record_accessor (O.e_variable env) (Var.to_name ev)) expr) + fun expr -> f (O.e_let_in (ev,None) true false (O.e_record_accessor (O.e_variable env) (Label (Var.to_name ev))) expr) in let ef = List.fold_left aux (fun e -> e) free_vars in expr (ef (O.e_skip ())) @@ -163,7 +163,7 @@ and compile_type_operator : I.type_operator -> O.type_operator result = let rec compile_expression : I.expression -> O.expression result = fun e -> - let return expr = ok @@ O.make_expr ~loc:e.location expr in + let return expr = ok @@ O.make_e ~loc:e.location expr in match e.expression_content with | I.E_literal literal -> return @@ O.E_literal literal | I.E_constant {cons_name;arguments} -> @@ -288,7 +288,7 @@ and compile_assign {variable; access_path; expression} expr = let accessor ?loc s a = match a with I.Access_tuple _i -> failwith "adding tuple soon" - | I.Access_record a -> ok @@ O.e_record_accessor ?loc s a + | I.Access_record a -> ok @@ O.e_record_accessor ?loc s (Label a) | I.Access_map k -> let%bind k = compile_expression k in ok @@ O.e_constant ?loc C_MAP_FIND_OPT [k;s] @@ -296,7 +296,7 @@ and compile_assign {variable; access_path; expression} expr = let update ?loc (s:O.expression) a e = match a with I.Access_tuple _i -> failwith "adding tuple soon" - | I.Access_record a -> ok @@ O.e_record_update ?loc s a e + | I.Access_record a -> ok @@ O.e_record_update ?loc s (Label a) e | I.Access_map k -> let%bind k = compile_expression k in ok @@ O.e_constant ?loc C_UPDATE [k;O.e_some (e);s] @@ -430,7 +430,7 @@ and compile_while I.{condition;body} = let for_body = add_to_end for_body ctrl in let aux name expr= - O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable binder) "0") (Var.to_name name)) expr + O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable binder) (Label "0")) (Label (Var.to_name name))) expr in let init_rec = O.e_tuple [store_mutable_variable @@ captured_name_list] in let restore = fun expr -> List.fold_right aux captured_name_list expr in @@ -445,7 +445,7 @@ and compile_while I.{condition;body} = let return_expr = fun expr -> O.E_let_in {let_binder; mut=false; inline=false; rhs=init_rec; let_result= O.e_let_in let_binder false false loop @@ - O.e_let_in let_binder false false (O.e_record_accessor (O.e_variable env_rec) "0") @@ + O.e_let_in let_binder false false (O.e_record_accessor (O.e_variable env_rec) (Label"0")) @@ expr } in @@ -461,7 +461,7 @@ and compile_for I.{binder;start;final;increment;body} = let continue_expr = O.e_constant C_FOLD_CONTINUE [(O.e_variable env_rec)] in let ctrl = O.e_let_in (binder,Some O.t_int) false false (O.e_constant C_ADD [ O.e_variable binder ; step ]) @@ - O.e_let_in (env_rec, None) false false (O.e_record_update (O.e_variable env_rec) "1" @@ O.e_variable binder)@@ + O.e_let_in (env_rec, None) false false (O.e_record_update (O.e_variable env_rec) (Label "1") @@ O.e_variable binder)@@ continue_expr in (* Modify the body loop*) @@ -470,7 +470,7 @@ and compile_for I.{binder;start;final;increment;body} = let for_body = add_to_end for_body ctrl in let aux name expr= - O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable env_rec) "0") (Var.to_name name)) expr + O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable env_rec) (Label "0")) (Label (Var.to_name name))) expr in (* restores the initial value of the free_var*) @@ -479,7 +479,7 @@ and compile_for I.{binder;start;final;increment;body} = (*Prep the lambda for the fold*) let stop_expr = O.e_constant C_FOLD_STOP [O.e_variable env_rec] in let aux_func = O.e_lambda env_rec None None @@ - O.e_let_in (binder,Some O.t_int) false false (O.e_record_accessor (O.e_variable env_rec) "1") @@ + O.e_let_in (binder,Some O.t_int) false false (O.e_record_accessor (O.e_variable env_rec) (Label "1")) @@ O.e_cond cond (restore for_body) (stop_expr) in (* Make the fold_while en precharge the vakye *) @@ -492,7 +492,7 @@ and compile_for I.{binder;start;final;increment;body} = O.E_let_in {let_binder=(binder, Some O.t_int);mut=false; inline=false;rhs=start;let_result= O.e_let_in let_binder false false init_rec @@ O.e_let_in let_binder false false loop @@ - O.e_let_in let_binder false false (O.e_record_accessor (O.e_variable env_rec) "0") @@ + O.e_let_in let_binder false false (O.e_record_accessor (O.e_variable env_rec) (Label "0")) @@ expr } in @@ -508,21 +508,21 @@ and compile_for_each I.{binder;collection;collection_type; body} = let env = Var.fresh () in let%bind body = compile_expression body in let%bind ((_,free_vars), body) = repair_mutable_variable_in_loops body element_names args in - let for_body = add_to_end body @@ (O.e_record_accessor (O.e_variable args) "0") in + let for_body = add_to_end body @@ (O.e_record_accessor (O.e_variable args) (Label "0")) in let init_record = store_mutable_variable free_vars in let%bind collect = compile_expression collection in let aux name expr= - O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "0") (Var.to_name name)) expr + O.e_let_in (name,None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) (Label "0")) (Label (Var.to_name name))) expr in let restore = fun expr -> List.fold_right aux free_vars expr in let restore = match collection_type with | Map -> (match snd binder with - | Some v -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "1") "0") - (O.e_let_in (v, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "1") "1") expr)) - | None -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) "1") "0") expr) + | Some v -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) (Label "1")) (Label "0")) + (O.e_let_in (v, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) (Label "1")) (Label "1")) expr)) + | None -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_record_accessor (O.e_variable args) (Label "1")) (Label "0")) expr) ) - | _ -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_variable args) "1") expr) + | _ -> fun expr -> restore (O.e_let_in (fst binder, None) false false (O.e_record_accessor (O.e_variable args) (Label "1")) expr) in let lambda = O.e_lambda args None None (restore for_body) in let%bind op_name = match collection_type with @@ -612,7 +612,7 @@ and uncompile_type_operator : O.type_operator -> I.type_operator result = let rec uncompile_expression : O.expression -> I.expression result = fun e -> - let return expr = ok @@ I.make_expr ~loc:e.location expr in + let return expr = ok @@ I.make_e ~loc:e.location expr in match e.expression_content with O.E_literal lit -> return @@ I.E_literal lit | O.E_constant {cons_name;arguments} -> diff --git a/src/passes/6-sugar_to_core/sugar_to_core.ml b/src/passes/6-sugar_to_core/sugar_to_core.ml index b175f8eb3..ec9ca7dd1 100644 --- a/src/passes/6-sugar_to_core/sugar_to_core.ml +++ b/src/passes/6-sugar_to_core/sugar_to_core.ml @@ -68,7 +68,7 @@ and idle_type_operator : I.type_operator -> O.type_operator result = let rec compile_expression : I.expression -> O.expression result = fun e -> - let return expr = ok @@ O.make_expr ~loc:e.location expr in + let return expr = ok @@ O.make_e ~loc:e.location expr in match e.expression_content with | I.E_literal literal -> return @@ O.E_literal literal | I.E_constant {cons_name;arguments} -> @@ -293,7 +293,7 @@ and uncompile_type_operator : O.type_operator -> I.type_operator result = let rec uncompile_expression : O.expression -> I.expression result = fun e -> - let return expr = ok @@ I.make_expr ~loc:e.location expr in + let return expr = ok @@ I.make_e ~loc:e.location expr in match e.expression_content with O.E_literal lit -> return @@ I.E_literal lit | O.E_constant {cons_name;arguments} -> diff --git a/src/passes/8-typer-new/typer.ml b/src/passes/8-typer-new/typer.ml index f9f6cf66a..ddbe3a139 100644 --- a/src/passes/8-typer-new/typer.ml +++ b/src/passes/8-typer-new/typer.ml @@ -367,7 +367,7 @@ and type_expression : environment -> Solver.state -> ?tv_opt:O.type_expression - let%bind new_state = aggregate_constraints state constraints in let tv = t_variable type_name () in let location = ae.location in - let expr' = make_a_e ~location expr tv e in + let expr' = make_e ~location expr tv e in ok @@ (expr' , new_state) in let return_wrapped expr state (constraints , expr_type) = return expr state constraints expr_type in let main_error = @@ -912,11 +912,9 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let Constructor n = constructor in return (e_constructor n p') | E_record r -> - let aux ( Label k ,v) = (k, v) in - let r = Map.String.of_list @@ List.map aux (LMap.to_kv_list r) in - let%bind r' = bind_smap - @@ Map.String.map untype_expression r in - return (e_record r') + let r = LMap.to_kv_list r in + let%bind r' = bind_map_list (fun (k,e) -> let%bind e = untype_expression e in ok (k,e)) r in + return (e_record @@ LMap.of_list r') | E_record_accessor {record; path} -> let%bind r' = untype_expression record in let Label s = path in @@ -924,8 +922,7 @@ let rec untype_expression (e:O.expression) : (I.expression) result = | E_record_update {record; path; update} -> let%bind r' = untype_expression record in let%bind e = untype_expression update in - let Label l = path in - return (e_record_update r' l e) + return (e_record_update r' path e) | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in let%bind m' = untype_matching untype_expression cases in diff --git a/src/passes/8-typer-old/typer.ml b/src/passes/8-typer-old/typer.ml index 96137dd56..1fd84ed0b 100644 --- a/src/passes/8-typer-old/typer.ml +++ b/src/passes/8-typer-old/typer.ml @@ -407,7 +407,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression | None -> ok () | Some tv' -> O.assert_type_expression_eq (tv' , tv) in let location = ae.location in - ok @@ make_a_e ~location expr tv e in + ok @@ make_e ~location expr tv e in let main_error = let title () = "typing expression" in let content () = "" in @@ -463,7 +463,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression generic_try (bad_record_access property ae prev.type_expression ae.location) @@ (fun () -> I.LMap.find property r_tv) in let location = ae.location in - ok @@ make_a_e ~location (E_record_accessor {record=prev; path=property}) tv e + ok @@ make_e ~location (E_record_accessor {record=prev; path=property}) tv e in let%bind ae = trace (simple_info "accessing") @@ aux e' path in @@ -544,7 +544,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let e' = Environment.add_ez_binder lname input_type e in let%bind body = type_expression' ?tv_opt:(Some tv_out) e' result in let output_type = body.type_expression in - let lambda' = make_a_e (E_lambda {binder = lname ; result=body}) (t_function input_type output_type ()) e' in + let lambda' = make_e (E_lambda {binder = lname ; result=body}) (t_function input_type output_type ()) e' in let lst' = [lambda'; v_col; v_initr] in let tv_lst = List.map get_type_expression lst' in let%bind (opname', tv) = @@ -565,7 +565,7 @@ and type_expression' : environment -> ?tv_opt:O.type_expression -> I.expression let e' = Environment.add_ez_binder lname input_type e in let%bind body = type_expression' e' result in let output_type = body.type_expression in - let lambda' = make_a_e (E_lambda {binder = lname ; result=body}) (t_function input_type output_type ()) e' in + let lambda' = make_e (E_lambda {binder = lname ; result=body}) (t_function input_type output_type ()) e' in let lst' = [lambda';v_initr] in let tv_lst = List.map get_type_expression lst' in let%bind (opname',tv) = type_constant opname tv_lst tv_opt in @@ -782,11 +782,9 @@ let rec untype_expression (e:O.expression) : (I.expression) result = let Constructor n = constructor in return (e_constructor n p') | E_record r -> - let aux ( Label k ,v) = (k, v) in - let r = Map.String.of_list @@ List.map aux (LMap.to_kv_list r) in - let%bind r' = bind_smap - @@ Map.String.map untype_expression r in - return (e_record r') + let r = LMap.to_kv_list r in + let%bind r' = bind_map_list (fun (k,e) -> let%bind e = untype_expression e in ok (k,e)) r in + return (e_record @@ LMap.of_list r') | E_record_accessor {record; path} -> let%bind r' = untype_expression record in let Label s = path in @@ -794,7 +792,6 @@ let rec untype_expression (e:O.expression) : (I.expression) result = | E_record_update {record=r; path=l; update=e} -> let%bind r' = untype_expression r in let%bind e = untype_expression e in - let Label l = l in return (e_record_update r' l e) | E_matching {matchee;cases} -> let%bind ae' = untype_expression matchee in diff --git a/src/stages/1-ast_imperative/combinators.ml b/src/stages/1-ast_imperative/combinators.ml index a71f5268e..595471fc5 100644 --- a/src/stages/1-ast_imperative/combinators.ml +++ b/src/stages/1-ast_imperative/combinators.ml @@ -74,67 +74,74 @@ let t_operator op lst: type_expression result = | TC_contract _ , [t] -> ok @@ t_contract t | _ , _ -> fail @@ bad_type_operator op -let make_expr ?(loc = Location.generated) expression_content = +let make_e ?(loc = Location.generated) expression_content = let location = loc in { expression_content; location } -let e_var ?loc (n: string) : expression = make_expr ?loc @@ E_variable (Var.of_name n) -let e_literal ?loc l : expression = make_expr ?loc @@ E_literal l -let e_unit ?loc () : expression = make_expr ?loc @@ E_literal (Literal_unit) -let e_int ?loc n : expression = make_expr ?loc @@ E_literal (Literal_int n) -let e_nat ?loc n : expression = make_expr ?loc @@ E_literal (Literal_nat n) -let e_timestamp ?loc n : expression = make_expr ?loc @@ E_literal (Literal_timestamp n) -let e_bool ?loc b : expression = make_expr ?loc @@ E_literal (Literal_bool b) -let e_string ?loc s : expression = make_expr ?loc @@ E_literal (Literal_string s) -let e_address ?loc s : expression = make_expr ?loc @@ E_literal (Literal_address s) -let e_mutez ?loc s : expression = make_expr ?loc @@ E_literal (Literal_mutez s) -let e_signature ?loc s : expression = make_expr ?loc @@ E_literal (Literal_signature s) -let e_key ?loc s : expression = make_expr ?loc @@ E_literal (Literal_key s) -let e_key_hash ?loc s : expression = make_expr ?loc @@ E_literal (Literal_key_hash s) -let e_chain_id ?loc s : expression = make_expr ?loc @@ E_literal (Literal_chain_id s) +let e_literal ?loc l : expression = make_e ?loc @@ E_literal l +let e_unit ?loc () : expression = make_e ?loc @@ E_literal (Literal_unit) +let e_int ?loc n : expression = make_e ?loc @@ E_literal (Literal_int n) +let e_nat ?loc n : expression = make_e ?loc @@ E_literal (Literal_nat n) +let e_timestamp ?loc n : expression = make_e ?loc @@ E_literal (Literal_timestamp n) +let e_bool ?loc b : expression = make_e ?loc @@ E_literal (Literal_bool b) +let e_string ?loc s : expression = make_e ?loc @@ E_literal (Literal_string s) +let e_address ?loc s : expression = make_e ?loc @@ E_literal (Literal_address s) +let e_mutez ?loc s : expression = make_e ?loc @@ E_literal (Literal_mutez s) +let e_signature ?loc s : expression = make_e ?loc @@ E_literal (Literal_signature s) +let e_key ?loc s : expression = make_e ?loc @@ E_literal (Literal_key s) +let e_key_hash ?loc s : expression = make_e ?loc @@ E_literal (Literal_key_hash s) +let e_chain_id ?loc s : expression = make_e ?loc @@ E_literal (Literal_chain_id s) let e'_bytes b : expression_content result = let%bind bytes = generic_try (simple_error "bad hex to bytes") (fun () -> Hex.to_bytes (`Hex b)) in ok @@ E_literal (Literal_bytes bytes) let e_bytes_hex ?loc b : expression result = let%bind e' = e'_bytes b in - ok @@ make_expr ?loc e' + ok @@ make_e ?loc e' let e_bytes_raw ?loc (b: bytes) : expression = - make_expr ?loc @@ E_literal (Literal_bytes b) + make_e ?loc @@ E_literal (Literal_bytes b) let e_bytes_string ?loc (s: string) : expression = - make_expr ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) -let e_big_map ?loc lst : expression = make_expr ?loc @@ E_big_map lst -let e_some ?loc s : expression = make_expr ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} -let e_none ?loc () : expression = make_expr ?loc @@ E_constant {cons_name = C_NONE; arguments = []} -let e_string_cat ?loc sl sr : expression = make_expr ?loc @@ E_constant {cons_name = C_CONCAT; arguments = [sl ; sr ]} -let e_map_add ?loc k v old : expression = make_expr ?loc @@ E_constant {cons_name = C_MAP_ADD; arguments = [k ; v ; old]} -let e_map ?loc lst : expression = make_expr ?loc @@ E_map lst -let e_set ?loc lst : expression = make_expr ?loc @@ E_set lst -let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst -let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} -let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} -let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path = Label b} -let e_accessor ?loc a b = e_record_accessor ?loc a b -let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_accessor ?loc a b) a b -let e_variable ?loc v = make_expr ?loc @@ E_variable v -let e_skip ?loc () = make_expr ?loc @@ E_skip -let e_let_in ?loc (binder, ascr) inline rhs let_result = - make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline } -let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} -let e_application ?loc a b = make_expr ?loc @@ E_application {lamb=a ; args=b} -let e_binop ?loc name a b = make_expr ?loc @@ E_constant {cons_name = name ; arguments = [a ; b]} -let e_constant ?loc name lst = make_expr ?loc @@ E_constant {cons_name=name ; arguments = lst} -let e_look_up ?loc x y = make_expr ?loc @@ E_look_up (x , y) -let e_sequence ?loc expr1 expr2 = make_expr ?loc @@ E_sequence {expr1; expr2} + make_e ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) +let e_some ?loc s : expression = make_e ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} +let e_none ?loc () : expression = make_e ?loc @@ E_constant {cons_name = C_NONE; arguments = []} +let e_string_cat ?loc sl sr : expression = make_e ?loc @@ E_constant {cons_name = C_CONCAT; arguments = [sl ; sr ]} +let e_map_add ?loc k v old : expression = make_e ?loc @@ E_constant {cons_name = C_MAP_ADD; arguments = [k ; v ; old]} +let e_binop ?loc name a b = make_e ?loc @@ E_constant {cons_name = name ; arguments = [a ; b]} -let e_while ?loc condition body = make_expr ?loc @@ E_while {condition; body} -let e_for ?loc binder start final increment body = make_expr ?loc @@ E_for {binder;start;final;increment;body} -let e_for_each ?loc binder collection collection_type body = make_expr ?loc @@ E_for_each {binder;collection;collection_type;body} +let e_constant ?loc name lst = make_e ?loc @@ E_constant {cons_name=name ; arguments = lst} +let e_variable ?loc v = make_e ?loc @@ E_variable v +let e_application ?loc a b = make_e ?loc @@ E_application {lamb=a ; args=b} +let e_lambda ?loc binder input_type output_type result : expression = make_e ?loc @@ E_lambda {binder; input_type; output_type; result} +let e_recursive ?loc fun_name fun_type lambda = make_e ?loc @@ E_recursive {fun_name; fun_type; lambda} +let e_let_in ?loc (binder, ascr) inline rhs let_result = make_e ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline } + +let e_constructor ?loc s a : expression = make_e ?loc @@ E_constructor { constructor = Constructor s; element = a} +let e_matching ?loc a b : expression = make_e ?loc @@ E_matching {matchee=a;cases=b} + +let e_record_accessor ?loc a b = make_e ?loc @@ E_record_accessor {record = a; path = Label b} +let e_accessor_list ?loc a b = List.fold_left (fun a b -> e_record_accessor ?loc a b) a b +let e_record_update ?loc record path update = make_e ?loc @@ E_record_update {record; path=Label path; update} + +let e_annotation ?loc anno_expr ty = make_e ?loc @@ E_ascription {anno_expr; type_annotation = ty} + +let e_tuple ?loc lst : expression = make_e ?loc @@ E_tuple lst +let e_tuple_accessor ?loc tuple path : expression = make_e ?loc @@ E_tuple_accessor {tuple; path} +let e_tuple_update ?loc tuple path update : expression = make_e ?loc @@ E_tuple_update {tuple; path; update} + +let e_pair ?loc a b : expression = e_tuple ?loc [a;b] +let e_cond ?loc condition then_clause else_clause = make_e ?loc @@ E_cond {condition;then_clause;else_clause} +let e_sequence ?loc expr1 expr2 = make_e ?loc @@ E_sequence {expr1; expr2} +let e_skip ?loc () = make_e ?loc @@ E_skip + +let e_list ?loc lst : expression = make_e ?loc @@ E_list lst +let e_set ?loc lst : expression = make_e ?loc @@ E_set lst +let e_map ?loc lst : expression = make_e ?loc @@ E_map lst +let e_big_map ?loc lst : expression = make_e ?loc @@ E_big_map lst +let e_look_up ?loc x y = make_e ?loc @@ E_look_up (x , y) + +let e_while ?loc condition body = make_e ?loc @@ E_while {condition; body} +let e_for ?loc binder start final increment body = make_e ?loc @@ E_for {binder;start;final;increment;body} +let e_for_each ?loc binder collection collection_type body = make_e ?loc @@ E_for_each {binder;collection;collection_type;body} -let e_cond ?loc condition then_clause else_clause = make_expr ?loc @@ E_cond {condition;then_clause;else_clause} -(* -let e_assign ?loc a b c = location_wrap ?loc @@ E_assign (Var.of_name a , b , c) (* TODO handlethat*) -*) let ez_match_variant (lst : ((string * string) * 'a) list) = let lst = List.map (fun ((c,n),a) -> ((Constructor c, Var.of_name n), a) ) lst in Match_variant (lst,()) @@ -142,18 +149,12 @@ let e_matching_variant ?loc a (lst : ((string * string)* 'a) list) = e_matching ?loc a (ez_match_variant lst) let e_record_ez ?loc (lst : (string * expr) list) : expression = let map = List.fold_left (fun m (x, y) -> LMap.add (Label x) y m) LMap.empty lst in - make_expr ?loc @@ E_record map + make_e ?loc @@ E_record map let e_record ?loc map = let lst = Map.String.to_kv_list map in e_record_ez ?loc lst -let e_record_update ?loc record path update = - let path = Label path in - make_expr ?loc @@ E_record_update {record; path; update} -let e_update ?loc record path update = e_record_update ?loc record path update -let e_tuple ?loc lst : expression = make_expr ?loc @@ E_tuple lst -let e_pair ?loc a b : expression = e_tuple ?loc [a;b] let make_option_typed ?loc e t_opt = match t_opt with @@ -175,22 +176,10 @@ let e_typed_big_map ?loc lst k v = e_annotation ?loc (e_big_map lst) (t_big_map let e_typed_set ?loc lst k = e_annotation ?loc (e_set lst) (t_set k) -let e_lambda ?loc (binder : expression_variable) - (input_type : type_expression option) - (output_type : type_expression option) - (result : expression) - : expression = - make_expr ?loc @@ E_lambda { - binder = binder ; - input_type = input_type ; - output_type = output_type ; - result ; - } -let e_recursive ?loc fun_name fun_type lambda = make_expr ?loc @@ E_recursive {fun_name; fun_type; lambda} let e_assign ?loc variable access_path expression = - make_expr ?loc @@ E_assign {variable;access_path;expression} + make_e ?loc @@ E_assign {variable;access_path;expression} let e_ez_assign ?loc variable access_path expression = let variable = Var.of_name variable in let access_path = List.map (fun s -> Access_record s) access_path in diff --git a/src/stages/1-ast_imperative/combinators.mli b/src/stages/1-ast_imperative/combinators.mli index 3d0ae94b9..b28bc4494 100644 --- a/src/stages/1-ast_imperative/combinators.mli +++ b/src/stages/1-ast_imperative/combinators.mli @@ -46,8 +46,8 @@ val t_map : type_expression -> type_expression -> type_expression val t_operator : type_operator -> type_expression list -> type_expression result val t_set : type_expression -> type_expression -val make_expr : ?loc:Location.t -> expression_content -> expression -val e_var : ?loc:Location.t -> string -> expression +val make_e : ?loc:Location.t -> expression_content -> expression + val e_literal : ?loc:Location.t -> literal -> expression val e_unit : ?loc:Location.t -> unit -> expression val e_int : ?loc:Location.t -> int -> expression @@ -65,36 +65,55 @@ val e'_bytes : string -> expression_content result val e_bytes_hex : ?loc:Location.t -> string -> expression result val e_bytes_raw : ?loc:Location.t -> bytes -> expression val e_bytes_string : ?loc:Location.t -> string -> expression -val e_big_map : ?loc:Location.t -> ( expr * expr ) list -> expression -val e_record_ez : ?loc:Location.t -> ( string * expr ) list -> expression -val e_tuple : ?loc:Location.t -> expression list -> expression +val e_binop : ?loc:Location.t -> constant' -> expression -> expression -> expression val e_some : ?loc:Location.t -> expression -> expression val e_none : ?loc:Location.t -> unit -> expression val e_string_cat : ?loc:Location.t -> expression -> expression -> expression val e_map_add : ?loc:Location.t -> expression -> expression -> expression -> expression -val e_map : ?loc:Location.t -> ( expression * expression ) list -> expression -val e_set : ?loc:Location.t -> expression list -> expression -val e_list : ?loc:Location.t -> expression list -> expression -val e_pair : ?loc:Location.t -> expression -> expression -> expression + +val e_constant : ?loc:Location.t -> constant' -> expression list -> expression +val e_variable : ?loc:Location.t -> expression_variable -> expression +val e_application : ?loc:Location.t -> expression -> expression -> expression +val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression +val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression +val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression + val e_constructor : ?loc:Location.t -> string -> expression -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression -val e_matching_bool : ?loc:Location.t -> expression -> expression -> expression -> expression -val e_accessor : ?loc:Location.t -> expression -> string -> expression -val e_accessor_list : ?loc:Location.t -> expression -> string list -> expression -val e_variable : ?loc:Location.t -> expression_variable -> expression -val e_skip : ?loc:Location.t -> unit -> expression -val e_sequence : ?loc:Location.t -> expression -> expression -> expression -val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression -val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression -val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression -val e_application : ?loc:Location.t -> expression -> expression -> expression -val e_binop : ?loc:Location.t -> constant' -> expression -> expression -> expression -val e_constant : ?loc:Location.t -> constant' -> expression list -> expression -val e_look_up : ?loc:Location.t -> expression -> expression -> expression val ez_match_variant : ((string * string ) * 'a ) list -> ('a,unit) matching_content val e_matching_variant : ?loc:Location.t -> expression -> ((string * string) * expression) list -> expression +val e_record : ?loc:Location.t -> expr Map.String.t -> expression +val e_record_ez : ?loc:Location.t -> ( string * expr ) list -> expression +val e_record_accessor : ?loc:Location.t -> expression -> string -> expression +val e_accessor_list : ?loc:Location.t -> expression -> string list -> expression +val e_record_update : ?loc:Location.t -> expression -> string -> expression -> expression + +val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression + +val e_tuple : ?loc:Location.t -> expression list -> expression +val e_tuple_accessor : ?loc:Location.t -> expression -> int -> expression +val e_tuple_update : ?loc:Location.t -> expression -> int -> expression -> expression +val e_pair : ?loc:Location.t -> expression -> expression -> expression + +val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression +val e_sequence : ?loc:Location.t -> expression -> expression -> expression +val e_skip : ?loc:Location.t -> unit -> expression + +val e_list : ?loc:Location.t -> expression list -> expression +val e_set : ?loc:Location.t -> expression list -> expression +val e_map : ?loc:Location.t -> ( expression * expression ) list -> expression +val e_big_map : ?loc:Location.t -> ( expr * expr ) list -> expression +val e_look_up : ?loc:Location.t -> expression -> expression -> expression + +val e_assign : ?loc:Location.t -> expression_variable -> access list -> expression -> expression +val e_ez_assign : ?loc:Location.t -> string -> string list -> expression -> expression + +val e_while : ?loc:Location.t -> expression -> expression -> expression +val e_for : ?loc:Location.t -> expression_variable -> expression -> expression -> expression -> expression -> expression +val e_for_each : ?loc:Location.t -> expression_variable * expression_variable option -> expression -> collect_type -> expression -> expression + val make_option_typed : ?loc:Location.t -> expression -> type_expression option -> expression val e_typed_none : ?loc:Location.t -> type_expression -> expression @@ -107,19 +126,7 @@ val e_typed_big_map : ?loc:Location.t -> ( expression * expression ) list -> ty val e_typed_set : ?loc:Location.t -> expression list -> type_expression -> expression -val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression -val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression -val e_record : ?loc:Location.t -> expr Map.String.t -> expression -val e_update : ?loc:Location.t -> expression -> string -> expression -> expression -val e_assign : ?loc:Location.t -> expression_variable -> access list -> expression -> expression -val e_ez_assign : ?loc:Location.t -> string -> string list -> expression -> expression -(* -val get_e_accessor : expression' -> ( expression * access_path ) result -*) -val e_while : ?loc:Location.t -> expression -> expression -> expression -val e_for : ?loc:Location.t -> expression_variable -> expression -> expression -> expression -> expression -> expression -val e_for_each : ?loc:Location.t -> expression_variable * expression_variable option -> expression -> collect_type -> expression -> expression val assert_e_accessor : expression_content -> unit result diff --git a/src/stages/2-ast_sugar/combinators.ml b/src/stages/2-ast_sugar/combinators.ml index 4edfc377f..611a33b77 100644 --- a/src/stages/2-ast_sugar/combinators.ml +++ b/src/stages/2-ast_sugar/combinators.ml @@ -79,83 +79,72 @@ let t_operator op lst: type_expression result = | TC_contract _ , [t] -> ok @@ t_contract t | _ , _ -> fail @@ bad_type_operator op -let make_expr ?(loc = Location.generated) expression_content = +let make_e ?(loc = Location.generated) expression_content = let location = loc in { expression_content; location } -let e_var ?loc (n: string) : expression = make_expr ?loc @@ E_variable (Var.of_name n) -let e_literal ?loc l : expression = make_expr ?loc @@ E_literal l -let e_unit ?loc () : expression = make_expr ?loc @@ E_literal (Literal_unit) -let e_int ?loc n : expression = make_expr ?loc @@ E_literal (Literal_int n) -let e_nat ?loc n : expression = make_expr ?loc @@ E_literal (Literal_nat n) -let e_timestamp ?loc n : expression = make_expr ?loc @@ E_literal (Literal_timestamp n) -let e_bool ?loc b : expression = make_expr ?loc @@ E_literal (Literal_bool b) -let e_string ?loc s : expression = make_expr ?loc @@ E_literal (Literal_string s) -let e_address ?loc s : expression = make_expr ?loc @@ E_literal (Literal_address s) -let e_mutez ?loc s : expression = make_expr ?loc @@ E_literal (Literal_mutez s) -let e_signature ?loc s : expression = make_expr ?loc @@ E_literal (Literal_signature s) -let e_key ?loc s : expression = make_expr ?loc @@ E_literal (Literal_key s) -let e_key_hash ?loc s : expression = make_expr ?loc @@ E_literal (Literal_key_hash s) -let e_chain_id ?loc s : expression = make_expr ?loc @@ E_literal (Literal_chain_id s) +let e_literal ?loc l : expression = make_e ?loc @@ E_literal l +let e_unit ?loc () : expression = make_e ?loc @@ E_literal (Literal_unit) +let e_int ?loc n : expression = make_e ?loc @@ E_literal (Literal_int n) +let e_nat ?loc n : expression = make_e ?loc @@ E_literal (Literal_nat n) +let e_timestamp ?loc n : expression = make_e ?loc @@ E_literal (Literal_timestamp n) +let e_bool ?loc b : expression = make_e ?loc @@ E_literal (Literal_bool b) +let e_string ?loc s : expression = make_e ?loc @@ E_literal (Literal_string s) +let e_address ?loc s : expression = make_e ?loc @@ E_literal (Literal_address s) +let e_mutez ?loc s : expression = make_e ?loc @@ E_literal (Literal_mutez s) +let e_signature ?loc s : expression = make_e ?loc @@ E_literal (Literal_signature s) +let e_key ?loc s : expression = make_e ?loc @@ E_literal (Literal_key s) +let e_key_hash ?loc s : expression = make_e ?loc @@ E_literal (Literal_key_hash s) +let e_chain_id ?loc s : expression = make_e ?loc @@ E_literal (Literal_chain_id s) let e'_bytes b : expression_content result = let%bind bytes = generic_try (simple_error "bad hex to bytes") (fun () -> Hex.to_bytes (`Hex b)) in ok @@ E_literal (Literal_bytes bytes) let e_bytes_hex ?loc b : expression result = let%bind e' = e'_bytes b in - ok @@ make_expr ?loc e' + ok @@ make_e ?loc e' let e_bytes_raw ?loc (b: bytes) : expression = - make_expr ?loc @@ E_literal (Literal_bytes b) + make_e ?loc @@ E_literal (Literal_bytes b) let e_bytes_string ?loc (s: string) : expression = - make_expr ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) -let e_some ?loc s : expression = make_expr ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} -let e_none ?loc () : expression = make_expr ?loc @@ E_constant {cons_name = C_NONE; arguments = []} -let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} -let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} -let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path = Label b} -let e_record_accessor_list ?loc a b = List.fold_left (fun a b -> e_record_accessor ?loc a b) a b -let e_variable ?loc v = make_expr ?loc @@ E_variable v -let e_let_in ?loc (binder, ascr) mut inline rhs let_result = - make_expr ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline; mut } -let e_application ?loc a b = make_expr ?loc @@ E_application {lamb=a ; args=b} -let e_constant ?loc name lst = make_expr ?loc @@ E_constant {cons_name=name ; arguments = lst} + make_e ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) +let e_some ?loc s : expression = make_e ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} +let e_none ?loc () : expression = make_e ?loc @@ E_constant {cons_name = C_NONE; arguments = []} -let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} +let e_constant ?loc name lst = make_e ?loc @@ E_constant {cons_name=name ; arguments = lst} +let e_variable ?loc v = make_e ?loc @@ E_variable v +let e_application ?loc a b = make_e ?loc @@ E_application {lamb=a ; args=b} +let e_lambda ?loc binder input_type output_type result : expression = make_e ?loc @@ E_lambda {binder; input_type; output_type; result} +let e_recursive ?loc fun_name fun_type lambda = make_e ?loc @@ E_recursive {fun_name; fun_type; lambda} +let e_let_in ?loc (binder, ascr) mut inline rhs let_result = make_e ?loc @@ E_let_in { let_binder = (binder, ascr) ; rhs ; let_result; inline; mut } -let e_cond ?loc condition then_clause else_clause = make_expr ?loc @@ E_cond {condition;then_clause;else_clause} -let e_sequence ?loc expr1 expr2 = make_expr ?loc @@ E_sequence {expr1; expr2} -let e_skip ?loc () = make_expr ?loc @@ E_skip +let e_constructor ?loc s a : expression = make_e ?loc @@ E_constructor { constructor = Constructor s; element = a} +let e_matching ?loc a b : expression = make_e ?loc @@ E_matching {matchee=a;cases=b} -let e_list ?loc lst : expression = make_expr ?loc @@ E_list lst -let e_set ?loc lst : expression = make_expr ?loc @@ E_set lst -let e_map ?loc lst : expression = make_expr ?loc @@ E_map lst -let e_big_map ?loc lst : expression = make_expr ?loc @@ E_big_map lst -let e_look_up ?loc a b : expression = make_expr ?loc @@ E_look_up (a,b) +let e_record ?loc map : expression = make_e ?loc @@ E_record map +let e_record_accessor ?loc record path = make_e ?loc @@ E_record_accessor {record; path} +let e_record_update ?loc record path update = make_e ?loc @@ E_record_update {record; path; update} -let ez_match_variant (lst : ((string * string) * 'a) list) = - let lst = List.map (fun ((c,n),a) -> ((Constructor c, Var.of_name n), a) ) lst in - Match_variant (lst,()) -let e_matching_variant ?loc a (lst : ((string * string)* 'a) list) = - e_matching ?loc a (ez_match_variant lst) -let e_record_ez ?loc (lst : (string * expr) list) : expression = - let map = List.fold_left (fun m (x, y) -> LMap.add (Label x) y m) LMap.empty lst in - make_expr ?loc @@ E_record map -let e_record ?loc map = - let lst = Map.String.to_kv_list map in - e_record_ez ?loc lst -let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path= Label b} +let e_annotation ?loc anno_expr ty = make_e ?loc @@ E_ascription {anno_expr; type_annotation = ty} -let e_record_update ?loc record path update = - let path = Label path in - make_expr ?loc @@ E_record_update {record; path; update} +let e_tuple ?loc lst : expression = make_e ?loc @@ E_tuple lst +let e_tuple_accessor ?loc tuple path = make_e ?loc @@ E_tuple_accessor {tuple; path} +let e_tuple_update ?loc tuple path update = make_e ?loc @@ E_tuple_update {tuple; path; update} +let e_pair ?loc a b : expression = e_tuple ?loc [a;b] + +let e_cond ?loc condition then_clause else_clause = make_e ?loc @@ E_cond {condition;then_clause;else_clause} +let e_sequence ?loc expr1 expr2 = make_e ?loc @@ E_sequence {expr1; expr2} +let e_skip ?loc () = make_e ?loc @@ E_skip + +let e_list ?loc lst : expression = make_e ?loc @@ E_list lst +let e_set ?loc lst : expression = make_e ?loc @@ E_set lst +let e_map ?loc lst : expression = make_e ?loc @@ E_map lst +let e_big_map ?loc lst : expression = make_e ?loc @@ E_big_map lst +let e_look_up ?loc a b : expression = make_e ?loc @@ E_look_up (a,b) let make_option_typed ?loc e t_opt = match t_opt with | None -> e | Some t -> e_annotation ?loc e t -let e_tuple ?loc lst : expression = e_record_ez ?loc (tuple_to_record lst) -let e_pair ?loc a b : expression = e_tuple ?loc [a;b] let e_typed_none ?loc t_opt = let type_annotation = t_option t_opt in @@ -170,18 +159,6 @@ let e_typed_big_map ?loc lst k v = e_annotation ?loc (e_big_map lst) (t_big_map let e_typed_set ?loc lst k = e_annotation ?loc (e_set lst) (t_set k) -let e_lambda ?loc (binder : expression_variable) - (input_type : type_expression option) - (output_type : type_expression option) - (result : expression) - : expression = - make_expr ?loc @@ E_lambda { - binder = binder ; - input_type = input_type ; - output_type = output_type ; - result ; - } -let e_recursive ?loc fun_name fun_type lambda = make_expr ?loc @@ E_recursive {fun_name; fun_type; lambda} let get_e_record_accessor = fun t -> match t with diff --git a/src/stages/2-ast_sugar/combinators.mli b/src/stages/2-ast_sugar/combinators.mli index 1fa10df89..c21951617 100644 --- a/src/stages/2-ast_sugar/combinators.mli +++ b/src/stages/2-ast_sugar/combinators.mli @@ -46,8 +46,7 @@ val t_map : type_expression -> type_expression -> type_expression val t_operator : type_operator -> type_expression list -> type_expression result val t_set : type_expression -> type_expression -val make_expr : ?loc:Location.t -> expression_content -> expression -val e_var : ?loc:Location.t -> string -> expression +val make_e : ?loc:Location.t -> expression_content -> expression val e_literal : ?loc:Location.t -> literal -> expression val e_unit : ?loc:Location.t -> unit -> expression val e_int : ?loc:Location.t -> int -> expression @@ -77,13 +76,17 @@ val e_application : ?loc:Location.t -> expression -> expression -> expression val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> bool -> expression -> expression -> expression -val e_record : ?loc:Location.t -> expr Map.String.t -> expression -val e_record_update : ?loc:Location.t -> expression -> string -> expression -> expression -val e_record_accessor : ?loc:Location.t -> expression -> string -> expression -val e_record_accessor_list : ?loc:Location.t -> expression -> string list -> expression +val e_record : ?loc:Location.t -> expr label_map -> expression +val e_record_accessor : ?loc:Location.t -> expression -> label -> expression +val e_record_update : ?loc:Location.t -> expression -> label -> expression -> expression val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression +val e_tuple : ?loc:Location.t -> expression list -> expression +val e_tuple_accessor : ?loc:Location.t -> expression -> int -> expression +val e_tuple_update : ?loc:Location.t -> expression -> int -> expression -> expression +val e_pair : ?loc:Location.t -> expression -> expression -> expression + val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression val e_sequence : ?loc:Location.t -> expression -> expression -> expression val e_skip : ?loc:Location.t -> unit -> expression @@ -95,9 +98,6 @@ val e_big_map : ?loc:Location.t -> ( expr * expr ) list -> expression val e_look_up : ?loc:Location.t -> expression -> expression -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression -val e_matching_bool : ?loc:Location.t -> expression -> expression -> expression -> expression -val ez_match_variant : ((string * string ) * 'a ) list -> ('a,unit) matching_content -val e_matching_variant : ?loc:Location.t -> expression -> ((string * string) * expression) list -> expression val make_option_typed : ?loc:Location.t -> expression -> type_expression option -> expression @@ -109,9 +109,6 @@ val e_typed_map : ?loc:Location.t -> ( expression * expression ) list -> type_e val e_typed_big_map : ?loc:Location.t -> ( expression * expression ) list -> type_expression -> type_expression -> expression val e_typed_set : ?loc:Location.t -> expression list -> type_expression -> expression -val e_record_ez : ?loc:Location.t -> (string * expression) list -> expression -val e_tuple : ?loc:Location.t -> expression list -> expression -val e_pair : ?loc:Location.t -> expression -> expression -> expression val assert_e_accessor : expression_content -> unit result diff --git a/src/stages/3-ast_core/combinators.ml b/src/stages/3-ast_core/combinators.ml index f5f5f103b..6d739acc2 100644 --- a/src/stages/3-ast_core/combinators.ml +++ b/src/stages/3-ast_core/combinators.ml @@ -79,112 +79,64 @@ let t_operator op lst: type_expression result = | TC_contract _ , [t] -> ok @@ t_contract t | _ , _ -> fail @@ bad_type_operator op -let make_expr ?(loc = Location.generated) expression_content = - let location = loc in - { expression_content; location } +let make_e ?(loc = Location.generated) expression_content = { expression_content; location=loc } -let e_var ?loc (n: string) : expression = make_expr ?loc @@ E_variable (Var.of_name n) -let e_literal ?loc l : expression = make_expr ?loc @@ E_literal l -let e_unit ?loc () : expression = make_expr ?loc @@ E_literal (Literal_unit) -let e_int ?loc n : expression = make_expr ?loc @@ E_literal (Literal_int n) -let e_nat ?loc n : expression = make_expr ?loc @@ E_literal (Literal_nat n) -let e_timestamp ?loc n : expression = make_expr ?loc @@ E_literal (Literal_timestamp n) -let e_bool ?loc b : expression = make_expr ?loc @@ E_literal (Literal_bool b) -let e_string ?loc s : expression = make_expr ?loc @@ E_literal (Literal_string s) -let e_address ?loc s : expression = make_expr ?loc @@ E_literal (Literal_address s) -let e_mutez ?loc s : expression = make_expr ?loc @@ E_literal (Literal_mutez s) -let e_signature ?loc s : expression = make_expr ?loc @@ E_literal (Literal_signature s) -let e_key ?loc s : expression = make_expr ?loc @@ E_literal (Literal_key s) -let e_key_hash ?loc s : expression = make_expr ?loc @@ E_literal (Literal_key_hash s) -let e_chain_id ?loc s : expression = make_expr ?loc @@ E_literal (Literal_chain_id s) +let e_var ?loc (n: string) : expression = make_e ?loc @@ E_variable (Var.of_name n) +let e_literal ?loc l : expression = make_e ?loc @@ E_literal l +let e_unit ?loc () : expression = make_e ?loc @@ E_literal (Literal_unit) +let e_int ?loc n : expression = make_e ?loc @@ E_literal (Literal_int n) +let e_nat ?loc n : expression = make_e ?loc @@ E_literal (Literal_nat n) +let e_timestamp ?loc n : expression = make_e ?loc @@ E_literal (Literal_timestamp n) +let e_bool ?loc b : expression = make_e ?loc @@ E_literal (Literal_bool b) +let e_string ?loc s : expression = make_e ?loc @@ E_literal (Literal_string s) +let e_address ?loc s : expression = make_e ?loc @@ E_literal (Literal_address s) +let e_mutez ?loc s : expression = make_e ?loc @@ E_literal (Literal_mutez s) +let e_signature ?loc s : expression = make_e ?loc @@ E_literal (Literal_signature s) +let e_key ?loc s : expression = make_e ?loc @@ E_literal (Literal_key s) +let e_key_hash ?loc s : expression = make_e ?loc @@ E_literal (Literal_key_hash s) +let e_chain_id ?loc s : expression = make_e ?loc @@ E_literal (Literal_chain_id s) let e'_bytes b : expression_content result = let%bind bytes = generic_try (simple_error "bad hex to bytes") (fun () -> Hex.to_bytes (`Hex b)) in ok @@ E_literal (Literal_bytes bytes) let e_bytes_hex ?loc b : expression result = let%bind e' = e'_bytes b in - ok @@ make_expr ?loc e' + ok @@ make_e ?loc e' let e_bytes_raw ?loc (b: bytes) : expression = - make_expr ?loc @@ E_literal (Literal_bytes b) + make_e ?loc @@ E_literal (Literal_bytes b) let e_bytes_string ?loc (s: string) : expression = - make_expr ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) -let e_some ?loc s : expression = make_expr ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} -let e_none ?loc () : expression = make_expr ?loc @@ E_constant {cons_name = C_NONE; arguments = []} -let e_string_cat ?loc sl sr : expression = make_expr ?loc @@ E_constant {cons_name = C_CONCAT; arguments = [sl ; sr ]} -let e_map_add ?loc k v old : expression = make_expr ?loc @@ E_constant {cons_name = C_MAP_ADD; arguments = [k ; v ; old]} -let e_constructor ?loc s a : expression = make_expr ?loc @@ E_constructor { constructor = Constructor s; element = a} -let e_matching ?loc a b : expression = make_expr ?loc @@ E_matching {matchee=a;cases=b} -let e_matching_bool ?loc a b c : expression = e_matching ?loc a (Match_bool {match_true = b ; match_false = c}) -let e_record_accessor ?loc a b = make_expr ?loc @@ E_record_accessor {record = a; path = Label b} -let e_record_accessor_list ?loc a b = List.fold_left (fun a b -> e_record_accessor ?loc a b) a b -let e_variable ?loc v = make_expr ?loc @@ E_variable v -let e_let_in ?loc (binder, ascr) inline rhs let_result = - make_expr ?loc @@ E_let_in { let_binder = (binder,ascr) ; rhs ; let_result; inline } -let e_annotation ?loc anno_expr ty = make_expr ?loc @@ E_ascription {anno_expr; type_annotation = ty} -let e_application ?loc a b = make_expr ?loc @@ E_application {lamb=a ; args=b} -let e_binop ?loc name a b = make_expr ?loc @@ E_constant {cons_name = name ; arguments = [a ; b]} -let e_constant ?loc name lst = make_expr ?loc @@ E_constant {cons_name=name ; arguments = lst} -let e_cond ?loc expr match_true match_false = e_matching expr ?loc (Match_bool {match_true; match_false}) -(* -let e_assign ?loc a b c = location_wrap ?loc @@ E_assign (Var.of_name a , b , c) (* TODO handlethat*) -*) -let ez_match_variant (lst : ((string * string) * 'a) list) = - let lst = List.map (fun ((c,n),a) -> ((Constructor c, Var.of_name n), a) ) lst in - Match_variant (lst,()) -let e_matching_variant ?loc a (lst : ((string * string)* 'a) list) = - e_matching ?loc a (ez_match_variant lst) -let e_record_ez ?loc (lst : (string * expr) list) : expression = - let map = List.fold_left (fun m (x, y) -> LMap.add (Label x) y m) LMap.empty lst in - make_expr ?loc @@ E_record map -let e_record ?loc map = - let lst = Map.String.to_kv_list map in - e_record_ez ?loc lst + make_e ?loc @@ E_literal (Literal_bytes (Hex.to_bytes (Hex.of_string s))) +let e_some ?loc s : expression = make_e ?loc @@ E_constant {cons_name = C_SOME; arguments = [s]} +let e_none ?loc () : expression = make_e ?loc @@ E_constant {cons_name = C_NONE; arguments = []} +let e_string_cat ?loc sl sr : expression = make_e ?loc @@ E_constant {cons_name = C_CONCAT; arguments = [sl ; sr ]} +let e_map_add ?loc k v old : expression = make_e ?loc @@ E_constant {cons_name = C_MAP_ADD; arguments = [k ; v ; old]} -let e_record_update ?loc record path update = - let path = Label path in - make_expr ?loc @@ E_record_update {record; path; update} +let e_constant ?loc name lst = make_e ?loc @@ E_constant {cons_name=name ; arguments = lst} +let e_variable ?loc v = make_e ?loc @@ E_variable v +let e_application ?loc a b = make_e ?loc @@ E_application {lamb=a ; args=b} +let e_lambda ?loc binder input_type output_type result = make_e ?loc @@ E_lambda {binder; input_type; output_type; result ; } +let e_recursive ?loc fun_name fun_type lambda = make_e ?loc @@ E_recursive {fun_name; fun_type; lambda} +let e_let_in ?loc (binder, ascr) inline rhs let_result = make_e ?loc @@ E_let_in { let_binder = (binder,ascr) ; rhs ; let_result; inline } -let e_tuple ?loc lst : expression = e_record_ez ?loc (tuple_to_record lst) -let e_pair ?loc a b : expression = e_tuple ?loc [a;b] +let e_constructor ?loc s a : expression = make_e ?loc @@ E_constructor { constructor = Constructor s; element = a} +let e_matching ?loc a b : expression = make_e ?loc @@ E_matching {matchee=a;cases=b} + +let e_record ?loc map = make_e ?loc @@ E_record map +let e_record_accessor ?loc a b = make_e ?loc @@ E_record_accessor {record = a; path = Label b} +let e_record_update ?loc record path update = make_e ?loc @@ E_record_update {record; path; update} + +let e_annotation ?loc anno_expr ty = make_e ?loc @@ E_ascription {anno_expr; type_annotation = ty} let make_option_typed ?loc e t_opt = match t_opt with | None -> e | Some t -> e_annotation ?loc e t - let e_typed_none ?loc t_opt = let type_annotation = t_option t_opt in e_annotation ?loc (e_none ?loc ()) type_annotation -let e_lambda ?loc (binder : expression_variable) - (input_type : type_expression option) - (output_type : type_expression option) - (result : expression) - : expression = - make_expr ?loc @@ E_lambda { - binder = binder ; - input_type = input_type ; - output_type = output_type ; - result ; - } -let e_recursive ?loc fun_name fun_type lambda = make_expr ?loc @@ E_recursive {fun_name; fun_type; lambda} -let e_assign_with_let ?loc var access_path expr = - let var = Var.of_name (var) in - match access_path with - | [] -> (var, None), true, expr, false - - | lst -> - let rec aux path record= match path with - | [] -> failwith "acces_path cannot be empty" - | [e] -> e_record_update ?loc record e expr - | elem::tail -> - let next_record = e_record_accessor record elem in - e_record_update ?loc record elem (aux tail next_record ) - in - (var, None), true, (aux lst (e_variable var)), false - let get_e_record_accessor = fun t -> match t with | E_record_accessor {record; path} -> ok (record, path) diff --git a/src/stages/3-ast_core/combinators.mli b/src/stages/3-ast_core/combinators.mli index e09ede186..56f3d2b35 100644 --- a/src/stages/3-ast_core/combinators.mli +++ b/src/stages/3-ast_core/combinators.mli @@ -46,7 +46,7 @@ val t_map : type_expression -> type_expression -> type_expression val t_operator : type_operator -> type_expression list -> type_expression result val t_set : type_expression -> type_expression -val make_expr : ?loc:Location.t -> expression_content -> expression +val make_e : ?loc:Location.t -> expression_content -> expression val e_var : ?loc:Location.t -> string -> expression val e_literal : ?loc:Location.t -> literal -> expression val e_unit : ?loc:Location.t -> unit -> expression @@ -66,27 +66,18 @@ val e_bytes_hex : ?loc:Location.t -> string -> expression result val e_bytes_raw : ?loc:Location.t -> bytes -> expression val e_bytes_string : ?loc:Location.t -> string -> expression -val e_record_ez : ?loc:Location.t -> ( string * expr ) list -> expression -val e_tuple : ?loc:Location.t -> expression list -> expression val e_some : ?loc:Location.t -> expression -> expression val e_none : ?loc:Location.t -> unit -> expression val e_string_cat : ?loc:Location.t -> expression -> expression -> expression val e_map_add : ?loc:Location.t -> expression -> expression -> expression -> expression -val e_pair : ?loc:Location.t -> expression -> expression -> expression val e_constructor : ?loc:Location.t -> string -> expression -> expression val e_matching : ?loc:Location.t -> expression -> matching_expr -> expression -val e_matching_bool : ?loc:Location.t -> expression -> expression -> expression -> expression val e_record_accessor : ?loc:Location.t -> expression -> string -> expression -val e_record_accessor_list : ?loc:Location.t -> expression -> string list -> expression val e_variable : ?loc:Location.t -> expression_variable -> expression -val e_cond: ?loc:Location.t -> expression -> expression -> expression -> expression val e_let_in : ?loc:Location.t -> ( expression_variable * type_expression option ) -> bool -> expression -> expression -> expression val e_annotation : ?loc:Location.t -> expression -> type_expression -> expression val e_application : ?loc:Location.t -> expression -> expression -> expression -val e_binop : ?loc:Location.t -> constant' -> expression -> expression -> expression val e_constant : ?loc:Location.t -> constant' -> expression list -> expression -val ez_match_variant : ((string * string ) * 'a ) list -> ('a,unit) matching_content -val e_matching_variant : ?loc:Location.t -> expression -> ((string * string) * expression) list -> expression val make_option_typed : ?loc:Location.t -> expression -> type_expression option -> expression @@ -94,9 +85,8 @@ val e_typed_none : ?loc:Location.t -> type_expression -> expression val e_lambda : ?loc:Location.t -> expression_variable -> type_expression option -> type_expression option -> expression -> expression val e_recursive : ?loc:Location.t -> expression_variable -> type_expression -> lambda -> expression -val e_record : ?loc:Location.t -> expr Map.String.t -> expression -val e_record_update : ?loc:Location.t -> expression -> string -> expression -> expression -val e_assign_with_let : ?loc:Location.t -> string -> string list -> expression -> ((expression_variable*type_expression option)*bool*expression*bool) +val e_record : ?loc:Location.t -> expr label_map-> expression +val e_record_update : ?loc:Location.t -> expression -> label -> expression -> expression (* val get_e_accessor : expression' -> ( expression * access_path ) result diff --git a/src/stages/4-ast_typed/combinators.ml b/src/stages/4-ast_typed/combinators.ml index 8f4aaac2a..2c6e50590 100644 --- a/src/stages/4-ast_typed/combinators.ml +++ b/src/stages/4-ast_typed/combinators.ml @@ -24,7 +24,7 @@ module Errors = struct end let make_t type_content core = { type_content ; type_meta=core } -let make_a_e ?(location = Location.generated) expression_content type_expression environment = { +let make_e ?(location = Location.generated) expression_content type_expression environment = { expression_content ; type_expression ; environment ; @@ -299,22 +299,22 @@ let e_application lamb args : expression_content = E_application {lamb;args} let e_variable v : expression_content = E_variable v let e_let_in let_binder inline rhs let_result = E_let_in { let_binder ; rhs ; let_result; inline } -let e_a_unit = make_a_e (e_unit ()) (t_unit ()) -let e_a_int n = make_a_e (e_int n) (t_int ()) -let e_a_nat n = make_a_e (e_nat n) (t_nat ()) -let e_a_mutez n = make_a_e (e_mutez n) (t_mutez ()) -let e_a_bool b = make_a_e (e_bool b) (t_bool ()) -let e_a_string s = make_a_e (e_string s) (t_string ()) -let e_a_address s = make_a_e (e_address s) (t_address ()) -let e_a_pair a b = make_a_e (e_pair a b) (t_pair a.type_expression b.type_expression ()) -let e_a_some s = make_a_e (e_some s) (t_option s.type_expression ()) -let e_a_lambda l in_ty out_ty = make_a_e (e_lambda l) (t_function in_ty out_ty ()) -let e_a_none t = make_a_e (e_none ()) (t_option t ()) -let e_a_record r = make_a_e (e_record r) (t_record (LMap.map get_type_expression r) ()) -let e_a_application a b = make_a_e (e_application a b) (get_type_expression b) -let e_a_variable v ty = make_a_e (e_variable v) ty -let ez_e_a_record r = make_a_e (ez_e_record r) (ez_t_record (List.map (fun (x, y) -> x, y.type_expression) r) ()) -let e_a_let_in binder expr body attributes = make_a_e (e_let_in binder expr body attributes) (get_type_expression body) +let e_a_unit = make_e (e_unit ()) (t_unit ()) +let e_a_int n = make_e (e_int n) (t_int ()) +let e_a_nat n = make_e (e_nat n) (t_nat ()) +let e_a_mutez n = make_e (e_mutez n) (t_mutez ()) +let e_a_bool b = make_e (e_bool b) (t_bool ()) +let e_a_string s = make_e (e_string s) (t_string ()) +let e_a_address s = make_e (e_address s) (t_address ()) +let e_a_pair a b = make_e (e_pair a b) (t_pair a.type_expression b.type_expression ()) +let e_a_some s = make_e (e_some s) (t_option s.type_expression ()) +let e_a_lambda l in_ty out_ty = make_e (e_lambda l) (t_function in_ty out_ty ()) +let e_a_none t = make_e (e_none ()) (t_option t ()) +let e_a_record r = make_e (e_record r) (t_record (LMap.map get_type_expression r) ()) +let e_a_application a b = make_e (e_application a b) (get_type_expression b) +let e_a_variable v ty = make_e (e_variable v) ty +let ez_e_a_record r = make_e (ez_e_record r) (ez_t_record (List.map (fun (x, y) -> x, y.type_expression) r) ()) +let e_a_let_in binder expr body attributes = make_e (e_let_in binder expr body attributes) (get_type_expression body) let get_a_int (t:expression) = diff --git a/src/stages/4-ast_typed/combinators.mli b/src/stages/4-ast_typed/combinators.mli index fadef09e3..6b865e119 100644 --- a/src/stages/4-ast_typed/combinators.mli +++ b/src/stages/4-ast_typed/combinators.mli @@ -3,7 +3,7 @@ open Types val make_n_t : type_variable -> type_expression -> named_type_content val make_t : type_content -> S.type_expression option -> type_expression -val make_a_e : ?location:Location.t -> expression_content -> type_expression -> full_environment -> expression +val make_e : ?location:Location.t -> expression_content -> type_expression -> full_environment -> expression val t_bool : ?s:S.type_expression -> unit -> type_expression val t_string : ?s:S.type_expression -> unit -> type_expression diff --git a/src/stages/4-ast_typed/combinators_environment.ml b/src/stages/4-ast_typed/combinators_environment.ml index f73c2b305..5d9ecddb6 100644 --- a/src/stages/4-ast_typed/combinators_environment.ml +++ b/src/stages/4-ast_typed/combinators_environment.ml @@ -1,7 +1,7 @@ open Types open Combinators -let make_a_e_empty expression type_annotation = make_a_e expression type_annotation Environment.full_empty +let make_a_e_empty expression type_annotation = make_e expression type_annotation Environment.full_empty let e_a_empty_unit = e_a_unit Environment.full_empty let e_a_empty_int n = e_a_int n Environment.full_empty diff --git a/src/test/typer_tests.ml b/src/test/typer_tests.ml index 57b06a7b7..3943a561e 100644 --- a/src/test/typer_tests.ml +++ b/src/test/typer_tests.ml @@ -51,7 +51,7 @@ module TestExpressions = struct let tuple () : unit result = test_expression - I.(e_tuple [e_int 32; e_string "foo"]) + I.(e_record @@ LMap.of_list [(Label "0",e_int 32); (Label "1",e_string "foo")]) O.(make_t_ez_record [("0",t_int ()); ("1",t_string ())]) let constructor () : unit result = @@ -64,7 +64,7 @@ module TestExpressions = struct let record () : unit result = test_expression - I.(e_record_ez [("foo", e_int 32); ("bar", e_string "foo")]) + I.(e_record @@ LMap.of_list [(Label "foo", e_int 32); (Label "bar", e_string "foo")]) O.(make_t_ez_record [("foo", t_int ()); ("bar", t_string ())]) diff --git a/test.mligo b/test.mligo deleted file mode 100644 index f197fc1da..000000000 --- a/test.mligo +++ /dev/null @@ -1,8 +0,0 @@ -let rec fibo2 ((n,n_1,n_0):int*int*int) : int = - let fibo2 : int -> int = fun (k : int) -> k in - if (n < 2) then n_1 else fibo2 3 - -let main (p,s : unit * int) : operation list * int = - let x : int = fibo2 (5, 1, 1) in - (([] : operation list), x) -