Pattern guards for functions and switch statements (#339)

* Add case guards to parser

* Add pattern guards to infer types and fcode generation

* Add functions guards

* Add test for patterns guards

* Update docs

* Update CHANGELOG.md

* Remove stateful context from Env for guards

* Elaborate on guards

* Add failing test for stateful pattern guards

* Implement multiple guards

* Fix tests

* Disable aevm related tests

* Split the sentence before if and otherwise

* Fix type in docs

* Implement multiple exprs in the same guard

* Fix pretty printing

* Change tests to include multiple guards

* Add test for non-boolean guards

* Desugar clauses with guards

* Fix incomplete patterns bug

* Fix docs

* Compile to icode when no guards are used

* Revert "Disable aevm related tests"

This reverts commit e828099bd97dffe11438f2e48f3a92ce3641e85b.
This commit is contained in:
Gaith Hallak
2021-10-20 11:04:00 +03:00
committed by GitHub
parent 20cab3ae57
commit a982f25262
17 changed files with 245 additions and 74 deletions
+1 -1
View File
@@ -59,7 +59,7 @@ calldata_aci_test_() ->
end} || {ContractName, Fun, Args} <- compilable_contracts()].
parse_args(Fun, Args) ->
[{contract_main, _, _, [{letfun, _, _, _, _, {app, _, _, AST}}]}] =
[{contract_main, _, _, [{letfun, _, _, _, _, [{guarded, _, [], {app, _, _, AST}}]}]}] =
aeso_parser:string("main contract Temp = function foo() = " ++ Fun ++ "(" ++ string:join(Args, ", ") ++ ")"),
strip_ann(AST).
+9
View File
@@ -202,6 +202,7 @@ compilable_contracts() ->
"child_contract_init_bug",
"using_namespace",
"assign_patterns",
"patterns_guards",
"test" % Custom general-purpose test file. Keep it last on the list.
].
@@ -808,6 +809,14 @@ failing_contracts() ->
[<<?Pos(8,23)
"Unbound variable g at line 8, column 23">>
])
, ?TYPE_ERROR(stateful_pattern_guard,
[<<?Pos(8,12)
"Cannot reference stateful function g (at line 8, column 12) in a pattern guard.">>
])
, ?TYPE_ERROR(non_boolean_pattern_guard,
[<<?Pos(4,24)
"Cannot unify string\n and bool\nwhen checking the type of the expression at line 4, column 24\n \"y\" : string\nagainst the expected type\n bool">>
])
].
-define(Path(File), "code_errors/" ??File).
+1 -1
View File
@@ -17,7 +17,7 @@ simple_contracts_test_() ->
?assertMatch(
[{contract_main, _, {con, _, "Identity"},
[{letfun, _, {id, _, "id"}, [{id, _, "x"}], {id, _, "_"},
{id, _, "x"}}]}], parse_string(Text)),
[{guarded, _, [], {id, _, "x"}}]}]}], parse_string(Text)),
ok
end},
{"Operator precedence test.",
@@ -0,0 +1,4 @@
contract C =
type state = int
entrypoint init(x) | "y" = 1
+19
View File
@@ -0,0 +1,19 @@
include "List.aes"
contract C =
type state = int
entrypoint init() = f([1, 2, 3, 4])
function
f(x::[])
| x > 1, x < 10 = 1
| x < 1 = 9
f(x::y::[]) = 2
f(x::y::z) = switch(z)
[] => 4
a::[]
| a > 10, a < 20 => 5
| a > 5 => 8
b | List.length(b) > 5 => 6
c => 7
+10
View File
@@ -0,0 +1,10 @@
contract C =
type state = int
entrypoint init() = f(4)
function
f(x) | x > 0 = 1
f(x) | g(x) = 2
stateful function g(x) = x < 0