From 79ae92a068eb70e7f6ef2e5025a8af55f2df69c1 Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Mon, 5 Aug 2019 14:01:35 +0200 Subject: [PATCH 1/2] Add missing error message --- src/aeso_ast_infer_types.erl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 697845b..725323d 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -156,7 +156,7 @@ bind_tvars(Xs, Env) -> check_tvar(#env{ typevars = TVars}, T = {tvar, _, X}) -> case TVars == unrestricted orelse lists:member(X, TVars) of true -> ok; - false -> type_error({unbound_type_variable, T}) + false -> type_error({unbound_type, T}) end, T. @@ -2160,6 +2160,8 @@ pp_error({contract_has_no_entrypoints, Con}) -> io_lib:format("The contract ~s (at ~s) has no entrypoints. Since Sophia version 3.2, public\n" "contract functions must be declared with the 'entrypoint' keyword instead of\n" "'function'.\n", [pp_expr("", Con), pp_loc(Con)]); +pp_error({unbound_type, Type}) -> + io_lib:format("Unbound type ~s (at ~s).\n", [pp_type("", Type), pp_loc(Type)]); pp_error(Err) -> io_lib:format("Unknown error: ~p\n", [Err]). From 2d6381dc6fdce5b5f139db9948e3f9de1a8d5466 Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Mon, 5 Aug 2019 14:36:40 +0200 Subject: [PATCH 2/2] Generate INIT function which writes the state instead of returning it --- src/aeso_ast_to_fcode.erl | 21 +++++++++++++++++---- src/aeso_fcode_to_fate.erl | 4 ++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/aeso_ast_to_fcode.erl b/src/aeso_ast_to_fcode.erl index d50e7e9..f4b3f13 100644 --- a/src/aeso_ast_to_fcode.erl +++ b/src/aeso_ast_to_fcode.erl @@ -239,7 +239,8 @@ to_fcode(Env, [{contract, _, {con, _, Main}, Decls}]) -> #{ contract_name => Main, state_type => StateType, event_type => EventType, - functions => add_event_function(Env1, EventType, Funs) }; + functions => add_init_function(Env1, + add_event_function(Env1, EventType, Funs)) }; to_fcode(Env, [{contract, _, {con, _, Con}, Decls} | Code]) -> Env1 = decls_to_fcode(Env#{ context => {abstract_contract, Con} }, Decls), to_fcode(Env1, Code); @@ -819,6 +820,19 @@ builtin_to_fcode(Builtin, Args) -> false -> {builtin, Builtin, Args} end. +%% -- Init function -- + +add_init_function(_Env, Funs) -> + InitName = {entrypoint, <<"init">>}, + InitFun = #{ args := InitArgs } = + case maps:get(InitName, Funs, none) of + none -> #{ attrs => [], args => [], return => {tuple, []}, body => {tuple, []} }; + Info -> Info + end, + Vars = [ {var, X} || {X, _} <- InitArgs ], + Funs#{ init => InitFun#{ return => {tuple, []}, + body => {builtin, set_state, [{def, InitName, Vars}]} } }. + %% -- Event function -- add_event_function(_Env, none, Funs) -> Funs; @@ -1009,8 +1023,7 @@ make_fun_name(#{ context := Context }, Ann, Name) -> Entrypoint = proplists:get_value(entrypoint, Ann, false), case Context of {main_contract, Main} -> - if Name == "init" -> init; - Entrypoint -> {entrypoint, list_to_binary(Name)}; + if Entrypoint -> {entrypoint, list_to_binary(Name)}; true -> {local_fun, [Main, Name]} end; {namespace, Lib} -> @@ -1288,7 +1301,7 @@ pp_fun(Name, #{ args := Args, return := Return, body := Body }) -> pp_text(" : "), pp_ftype(Return), pp_text(" =")]), prettypr:nest(2, pp_fexpr(Body))). -pp_fun_name(init) -> pp_text(init); +pp_fun_name(init) -> pp_text('INIT'); pp_fun_name(event) -> pp_text(event); pp_fun_name({entrypoint, E}) -> pp_text(binary_to_list(E)); pp_fun_name({local_fun, Q}) -> pp_text(string:join(Q, ".")). diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index 7ac1441..071004f 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -139,7 +139,7 @@ compile(FCode, Options) -> make_function_id(X) -> aeb_fate_code:symbol_identifier(make_function_name(X)). -make_function_name(init) -> <<"init">>; +make_function_name(init) -> <<"INIT">>; make_function_name(event) -> <<"Chain.event">>; make_function_name({entrypoint, Name}) -> Name; make_function_name({local_fun, Xs}) -> list_to_binary("." ++ string:join(Xs, ".")). @@ -196,7 +196,7 @@ add_default_init_function(SFuns, StateType) when StateType /= {tuple, []} -> SFuns; add_default_init_function(SFuns, {tuple, []}) -> %% Only add default if the init function is not present - InitName = make_function_name(init), + InitName = make_function_name({entrypoint, <<"init">>}), case maps:find(InitName, SFuns) of {ok, _} -> SFuns;