From b1b2dc849ab975fe9e3202c3024bcad8dab76b5c Mon Sep 17 00:00:00 2001 From: radrow Date: Tue, 11 May 2021 11:39:33 +0200 Subject: [PATCH] Fix some tests. Remove optimization of singleton tuples --- priv/stdlib/List.aes | 9 ++++++--- src/aeso_ast_infer_types.erl | 9 ++++++--- src/aeso_ast_to_fcode.erl | 4 ++-- src/aeso_fcode_to_fate.erl | 4 ---- test/aeso_compiler_tests.erl | 6 +++--- test/contracts/clone.aes | 4 +++- test/contracts/clone_simple.aes | 2 +- test/contracts/test.aes | 36 ++++++++++----------------------- 8 files changed, 32 insertions(+), 42 deletions(-) diff --git a/priv/stdlib/List.aes b/priv/stdlib/List.aes index faf853c..9493037 100644 --- a/priv/stdlib/List.aes +++ b/priv/stdlib/List.aes @@ -208,10 +208,13 @@ namespace List = [] => false h::t => if(p(h)) true else any(p, t) - function sum(l : list(int)) : int = foldl ((a, b) => a + b, 0, l) - - function product(l : list(int)) : int = foldl((a, b) => a * b, 1, l) + function sum(l : list(int)) : int = switch(l) + [] => 0 + h::t => h + sum(t) + function product(l : list(int)) : int = switch(l) + [] => 1 + h::t => h * sum(t) /** Zips two list by applying bimapping function on respective elements. * Drops the tail of the longer list. diff --git a/src/aeso_ast_infer_types.erl b/src/aeso_ast_infer_types.erl index 88e7e43..f673607 100644 --- a/src/aeso_ast_infer_types.erl +++ b/src/aeso_ast_infer_types.erl @@ -283,10 +283,13 @@ bind_contract({Contract, Ann, Id, Contents}, Env) [ {field_t, Sys, {id, Sys, "address"}, {id, Sys, "address"}} ] ++ [ {field_t, Sys, {id, Sys, ?CONSTRUCTOR_MOCK_NAME}, contract_call_type( - case [ {fun_t, [stateful,payable|Sys], [], [ArgT || {typed, _, _, ArgT} <- Args], {id, Sys, "void"}} - || {letfun, _, {id, _, "init"}, Args, _, _} <- Contents] of + case [ [ArgT || {typed, _, _, ArgT} <- Args] + || {letfun, _, {id, _, "init"}, Args, _, _} <- Contents] + ++ [ Args || {fun_decl, _, {id, _, "init"}, {fun_t, _, _, Args, _}} <- Contents] + ++ [ Args || {fun_decl, _, {id, _, "init"}, {type_sig, _, _, _, Args, _}} <- Contents] + of [] -> {fun_t, [stateful,payable|Sys], [], [], {id, Sys, "void"}}; - [T] -> T + [Args] -> {fun_t, [stateful,payable|Sys], [], Args, {id, Sys, "void"}} end ) } diff --git a/src/aeso_ast_to_fcode.erl b/src/aeso_ast_to_fcode.erl index 54db9e0..9d596d4 100644 --- a/src/aeso_ast_to_fcode.erl +++ b/src/aeso_ast_to_fcode.erl @@ -715,14 +715,14 @@ expr_to_fcode(Env, Type, {app, _, Fun = {typed, _, FunE, {fun_t, _, NamedArgsT, case ArgsT of var_args -> fcode_error({var_args_not_set, FunE}); _ -> - FInitArgsT = {typerep, {tuple, [type_to_fcode(Env, T) || T <- ArgsT]}}, + FInitArgsT = aeb_fate_data:make_typerep({tuple, [type_to_fcode(Env, T) || T <- ArgsT]}), builtin_to_fcode(state_layout(Env), chain_clone, [{lit, FInitArgsT}|FArgs]) end; {builtin_u, chain_create, _Ar} -> case {ArgsT, Type} of {var_args, _} -> fcode_error({var_args_not_set, FunE}); {_, {con, _, Contract}} -> - FInitArgsT = {typerep, {tuple, [type_to_fcode(Env, T) || T <- ArgsT]}}, + FInitArgsT = aeb_fate_data:make_typerep({tuple, [type_to_fcode(Env, T) || T <- ArgsT]}), builtin_to_fcode(state_layout(Env), chain_create, [{lit, {contract_code, Contract}}, {lit, FInitArgsT}|FArgs]); {_, _} -> fcode_error({not_a_contract_type, Type}) end; diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index ba4ade7..f27405a 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -125,7 +125,6 @@ type_to_scode(bits) -> bits; type_to_scode(any) -> any; type_to_scode({variant, Cons}) -> {variant, [{tuple, types_to_scode(Con)} || Con <- Cons]}; type_to_scode({list, Type}) -> {list, type_to_scode(Type)}; -type_to_scode({tuple, [Type]}) -> type_to_scode(Type); type_to_scode({tuple, Types}) -> {tuple, types_to_scode(Types)}; type_to_scode({map, Key, Val}) -> {map, type_to_scode(Key), type_to_scode(Val)}; type_to_scode({function, _Args, _Res}) -> {tuple, [string, any]}; @@ -590,9 +589,6 @@ builtin_to_scode(Env, chain_create, [Code, TypeRep, Value | InitArgs] ). - - - %% -- Operators -- op_to_scode('+') -> aeb_fate_ops:add(?a, ?a, ?a); diff --git a/test/aeso_compiler_tests.erl b/test/aeso_compiler_tests.erl index feb75d2..2691b03 100644 --- a/test/aeso_compiler_tests.erl +++ b/test/aeso_compiler_tests.erl @@ -127,9 +127,9 @@ compile(Backend, Name, Options) -> %% compilable_contracts() -> [ContractName]. %% The currently compilable contracts. -compilable_contracts() -> ["test"]; % FIXME remove compilable_contracts() -> - ["complex_types", + ["test", + "complex_types", "counter", "dutch_auction", "environment", @@ -182,7 +182,7 @@ compilable_contracts() -> "protected_call", "hermetization_turnoff", "multiple_contracts", - "clone", "clone_simple" + "clone", "clone_simple", "create" ]. not_compilable_on(fate) -> []; diff --git a/test/contracts/clone.aes b/test/contracts/clone.aes index d66408d..0b3b04a 100644 --- a/test/contracts/clone.aes +++ b/test/contracts/clone.aes @@ -5,13 +5,15 @@ contract interface HigherOrderState = stateful entrypoint inc : int => unit contract interface LowerDisorderAnarchy = - entrypoint init : int => void + entrypoint init : (int) => void main contract C = + // both `s` and `l` should be of type `HigherOrderState` in this test stateful entrypoint run_clone(s : HigherOrderState, l : LowerDisorderAnarchy) : HigherOrderState = let s1 = Chain.clone(ref=s) let Some(s2) = Chain.clone(ref=s, protected=true) + let None = Chain.clone(ref=s, protected=true, gas=1) let None = Chain.clone(ref=l, protected=true, 123) let s3 = Chain.clone(ref=s1) require(s1.apply(2137) == 2137, "APPLY_S1_0") diff --git a/test/contracts/clone_simple.aes b/test/contracts/clone_simple.aes index 72333e3..f51fa64 100644 --- a/test/contracts/clone_simple.aes +++ b/test/contracts/clone_simple.aes @@ -2,6 +2,6 @@ contract interface I = entrypoint init : () => void contract C = - entrypoint f(i : I) = + stateful entrypoint f(i : I) = let Some(c1) = Chain.clone(ref=i, protected = true) 2 \ No newline at end of file diff --git a/test/contracts/test.aes b/test/contracts/test.aes index 016860d..2a0aab7 100644 --- a/test/contracts/test.aes +++ b/test/contracts/test.aes @@ -1,28 +1,14 @@ -contract T = - entrypoint f(x, y) = 3 -main contract IntegerAdderFactory = - stateful payable entrypoint calculateSomething(x, t) = -// let t = Chain.create(value = 5555) : T - t.f(protected = true, gas = 999999999, value = 777777, 111, 222) -// t.f() +include "List.aes" -/* -contract IntegerAdder = - entrypoint init() = () - entrypoint addIntegers(x, y) = x + y - -contract IntegerAdderHolder = - type state = IntegerAdder - entrypoint init() = Chain.create() : IntegerAdder +contract IntegerHolder = + type state = int + entrypoint init(x) = x entrypoint get() = state -namespace IntegerAdderFactory = - function new() = - let i = Chain.create() : IntegerAdderHolder - i.get() - -main contract EnterpriseContract = - entrypoint calculateSomething(x) = - let adder = IntegerAdderFactory.new() - adder.addIntegers(x, 2137) -*/ \ No newline at end of file +main contract IntegerCollection = + record state = {template: IntegerHolder, payload: list(IntegerHolder)} + stateful entrypoint init() = {template = Chain.create(0), payload = []} + stateful entrypoint add(x) = + put(state{payload @ p = Chain.clone(ref=state.template, x) :: p}) + x + entrypoint sum() = List.sum(List.map((h) => h.get(), state.payload)) \ No newline at end of file