Compare commits
60 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cf46c9e303 | |||
| 89971fb275 | |||
| cdc7b901e6 | |||
| f266c5eed8 | |||
| 4d9d3077ad | |||
| 3efde2a2a1 | |||
| edc37bcf1b | |||
| 20f2a05638 | |||
| c2a5ed28cf | |||
| 56f70fea6c | |||
| 9e908369ec | |||
| 9984679a24 | |||
| 8f27168908 | |||
| 8619f47ee6 | |||
| 0d56130baa | |||
| 7448da16bb | |||
| 6f582af83e | |||
| 931f2d3dcb | |||
| 5d116b2e5a | |||
| cea581988d | |||
| 2f36380a81 | |||
| d330133b3f | |||
| 6fccc902d0 | |||
| eb77a73d15 | |||
| 62aa06cc3a | |||
| 6d6fff2612 | |||
| 95bf0d4b6c | |||
| e94c1f9d84 | |||
| a263b09e57 | |||
| 5a3c8530b4 | |||
| 4c79f7b9f2 | |||
| e0fff00e64 | |||
| 7c95aafbb8 | |||
| 5a4a84805f | |||
| cc3e322179 | |||
| f16d699f6d | |||
| 7b474e439c | |||
| eb926b1352 | |||
| dc4a2ca2f9 | |||
| f866e24624 | |||
| ccad660eac | |||
| 202a06a580 | |||
| b5b0d30fc4 | |||
| 236ef6eb89 | |||
| aa6d56ce9b | |||
| 0b86cdc318 | |||
| 27cbedc7ab | |||
| 2ac47059c1 | |||
| 421bc01012 | |||
| 2b7490776e | |||
| 0a5b80668f | |||
| 6cdba58e35 | |||
| 8262d7780f | |||
| e6f01481bf | |||
| d9188d58a7 | |||
| dfa286d43c | |||
| 478da2af33 | |||
| 10be09fe30 | |||
| e6c9d0fac1 | |||
| 367f87b612 |
@@ -1,3 +0,0 @@
|
|||||||
{build, "rebar3 as eqc compile"}.
|
|
||||||
{test_root, "."}.
|
|
||||||
{test_path, "_build/eqc/lib/aesophia/quickcheck"}. %% here are the properties
|
|
||||||
+1
-3
@@ -16,8 +16,6 @@ _build
|
|||||||
.idea
|
.idea
|
||||||
*.iml
|
*.iml
|
||||||
rebar3.crashdump
|
rebar3.crashdump
|
||||||
current_counterexample.eqc
|
|
||||||
.qcci
|
|
||||||
*.erl~
|
*.erl~
|
||||||
*.aes~
|
*.aes~
|
||||||
|
aesophia
|
||||||
|
|||||||
@@ -9,8 +9,17 @@ It is an OTP application written in Erlang and is by default included in
|
|||||||
also be included in other systems to compile contracts coded in sophia which
|
also be included in other systems to compile contracts coded in sophia which
|
||||||
can then be loaded into the æternity system.
|
can then be loaded into the æternity system.
|
||||||
|
|
||||||
|
## Versioning
|
||||||
|
|
||||||
|
`aesophia` has a version that is only loosely connected to the version of the
|
||||||
|
Aeternity node - in principle they will share the major version but not
|
||||||
|
minor/patch version. The `aesophia` compiler version MUST be bumped whenever
|
||||||
|
there is a change in how byte code is generated, but it MAY also be bumped upon
|
||||||
|
API changes etc.
|
||||||
|
|
||||||
## Interface Modules
|
## Interface Modules
|
||||||
|
|
||||||
The basic modules for interfacing the compiler:
|
The basic modules for interfacing the compiler:
|
||||||
|
|
||||||
* [aeso_compiler: the Sophia compiler](./docs/aeso_compiler.md)
|
* [aeso_compiler: the Sophia compiler](./docs/aeso_compiler.md)
|
||||||
|
* [aeso_aci: the ACI interface](./docs/aeso_aci.md)
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
# About this release
|
||||||
|
|
||||||
|
This is the `aesophia` compiler version 2.0.0. The main changes compared to version 1.2.0 are:
|
||||||
|
|
||||||
|
* Add `Crypto.ecverify` to the compiler.
|
||||||
|
* Add `Crypto.sha3`, `Crypto.blake2`, `Crypto.sha256`, `String.blake2` and
|
||||||
|
`String.sha256` to the compiler.
|
||||||
|
* Add the `bits` type for working with bit fields in Sophia.
|
||||||
|
* Use native bit shift operations in builtin functions, reducing gas cost.
|
||||||
|
* Add Namespaces to Sophia in order to simplify using library contracts, etc.
|
||||||
|
* Simplify calldata creation - instead of passing a compiled contract, simply
|
||||||
|
pass a (stubbed) contract string.
|
||||||
|
* Add a missig type check on the `init` function - detects programmer errors earlier.
|
||||||
|
* Improve type checking of `record` fields - generates more understandable error messages.
|
||||||
|
* Improved, more coherent, error messages.
|
||||||
|
* Add the ACI (Aeternity Contract Interface) generator.
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
# aeso_aci
|
||||||
|
|
||||||
|
### Module
|
||||||
|
|
||||||
|
### aeso_aci
|
||||||
|
|
||||||
|
The ACI interface encoder and decoder.
|
||||||
|
|
||||||
|
### Description
|
||||||
|
|
||||||
|
This module provides an interface to generate and convert between
|
||||||
|
Sophia contracts and a suitable JSON encoding of contract
|
||||||
|
interface. As yet the interface is very basic.
|
||||||
|
|
||||||
|
Encoding this contract:
|
||||||
|
|
||||||
|
```
|
||||||
|
contract Answers =
|
||||||
|
record state = { a : answers }
|
||||||
|
type answers() = map(string, int)
|
||||||
|
|
||||||
|
stateful function init() = { a = {} }
|
||||||
|
private function the_answer() = 42
|
||||||
|
function new_answer(q : string, a : int) : answers() = { [q] = a }
|
||||||
|
```
|
||||||
|
|
||||||
|
generates the following JSON structure representing the contract interface:
|
||||||
|
|
||||||
|
|
||||||
|
``` json
|
||||||
|
{
|
||||||
|
"contract": {
|
||||||
|
"name": "Answers",
|
||||||
|
"type_defs": [
|
||||||
|
{
|
||||||
|
"name": "state",
|
||||||
|
"vars": [],
|
||||||
|
"typedef": "{a : map(string,int)}"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "answers",
|
||||||
|
"vars": [],
|
||||||
|
"typedef": "map(string,int)"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"functions": [
|
||||||
|
{
|
||||||
|
"name": "init",
|
||||||
|
"arguments": [],
|
||||||
|
"type": "{a : map(string,int)}",
|
||||||
|
"stateful": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "new_answer",
|
||||||
|
"arguments": [
|
||||||
|
{
|
||||||
|
"name": "q",
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "a",
|
||||||
|
"type": "int"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"type": "map(string,int)",
|
||||||
|
"stateful": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
When that encoding is decoded the following include definition is generated:
|
||||||
|
|
||||||
|
```
|
||||||
|
contract Answers =
|
||||||
|
function new_answer : (string, int) => map(string,int)
|
||||||
|
```
|
||||||
|
|
||||||
|
### Types
|
||||||
|
``` erlang
|
||||||
|
contract_string() = string() | binary()
|
||||||
|
json_string() = binary()
|
||||||
|
```
|
||||||
|
|
||||||
|
### Exports
|
||||||
|
|
||||||
|
#### encode(ContractString) -> {ok,JSONstring} | {error,ErrorString}
|
||||||
|
|
||||||
|
Types
|
||||||
|
|
||||||
|
``` erlang
|
||||||
|
ConstractString = contract_string()
|
||||||
|
JSONstring = json_string()
|
||||||
|
```
|
||||||
|
|
||||||
|
Generate the JSON encoding of the interface to a contract. The type definitions and non-private functions are included in the JSON string.
|
||||||
|
|
||||||
|
#### decode(JSONstring) -> ConstractString.
|
||||||
|
|
||||||
|
Types
|
||||||
|
|
||||||
|
``` erlang
|
||||||
|
ConstractString = contract_string()
|
||||||
|
JSONstring = json_string()
|
||||||
|
```
|
||||||
|
|
||||||
|
Take a JSON encoding of a contract interface and generate and generate a contract definition which can be included in another contract.
|
||||||
|
|
||||||
|
### Example run
|
||||||
|
|
||||||
|
This is an example of using the ACI generator from an Erlang shell. The file called `aci_test.aes` contains the contract in the description from which we want to generate files `aci_test.json` which is the JSON encoding of the contract interface and `aci_test.include` which is the contract definition to be included inside another contract.
|
||||||
|
|
||||||
|
``` erlang
|
||||||
|
1> {ok,Contract} = file:read_file("aci_test.aes").
|
||||||
|
{ok,<<"contract Answers =\n record state = { a : answers }\n type answers() = map(string, int)\n\n stateful function"...>>}
|
||||||
|
2> {ok,Encoding} = aeso_aci:encode(Contract).
|
||||||
|
<<"{\"contract\":{\"name\":\"Answers\",\"type_defs\":[{\"name\":\"state\",\"vars\":[],\"typedef\":\"{a : map(string,int)}\"},{\"name\":\"ans"...>>
|
||||||
|
3> file:write_file("aci_test.aci", Encoding).
|
||||||
|
ok
|
||||||
|
4> Decoded = aeso_aci:decode(Encoding).
|
||||||
|
<<"contract Answers =\n function new_answer : (string, int) => map(string,int)\n">>
|
||||||
|
5> file:write_file("aci_test.include", Decoded).
|
||||||
|
ok
|
||||||
|
6> jsx:prettify(Encoding).
|
||||||
|
<<"{\n \"contract\": {\n \"name\": \"Answers\",\n \"type_defs\": [\n {\n \"name\": \"state\",\n \"vars\": [],\n "...>>
|
||||||
|
```
|
||||||
|
|
||||||
|
The final call to `jsx:prettify(Encoding)` returns the encoding in a
|
||||||
|
more easily readable form. This is what is shown in the description
|
||||||
|
above.
|
||||||
|
|
||||||
|
### Notes
|
||||||
|
|
||||||
|
The ACI generator currently cannot properly handle types defined using `datatype`.
|
||||||
@@ -15,7 +15,7 @@ returns the compiled module in a map which can then be loaded.
|
|||||||
``` erlang
|
``` erlang
|
||||||
contract_string() = string() | binary()
|
contract_string() = string() | binary()
|
||||||
contract_map() = #{bytecode => binary(),
|
contract_map() = #{bytecode => binary(),
|
||||||
compiler_version => string(),
|
compiler_version => binary(),
|
||||||
contract_souce => string(),
|
contract_souce => string(),
|
||||||
type_info => type_info()}
|
type_info => type_info()}
|
||||||
type_info()
|
type_info()
|
||||||
@@ -75,12 +75,12 @@ Types
|
|||||||
|
|
||||||
Get the type representation of a type declaration.
|
Get the type representation of a type declaration.
|
||||||
|
|
||||||
#### version() -> Version
|
#### version() -> {ok, Version} | {error, term()}
|
||||||
|
|
||||||
Types
|
Types
|
||||||
|
|
||||||
``` erlang
|
``` erlang
|
||||||
Version = integer()
|
Version = binary()
|
||||||
```
|
```
|
||||||
|
|
||||||
Get the current version of the Sophia compiler.
|
Get the current version of the Sophia compiler.
|
||||||
|
|||||||
@@ -1,109 +0,0 @@
|
|||||||
%%% File : aeso_heap_eqc.erl
|
|
||||||
%%% Author : Ulf Norell
|
|
||||||
%%% Description :
|
|
||||||
%%% Created : 28 May 2018 by Ulf Norell
|
|
||||||
-module(aeso_heap_eqc).
|
|
||||||
|
|
||||||
-compile([export_all, nowarn_export_all]).
|
|
||||||
-include_lib("eqc/include/eqc.hrl").
|
|
||||||
|
|
||||||
-define(SANDBOX(Code), sandbox(fun() -> Code end)).
|
|
||||||
|
|
||||||
sandbox(Code) ->
|
|
||||||
Parent = self(),
|
|
||||||
Tag = make_ref(),
|
|
||||||
{Pid, Ref} = spawn_monitor(fun() -> Parent ! {Tag, Code()} end),
|
|
||||||
receive
|
|
||||||
{Tag, Res} -> erlang:demonitor(Ref, [flush]), {ok, Res};
|
|
||||||
{'DOWN', Ref, process, Pid, Reason} -> {error, Reason}
|
|
||||||
after 100 ->
|
|
||||||
exit(Pid, kill),
|
|
||||||
{error, loop}
|
|
||||||
end.
|
|
||||||
|
|
||||||
prop_from_binary() ->
|
|
||||||
?FORALL({T, Bin}, {type(), blob()},
|
|
||||||
begin
|
|
||||||
Tag = fun(X) when is_atom(X) -> X; (X) when is_tuple(X) -> element(1, X) end,
|
|
||||||
case ?SANDBOX(aeso_heap:from_binary(T, Bin)) of
|
|
||||||
{ok, Res} -> collect({Tag(T), element(1, Res)}, true);
|
|
||||||
Err -> equals(Err, {ok, '_'})
|
|
||||||
end end).
|
|
||||||
|
|
||||||
type() -> ?LET(Depth, choose(0, 2), type(Depth, true)).
|
|
||||||
type(Depth, TypeRep) ->
|
|
||||||
oneof(
|
|
||||||
[ elements([word, string] ++ [typerep || TypeRep]) ] ++
|
|
||||||
[ ?LETSHRINK([T], [type(Depth - 1, TypeRep)], {list, T}) || Depth > 0 ] ++
|
|
||||||
[ ?LETSHRINK([T], [type(Depth - 1, TypeRep)], {option, T}) || Depth > 0 ] ++
|
|
||||||
[ ?LETSHRINK(Ts, list(type(Depth - 1, TypeRep)), {tuple, Ts}) || Depth > 0 ] ++
|
|
||||||
[ ?LETSHRINK([K, V], vector(2, type(Depth - 1, TypeRep)), {map, K, V}) || Depth > 0 ] ++
|
|
||||||
[]
|
|
||||||
).
|
|
||||||
|
|
||||||
blob() ->
|
|
||||||
?LET(Blobs, list(oneof([ ?LET(Ws, words(), return(from_words(Ws)))
|
|
||||||
, binary() ])),
|
|
||||||
return(list_to_binary(Blobs))).
|
|
||||||
|
|
||||||
words() -> list(word()).
|
|
||||||
word() ->
|
|
||||||
frequency(
|
|
||||||
[ {4, ?LET(N, nat(), 32 * N)}
|
|
||||||
, {1, choose(0, 320)}
|
|
||||||
, {2, -1}
|
|
||||||
, {2, elements(["foo", "zzzzz"])} ]).
|
|
||||||
|
|
||||||
from_words(Ws) ->
|
|
||||||
<< <<(from_word(W))/binary>> || W <- Ws >>.
|
|
||||||
|
|
||||||
from_word(W) when is_integer(W) ->
|
|
||||||
<<W:256>>;
|
|
||||||
from_word(S) when is_list(S) ->
|
|
||||||
Len = length(S),
|
|
||||||
Bin = <<(list_to_binary(S))/binary, 0:(32 - Len)/unit:8>>,
|
|
||||||
<<Len:256, Bin/binary>>.
|
|
||||||
|
|
||||||
typerep() -> ?LET(Depth, choose(0, 2),
|
|
||||||
?LET(T, type(Depth, true), return(typerep(T)))).
|
|
||||||
|
|
||||||
typerep(word) -> word;
|
|
||||||
typerep(string) -> string;
|
|
||||||
typerep(typerep) -> typerep;
|
|
||||||
typerep({tuple, Ts}) -> {tuple, typerep(Ts)};
|
|
||||||
typerep({list, T}) -> {list, typerep(T)};
|
|
||||||
typerep({variant, Cs}) -> {variant, typerep(Cs)};
|
|
||||||
typerep({option, T}) -> {variant, [[], [typerep(T)]]};
|
|
||||||
typerep({map, K, V}) -> {list, typerep({tuple, [K, V]})};
|
|
||||||
typerep([]) -> [];
|
|
||||||
typerep([T | Ts]) -> [typerep(T) | typerep(Ts)].
|
|
||||||
|
|
||||||
value(word) ->
|
|
||||||
<<N:256>> = <<(-1):256>>,
|
|
||||||
choose(0, N);
|
|
||||||
value(string) ->
|
|
||||||
?LET(N, choose(0, 128), binary(N));
|
|
||||||
value(typerep) ->
|
|
||||||
typerep();
|
|
||||||
value({list, T}) ->
|
|
||||||
list(value(T));
|
|
||||||
value({option, T}) ->
|
|
||||||
weighted_default({1, none}, {3, {some, value(T)}});
|
|
||||||
value({tuple, Ts}) ->
|
|
||||||
?LET(Vs, [ value(T) || T <- Ts ], list_to_tuple(Vs));
|
|
||||||
value({map, K, V}) ->
|
|
||||||
map(value(K), value(V));
|
|
||||||
value({variant, Cs}) ->
|
|
||||||
?LET(I, choose(0, length(Cs) - 1),
|
|
||||||
{variant, I, [ value(T) || T <- lists:nth(I + 1, Cs) ]}).
|
|
||||||
|
|
||||||
typed_val() ->
|
|
||||||
?LET(T, type(), ?LET(V, value(T), return({T, V}))).
|
|
||||||
|
|
||||||
prop_roundtrip() ->
|
|
||||||
?FORALL(T, type(),
|
|
||||||
?FORALL(V, value(T),
|
|
||||||
?FORALL(B, choose(0, 4),
|
|
||||||
equals(aeso_heap:from_binary(T, aeso_heap:to_binary(V, B * 32), B * 32),
|
|
||||||
{ok, V})))).
|
|
||||||
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
%%% File : aeso_utils_eqc.erl
|
|
||||||
%%% Author : Ulf Norell
|
|
||||||
%%% Description :
|
|
||||||
%%% Created : 2 Jul 2018 by Ulf Norell
|
|
||||||
-module(aeso_utils_eqc).
|
|
||||||
|
|
||||||
-compile([export_all, nowarn_export_all]).
|
|
||||||
|
|
||||||
-include_lib("eqc/include/eqc.hrl").
|
|
||||||
|
|
||||||
%% QuickCheck property
|
|
||||||
|
|
||||||
graph() ->
|
|
||||||
?LET(M, map(choose(0, 10), list(choose(0, 10))),
|
|
||||||
return(complete(M))).
|
|
||||||
|
|
||||||
complete(G) ->
|
|
||||||
Is = lists:usort(lists:concat(maps:values(G))),
|
|
||||||
maps:merge(maps:from_list([ {I, []} || I <- Is ]), G).
|
|
||||||
|
|
||||||
prop_scc() ->
|
|
||||||
?FORALL(G, graph(),
|
|
||||||
begin
|
|
||||||
SCCs = aeso_utils:scc(G),
|
|
||||||
BadSCC = fun({acyclic, I}) -> reachable_from(G, I, I);
|
|
||||||
({cyclic, Is}) -> [] /= [ {I, J} || I <- Is, J <- Is, not reachable_from(G, I, J) ]
|
|
||||||
end,
|
|
||||||
ToList = fun({acyclic, I}) -> [I];
|
|
||||||
({cyclic, Is}) -> Is end,
|
|
||||||
?WHENFAIL(eqc:format("SCCs = ~p\n", [SCCs]),
|
|
||||||
conjunction(
|
|
||||||
[ {elems, equals(lists:sort(lists:flatmap(ToList, SCCs)), lists:sort(maps:keys(G)))}
|
|
||||||
, {sorted, equals([], [ {I, J} || {I, Js} <- maps:to_list(G),
|
|
||||||
J <- Js,
|
|
||||||
find_component(I, SCCs) < find_component(J, SCCs) ])}
|
|
||||||
, {precise, equals([], [ SCC || SCC <- SCCs, BadSCC(SCC) ])}
|
|
||||||
]))
|
|
||||||
end).
|
|
||||||
|
|
||||||
reachable_from(Graph, I, J) ->
|
|
||||||
reachable_from1(Graph, maps:get(I, Graph, []), J).
|
|
||||||
|
|
||||||
reachable_from1(_, [], _) -> false;
|
|
||||||
reachable_from1(_, [I | _], I) -> true;
|
|
||||||
reachable_from1(Graph, [I | Is], J) ->
|
|
||||||
case maps:get(I, Graph, undefined) of
|
|
||||||
undefined -> reachable_from1(Graph, Is, J);
|
|
||||||
Js -> reachable_from1(maps:remove(I, Graph), Js ++ Is, J)
|
|
||||||
end.
|
|
||||||
|
|
||||||
find_component(X, SCCs) ->
|
|
||||||
ISCCs = lists:zip(SCCs, lists:seq(1, length(SCCs))),
|
|
||||||
HasX = fun({acyclic, Y}) -> X == Y;
|
|
||||||
({cyclic, Ys}) -> lists:member(X, Ys) end,
|
|
||||||
case [ I || {SCC, I} <- ISCCs, HasX(SCC) ] of
|
|
||||||
[I | _] -> I;
|
|
||||||
[] -> false
|
|
||||||
end.
|
|
||||||
|
|
||||||
+4
-7
@@ -1,17 +1,14 @@
|
|||||||
|
%% -*- mode: erlang; indent-tabs-mode: nil -*-
|
||||||
|
|
||||||
{erl_opts, [debug_info]}.
|
{erl_opts, [debug_info]}.
|
||||||
|
|
||||||
{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git",
|
{deps, [ {aebytecode, {git, "https://github.com/aeternity/aebytecode.git",
|
||||||
{ref,"720510a"}}}
|
{ref,"720510a"}}}
|
||||||
, {getopt, "1.0.1"}
|
, {getopt, "1.0.1"}
|
||||||
|
, {jsx, {git, "https://github.com/talentdeficit/jsx.git",
|
||||||
|
{tag, "2.8.0"}}}
|
||||||
]}.
|
]}.
|
||||||
|
|
||||||
|
|
||||||
{profiles, [ {eqc, [{erl_opts, [{parse_transform, eqc_cover}]},
|
|
||||||
{deps, [{eqc_ci, "1.0.0"}]},
|
|
||||||
{extra_src_dirs, ["quickcheck"]} %% May not be called eqc!
|
|
||||||
]}
|
|
||||||
]}.
|
|
||||||
|
|
||||||
{escript_incl_apps, [aesophia, aebytecode, getopt]}.
|
{escript_incl_apps, [aesophia, aebytecode, getopt]}.
|
||||||
{escript_main_app, aesophia}.
|
{escript_main_app, aesophia}.
|
||||||
{escript_name, aesophia}.
|
{escript_name, aesophia}.
|
||||||
|
|||||||
+5
-1
@@ -3,7 +3,11 @@
|
|||||||
{git,"https://github.com/aeternity/aebytecode.git",
|
{git,"https://github.com/aeternity/aebytecode.git",
|
||||||
{ref,"720510a24de32c9bad6486f34ca7babde124bf1e"}},
|
{ref,"720510a24de32c9bad6486f34ca7babde124bf1e"}},
|
||||||
0},
|
0},
|
||||||
{<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},0}]}.
|
{<<"getopt">>,{pkg,<<"getopt">>,<<"1.0.1">>},0},
|
||||||
|
{<<"jsx">>,
|
||||||
|
{git,"https://github.com/talentdeficit/jsx.git",
|
||||||
|
{ref,"3074d4865b3385a050badf7828ad31490d860df5"}},
|
||||||
|
0}]}.
|
||||||
[
|
[
|
||||||
{pkg_hash,[
|
{pkg_hash,[
|
||||||
{<<"getopt">>, <<"C73A9FA687B217F2FF79F68A3B637711BB1936E712B521D8CE466B29CBF7808A">>}]}
|
{<<"getopt">>, <<"C73A9FA687B217F2FF79F68A3B637711BB1936E712B521D8CE466B29CBF7808A">>}]}
|
||||||
|
|||||||
+9
-39
@@ -11,7 +11,7 @@
|
|||||||
-define(HASH_SIZE, 32).
|
-define(HASH_SIZE, 32).
|
||||||
|
|
||||||
-export([ old_create_calldata/3
|
-export([ old_create_calldata/3
|
||||||
, create_calldata/5
|
, create_calldata/4
|
||||||
, check_calldata/2
|
, check_calldata/2
|
||||||
, function_type_info/3
|
, function_type_info/3
|
||||||
, function_type_hash/3
|
, function_type_hash/3
|
||||||
@@ -39,22 +39,12 @@
|
|||||||
%%%===================================================================
|
%%%===================================================================
|
||||||
%%% Handle calldata
|
%%% Handle calldata
|
||||||
|
|
||||||
create_calldata(Contract, FunName, Args, ArgTypes, RetType) ->
|
create_calldata(FunName, Args, ArgTypes0, RetType) ->
|
||||||
case get_type_info_and_hash(Contract, FunName) of
|
ArgTypes = {tuple, ArgTypes0},
|
||||||
{ok, TypeInfo, TypeHashInt} ->
|
<<TypeHashInt:?HASH_SIZE/unit:8>> =
|
||||||
Data = aeso_heap:to_binary({TypeHashInt, list_to_tuple(Args)}),
|
function_type_hash(list_to_binary(FunName), ArgTypes, RetType),
|
||||||
case check_calldata(Data, TypeInfo) of
|
Data = aeso_heap:to_binary({TypeHashInt, list_to_tuple(Args)}),
|
||||||
{ok, CallDataType, OutType} ->
|
{ok, Data, {tuple, [word, ArgTypes]}, RetType}.
|
||||||
case check_given_type(FunName, ArgTypes, RetType, CallDataType, OutType) of
|
|
||||||
ok ->
|
|
||||||
{ok, Data, CallDataType, OutType};
|
|
||||||
{error, _} = Err ->
|
|
||||||
Err
|
|
||||||
end;
|
|
||||||
{error,_What} = Err -> Err
|
|
||||||
end;
|
|
||||||
{error, _} = Err -> Err
|
|
||||||
end.
|
|
||||||
|
|
||||||
get_type_info_and_hash(#{type_info := TypeInfo}, FunName) ->
|
get_type_info_and_hash(#{type_info := TypeInfo}, FunName) ->
|
||||||
FunBin = list_to_binary(FunName),
|
FunBin = list_to_binary(FunName),
|
||||||
@@ -64,26 +54,6 @@ get_type_info_and_hash(#{type_info := TypeInfo}, FunName) ->
|
|||||||
{error, _} = Err -> Err
|
{error, _} = Err -> Err
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% Check that the given type matches the type from the metadata.
|
|
||||||
check_given_type(FunName, GivenArgs, GivenRet, CalldataType, ExpectRet) ->
|
|
||||||
{tuple, [word, {tuple, ExpectArgs}]} = CalldataType,
|
|
||||||
ReturnOk = if FunName == "init" -> true;
|
|
||||||
GivenRet == any -> true;
|
|
||||||
true -> GivenRet == ExpectRet
|
|
||||||
end,
|
|
||||||
ArgsOk = ExpectArgs == GivenArgs,
|
|
||||||
case ReturnOk andalso ArgsOk of
|
|
||||||
true -> ok;
|
|
||||||
false when FunName == "init" ->
|
|
||||||
{error, {init_args_mismatch,
|
|
||||||
{given, GivenArgs},
|
|
||||||
{expected, ExpectArgs}}};
|
|
||||||
false ->
|
|
||||||
{error, {call_type_mismatch,
|
|
||||||
{given, GivenArgs, '=>', GivenRet},
|
|
||||||
{expected, ExpectArgs, '=>', ExpectRet}}}
|
|
||||||
end.
|
|
||||||
|
|
||||||
-spec check_calldata(binary(), type_info()) ->
|
-spec check_calldata(binary(), type_info()) ->
|
||||||
{'ok', typerep(), typerep()} | {'error', atom()}.
|
{'ok', typerep(), typerep()} | {'error', atom()}.
|
||||||
check_calldata(CallData, TypeInfo) ->
|
check_calldata(CallData, TypeInfo) ->
|
||||||
@@ -121,8 +91,8 @@ get_function_hash_from_calldata(CallData) ->
|
|||||||
|
|
||||||
-spec function_type_info(function_name(), [typerep()], typerep()) ->
|
-spec function_type_info(function_name(), [typerep()], typerep()) ->
|
||||||
function_type_info().
|
function_type_info().
|
||||||
function_type_info(Name, Args, OutType) ->
|
function_type_info(Name, ArgTypes, OutType) ->
|
||||||
ArgType = {tuple, [T || {_, T} <- Args]},
|
ArgType = {tuple, ArgTypes},
|
||||||
{ function_type_hash(Name, ArgType, OutType)
|
{ function_type_hash(Name, ArgType, OutType)
|
||||||
, Name
|
, Name
|
||||||
, aeso_heap:to_binary(ArgType)
|
, aeso_heap:to_binary(ArgType)
|
||||||
|
|||||||
@@ -0,0 +1,276 @@
|
|||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
%%% @author Robert Virding
|
||||||
|
%%% @copyright (C) 2017, Aeternity Anstalt
|
||||||
|
%%% @doc
|
||||||
|
%%% ACI interface
|
||||||
|
%%% @end
|
||||||
|
%%% Created : 12 Dec 2017
|
||||||
|
%%%-------------------------------------------------------------------
|
||||||
|
|
||||||
|
-module(aeso_aci).
|
||||||
|
|
||||||
|
-export([encode/1,encode/2,decode/1]).
|
||||||
|
|
||||||
|
%% Define records for the various typed syntactic forms. These make
|
||||||
|
%% the code easier but don't seem to exist elsewhere.
|
||||||
|
|
||||||
|
-record(contract, {ann,con,decls}).
|
||||||
|
-record(namespace, {ann,con,decls}).
|
||||||
|
-record(letfun, {ann,id,args,type,body}).
|
||||||
|
-record(type_def, {ann,id,vars,typedef}).
|
||||||
|
|
||||||
|
-record(app_t, {ann,id,fields}).
|
||||||
|
-record(tuple_t, {ann,args}).
|
||||||
|
-record(record_t, {fields}).
|
||||||
|
-record(field_t, {ann,id,type}).
|
||||||
|
-record(alias_t, {type}).
|
||||||
|
-record(variant_t, {cons}).
|
||||||
|
-record(constr_t, {ann,con,args}).
|
||||||
|
-record(fun_t, {ann,named,args,type}).
|
||||||
|
|
||||||
|
-record(arg, {ann,id,type}).
|
||||||
|
-record(id, {ann,name}).
|
||||||
|
-record(con, {ann,name}).
|
||||||
|
-record(qid, {ann,names}).
|
||||||
|
-record(qcon, {ann,names}).
|
||||||
|
-record(tvar, {ann,name}).
|
||||||
|
|
||||||
|
%% encode(ContractString) -> {ok,JSON} | {error,String}.
|
||||||
|
%% encode(ContractString, Options) -> {ok,JSON} | {error,String}.
|
||||||
|
%% Build a JSON structure with lists and tuples, not maps, as this
|
||||||
|
%% allows us to order the fields in the contructed JSON string.
|
||||||
|
|
||||||
|
encode(ContractString) -> encode(ContractString, []).
|
||||||
|
|
||||||
|
encode(ContractString, Options) when is_binary(ContractString) ->
|
||||||
|
encode(binary_to_list(ContractString), Options);
|
||||||
|
encode(ContractString, Options) ->
|
||||||
|
try
|
||||||
|
Ast = parse(ContractString, Options),
|
||||||
|
%%io:format("~p\n", [Ast]),
|
||||||
|
%% aeso_ast:pp(Ast),
|
||||||
|
TypedAst = aeso_ast_infer_types:infer(Ast, Options),
|
||||||
|
%% io:format("~p\n", [TypedAst]),
|
||||||
|
%% aeso_ast:pp_typed(TypedAst),
|
||||||
|
%% We find and look at the last contract.
|
||||||
|
Contract = lists:last(TypedAst),
|
||||||
|
Cname = contract_name(Contract),
|
||||||
|
Tdefs = [ encode_typedef(T) ||
|
||||||
|
T <- sort_decls(contract_types(Contract)) ],
|
||||||
|
Fdefs = [ encode_func(F) || F <- sort_decls(contract_funcs(Contract)),
|
||||||
|
not is_private_func(F) ],
|
||||||
|
Jmap = [{<<"contract">>, [{<<"name">>, list_to_binary(Cname)},
|
||||||
|
{<<"type_defs">>, Tdefs},
|
||||||
|
{<<"functions">>, Fdefs}]}],
|
||||||
|
%% io:format("~p\n", [Jmap]),
|
||||||
|
{ok,jsx:encode(Jmap)}
|
||||||
|
catch
|
||||||
|
%% The compiler errors.
|
||||||
|
error:{parse_errors, Errors} ->
|
||||||
|
{error, join_errors("Parse errors", Errors, fun(E) -> E end)};
|
||||||
|
error:{type_errors, Errors} ->
|
||||||
|
{error, join_errors("Type errors", Errors, fun(E) -> E end)};
|
||||||
|
error:{code_errors, Errors} ->
|
||||||
|
{error, join_errors("Code errors", Errors,
|
||||||
|
fun (E) -> io_lib:format("~p", [E]) end)}
|
||||||
|
%% General programming errors in the compiler just signal error.
|
||||||
|
end.
|
||||||
|
|
||||||
|
join_errors(Prefix, Errors, Pfun) ->
|
||||||
|
Ess = [ Pfun(E) || E <- Errors ],
|
||||||
|
list_to_binary(string:join([Prefix|Ess], "\n")).
|
||||||
|
|
||||||
|
encode_func(Fdef) ->
|
||||||
|
Name = function_name(Fdef),
|
||||||
|
Args = function_args(Fdef),
|
||||||
|
Type = function_type(Fdef),
|
||||||
|
[{<<"name">>, list_to_binary(Name)},
|
||||||
|
{<<"arguments">>, encode_args(Args)},
|
||||||
|
{<<"type">>, list_to_binary(encode_type(Type))},
|
||||||
|
{<<"stateful">>, is_stateful_func(Fdef)}].
|
||||||
|
|
||||||
|
encode_args(Args) ->
|
||||||
|
[ encode_arg(A) || A <- Args ].
|
||||||
|
|
||||||
|
encode_arg(#arg{id=Id,type=T}) ->
|
||||||
|
[{<<"name">>,list_to_binary(encode_type(Id))},
|
||||||
|
{<<"type">>,list_to_binary(encode_type(T))}].
|
||||||
|
|
||||||
|
encode_types(Types) ->
|
||||||
|
[ encode_type(T) || T <- Types ].
|
||||||
|
|
||||||
|
encode_type(#tvar{name=N}) -> N;
|
||||||
|
encode_type(#id{name=N}) -> N;
|
||||||
|
encode_type(#con{name=N}) -> N;
|
||||||
|
encode_type(#qid{names=Ns}) ->
|
||||||
|
lists:join(".", Ns);
|
||||||
|
encode_type(#qcon{names=Ns}) ->
|
||||||
|
lists:join(".", Ns); %?
|
||||||
|
encode_type(#tuple_t{args=As}) ->
|
||||||
|
Eas = encode_types(As),
|
||||||
|
[$(,lists:join(",", Eas),$)];
|
||||||
|
encode_type(#record_t{fields=Fs}) ->
|
||||||
|
Efs = encode_types(Fs),
|
||||||
|
[${,lists:join(",", Efs),$}];
|
||||||
|
encode_type(#app_t{id=Id,fields=Fs}) ->
|
||||||
|
Name = encode_type(Id),
|
||||||
|
Efs = encode_types(Fs),
|
||||||
|
[Name,"(",lists:join(",", Efs),")"];
|
||||||
|
encode_type(#field_t{id=Id,type=T}) ->
|
||||||
|
[encode_type(Id)," : ",encode_type(T)];
|
||||||
|
encode_type(#variant_t{cons=Cs}) ->
|
||||||
|
Ecs = encode_types(Cs),
|
||||||
|
lists:join(" | ", Ecs);
|
||||||
|
encode_type(#constr_t{con=C,args=As}) ->
|
||||||
|
Ec = encode_type(C),
|
||||||
|
Eas = encode_types(As),
|
||||||
|
[Ec,$(,lists:join(", ", Eas),$)];
|
||||||
|
encode_type(#fun_t{args=As,type=T}) ->
|
||||||
|
Eas = encode_types(As),
|
||||||
|
Et = encode_type(T),
|
||||||
|
[$(,lists:join(", ", Eas),") => ",Et].
|
||||||
|
|
||||||
|
encode_typedef(Type) ->
|
||||||
|
Name = typedef_name(Type),
|
||||||
|
Vars = typedef_vars(Type),
|
||||||
|
Def = typedef_def(Type),
|
||||||
|
[{<<"name">>, list_to_binary(Name)},
|
||||||
|
{<<"vars">>, encode_tvars(Vars)},
|
||||||
|
{<<"typedef">>, list_to_binary(encode_alias(Def))}].
|
||||||
|
|
||||||
|
encode_tvars(Vars) ->
|
||||||
|
[ encode_tvar(V) || V <- Vars ].
|
||||||
|
|
||||||
|
encode_tvar(#tvar{name=N}) ->
|
||||||
|
[{<<"name">>, list_to_binary(N)}].
|
||||||
|
|
||||||
|
encode_alias(#alias_t{type=T}) ->
|
||||||
|
encode_type(T);
|
||||||
|
encode_alias(A) -> encode_type(A).
|
||||||
|
|
||||||
|
%% decode(JSON) -> ContractString.
|
||||||
|
%% Decode a JSON string and generate a suitable contract string which
|
||||||
|
%% can be included in a contract definition. We decode into a map
|
||||||
|
%% here as this is easier to work with and order is not important.
|
||||||
|
|
||||||
|
decode(Json) ->
|
||||||
|
Map = jsx:decode(Json, [return_maps]),
|
||||||
|
%% io:format("~p\n", [Map]),
|
||||||
|
#{<<"contract">> := C} = Map,
|
||||||
|
list_to_binary(decode_contract(C)).
|
||||||
|
|
||||||
|
decode_contract(#{<<"name">> := Name,
|
||||||
|
<<"type_defs">> := _Ts,
|
||||||
|
<<"functions">> := Fs}) ->
|
||||||
|
["contract"," ",io_lib:format("~s", [Name])," =\n",
|
||||||
|
[], %Don't include types yet.
|
||||||
|
%% decode_tdefs(Ts),
|
||||||
|
decode_funcs(Fs)].
|
||||||
|
|
||||||
|
decode_funcs(Fs) -> [ decode_func(F) || F <- Fs ].
|
||||||
|
|
||||||
|
decode_func(#{<<"name">> := <<"init">>}) -> [];
|
||||||
|
decode_func(#{<<"name">> := Name,<<"arguments">> := As,<<"type">> := T}) ->
|
||||||
|
[" function"," ",io_lib:format("~s", [Name])," : ",
|
||||||
|
decode_args(As)," => ",decode_type(T),$\n].
|
||||||
|
|
||||||
|
decode_type(T) -> io_lib:format("~s", [T]).
|
||||||
|
|
||||||
|
decode_args(As) ->
|
||||||
|
Das = [ decode_arg(A) || A <- As ],
|
||||||
|
[$(,lists:join(", ", Das),$)].
|
||||||
|
|
||||||
|
decode_arg(#{<<"type">> := T}) -> decode_type(T).
|
||||||
|
|
||||||
|
%% To keep dialyzer happy and quiet.
|
||||||
|
%% decode_tdefs(Ts) -> [ decode_tdef(T) || T <- Ts ].
|
||||||
|
%%
|
||||||
|
%% decode_tdef(#{<<"name">> := Name,<<"vars">> := Vs,<<"typedef">> := T}) ->
|
||||||
|
%% [" type"," ",io_lib:format("~s", [Name]),decode_tvars(Vs),
|
||||||
|
%% " = ",decode_type(T),$\n].
|
||||||
|
%%
|
||||||
|
%% decode_tvars([]) -> []; %No tvars, no parentheses
|
||||||
|
%% decode_tvars(Vs) ->
|
||||||
|
%% Dvs = [ decode_tvar(V) || V <- Vs ],
|
||||||
|
%% [$(,lists:join(", ", Dvs),$)].
|
||||||
|
%%
|
||||||
|
%% decode_tvar(#{<<"name">> := N}) -> io_lib:format("~s", [N]).
|
||||||
|
%%
|
||||||
|
%% #contract{Ann, Con, [Declarations]}.
|
||||||
|
|
||||||
|
contract_name(#contract{con=#con{name=N}}) -> N.
|
||||||
|
|
||||||
|
contract_funcs(#contract{decls=Decls}) ->
|
||||||
|
[ D || D <- Decls, is_record(D, letfun) ].
|
||||||
|
|
||||||
|
contract_types(#contract{decls=Decls}) ->
|
||||||
|
[ D || D <- Decls, is_record(D, type_def) ].
|
||||||
|
|
||||||
|
%% To keep dialyzer happy and quiet.
|
||||||
|
%% namespace_name(#namespace{con=#con{name=N}}) -> N.
|
||||||
|
%%
|
||||||
|
%% namespace_funcs(#namespace{decls=Decls}) ->
|
||||||
|
%% [ D || D <- Decls, is_record(D, letfun) ].
|
||||||
|
%%
|
||||||
|
%% namespace_types(#namespace{decls=Decls}) ->
|
||||||
|
%% [ D || D <- Decls, is_record(D, type_def) ].
|
||||||
|
|
||||||
|
sort_decls(Ds) ->
|
||||||
|
Sort = fun (D1, D2) ->
|
||||||
|
aeso_syntax:get_ann(line, D1, 0) =<
|
||||||
|
aeso_syntax:get_ann(line, D2, 0)
|
||||||
|
end,
|
||||||
|
lists:sort(Sort, Ds).
|
||||||
|
|
||||||
|
%% #letfun{Ann, Id, [Arg], Type, Typedef}.
|
||||||
|
|
||||||
|
function_name(#letfun{id=#id{name=N}}) -> N.
|
||||||
|
|
||||||
|
function_args(#letfun{args=Args}) -> Args.
|
||||||
|
|
||||||
|
function_type(#letfun{type=Type}) -> Type.
|
||||||
|
|
||||||
|
is_private_func(#letfun{ann=A}) -> aeso_syntax:get_ann(private, A, false).
|
||||||
|
|
||||||
|
is_stateful_func(#letfun{ann=A}) -> aeso_syntax:get_ann(stateful, A, false).
|
||||||
|
|
||||||
|
%% #type_def{Ann, Id, [Var], Typedef}.
|
||||||
|
|
||||||
|
typedef_name(#type_def{id=#id{name=N}}) -> N.
|
||||||
|
|
||||||
|
typedef_vars(#type_def{vars=Vars}) -> Vars.
|
||||||
|
|
||||||
|
typedef_def(#type_def{typedef=Def}) -> Def.
|
||||||
|
|
||||||
|
parse(Text, Options) ->
|
||||||
|
%% Try and return something sensible here!
|
||||||
|
case aeso_parser:string(Text, Options) of
|
||||||
|
%% Yay, it worked!
|
||||||
|
{ok, Contract} -> Contract;
|
||||||
|
%% Scan errors.
|
||||||
|
{error, {Pos, scan_error}} ->
|
||||||
|
parse_error(Pos, "scan error");
|
||||||
|
{error, {Pos, scan_error_no_state}} ->
|
||||||
|
parse_error(Pos, "scan error");
|
||||||
|
%% Parse errors.
|
||||||
|
{error, {Pos, parse_error, Error}} ->
|
||||||
|
parse_error(Pos, Error);
|
||||||
|
{error, {Pos, ambiguous_parse, As}} ->
|
||||||
|
ErrorString = io_lib:format("Ambiguous ~p", [As]),
|
||||||
|
parse_error(Pos, ErrorString);
|
||||||
|
%% Include error
|
||||||
|
{error, {Pos, include_error, File}} ->
|
||||||
|
parse_error(Pos, io_lib:format("could not find include file '~s'", [File]))
|
||||||
|
end.
|
||||||
|
|
||||||
|
parse_error(Pos, ErrorString) ->
|
||||||
|
io:format("Error ~p ~p\n", [Pos,ErrorString]),
|
||||||
|
Error = io_lib:format("~s: ~s", [pos_error(Pos), ErrorString]),
|
||||||
|
error({parse_errors, [Error]}).
|
||||||
|
|
||||||
|
pos_error({Line, Pos}) ->
|
||||||
|
io_lib:format("line ~p, column ~p", [Line, Pos]);
|
||||||
|
pos_error({no_file, Line, Pos}) ->
|
||||||
|
pos_error({Line, Pos});
|
||||||
|
pos_error({File, Line, Pos}) ->
|
||||||
|
io_lib:format("file ~s, line ~p, column ~p", [File, Line, Pos]).
|
||||||
@@ -17,7 +17,6 @@ line({symbol, Line, _}) -> Line.
|
|||||||
symbol_name({symbol, _, Name}) -> Name.
|
symbol_name({symbol, _, Name}) -> Name.
|
||||||
|
|
||||||
pp(Ast) ->
|
pp(Ast) ->
|
||||||
%% io:format("Tree:\n~p\n",[Ast]),
|
|
||||||
String = prettypr:format(aeso_pretty:decls(Ast, [])),
|
String = prettypr:format(aeso_pretty:decls(Ast, [])),
|
||||||
io:format("Ast:\n~s\n", [String]).
|
io:format("Ast:\n~s\n", [String]).
|
||||||
|
|
||||||
|
|||||||
+895
-500
File diff suppressed because it is too large
Load Diff
+94
-23
@@ -17,11 +17,19 @@
|
|||||||
|
|
||||||
-spec convert_typed(aeso_syntax:ast(), list()) -> aeso_icode:icode().
|
-spec convert_typed(aeso_syntax:ast(), list()) -> aeso_icode:icode().
|
||||||
convert_typed(TypedTree, Options) ->
|
convert_typed(TypedTree, Options) ->
|
||||||
code(TypedTree, aeso_icode:new(Options)).
|
Name = case lists:last(TypedTree) of
|
||||||
|
{contract, _, {con, _, Con}, _} -> Con;
|
||||||
|
_ -> gen_error(last_declaration_must_be_contract)
|
||||||
|
end,
|
||||||
|
Icode = code(TypedTree, aeso_icode:set_name(Name, aeso_icode:new(Options))),
|
||||||
|
deadcode_elimination(Icode).
|
||||||
|
|
||||||
code([{contract, _Attribs, {con, _, Name}, Code}|Rest], Icode) ->
|
code([{contract, _Attribs, Con, Code}|Rest], Icode) ->
|
||||||
NewIcode = contract_to_icode(Code,
|
NewIcode = contract_to_icode(Code, aeso_icode:set_namespace(Con, Icode)),
|
||||||
aeso_icode:set_name(Name, Icode)),
|
code(Rest, NewIcode);
|
||||||
|
code([{namespace, _Ann, Name, Code}|Rest], Icode) ->
|
||||||
|
%% TODO: nested namespaces
|
||||||
|
NewIcode = contract_to_icode(Code, aeso_icode:set_namespace(Name, Icode)),
|
||||||
code(Rest, NewIcode);
|
code(Rest, NewIcode);
|
||||||
code([], Icode) ->
|
code([], Icode) ->
|
||||||
add_default_init_function(add_builtins(Icode)).
|
add_default_init_function(add_builtins(Icode)).
|
||||||
@@ -33,30 +41,38 @@ gen_error(Error) ->
|
|||||||
|
|
||||||
%% Create default init function (only if state is unit).
|
%% Create default init function (only if state is unit).
|
||||||
add_default_init_function(Icode = #{functions := Funs, state_type := State}) ->
|
add_default_init_function(Icode = #{functions := Funs, state_type := State}) ->
|
||||||
case lists:keymember("init", 1, Funs) of
|
{_, _, QInit} = aeso_icode:qualify({id, [], "init"}, Icode),
|
||||||
|
case lists:keymember(QInit, 1, Funs) of
|
||||||
true -> Icode;
|
true -> Icode;
|
||||||
false when State /= {tuple, []} -> gen_error(missing_init_function);
|
false when State /= {tuple, []} ->
|
||||||
|
gen_error(missing_init_function);
|
||||||
false ->
|
false ->
|
||||||
Type = {tuple, [typerep, {tuple, []}]},
|
Type = {tuple, [typerep, {tuple, []}]},
|
||||||
Value = #tuple{ cpts = [type_value({tuple, []}), {tuple, []}] },
|
Value = #tuple{ cpts = [type_value({tuple, []}), {tuple, []}] },
|
||||||
DefaultInit = {"init", [], [], Value, Type},
|
DefaultInit = {QInit, [], [], Value, Type},
|
||||||
Icode#{ functions => [DefaultInit | Funs] }
|
Icode#{ functions => [DefaultInit | Funs] }
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec contract_to_icode(aeso_syntax:ast(), aeso_icode:icode()) ->
|
-spec contract_to_icode(aeso_syntax:ast(), aeso_icode:icode()) ->
|
||||||
aeso_icode:icode().
|
aeso_icode:icode().
|
||||||
contract_to_icode([{type_def, _Attrib, {id, _, Name}, Args, Def} | Rest],
|
contract_to_icode([{namespace, _, Name, Defs} | Rest], Icode) ->
|
||||||
|
NS = aeso_icode:get_namespace(Icode),
|
||||||
|
Icode1 = contract_to_icode(Defs, aeso_icode:enter_namespace(Name, Icode)),
|
||||||
|
contract_to_icode(Rest, aeso_icode:set_namespace(NS, Icode1));
|
||||||
|
contract_to_icode([{type_def, _Attrib, Id = {id, _, Name}, Args, Def} | Rest],
|
||||||
Icode = #{ types := Types, constructors := Constructors }) ->
|
Icode = #{ types := Types, constructors := Constructors }) ->
|
||||||
TypeDef = make_type_def(Args, Def, Icode),
|
TypeDef = make_type_def(Args, Def, Icode),
|
||||||
NewConstructors =
|
NewConstructors =
|
||||||
case Def of
|
case Def of
|
||||||
{variant_t, Cons} ->
|
{variant_t, Cons} ->
|
||||||
Tags = lists:seq(0, length(Cons) - 1),
|
Tags = lists:seq(0, length(Cons) - 1),
|
||||||
GetName = fun({constr_t, _, {con, _, C}, _}) -> C end,
|
GetName = fun({constr_t, _, C, _}) -> C end,
|
||||||
maps:from_list([ {GetName(Con), Tag} || {Tag, Con} <- lists:zip(Tags, Cons) ]);
|
QName = fun(Con) -> {_, _, Xs} = aeso_icode:qualify(GetName(Con), Icode), Xs end,
|
||||||
|
maps:from_list([ {QName(Con), Tag} || {Tag, Con} <- lists:zip(Tags, Cons) ]);
|
||||||
_ -> #{}
|
_ -> #{}
|
||||||
end,
|
end,
|
||||||
Icode1 = Icode#{ types := Types#{ Name => TypeDef },
|
{_, _, TName} = aeso_icode:qualify(Id, Icode),
|
||||||
|
Icode1 = Icode#{ types := Types#{ TName => TypeDef },
|
||||||
constructors := maps:merge(Constructors, NewConstructors) },
|
constructors := maps:merge(Constructors, NewConstructors) },
|
||||||
Icode2 = case Name of
|
Icode2 = case Name of
|
||||||
"state" when Args == [] -> Icode1#{ state_type => ast_typerep(Def, Icode) };
|
"state" when Args == [] -> Icode1#{ state_type => ast_typerep(Def, Icode) };
|
||||||
@@ -68,8 +84,7 @@ contract_to_icode([{type_def, _Attrib, {id, _, Name}, Args, Def} | Rest],
|
|||||||
contract_to_icode(Rest, Icode2);
|
contract_to_icode(Rest, Icode2);
|
||||||
contract_to_icode([{letfun, Attrib, Name, Args, _What, Body={typed,_,_,T}}|Rest], Icode) ->
|
contract_to_icode([{letfun, Attrib, Name, Args, _What, Body={typed,_,_,T}}|Rest], Icode) ->
|
||||||
FunAttrs = [ stateful || proplists:get_value(stateful, Attrib, false) ] ++
|
FunAttrs = [ stateful || proplists:get_value(stateful, Attrib, false) ] ++
|
||||||
[ private || proplists:get_value(private, Attrib, false) orelse
|
[ private || is_private(Attrib, Icode) ],
|
||||||
proplists:get_value(internal, Attrib, false) ],
|
|
||||||
%% TODO: Handle types
|
%% TODO: Handle types
|
||||||
FunName = ast_id(Name),
|
FunName = ast_id(Name),
|
||||||
%% TODO: push funname to env
|
%% TODO: push funname to env
|
||||||
@@ -84,7 +99,8 @@ contract_to_icode([{letfun, Attrib, Name, Args, _What, Body={typed,_,_,T}}|Rest]
|
|||||||
{tuple, [typerep, ast_typerep(T, Icode)]}};
|
{tuple, [typerep, ast_typerep(T, Icode)]}};
|
||||||
_ -> {ast_body(Body, Icode), ast_typerep(T, Icode)}
|
_ -> {ast_body(Body, Icode), ast_typerep(T, Icode)}
|
||||||
end,
|
end,
|
||||||
NewIcode = ast_fun_to_icode(FunName, FunAttrs, FunArgs, FunBody, TypeRep, Icode),
|
QName = aeso_icode:qualify(Name, Icode),
|
||||||
|
NewIcode = ast_fun_to_icode(ast_id(QName), FunAttrs, FunArgs, FunBody, TypeRep, Icode),
|
||||||
contract_to_icode(Rest, NewIcode);
|
contract_to_icode(Rest, NewIcode);
|
||||||
contract_to_icode([{letrec,_,Defs}|Rest], Icode) ->
|
contract_to_icode([{letrec,_,Defs}|Rest], Icode) ->
|
||||||
%% OBS! This code ignores the letrec structure of the source,
|
%% OBS! This code ignores the letrec structure of the source,
|
||||||
@@ -94,11 +110,14 @@ contract_to_icode([{letrec,_,Defs}|Rest], Icode) ->
|
|||||||
%% just to parse a list of (mutually recursive) definitions.
|
%% just to parse a list of (mutually recursive) definitions.
|
||||||
contract_to_icode(Defs++Rest, Icode);
|
contract_to_icode(Defs++Rest, Icode);
|
||||||
contract_to_icode([], Icode) -> Icode;
|
contract_to_icode([], Icode) -> Icode;
|
||||||
contract_to_icode(_Code, Icode) ->
|
contract_to_icode([{fun_decl, _, _, _} | Code], Icode) ->
|
||||||
%% TODO debug output for debug("Unhandled code ~p~n",[Code]),
|
contract_to_icode(Code, Icode);
|
||||||
Icode.
|
contract_to_icode([Decl | Code], Icode) ->
|
||||||
|
io:format("Unhandled declaration: ~p\n", [Decl]),
|
||||||
|
contract_to_icode(Code, Icode).
|
||||||
|
|
||||||
ast_id({id, _, Id}) -> Id.
|
ast_id({id, _, Id}) -> Id;
|
||||||
|
ast_id({qid, _, Id}) -> Id.
|
||||||
|
|
||||||
ast_args([{arg, _, Name, Type}|Rest], Acc, Icode) ->
|
ast_args([{arg, _, Name, Type}|Rest], Acc, Icode) ->
|
||||||
ast_args(Rest, [{ast_id(Name), ast_type(Type, Icode)}| Acc], Icode);
|
ast_args(Rest, [{ast_id(Name), ast_type(Type, Icode)}| Acc], Icode);
|
||||||
@@ -121,7 +140,7 @@ ast_type(T, Icode) ->
|
|||||||
ast_body(?qid_app(["Chain","spend"], [To, Amount], _, _), Icode) ->
|
ast_body(?qid_app(["Chain","spend"], [To, Amount], _, _), Icode) ->
|
||||||
prim_call(?PRIM_CALL_SPEND, ast_body(Amount, Icode), [ast_body(To, Icode)], [word], {tuple, []});
|
prim_call(?PRIM_CALL_SPEND, ast_body(Amount, Icode), [ast_body(To, Icode)], [word], {tuple, []});
|
||||||
|
|
||||||
ast_body(?qid_app(["Chain","event"], [Event], _, _), Icode) ->
|
ast_body(?qid_app([Con, "Chain", "event"], [Event], _, _), Icode = #{ contract_name := Con }) ->
|
||||||
aeso_builtins:check_event_type(Icode),
|
aeso_builtins:check_event_type(Icode),
|
||||||
builtin_call({event, maps:get(event_type, Icode)}, [ast_body(Event, Icode)]);
|
builtin_call({event, maps:get(event_type, Icode)}, [ast_body(Event, Icode)]);
|
||||||
|
|
||||||
@@ -152,10 +171,10 @@ ast_body({qid, _, ["Chain", "spend"]}, _Icode) ->
|
|||||||
gen_error({underapplied_primitive, 'Chain.spend'});
|
gen_error({underapplied_primitive, 'Chain.spend'});
|
||||||
|
|
||||||
%% State
|
%% State
|
||||||
ast_body({id, _, "state"}, _Icode) -> prim_state;
|
ast_body({qid, _, [Con, "state"]}, #{ contract_name := Con }) -> prim_state;
|
||||||
ast_body(?id_app("put", [NewState], _, _), Icode) ->
|
ast_body(?qid_app([Con, "put"], [NewState], _, _), Icode = #{ contract_name := Con }) ->
|
||||||
#prim_put{ state = ast_body(NewState, Icode) };
|
#prim_put{ state = ast_body(NewState, Icode) };
|
||||||
ast_body({id, _, "put"}, _Icode) ->
|
ast_body({qid, _, [Con, "put"]}, #{ contract_name := Con }) ->
|
||||||
gen_error({underapplied_primitive, put}); %% TODO: eta
|
gen_error({underapplied_primitive, put}); %% TODO: eta
|
||||||
|
|
||||||
%% Abort
|
%% Abort
|
||||||
@@ -384,7 +403,8 @@ ast_body(?qid_app(["Address", "to_str"], [Addr], _, _), Icode) ->
|
|||||||
|
|
||||||
%% Other terms
|
%% Other terms
|
||||||
ast_body({id, _, Name}, _Icode) ->
|
ast_body({id, _, Name}, _Icode) ->
|
||||||
%% TODO Look up id in env
|
#var_ref{name = Name};
|
||||||
|
ast_body({qid, _, Name}, _Icode) ->
|
||||||
#var_ref{name = Name};
|
#var_ref{name = Name};
|
||||||
ast_body({bool, _, Bool}, _Icode) -> %BOOL as ints
|
ast_body({bool, _, Bool}, _Icode) -> %BOOL as ints
|
||||||
Value = if Bool -> 1 ; true -> 0 end,
|
Value = if Bool -> 1 ; true -> 0 end,
|
||||||
@@ -441,9 +461,15 @@ ast_body({proj, _, {typed, _, _, {con, _, Contract}}, {id, _, FunName}}, _Icode)
|
|||||||
string:join([Contract, FunName], ".")});
|
string:join([Contract, FunName], ".")});
|
||||||
|
|
||||||
ast_body({con, _, Name}, Icode) ->
|
ast_body({con, _, Name}, Icode) ->
|
||||||
|
Tag = aeso_icode:get_constructor_tag([Name], Icode),
|
||||||
|
#tuple{cpts = [#integer{value = Tag}]};
|
||||||
|
ast_body({qcon, _, Name}, Icode) ->
|
||||||
Tag = aeso_icode:get_constructor_tag(Name, Icode),
|
Tag = aeso_icode:get_constructor_tag(Name, Icode),
|
||||||
#tuple{cpts = [#integer{value = Tag}]};
|
#tuple{cpts = [#integer{value = Tag}]};
|
||||||
ast_body({app, _, {typed, _, {con, _, Name}, _}, Args}, Icode) ->
|
ast_body({app, _, {typed, _, {con, _, Name}, _}, Args}, Icode) ->
|
||||||
|
Tag = aeso_icode:get_constructor_tag([Name], Icode),
|
||||||
|
#tuple{cpts = [#integer{value = Tag} | [ ast_body(Arg, Icode) || Arg <- Args ]]};
|
||||||
|
ast_body({app, _, {typed, _, {qcon, _, Name}, _}, Args}, Icode) ->
|
||||||
Tag = aeso_icode:get_constructor_tag(Name, Icode),
|
Tag = aeso_icode:get_constructor_tag(Name, Icode),
|
||||||
#tuple{cpts = [#integer{value = Tag} | [ ast_body(Arg, Icode) || Arg <- Args ]]};
|
#tuple{cpts = [#integer{value = Tag} | [ ast_body(Arg, Icode) || Arg <- Args ]]};
|
||||||
ast_body({app,As,Fun,Args}, Icode) ->
|
ast_body({app,As,Fun,Args}, Icode) ->
|
||||||
@@ -745,6 +771,15 @@ has_maps({list, T}) -> has_maps(T);
|
|||||||
has_maps({tuple, Ts}) -> lists:any(fun has_maps/1, Ts);
|
has_maps({tuple, Ts}) -> lists:any(fun has_maps/1, Ts);
|
||||||
has_maps({variant, Cs}) -> lists:any(fun has_maps/1, lists:append(Cs)).
|
has_maps({variant, Cs}) -> lists:any(fun has_maps/1, lists:append(Cs)).
|
||||||
|
|
||||||
|
%% A function is private if marked 'private' or 'internal', or if it's not
|
||||||
|
%% defined in the main contract name space. (NOTE: changes when we introduce
|
||||||
|
%% inheritance).
|
||||||
|
is_private(Ann, #{ contract_name := MainContract } = Icode) ->
|
||||||
|
{_, _, CurrentNamespace} = aeso_icode:get_namespace(Icode),
|
||||||
|
proplists:get_value(private, Ann, false) orelse
|
||||||
|
proplists:get_value(internal, Ann, false) orelse
|
||||||
|
MainContract /= CurrentNamespace.
|
||||||
|
|
||||||
%% -------------------------------------------------------------------
|
%% -------------------------------------------------------------------
|
||||||
%% Builtins
|
%% Builtins
|
||||||
%% -------------------------------------------------------------------
|
%% -------------------------------------------------------------------
|
||||||
@@ -756,3 +791,39 @@ builtin_call(Builtin, Args) ->
|
|||||||
add_builtins(Icode = #{functions := Funs}) ->
|
add_builtins(Icode = #{functions := Funs}) ->
|
||||||
Builtins = aeso_builtins:used_builtins(Funs),
|
Builtins = aeso_builtins:used_builtins(Funs),
|
||||||
Icode#{functions := [ aeso_builtins:builtin_function(B) || B <- Builtins ] ++ Funs}.
|
Icode#{functions := [ aeso_builtins:builtin_function(B) || B <- Builtins ] ++ Funs}.
|
||||||
|
|
||||||
|
|
||||||
|
%% -------------------------------------------------------------------
|
||||||
|
%% Deadcode elimination
|
||||||
|
%% -------------------------------------------------------------------
|
||||||
|
|
||||||
|
deadcode_elimination(Icode = #{ functions := Funs }) ->
|
||||||
|
PublicNames = [ Name || {Name, Ann, _, _, _} <- Funs, not lists:member(private, Ann) ],
|
||||||
|
ArgsToPat = fun(Args) -> [ #var_ref{ name = X } || {X, _} <- Args ] end,
|
||||||
|
Defs = maps:from_list([ {Name, {binder, ArgsToPat(Args), Body}} || {Name, _, Args, Body, _} <- Funs ]),
|
||||||
|
UsedNames = chase_names(Defs, PublicNames, #{}),
|
||||||
|
UsedFuns = [ Def || Def = {Name, _, _, _, _} <- Funs, maps:is_key(Name, UsedNames) ],
|
||||||
|
Icode#{ functions := UsedFuns }.
|
||||||
|
|
||||||
|
chase_names(_Defs, [], Used) -> Used;
|
||||||
|
chase_names(Defs, [X | Xs], Used) ->
|
||||||
|
%% can happen when compiling __call contracts
|
||||||
|
case maps:is_key(X, Used) orelse not maps:is_key(X, Defs) of
|
||||||
|
true -> chase_names(Defs, Xs, Used); %% already chased
|
||||||
|
false ->
|
||||||
|
Def = maps:get(X, Defs),
|
||||||
|
Vars = maps:keys(free_vars(Def)),
|
||||||
|
chase_names(Defs, Vars ++ Xs, Used#{ X => true })
|
||||||
|
end.
|
||||||
|
|
||||||
|
free_vars(#var_ref{ name = X }) -> #{ X => true };
|
||||||
|
free_vars(#arg{ name = X }) -> #{ X => true };
|
||||||
|
free_vars({binder, Pat, Body}) ->
|
||||||
|
maps:without(maps:keys(free_vars(Pat)), free_vars(Body));
|
||||||
|
free_vars(#switch{ expr = E, cases = Cases }) ->
|
||||||
|
free_vars([E | [{binder, P, B} || {P, B} <- Cases]]);
|
||||||
|
free_vars(#lambda{ args = Xs, body = E }) ->
|
||||||
|
free_vars({binder, Xs, E});
|
||||||
|
free_vars(T) when is_tuple(T) -> free_vars(tuple_to_list(T));
|
||||||
|
free_vars([H | T]) -> maps:merge(free_vars(H), free_vars(T));
|
||||||
|
free_vars(_) -> #{}.
|
||||||
|
|||||||
+14
-14
@@ -106,21 +106,22 @@ check_event_type(Icode) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
check_event_type(Evts, Icode) ->
|
check_event_type(Evts, Icode) ->
|
||||||
[ check_event_type(Name, T, Icode)
|
[ check_event_type(Name, Ix, T, Icode)
|
||||||
|| {constr_t, _, {con, _, Name}, Types} <- Evts, T <- Types ].
|
|| {constr_t, Ann, {con, _, Name}, Types} <- Evts,
|
||||||
|
{Ix, T} <- lists:zip(aeso_syntax:get_ann(indices, Ann), Types) ].
|
||||||
|
|
||||||
check_event_type(EvtName, Type, Icode) ->
|
check_event_type(EvtName, Ix, Type, Icode) ->
|
||||||
VMType =
|
VMType =
|
||||||
try
|
try
|
||||||
aeso_ast_to_icode:ast_typerep(Type, Icode)
|
aeso_ast_to_icode:ast_typerep(Type, Icode)
|
||||||
catch _:_ ->
|
catch _:_ ->
|
||||||
error({EvtName, could_not_resolve_type, Type})
|
error({EvtName, could_not_resolve_type, Type})
|
||||||
end,
|
end,
|
||||||
case aeso_syntax:get_ann(indexed, Type, false) of
|
case {Ix, VMType} of
|
||||||
true when VMType == word -> ok;
|
{indexed, word} -> ok;
|
||||||
false when VMType == string -> ok;
|
{notindexed, string} -> ok;
|
||||||
true -> error({EvtName, indexed_field_should_be_word, is, VMType});
|
{indexed, _} -> error({EvtName, indexed_field_should_be_word, is, VMType});
|
||||||
false -> error({EvtName, payload_should_be_string, is, VMType})
|
{notindexed, _} -> error({EvtName, payload_should_be_string, is, VMType})
|
||||||
end.
|
end.
|
||||||
|
|
||||||
bfun(B, {IArgs, IExpr, IRet}) ->
|
bfun(B, {IArgs, IExpr, IRet}) ->
|
||||||
@@ -169,16 +170,15 @@ builtin_event(EventT) ->
|
|||||||
A = fun(X) -> aeb_opcodes:mnemonic(X) end,
|
A = fun(X) -> aeb_opcodes:mnemonic(X) end,
|
||||||
VIx = fun(Ix) -> v(lists:concat(["v", Ix])) end,
|
VIx = fun(Ix) -> v(lists:concat(["v", Ix])) end,
|
||||||
ArgPats = fun(Ts) -> [ VIx(Ix) || Ix <- lists:seq(0, length(Ts) - 1) ] end,
|
ArgPats = fun(Ts) -> [ VIx(Ix) || Ix <- lists:seq(0, length(Ts) - 1) ] end,
|
||||||
IsIndexed = fun(T) -> aeso_syntax:get_ann(indexed, T, false) end,
|
|
||||||
Payload = %% Should put data ptr, length on stack.
|
Payload = %% Should put data ptr, length on stack.
|
||||||
fun([]) -> {inline_asm, [A(?PUSH1), 0, A(?PUSH1), 0]};
|
fun([]) -> {inline_asm, [A(?PUSH1), 0, A(?PUSH1), 0]};
|
||||||
([V]) -> {seq, [V, {inline_asm, [A(?DUP1), A(?MLOAD), %% length, ptr
|
([V]) -> {seq, [V, {inline_asm, [A(?DUP1), A(?MLOAD), %% length, ptr
|
||||||
A(?SWAP1), A(?PUSH1), 32, A(?ADD)]}]} %% ptr+32, length
|
A(?SWAP1), A(?PUSH1), 32, A(?ADD)]}]} %% ptr+32, length
|
||||||
end,
|
end,
|
||||||
Clause =
|
Clause =
|
||||||
fun(_Tag, {con, _, Con}, Types) ->
|
fun(_Tag, {con, _, Con}, IxTypes) ->
|
||||||
Indexed = [ Var || {Var, Type} <- lists:zip(ArgPats(Types), Types),
|
Types = [ T || {_Ix, T} <- IxTypes ],
|
||||||
IsIndexed(Type) ],
|
Indexed = [ Var || {Var, {indexed, _Type}} <- lists:zip(ArgPats(Types), IxTypes) ],
|
||||||
EvtIndex = {unop, 'sha3', str_to_icode(Con)},
|
EvtIndex = {unop, 'sha3', str_to_icode(Con)},
|
||||||
{event, lists:reverse(Indexed) ++ [EvtIndex], Payload(ArgPats(Types) -- Indexed)}
|
{event, lists:reverse(Indexed) ++ [EvtIndex], Payload(ArgPats(Types) -- Indexed)}
|
||||||
end,
|
end,
|
||||||
@@ -189,8 +189,8 @@ builtin_event(EventT) ->
|
|||||||
|
|
||||||
{[{"e", event}],
|
{[{"e", event}],
|
||||||
{switch, v(e),
|
{switch, v(e),
|
||||||
[{Pat(Tag, Types), Clause(Tag, Con, Types)}
|
[{Pat(Tag, Types), Clause(Tag, Con, lists:zip(aeso_syntax:get_ann(indices, Ann), Types))}
|
||||||
|| {Tag, {constr_t, _, Con, Types}} <- lists:zip(Tags, Cons) ]},
|
|| {Tag, {constr_t, Ann, Con, Types}} <- lists:zip(Tags, Cons) ]},
|
||||||
{tuple, []}}.
|
{tuple, []}}.
|
||||||
|
|
||||||
%% Abort primitive.
|
%% Abort primitive.
|
||||||
|
|||||||
+260
-76
@@ -11,41 +11,63 @@
|
|||||||
-export([ file/1
|
-export([ file/1
|
||||||
, file/2
|
, file/2
|
||||||
, from_string/2
|
, from_string/2
|
||||||
, check_call/2
|
, check_call/4
|
||||||
, create_calldata/3
|
, create_calldata/3
|
||||||
, version/0
|
, version/0
|
||||||
, sophia_type_to_typerep/1
|
, sophia_type_to_typerep/1
|
||||||
|
, to_sophia_value/4
|
||||||
|
, to_sophia_value/5
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-include_lib("aebytecode/include/aeb_opcodes.hrl").
|
-include_lib("aebytecode/include/aeb_opcodes.hrl").
|
||||||
-include("aeso_icode.hrl").
|
-include("aeso_icode.hrl").
|
||||||
|
|
||||||
|
|
||||||
-type option() :: pp_sophia_code | pp_ast | pp_types | pp_typed_ast |
|
-type option() :: pp_sophia_code
|
||||||
pp_icode| pp_assembler | pp_bytecode.
|
| pp_ast
|
||||||
|
| pp_types
|
||||||
|
| pp_typed_ast
|
||||||
|
| pp_icode
|
||||||
|
| pp_assembler
|
||||||
|
| pp_bytecode
|
||||||
|
| {include, {file_system, [string()]} |
|
||||||
|
{explicit_files, #{string() => binary()}}}
|
||||||
|
| {src_file, string()}.
|
||||||
-type options() :: [option()].
|
-type options() :: [option()].
|
||||||
|
|
||||||
-export_type([ option/0
|
-export_type([ option/0
|
||||||
, options/0
|
, options/0
|
||||||
]).
|
]).
|
||||||
|
|
||||||
-define(COMPILER_VERSION_1, 1).
|
-spec version() -> {ok, binary()} | {error, term()}.
|
||||||
-define(COMPILER_VERSION_2, 2).
|
|
||||||
|
|
||||||
-define(COMPILER_VERSION, ?COMPILER_VERSION_2).
|
|
||||||
|
|
||||||
-spec version() -> pos_integer().
|
|
||||||
version() ->
|
version() ->
|
||||||
?COMPILER_VERSION.
|
case lists:keyfind(aesophia, 1, application:loaded_applications()) of
|
||||||
|
false ->
|
||||||
|
case application:load(aesophia) of
|
||||||
|
ok ->
|
||||||
|
case application:get_key(aesophia, vsn) of
|
||||||
|
{ok, VsnString} ->
|
||||||
|
{ok, list_to_binary(VsnString)};
|
||||||
|
undefined ->
|
||||||
|
{error, failed_to_load_aesophia}
|
||||||
|
end;
|
||||||
|
Err = {error, _} ->
|
||||||
|
Err
|
||||||
|
end;
|
||||||
|
{_App, _Des, VsnString} ->
|
||||||
|
{ok, list_to_binary(VsnString)}
|
||||||
|
end.
|
||||||
|
|
||||||
-spec file(string()) -> {ok, map()} | {error, binary()}.
|
-spec file(string()) -> {ok, map()} | {error, binary()}.
|
||||||
file(Filename) ->
|
file(Filename) ->
|
||||||
file(Filename, []).
|
Dir = filename:dirname(Filename),
|
||||||
|
{ok, Cwd} = file:get_cwd(),
|
||||||
|
file(Filename, [{include, {file_system, [Cwd, Dir]}}]).
|
||||||
|
|
||||||
-spec file(string(), options()) -> {ok, map()} | {error, binary()}.
|
-spec file(string(), options()) -> {ok, map()} | {error, binary()}.
|
||||||
file(File, Options) ->
|
file(File, Options) ->
|
||||||
case read_contract(File) of
|
case read_contract(File) of
|
||||||
{ok, Bin} -> from_string(Bin, Options);
|
{ok, Bin} -> from_string(Bin, [{src_file, File} | Options]);
|
||||||
{error, Error} ->
|
{error, Error} ->
|
||||||
ErrorString = [File,": ",file:format_error(Error)],
|
ErrorString = [File,": ",file:format_error(Error)],
|
||||||
{error, join_errors("File errors", [ErrorString], fun(E) -> E end)}
|
{error, join_errors("File errors", [ErrorString], fun(E) -> E end)}
|
||||||
@@ -56,22 +78,16 @@ from_string(ContractBin, Options) when is_binary(ContractBin) ->
|
|||||||
from_string(binary_to_list(ContractBin), Options);
|
from_string(binary_to_list(ContractBin), Options);
|
||||||
from_string(ContractString, Options) ->
|
from_string(ContractString, Options) ->
|
||||||
try
|
try
|
||||||
Ast = parse(ContractString, Options),
|
#{icode := Icode} = string_to_icode(ContractString, Options),
|
||||||
ok = pp_sophia_code(Ast, Options),
|
TypeInfo = extract_type_info(Icode),
|
||||||
ok = pp_ast(Ast, Options),
|
Assembler = assemble(Icode, Options),
|
||||||
TypedAst = aeso_ast_infer_types:infer(Ast, Options),
|
pp_assembler(Assembler, Options),
|
||||||
%% pp_types is handled inside aeso_ast_infer_types.
|
|
||||||
ok = pp_typed_ast(TypedAst, Options),
|
|
||||||
ICode = to_icode(TypedAst, Options),
|
|
||||||
TypeInfo = extract_type_info(ICode),
|
|
||||||
ok = pp_icode(ICode, Options),
|
|
||||||
Assembler = assemble(ICode, Options),
|
|
||||||
ok = pp_assembler(Assembler, Options),
|
|
||||||
ByteCodeList = to_bytecode(Assembler, Options),
|
ByteCodeList = to_bytecode(Assembler, Options),
|
||||||
ByteCode = << << B:8 >> || B <- ByteCodeList >>,
|
ByteCode = << << B:8 >> || B <- ByteCodeList >>,
|
||||||
ok = pp_bytecode(ByteCode, Options),
|
pp_bytecode(ByteCode, Options),
|
||||||
|
{ok, Version} = version(),
|
||||||
{ok, #{byte_code => ByteCode,
|
{ok, #{byte_code => ByteCode,
|
||||||
compiler_version => version(),
|
compiler_version => Version,
|
||||||
contract_source => ContractString,
|
contract_source => ContractString,
|
||||||
type_info => TypeInfo
|
type_info => TypeInfo
|
||||||
}}
|
}}
|
||||||
@@ -87,38 +103,69 @@ from_string(ContractString, Options) ->
|
|||||||
%% General programming errors in the compiler just signal error.
|
%% General programming errors in the compiler just signal error.
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec string_to_icode(string(), [option() | permissive_address_literals]) -> map().
|
||||||
|
string_to_icode(ContractString, Options0) ->
|
||||||
|
{InferOptions, Options} = lists:partition(fun(Opt) -> Opt == permissive_address_literals end, Options0),
|
||||||
|
Ast = parse(ContractString, Options),
|
||||||
|
pp_sophia_code(Ast, Options),
|
||||||
|
pp_ast(Ast, Options),
|
||||||
|
{TypeEnv, TypedAst} = aeso_ast_infer_types:infer(Ast, [return_env | InferOptions]),
|
||||||
|
pp_typed_ast(TypedAst, Options),
|
||||||
|
Icode = ast_to_icode(TypedAst, Options),
|
||||||
|
pp_icode(Icode, Options),
|
||||||
|
#{ typed_ast => TypedAst,
|
||||||
|
type_env => TypeEnv,
|
||||||
|
icode => Icode }.
|
||||||
|
|
||||||
join_errors(Prefix, Errors, Pfun) ->
|
join_errors(Prefix, Errors, Pfun) ->
|
||||||
Ess = [ Pfun(E) || E <- Errors ],
|
Ess = [ Pfun(E) || E <- Errors ],
|
||||||
list_to_binary(string:join([Prefix|Ess], "\n")).
|
list_to_binary(string:join([Prefix|Ess], "\n")).
|
||||||
|
|
||||||
-define(CALL_NAME, "__call").
|
-define(CALL_NAME, "__call").
|
||||||
|
-define(DECODE_NAME, "__decode").
|
||||||
|
|
||||||
%% Takes a string containing a contract with a declaration/prototype of a
|
%% Takes a string containing a contract with a declaration/prototype of a
|
||||||
%% function (foo, say) and a function __call() = foo(args) calling this
|
%% function (foo, say) and adds function __call() = foo(args) calling this
|
||||||
%% function. Returns the name of the called functions, typereps and Erlang
|
%% function. Returns the name of the called functions, typereps and Erlang
|
||||||
%% terms for the arguments.
|
%% terms for the arguments.
|
||||||
-spec check_call(string(), options()) -> {ok, string(), {[Type], Type | any}, [term()]} | {error, term()}
|
%% NOTE: Special treatment for "init" since it might be implicit and has
|
||||||
|
%% a special return type (typerep, T)
|
||||||
|
-spec check_call(string(), string(), [string()], options()) -> {ok, string(), {[Type], Type}, [term()]} | {error, term()}
|
||||||
when Type :: term().
|
when Type :: term().
|
||||||
check_call(ContractString, Options) ->
|
check_call(Source, "init" = FunName, Args, Options) ->
|
||||||
|
PatchFun = fun(T) -> {tuple, [typerep, T]} end,
|
||||||
|
case check_call(Source, FunName, Args, Options, PatchFun) of
|
||||||
|
Err = {error, _} when Args == [] ->
|
||||||
|
%% Try with default init-function
|
||||||
|
case check_call(insert_init_function(Source, Options), FunName, Args, Options, PatchFun) of
|
||||||
|
{error, _} -> Err; %% The first error is most likely better...
|
||||||
|
Res -> Res
|
||||||
|
end;
|
||||||
|
Res ->
|
||||||
|
Res
|
||||||
|
end;
|
||||||
|
check_call(Source, FunName, Args, Options) ->
|
||||||
|
PatchFun = fun(T) -> T end,
|
||||||
|
check_call(Source, FunName, Args, Options, PatchFun).
|
||||||
|
|
||||||
|
check_call(ContractString0, FunName, Args, Options, PatchFun) ->
|
||||||
try
|
try
|
||||||
Ast = parse(ContractString, Options),
|
%% First check the contract without the __call function and no permissive literals
|
||||||
ok = pp_sophia_code(Ast, Options),
|
#{} = string_to_icode(ContractString0, Options),
|
||||||
ok = pp_ast(Ast, Options),
|
ContractString = insert_call_function(ContractString0, FunName, Args, Options),
|
||||||
TypedAst = aeso_ast_infer_types:infer(Ast, [permissive_address_literals]),
|
#{typed_ast := TypedAst,
|
||||||
|
icode := Icode} = string_to_icode(ContractString, [permissive_address_literals | Options]),
|
||||||
{ok, {FunName, {fun_t, _, _, ArgTypes, RetType}}} = get_call_type(TypedAst),
|
{ok, {FunName, {fun_t, _, _, ArgTypes, RetType}}} = get_call_type(TypedAst),
|
||||||
ok = pp_typed_ast(TypedAst, Options),
|
|
||||||
Icode = to_icode(TypedAst, Options),
|
|
||||||
ArgVMTypes = [ aeso_ast_to_icode:ast_typerep(T, Icode) || T <- ArgTypes ],
|
ArgVMTypes = [ aeso_ast_to_icode:ast_typerep(T, Icode) || T <- ArgTypes ],
|
||||||
RetVMType = case RetType of
|
RetVMType = case RetType of
|
||||||
{id, _, "_"} -> any;
|
{id, _, "_"} -> any;
|
||||||
_ -> aeso_ast_to_icode:ast_typerep(RetType, Icode)
|
_ -> aeso_ast_to_icode:ast_typerep(RetType, Icode)
|
||||||
end,
|
end,
|
||||||
ok = pp_icode(Icode, Options),
|
|
||||||
#{ functions := Funs } = Icode,
|
#{ functions := Funs } = Icode,
|
||||||
ArgIcode = get_arg_icode(Funs),
|
ArgIcode = get_arg_icode(Funs),
|
||||||
ArgTerms = [ icode_to_term(T, Arg) ||
|
ArgTerms = [ icode_to_term(T, Arg) ||
|
||||||
{T, Arg} <- lists:zip(ArgVMTypes, ArgIcode) ],
|
{T, Arg} <- lists:zip(ArgVMTypes, ArgIcode) ],
|
||||||
{ok, FunName, {ArgVMTypes, RetVMType}, ArgTerms}
|
{ok, FunName, {ArgVMTypes, PatchFun(RetVMType)}, ArgTerms}
|
||||||
catch
|
catch
|
||||||
error:{parse_errors, Errors} ->
|
error:{parse_errors, Errors} ->
|
||||||
{error, join_errors("Parse errors", Errors, fun (E) -> E end)};
|
{error, join_errors("Parse errors", Errors, fun (E) -> E end)};
|
||||||
@@ -132,44 +179,159 @@ check_call(ContractString, Options) ->
|
|||||||
fun (E) -> io_lib:format("~p", [E]) end)}
|
fun (E) -> io_lib:format("~p", [E]) end)}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec create_calldata(map(), string(), string()) ->
|
%% Add the __call function to a contract.
|
||||||
{ok, binary(), aeso_sophia:type(), aeso_sophia:type()}
|
-spec insert_call_function(string(), string(), [string()], options()) -> string().
|
||||||
| {error, argument_syntax_error}.
|
insert_call_function(Code, FunName, Args, Options) ->
|
||||||
create_calldata(Contract, "", CallCode) when is_map(Contract) ->
|
Ast = parse(Code, Options),
|
||||||
case check_call(CallCode, []) of
|
Ind = last_contract_indent(Ast),
|
||||||
{ok, FunName, {ArgTypes, RetType}, Args} ->
|
lists:flatten(
|
||||||
aeso_abi:create_calldata(Contract, FunName, Args, ArgTypes, RetType);
|
[ Code,
|
||||||
{error, _} = Err -> Err
|
"\n\n",
|
||||||
end;
|
lists:duplicate(Ind, " "),
|
||||||
create_calldata(Contract, Function, Argument) when is_map(Contract) ->
|
"function __call() = ", FunName, "(", string:join(Args, ","), ")\n"
|
||||||
%% Slightly hacky shortcut to let you get away without writing the full
|
]).
|
||||||
%% call contract code.
|
|
||||||
%% Function should be "foo : type", and
|
-spec insert_init_function(string(), options()) -> string().
|
||||||
%% Argument should be "Arg1, Arg2, .., ArgN" (no parens)
|
insert_init_function(Code, Options) ->
|
||||||
case string:lexemes(Function, ": ") of
|
Ast = parse(Code, Options),
|
||||||
%% If function is a single word fallback to old calldata generation
|
Ind = last_contract_indent(Ast),
|
||||||
[FunName] -> aeso_abi:old_create_calldata(Contract, FunName, Argument);
|
lists:flatten(
|
||||||
[FunName | _] ->
|
[ Code,
|
||||||
Args = lists:map(fun($\n) -> 32; (X) -> X end, Argument), %% newline to space
|
"\n\n",
|
||||||
CallContract = lists:flatten(
|
lists:duplicate(Ind, " "), "function init() = ()\n"
|
||||||
[ "contract Call =\n"
|
]).
|
||||||
, " function ", Function, "\n"
|
|
||||||
, " function __call() = ", FunName, "(", Args, ")"
|
last_contract_indent(Decls) ->
|
||||||
]),
|
case lists:last(Decls) of
|
||||||
create_calldata(Contract, "", CallContract)
|
{_, _, _, [Decl | _]} -> aeso_syntax:get_ann(col, Decl, 1) - 1;
|
||||||
|
_ -> 0
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
-spec to_sophia_value(string(), string(), ok | error | revert, aeso_sophia:data()) ->
|
||||||
|
{ok, aeso_syntax:expr()} | {error, term()}.
|
||||||
|
to_sophia_value(ContractString, Fun, ResType, Data) ->
|
||||||
|
to_sophia_value(ContractString, Fun, ResType, Data, []).
|
||||||
|
|
||||||
|
-spec to_sophia_value(string(), string(), ok | error | revert, binary(), options()) ->
|
||||||
|
{ok, aeso_syntax:expr()} | {error, term()}.
|
||||||
|
to_sophia_value(_, _, error, Err, _Options) ->
|
||||||
|
{ok, {app, [], {id, [], "error"}, [{string, [], Err}]}};
|
||||||
|
to_sophia_value(_, _, revert, Data, _Options) ->
|
||||||
|
case aeso_heap:from_binary(string, Data) of
|
||||||
|
{ok, Err} -> {ok, {app, [], {id, [], "abort"}, [{string, [], Err}]}};
|
||||||
|
{error, _} = Err -> Err
|
||||||
|
end;
|
||||||
|
to_sophia_value(ContractString, FunName, ok, Data, Options) ->
|
||||||
|
try
|
||||||
|
#{ typed_ast := TypedAst,
|
||||||
|
type_env := TypeEnv,
|
||||||
|
icode := Icode } = string_to_icode(ContractString, Options),
|
||||||
|
{ok, Type0} = get_decode_type(FunName, TypedAst),
|
||||||
|
Type = aeso_ast_infer_types:unfold_types_in_type(TypeEnv, Type0, [unfold_record_types, unfold_variant_types]),
|
||||||
|
VmType = aeso_ast_to_icode:ast_typerep(Type, Icode),
|
||||||
|
case aeso_heap:from_binary(VmType, Data) of
|
||||||
|
{ok, VmValue} ->
|
||||||
|
try
|
||||||
|
{ok, translate_vm_value(VmType, Type, VmValue)}
|
||||||
|
catch throw:cannot_translate_to_sophia ->
|
||||||
|
Type0Str = prettypr:format(aeso_pretty:type(Type0)),
|
||||||
|
{error, join_errors("Translation error", [lists:flatten(io_lib:format("Cannot translate VM value ~p\n of type ~p\n to Sophia type ~s\n",
|
||||||
|
[Data, VmType, Type0Str]))],
|
||||||
|
fun (E) -> E end)}
|
||||||
|
end;
|
||||||
|
{error, _Err} ->
|
||||||
|
{error, join_errors("Decode errors", [lists:flatten(io_lib:format("Failed to decode binary at type ~p", [VmType]))],
|
||||||
|
fun(E) -> E end)}
|
||||||
|
end
|
||||||
|
catch
|
||||||
|
error:{parse_errors, Errors} ->
|
||||||
|
{error, join_errors("Parse errors", Errors, fun (E) -> E end)};
|
||||||
|
error:{type_errors, Errors} ->
|
||||||
|
{error, join_errors("Type errors", Errors, fun (E) -> E end)};
|
||||||
|
error:{badmatch, {error, missing_function}} ->
|
||||||
|
{error, join_errors("Type errors", ["no function: '" ++ FunName ++ "'"],
|
||||||
|
fun (E) -> E end)};
|
||||||
|
throw:Error -> %Don't ask
|
||||||
|
{error, join_errors("Code errors", [Error],
|
||||||
|
fun (E) -> io_lib:format("~p", [E]) end)}
|
||||||
|
end.
|
||||||
|
|
||||||
|
address_literal(N) -> {hash, [], <<N:256>>}. % TODO
|
||||||
|
|
||||||
|
%% TODO: somewhere else
|
||||||
|
translate_vm_value(word, {id, _, "address"}, N) -> address_literal(N);
|
||||||
|
translate_vm_value(word, {app_t, _, {id, _, "oracle"}, _}, N) -> address_literal(N);
|
||||||
|
translate_vm_value(word, {app_t, _, {id, _, "oracle_query"}, _}, N) -> address_literal(N);
|
||||||
|
translate_vm_value(word, {id, _, "hash"}, N) -> {hash, [], <<N:256>>};
|
||||||
|
translate_vm_value(word, {id, _, "int"}, N) -> {int, [], N};
|
||||||
|
translate_vm_value(word, {id, _, "bits"}, N) -> error({todo, bits, N});
|
||||||
|
translate_vm_value(word, {id, _, "bool"}, N) -> {bool, [], N /= 0};
|
||||||
|
translate_vm_value({tuple, [word, word]}, {id, _, "signature"}, {tuple, [Hi, Lo]}) ->
|
||||||
|
{hash, [], <<Hi:256, Lo:256>>};
|
||||||
|
translate_vm_value(string, {id, _, "string"}, S) -> {string, [], S};
|
||||||
|
translate_vm_value({list, VmType}, {app_t, _, {id, _, "list"}, [Type]}, List) ->
|
||||||
|
{list, [], [translate_vm_value(VmType, Type, X) || X <- List]};
|
||||||
|
translate_vm_value({option, VmType}, {app_t, _, {id, _, "option"}, [Type]}, Val) ->
|
||||||
|
case Val of
|
||||||
|
none -> {con, [], "None"};
|
||||||
|
{some, X} -> {app, [], {con, [], "Some"}, [translate_vm_value(VmType, Type, X)]}
|
||||||
|
end;
|
||||||
|
translate_vm_value({variant, [[], [VmType]]}, {app_t, _, {id, _, "option"}, [Type]}, Val) ->
|
||||||
|
case Val of
|
||||||
|
{variant, 0, []} -> {con, [], "None"};
|
||||||
|
{variant, 1, [X]} -> {app, [], {con, [], "Some"}, [translate_vm_value(VmType, Type, X)]}
|
||||||
|
end;
|
||||||
|
translate_vm_value({tuple, VmTypes}, {tuple_t, _, Types}, Val)
|
||||||
|
when length(VmTypes) == length(Types),
|
||||||
|
length(VmTypes) == tuple_size(Val) ->
|
||||||
|
{tuple, [], [translate_vm_value(VmType, Type, X)
|
||||||
|
|| {VmType, Type, X} <- lists:zip3(VmTypes, Types, tuple_to_list(Val))]};
|
||||||
|
translate_vm_value({tuple, VmTypes}, {record_t, Fields}, Val)
|
||||||
|
when length(VmTypes) == length(Fields),
|
||||||
|
length(VmTypes) == tuple_size(Val) ->
|
||||||
|
{record, [], [ {field, [], [{proj, [], FName}], translate_vm_value(VmType, FType, X)}
|
||||||
|
|| {VmType, {field_t, _, FName, FType}, X} <- lists:zip3(VmTypes, Fields, tuple_to_list(Val)) ]};
|
||||||
|
translate_vm_value({map, VmKeyType, VmValType}, {app_t, _, {id, _, "map"}, [KeyType, ValType]}, Map)
|
||||||
|
when is_map(Map) ->
|
||||||
|
{map, [], [ {translate_vm_value(VmKeyType, KeyType, Key),
|
||||||
|
translate_vm_value(VmValType, ValType, Val)}
|
||||||
|
|| {Key, Val} <- maps:to_list(Map) ]};
|
||||||
|
translate_vm_value({variant, VmCons}, {variant_t, Cons}, {variant, Tag, Args})
|
||||||
|
when length(VmCons) == length(Cons),
|
||||||
|
length(VmCons) > Tag ->
|
||||||
|
VmTypes = lists:nth(Tag + 1, VmCons),
|
||||||
|
ConType = lists:nth(Tag + 1, Cons),
|
||||||
|
translate_vm_value(VmTypes, ConType, Args);
|
||||||
|
translate_vm_value(VmTypes, {constr_t, _, Con, Types}, Args)
|
||||||
|
when length(VmTypes) == length(Types),
|
||||||
|
length(VmTypes) == length(Args) ->
|
||||||
|
{app, [], Con, [ translate_vm_value(VmType, Type, Arg)
|
||||||
|
|| {VmType, Type, Arg} <- lists:zip3(VmTypes, Types, Args) ]};
|
||||||
|
translate_vm_value(_VmType, _Type, _Data) ->
|
||||||
|
throw(cannot_translate_to_sophia).
|
||||||
|
|
||||||
|
-spec create_calldata(string(), string(), [string()]) ->
|
||||||
|
{ok, binary(), aeso_sophia:type(), aeso_sophia:type()}
|
||||||
|
| {error, term()}.
|
||||||
|
create_calldata(Code, Fun, Args) ->
|
||||||
|
case check_call(Code, Fun, Args, []) of
|
||||||
|
{ok, FunName, {ArgTypes, RetType}, VMArgs} ->
|
||||||
|
aeso_abi:create_calldata(FunName, VMArgs, ArgTypes, RetType);
|
||||||
|
{error, _} = Err -> Err
|
||||||
|
end.
|
||||||
|
|
||||||
get_arg_icode(Funs) ->
|
get_arg_icode(Funs) ->
|
||||||
[Args] = [ Args || {?CALL_NAME, _, _, {funcall, _, Args}, _} <- Funs ],
|
case [ Args || {[_, ?CALL_NAME], _, _, {funcall, _, Args}, _} <- Funs ] of
|
||||||
Args.
|
[Args] -> Args;
|
||||||
|
[] -> error({missing_call_function, Funs})
|
||||||
|
end.
|
||||||
|
|
||||||
get_call_type([{contract, _, _, Defs}]) ->
|
get_call_type([{contract, _, _, Defs}]) ->
|
||||||
case [ {FunName, FunType}
|
case [ {lists:last(QFunName), FunType}
|
||||||
|| {letfun, _, {id, _, ?CALL_NAME}, [], _Ret,
|
|| {letfun, _, {id, _, ?CALL_NAME}, [], _Ret,
|
||||||
{typed, _,
|
{typed, _,
|
||||||
{app, _,
|
{app, _,
|
||||||
{typed, _, {id, _, FunName}, FunType}, _}, _}} <- Defs ] of
|
{typed, _, {qid, _, QFunName}, FunType}, _}, _}} <- Defs ] of
|
||||||
[Call] -> {ok, Call};
|
[Call] -> {ok, Call};
|
||||||
[] -> {error, missing_call_function}
|
[] -> {error, missing_call_function}
|
||||||
end;
|
end;
|
||||||
@@ -177,6 +339,18 @@ get_call_type([_ | Contracts]) ->
|
|||||||
%% The __call should be in the final contract
|
%% The __call should be in the final contract
|
||||||
get_call_type(Contracts).
|
get_call_type(Contracts).
|
||||||
|
|
||||||
|
get_decode_type(FunName, [{contract, _, _, Defs}]) ->
|
||||||
|
GetType = fun({letfun, _, {id, _, Name}, _, Ret, _}) when Name == FunName -> [Ret];
|
||||||
|
({fun_decl, _, {id, _, Name}, {fun_t, _, _, _, Ret}}) when Name == FunName -> [Ret];
|
||||||
|
(_) -> [] end,
|
||||||
|
case lists:flatmap(GetType, Defs) of
|
||||||
|
[Type] -> {ok, Type};
|
||||||
|
[] -> {error, missing_function}
|
||||||
|
end;
|
||||||
|
get_decode_type(FunName, [_ | Contracts]) ->
|
||||||
|
%% The __decode should be in the final contract
|
||||||
|
get_decode_type(FunName, Contracts).
|
||||||
|
|
||||||
%% Translate an icode value (error if not value) to an Erlang term that can be
|
%% Translate an icode value (error if not value) to an Erlang term that can be
|
||||||
%% consumed by aeso_heap:to_binary().
|
%% consumed by aeso_heap:to_binary().
|
||||||
icode_to_term(word, {integer, N}) -> N;
|
icode_to_term(word, {integer, N}) -> N;
|
||||||
@@ -211,10 +385,7 @@ icode_to_term(T, V) ->
|
|||||||
icodes_to_terms(Ts, Vs) ->
|
icodes_to_terms(Ts, Vs) ->
|
||||||
[ icode_to_term(T, V) || {T, V} <- lists:zip(Ts, Vs) ].
|
[ icode_to_term(T, V) || {T, V} <- lists:zip(Ts, Vs) ].
|
||||||
|
|
||||||
parse(C,_Options) ->
|
ast_to_icode(TypedAst, Options) ->
|
||||||
parse_string(C).
|
|
||||||
|
|
||||||
to_icode(TypedAst, Options) ->
|
|
||||||
aeso_ast_to_icode:convert_typed(TypedAst, Options).
|
aeso_ast_to_icode:convert_typed(TypedAst, Options).
|
||||||
|
|
||||||
assemble(Icode, Options) ->
|
assemble(Icode, Options) ->
|
||||||
@@ -228,7 +399,9 @@ to_bytecode([Op|Rest], Options) ->
|
|||||||
to_bytecode([], _) -> [].
|
to_bytecode([], _) -> [].
|
||||||
|
|
||||||
extract_type_info(#{functions := Functions} =_Icode) ->
|
extract_type_info(#{functions := Functions} =_Icode) ->
|
||||||
TypeInfo = [aeso_abi:function_type_info(list_to_binary(Name), Args, TypeRep)
|
ArgTypesOnly = fun(As) -> [ T || {_, T} <- As ] end,
|
||||||
|
TypeInfo = [aeso_abi:function_type_info(list_to_binary(lists:last(Name)),
|
||||||
|
ArgTypesOnly(Args), TypeRep)
|
||||||
|| {Name, Attrs, Args,_Body, TypeRep} <- Functions,
|
|| {Name, Attrs, Args,_Body, TypeRep} <- Functions,
|
||||||
not is_tuple(Name),
|
not is_tuple(Name),
|
||||||
not lists:member(private, Attrs)
|
not lists:member(private, Attrs)
|
||||||
@@ -263,9 +436,9 @@ sophia_type_to_typerep(String) ->
|
|||||||
catch _:_ -> {error, bad_type}
|
catch _:_ -> {error, bad_type}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
parse_string(Text) ->
|
parse(Text, Options) ->
|
||||||
%% Try and return something sensible here!
|
%% Try and return something sensible here!
|
||||||
case aeso_parser:string(Text) of
|
case aeso_parser:string(Text, Options) of
|
||||||
%% Yay, it worked!
|
%% Yay, it worked!
|
||||||
{ok, Contract} -> Contract;
|
{ok, Contract} -> Contract;
|
||||||
%% Scan errors.
|
%% Scan errors.
|
||||||
@@ -278,12 +451,23 @@ parse_string(Text) ->
|
|||||||
parse_error(Pos, Error);
|
parse_error(Pos, Error);
|
||||||
{error, {Pos, ambiguous_parse, As}} ->
|
{error, {Pos, ambiguous_parse, As}} ->
|
||||||
ErrorString = io_lib:format("Ambiguous ~p", [As]),
|
ErrorString = io_lib:format("Ambiguous ~p", [As]),
|
||||||
parse_error(Pos, ErrorString)
|
parse_error(Pos, ErrorString);
|
||||||
|
%% Include error
|
||||||
|
{error, {Pos, include_error, File}} ->
|
||||||
|
parse_error(Pos, io_lib:format("could not find include file '~s'", [File]))
|
||||||
end.
|
end.
|
||||||
|
|
||||||
parse_error({Line, Pos}, ErrorString) ->
|
parse_error(Pos, ErrorString) ->
|
||||||
Error = io_lib:format("line ~p, column ~p: ~s", [Line, Pos, ErrorString]),
|
Error = io_lib:format("~s: ~s", [pos_error(Pos), ErrorString]),
|
||||||
error({parse_errors, [Error]}).
|
error({parse_errors, [Error]}).
|
||||||
|
|
||||||
read_contract(Name) ->
|
read_contract(Name) ->
|
||||||
file:read_file(Name).
|
file:read_file(Name).
|
||||||
|
|
||||||
|
pos_error({Line, Pos}) ->
|
||||||
|
io_lib:format("line ~p, column ~p", [Line, Pos]);
|
||||||
|
pos_error({no_file, Line, Pos}) ->
|
||||||
|
pos_error({Line, Pos});
|
||||||
|
pos_error({File, Line, Pos}) ->
|
||||||
|
io_lib:format("file ~s, line ~p, column ~p", [File, Line, Pos]).
|
||||||
|
|
||||||
|
|||||||
+38
-7
@@ -9,7 +9,18 @@
|
|||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(aeso_icode).
|
-module(aeso_icode).
|
||||||
|
|
||||||
-export([new/1, pp/1, set_name/2, set_functions/2, map_typerep/2, option_typerep/1, get_constructor_tag/2]).
|
-export([new/1,
|
||||||
|
pp/1,
|
||||||
|
set_name/2,
|
||||||
|
set_namespace/2,
|
||||||
|
enter_namespace/2,
|
||||||
|
get_namespace/1,
|
||||||
|
qualify/2,
|
||||||
|
set_functions/2,
|
||||||
|
map_typerep/2,
|
||||||
|
option_typerep/1,
|
||||||
|
get_constructor_tag/2]).
|
||||||
|
|
||||||
-export_type([icode/0]).
|
-export_type([icode/0]).
|
||||||
|
|
||||||
-include("aeso_icode.hrl").
|
-include("aeso_icode.hrl").
|
||||||
@@ -29,12 +40,13 @@
|
|||||||
|
|
||||||
-type icode() :: #{ contract_name => string()
|
-type icode() :: #{ contract_name => string()
|
||||||
, functions => [fun_dec()]
|
, functions => [fun_dec()]
|
||||||
|
, namespace => aeso_syntax:con() | aeso_syntax:qcon()
|
||||||
, env => [bindings()]
|
, env => [bindings()]
|
||||||
, state_type => aeso_sophia:type()
|
, state_type => aeso_sophia:type()
|
||||||
, event_type => aeso_sophia:type()
|
, event_type => aeso_sophia:type()
|
||||||
, types => #{ type_name() => type_def() }
|
, types => #{ type_name() => type_def() }
|
||||||
, type_vars => #{ string() => aeso_sophia:type() }
|
, type_vars => #{ string() => aeso_sophia:type() }
|
||||||
, constructors => #{ string() => integer() } %% name to tag
|
, constructors => #{ [string()] => integer() } %% name to tag
|
||||||
, options => [any()]
|
, options => [any()]
|
||||||
}.
|
}.
|
||||||
|
|
||||||
@@ -73,10 +85,10 @@ builtin_types() ->
|
|||||||
}.
|
}.
|
||||||
|
|
||||||
builtin_constructors() ->
|
builtin_constructors() ->
|
||||||
#{ "RelativeTTL" => 0
|
#{ ["RelativeTTL"] => 0
|
||||||
, "FixedTTL" => 1
|
, ["FixedTTL"] => 1
|
||||||
, "None" => 0
|
, ["None"] => 0
|
||||||
, "Some" => 1 }.
|
, ["Some"] => 1 }.
|
||||||
|
|
||||||
map_typerep(K, V) ->
|
map_typerep(K, V) ->
|
||||||
{map, K, V}.
|
{map, K, V}.
|
||||||
@@ -91,11 +103,30 @@ new_env() ->
|
|||||||
set_name(Name, Icode) ->
|
set_name(Name, Icode) ->
|
||||||
maps:put(contract_name, Name, Icode).
|
maps:put(contract_name, Name, Icode).
|
||||||
|
|
||||||
|
-spec set_namespace(aeso_syntax:con() | aeso_syntax:qcon(), icode()) -> icode().
|
||||||
|
set_namespace(NS, Icode) -> Icode#{ namespace => NS }.
|
||||||
|
|
||||||
|
-spec enter_namespace(aeso_syntax:con(), icode()) -> icode().
|
||||||
|
enter_namespace(NS, Icode = #{ namespace := NS1 }) ->
|
||||||
|
Icode#{ namespace => aeso_syntax:qualify(NS1, NS) };
|
||||||
|
enter_namespace(NS, Icode) ->
|
||||||
|
Icode#{ namespace => NS }.
|
||||||
|
|
||||||
|
-spec get_namespace(icode()) -> false | aeso_syntax:con() | aeso_syntax:qcon().
|
||||||
|
get_namespace(Icode) -> maps:get(namespace, Icode, false).
|
||||||
|
|
||||||
|
-spec qualify(aeso_syntax:id() | aeso_syntax:con(), icode()) -> aeso_syntax:id() | aeso_syntax:qid() | aeso_syntax:con() | aeso_syntax:qcon().
|
||||||
|
qualify(X, Icode) ->
|
||||||
|
case get_namespace(Icode) of
|
||||||
|
false -> X;
|
||||||
|
NS -> aeso_syntax:qualify(NS, X)
|
||||||
|
end.
|
||||||
|
|
||||||
-spec set_functions([fun_dec()], icode()) -> icode().
|
-spec set_functions([fun_dec()], icode()) -> icode().
|
||||||
set_functions(NewFuns, Icode) ->
|
set_functions(NewFuns, Icode) ->
|
||||||
maps:put(functions, NewFuns, Icode).
|
maps:put(functions, NewFuns, Icode).
|
||||||
|
|
||||||
-spec get_constructor_tag(string(), icode()) -> integer().
|
-spec get_constructor_tag([string()], icode()) -> integer().
|
||||||
get_constructor_tag(Name, #{constructors := Constructors}) ->
|
get_constructor_tag(Name, #{constructors := Constructors}) ->
|
||||||
case maps:get(Name, Constructors, undefined) of
|
case maps:get(Name, Constructors, undefined) of
|
||||||
undefined -> error({undefined_constructor, Name});
|
undefined -> error({undefined_constructor, Name});
|
||||||
|
|||||||
+1
-1
@@ -20,7 +20,7 @@
|
|||||||
, args :: arg_list()
|
, args :: arg_list()
|
||||||
, body :: expr()}).
|
, body :: expr()}).
|
||||||
|
|
||||||
-record(var_ref, { name :: string() | {builtin, atom() | tuple()}}).
|
-record(var_ref, { name :: string() | list(string()) | {builtin, atom() | tuple()}}).
|
||||||
|
|
||||||
-record(prim_call_contract,
|
-record(prim_call_contract,
|
||||||
{ gas :: expr()
|
{ gas :: expr()
|
||||||
|
|||||||
@@ -17,7 +17,7 @@
|
|||||||
i(Code) -> aeb_opcodes:mnemonic(Code).
|
i(Code) -> aeb_opcodes:mnemonic(Code).
|
||||||
|
|
||||||
%% We don't track purity or statefulness in the type checker yet.
|
%% We don't track purity or statefulness in the type checker yet.
|
||||||
is_stateful({FName, _, _, _, _}) -> FName /= "init".
|
is_stateful({FName, _, _, _, _}) -> lists:last(FName) /= "init".
|
||||||
|
|
||||||
is_public({_Name, Attrs, _Args, _Body, _Type}) -> not lists:member(private, Attrs).
|
is_public({_Name, Attrs, _Args, _Body, _Type}) -> not lists:member(private, Attrs).
|
||||||
|
|
||||||
@@ -105,7 +105,7 @@ make_args(Args) ->
|
|||||||
|
|
||||||
fun_hash({FName, _, Args, _, TypeRep}) ->
|
fun_hash({FName, _, Args, _, TypeRep}) ->
|
||||||
ArgType = {tuple, [T || {_, T} <- Args]},
|
ArgType = {tuple, [T || {_, T} <- Args]},
|
||||||
<<Hash:256>> = aeso_abi:function_type_hash(list_to_binary(FName), ArgType, TypeRep),
|
<<Hash:256>> = aeso_abi:function_type_hash(list_to_binary(lists:last(FName)), ArgType, TypeRep),
|
||||||
{integer, Hash}.
|
{integer, Hash}.
|
||||||
|
|
||||||
%% Expects two return addresses below N elements on the stack. Picks the top
|
%% Expects two return addresses below N elements on the stack. Picks the top
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
-export_type([parser/1, parser_expr/1, pos/0, token/0, tokens/0]).
|
-export_type([parser/1, parser_expr/1, pos/0, token/0, tokens/0]).
|
||||||
|
|
||||||
-type pos() :: {integer(), integer()}.
|
-type pos() :: {string() | no_file, integer(), integer()} | {integer(), integer()}.
|
||||||
-type token() :: {atom(), pos(), term()} | {atom(), pos()}.
|
-type token() :: {atom(), pos(), term()} | {atom(), pos()}.
|
||||||
-type tokens() :: [token()].
|
-type tokens() :: [token()].
|
||||||
-type error() :: {pos(), string() | no_error}.
|
-type error() :: {pos(), string() | no_error}.
|
||||||
|
|||||||
+86
-25
@@ -5,28 +5,37 @@
|
|||||||
-module(aeso_parser).
|
-module(aeso_parser).
|
||||||
|
|
||||||
-export([string/1,
|
-export([string/1,
|
||||||
|
string/2,
|
||||||
type/1]).
|
type/1]).
|
||||||
|
|
||||||
-include("aeso_parse_lib.hrl").
|
-include("aeso_parse_lib.hrl").
|
||||||
|
|
||||||
-spec string(string()) ->
|
-type parse_result() :: {ok, aeso_syntax:ast()}
|
||||||
{ok, aeso_syntax:ast()}
|
| {error, {aeso_parse_lib:pos(), atom(), term()}}
|
||||||
| {error, {aeso_parse_lib:pos(),
|
| {error, {aeso_parse_lib:pos(), atom()}}.
|
||||||
atom(),
|
|
||||||
term()}}
|
-spec string(string()) -> parse_result().
|
||||||
| {error, {aeso_parse_lib:pos(),
|
|
||||||
atom()}}.
|
|
||||||
string(String) ->
|
string(String) ->
|
||||||
parse_and_scan(file(), String).
|
string(String, []).
|
||||||
|
|
||||||
|
-spec string(string(), aeso_compiler:options()) -> parse_result().
|
||||||
|
string(String, Opts) ->
|
||||||
|
case parse_and_scan(file(), String, Opts) of
|
||||||
|
{ok, AST} ->
|
||||||
|
expand_includes(AST, Opts);
|
||||||
|
Err = {error, _} ->
|
||||||
|
Err
|
||||||
|
end.
|
||||||
|
|
||||||
type(String) ->
|
type(String) ->
|
||||||
parse_and_scan(type(), String).
|
parse_and_scan(type(), String, []).
|
||||||
|
|
||||||
parse_and_scan(P, S) ->
|
parse_and_scan(P, S, Opts) ->
|
||||||
case aeso_scan:scan(S) of
|
set_current_file(proplists:get_value(src_file, Opts, no_file)),
|
||||||
{ok, Tokens} -> aeso_parse_lib:parse(P, Tokens);
|
case aeso_scan:scan(S) of
|
||||||
Error -> Error
|
{ok, Tokens} -> aeso_parse_lib:parse(P, Tokens);
|
||||||
end.
|
Error -> Error
|
||||||
|
end.
|
||||||
|
|
||||||
%% -- Parsing rules ----------------------------------------------------------
|
%% -- Parsing rules ----------------------------------------------------------
|
||||||
|
|
||||||
@@ -36,7 +45,9 @@ decl() ->
|
|||||||
?LAZY_P(
|
?LAZY_P(
|
||||||
choice(
|
choice(
|
||||||
%% Contract declaration
|
%% Contract declaration
|
||||||
[ ?RULE(keyword(contract), con(), tok('='), maybe_block(decl()), {contract, _1, _2, _4})
|
[ ?RULE(keyword(contract), con(), tok('='), maybe_block(decl()), {contract, _1, _2, _4})
|
||||||
|
, ?RULE(keyword(namespace), con(), tok('='), maybe_block(decl()), {namespace, _1, _2, _4})
|
||||||
|
, ?RULE(keyword(include), str(), {include, _2})
|
||||||
|
|
||||||
%% Type declarations TODO: format annotation for "type bla" vs "type bla()"
|
%% Type declarations TODO: format annotation for "type bla" vs "type bla()"
|
||||||
, ?RULE(keyword(type), id(), {type_decl, _1, _2, []})
|
, ?RULE(keyword(type), id(), {type_decl, _1, _2, []})
|
||||||
@@ -57,9 +68,14 @@ decl() ->
|
|||||||
modifiers() ->
|
modifiers() ->
|
||||||
many(choice([token(stateful), token(public), token(private), token(internal)])).
|
many(choice([token(stateful), token(public), token(private), token(internal)])).
|
||||||
|
|
||||||
add_modifiers(Mods, Node) ->
|
add_modifiers([], Node) -> Node;
|
||||||
lists:foldl(fun({Mod, _}, X) -> set_ann(Mod, true, X) end,
|
add_modifiers(Mods = [Tok | _], Node) ->
|
||||||
Node, Mods).
|
%% Set the position to the position of the first modifier. This is
|
||||||
|
%% important for code transformation tools (like what we do in
|
||||||
|
%% create_calldata) to be able to get the indentation of the declaration.
|
||||||
|
set_pos(get_pos(Tok),
|
||||||
|
lists:foldl(fun({Mod, _}, X) -> set_ann(Mod, true, X) end,
|
||||||
|
Node, Mods)).
|
||||||
|
|
||||||
%% -- Type declarations ------------------------------------------------------
|
%% -- Type declarations ------------------------------------------------------
|
||||||
|
|
||||||
@@ -301,6 +317,7 @@ binop(Ops) ->
|
|||||||
con() -> token(con).
|
con() -> token(con).
|
||||||
id() -> token(id).
|
id() -> token(id).
|
||||||
tvar() -> token(tvar).
|
tvar() -> token(tvar).
|
||||||
|
str() -> token(string).
|
||||||
|
|
||||||
token(Tag) ->
|
token(Tag) ->
|
||||||
?RULE(tok(Tag),
|
?RULE(tok(Tag),
|
||||||
@@ -336,10 +353,17 @@ bracket_list(P) -> brackets(comma_sep(P)).
|
|||||||
-type ann_col() :: aeso_syntax:ann_col().
|
-type ann_col() :: aeso_syntax:ann_col().
|
||||||
|
|
||||||
-spec pos_ann(ann_line(), ann_col()) -> ann().
|
-spec pos_ann(ann_line(), ann_col()) -> ann().
|
||||||
pos_ann(Line, Col) -> [{line, Line}, {col, Col}].
|
pos_ann(Line, Col) -> [{file, current_file()}, {line, Line}, {col, Col}].
|
||||||
|
|
||||||
|
current_file() ->
|
||||||
|
get('$current_file').
|
||||||
|
|
||||||
|
set_current_file(File) ->
|
||||||
|
put('$current_file', File).
|
||||||
|
|
||||||
ann_pos(Ann) ->
|
ann_pos(Ann) ->
|
||||||
{proplists:get_value(line, Ann),
|
{proplists:get_value(file, Ann),
|
||||||
|
proplists:get_value(line, Ann),
|
||||||
proplists:get_value(col, Ann)}.
|
proplists:get_value(col, Ann)}.
|
||||||
|
|
||||||
get_ann(Ann) when is_list(Ann) -> Ann;
|
get_ann(Ann) when is_list(Ann) -> Ann;
|
||||||
@@ -357,10 +381,10 @@ set_ann(Key, Val, Node) ->
|
|||||||
setelement(2, Node, lists:keystore(Key, 1, Ann, {Key, Val})).
|
setelement(2, Node, lists:keystore(Key, 1, Ann, {Key, Val})).
|
||||||
|
|
||||||
get_pos(Node) ->
|
get_pos(Node) ->
|
||||||
{get_ann(line, Node), get_ann(col, Node)}.
|
{current_file(), get_ann(line, Node), get_ann(col, Node)}.
|
||||||
|
|
||||||
set_pos({L, C}, Node) ->
|
set_pos({F, L, C}, Node) ->
|
||||||
set_ann(line, L, set_ann(col, C, Node)).
|
set_ann(file, F, set_ann(line, L, set_ann(col, C, Node))).
|
||||||
|
|
||||||
infix(L, Op, R) -> set_ann(format, infix, {app, get_ann(L), Op, [L, R]}).
|
infix(L, Op, R) -> set_ann(format, infix, {app, get_ann(L), Op, [L, R]}).
|
||||||
|
|
||||||
@@ -442,8 +466,10 @@ parse_pattern(E) -> bad_expr_err("Not a valid pattern", E).
|
|||||||
parse_field_pattern({field, Ann, F, E}) ->
|
parse_field_pattern({field, Ann, F, E}) ->
|
||||||
{field, Ann, F, parse_pattern(E)}.
|
{field, Ann, F, parse_pattern(E)}.
|
||||||
|
|
||||||
return_error({L, C}, Err) ->
|
return_error({no_file, L, C}, Err) ->
|
||||||
fail(io_lib:format("~p:~p:\n~s", [L, C, Err])).
|
fail(io_lib:format("~p:~p:\n~s", [L, C, Err]));
|
||||||
|
return_error({F, L, C}, Err) ->
|
||||||
|
fail(io_lib:format("In ~s at ~p:~p:\n~s", [F, L, C, Err])).
|
||||||
|
|
||||||
-spec ret_doc_err(ann(), prettypr:document()) -> no_return().
|
-spec ret_doc_err(ann(), prettypr:document()) -> no_return().
|
||||||
ret_doc_err(Ann, Doc) ->
|
ret_doc_err(Ann, Doc) ->
|
||||||
@@ -455,3 +481,38 @@ bad_expr_err(Reason, E) ->
|
|||||||
prettypr:sep([prettypr:text(Reason ++ ":"),
|
prettypr:sep([prettypr:text(Reason ++ ":"),
|
||||||
prettypr:nest(2, aeso_pretty:expr(E))])).
|
prettypr:nest(2, aeso_pretty:expr(E))])).
|
||||||
|
|
||||||
|
%% -- Helper functions -------------------------------------------------------
|
||||||
|
expand_includes(AST, Opts) ->
|
||||||
|
expand_includes(AST, [], Opts).
|
||||||
|
|
||||||
|
expand_includes([], Acc, _Opts) ->
|
||||||
|
{ok, lists:reverse(Acc)};
|
||||||
|
expand_includes([{include, S = {string, _, File}} | AST], Acc, Opts) ->
|
||||||
|
case read_file(File, Opts) of
|
||||||
|
{ok, Bin} ->
|
||||||
|
Opts1 = lists:keystore(src_file, 1, Opts, {src_file, File}),
|
||||||
|
case string(binary_to_list(Bin), Opts1) of
|
||||||
|
{ok, AST1} ->
|
||||||
|
expand_includes(AST1 ++ AST, Acc, Opts);
|
||||||
|
Err = {error, _} ->
|
||||||
|
Err
|
||||||
|
end;
|
||||||
|
{error, _} ->
|
||||||
|
{error, {get_pos(S), include_error, File}}
|
||||||
|
end;
|
||||||
|
expand_includes([E | AST], Acc, Opts) ->
|
||||||
|
expand_includes(AST, [E | Acc], Opts).
|
||||||
|
|
||||||
|
read_file(File, Opts) ->
|
||||||
|
case proplists:get_value(include, Opts, {explicit_files, #{}}) of
|
||||||
|
{file_system, Paths} ->
|
||||||
|
CandidateNames = [ filename:join(Dir, File) || Dir <- Paths ],
|
||||||
|
lists:foldr(fun(F, {error, _}) -> file:read_file(F);
|
||||||
|
(_F, OK) -> OK end, {error, not_found}, CandidateNames);
|
||||||
|
{explicit_files, Files} ->
|
||||||
|
case maps:get(binary_to_list(File), Files, not_found) of
|
||||||
|
not_found -> {error, not_found};
|
||||||
|
Src -> {ok, Src}
|
||||||
|
end
|
||||||
|
end.
|
||||||
|
|
||||||
|
|||||||
+9
-2
@@ -147,6 +147,8 @@ decl(D, Options) ->
|
|||||||
-spec decl(aeso_syntax:decl()) -> doc().
|
-spec decl(aeso_syntax:decl()) -> doc().
|
||||||
decl({contract, _, C, Ds}) ->
|
decl({contract, _, C, Ds}) ->
|
||||||
block(follow(text("contract"), hsep(name(C), text("="))), decls(Ds));
|
block(follow(text("contract"), hsep(name(C), text("="))), decls(Ds));
|
||||||
|
decl({namespace, _, C, Ds}) ->
|
||||||
|
block(follow(text("namespace"), hsep(name(C), text("="))), decls(Ds));
|
||||||
decl({type_decl, _, T, Vars}) -> typedecl(alias_t, T, Vars);
|
decl({type_decl, _, T, Vars}) -> typedecl(alias_t, T, Vars);
|
||||||
decl({type_def, _, T, Vars, Def}) ->
|
decl({type_def, _, T, Vars, Def}) ->
|
||||||
Kind = element(1, Def),
|
Kind = element(1, Def),
|
||||||
@@ -234,8 +236,10 @@ type({app_t, _, Type, Args}) ->
|
|||||||
beside(type(Type), tuple_type(Args));
|
beside(type(Type), tuple_type(Args));
|
||||||
type({tuple_t, _, Args}) ->
|
type({tuple_t, _, Args}) ->
|
||||||
tuple_type(Args);
|
tuple_type(Args);
|
||||||
type({named_arg_t, _, Name, Type, Default}) ->
|
type({named_arg_t, _, Name, Type, _Default}) ->
|
||||||
follow(hsep(typed(name(Name), Type), text("=")), expr(Default));
|
%% Drop the default value
|
||||||
|
%% follow(hsep(typed(name(Name), Type), text("=")), expr(Default));
|
||||||
|
typed(name(Name), Type);
|
||||||
|
|
||||||
type(R = {record_t, _}) -> typedef(R);
|
type(R = {record_t, _}) -> typedef(R);
|
||||||
type(T = {id, _, _}) -> name(T);
|
type(T = {id, _, _}) -> name(T);
|
||||||
@@ -303,6 +307,8 @@ expr_p(P, E = {app, _, F = {Op, _}, Args}) when is_atom(Op) ->
|
|||||||
{prefix, [A]} -> prefix(P, Op, A);
|
{prefix, [A]} -> prefix(P, Op, A);
|
||||||
_ -> app(P, F, Args)
|
_ -> app(P, F, Args)
|
||||||
end;
|
end;
|
||||||
|
expr_p(_, {app, _, C={Tag, _, _}, []}) when Tag == con; Tag == qcon ->
|
||||||
|
expr_p(0, C);
|
||||||
expr_p(P, {app, _, F, Args}) ->
|
expr_p(P, {app, _, F, Args}) ->
|
||||||
app(P, F, Args);
|
app(P, F, Args);
|
||||||
%% -- Constants
|
%% -- Constants
|
||||||
@@ -314,6 +320,7 @@ expr_p(_, E = {int, _, N}) ->
|
|||||||
text(S);
|
text(S);
|
||||||
expr_p(_, {bool, _, B}) -> text(atom_to_list(B));
|
expr_p(_, {bool, _, B}) -> text(atom_to_list(B));
|
||||||
expr_p(_, {hash, _, <<N:256>>}) -> text("#" ++ integer_to_list(N, 16));
|
expr_p(_, {hash, _, <<N:256>>}) -> text("#" ++ integer_to_list(N, 16));
|
||||||
|
expr_p(_, {hash, _, <<N:512>>}) -> text("#" ++ integer_to_list(N, 16));
|
||||||
expr_p(_, {unit, _}) -> text("()");
|
expr_p(_, {unit, _}) -> text("()");
|
||||||
expr_p(_, {string, _, S}) -> term(binary_to_list(S));
|
expr_p(_, {string, _, S}) -> term(binary_to_list(S));
|
||||||
expr_p(_, {char, _, C}) ->
|
expr_p(_, {char, _, C}) ->
|
||||||
|
|||||||
+2
-2
@@ -36,8 +36,8 @@ lexer() ->
|
|||||||
, {"\\*/", pop(skip())}
|
, {"\\*/", pop(skip())}
|
||||||
, {"[^/*]+|[/*]", skip()} ],
|
, {"[^/*]+|[/*]", skip()} ],
|
||||||
|
|
||||||
Keywords = ["contract", "import", "let", "rec", "switch", "type", "record", "datatype", "if", "elif", "else", "function",
|
Keywords = ["contract", "include", "let", "rec", "switch", "type", "record", "datatype", "if", "elif", "else", "function",
|
||||||
"stateful", "true", "false", "and", "mod", "public", "private", "indexed", "internal"],
|
"stateful", "true", "false", "and", "mod", "public", "private", "indexed", "internal", "namespace"],
|
||||||
KW = string:join(Keywords, "|"),
|
KW = string:join(Keywords, "|"),
|
||||||
|
|
||||||
Rules =
|
Rules =
|
||||||
|
|||||||
+9
-3
@@ -8,14 +8,14 @@
|
|||||||
|
|
||||||
-module(aeso_syntax).
|
-module(aeso_syntax).
|
||||||
|
|
||||||
-export([get_ann/1, get_ann/2, get_ann/3, set_ann/2]).
|
-export([get_ann/1, get_ann/2, get_ann/3, set_ann/2, qualify/2]).
|
||||||
|
|
||||||
-export_type([ann_line/0, ann_col/0, ann_origin/0, ann_format/0, ann/0]).
|
-export_type([ann_line/0, ann_col/0, ann_origin/0, ann_format/0, ann/0]).
|
||||||
-export_type([name/0, id/0, con/0, qid/0, qcon/0, tvar/0, op/0]).
|
-export_type([name/0, id/0, con/0, qid/0, qcon/0, tvar/0, op/0]).
|
||||||
-export_type([bin_op/0, un_op/0]).
|
-export_type([bin_op/0, un_op/0]).
|
||||||
-export_type([decl/0, letbind/0, typedef/0]).
|
-export_type([decl/0, letbind/0, typedef/0]).
|
||||||
-export_type([arg/0, field_t/0, constructor_t/0]).
|
-export_type([arg/0, field_t/0, constructor_t/0, named_arg_t/0]).
|
||||||
-export_type([type/0, constant/0, expr/0, arg_expr/0, field/1, stmt/0, alt/0, lvalue/0, pat/0]).
|
-export_type([type/0, constant/0, expr/0, arg_expr/0, field/1, stmt/0, alt/0, lvalue/0, elim/0, pat/0]).
|
||||||
-export_type([ast/0]).
|
-export_type([ast/0]).
|
||||||
|
|
||||||
-type ast() :: [decl()].
|
-type ast() :: [decl()].
|
||||||
@@ -35,6 +35,7 @@
|
|||||||
-type tvar() :: {tvar, ann(), name()}.
|
-type tvar() :: {tvar, ann(), name()}.
|
||||||
|
|
||||||
-type decl() :: {contract, ann(), con(), [decl()]}
|
-type decl() :: {contract, ann(), con(), [decl()]}
|
||||||
|
| {namespace, ann(), con(), [decl()]}
|
||||||
| {type_decl, ann(), id(), [tvar()]}
|
| {type_decl, ann(), id(), [tvar()]}
|
||||||
| {type_def, ann(), id(), [tvar()], typedef()}
|
| {type_def, ann(), id(), [tvar()], typedef()}
|
||||||
| {fun_decl, ann(), id(), type()}
|
| {fun_decl, ann(), id(), type()}
|
||||||
@@ -140,3 +141,8 @@ get_ann(Key, Node) ->
|
|||||||
|
|
||||||
get_ann(Key, Node, Default) ->
|
get_ann(Key, Node, Default) ->
|
||||||
proplists:get_value(Key, get_ann(Node), Default).
|
proplists:get_value(Key, get_ann(Node), Default).
|
||||||
|
|
||||||
|
qualify({con, Ann, N}, X) -> qualify({qcon, Ann, [N]}, X);
|
||||||
|
qualify({qcon, _, NS}, {con, Ann, C}) -> {qcon, Ann, NS ++ [C]};
|
||||||
|
qualify({qcon, _, NS}, {id, Ann, X}) -> {qid, Ann, NS ++ [X]}.
|
||||||
|
|
||||||
|
|||||||
+134
-81
@@ -6,89 +6,142 @@
|
|||||||
%%%-------------------------------------------------------------------
|
%%%-------------------------------------------------------------------
|
||||||
-module(aeso_syntax_utils).
|
-module(aeso_syntax_utils).
|
||||||
|
|
||||||
-export([used_ids/1, used_types/1]).
|
-export([used_ids/1, used_types/1, used/1]).
|
||||||
|
|
||||||
%% Var set combinators
|
-record(alg, {zero, plus, scoped}).
|
||||||
none() -> [].
|
|
||||||
one(X) -> [X].
|
|
||||||
union_map(F, Xs) -> lists:umerge(lists:map(F, Xs)).
|
|
||||||
minus(Xs, Ys) -> Xs -- Ys.
|
|
||||||
|
|
||||||
%% Compute names used by a definition or expression.
|
-type alg(A) :: #alg{ zero :: A
|
||||||
used_ids(Es) when is_list(Es) ->
|
, plus :: fun((A, A) -> A)
|
||||||
union_map(fun used_ids/1, Es);
|
, scoped :: fun((A, A) -> A) }.
|
||||||
used_ids({bind, A, B}) ->
|
|
||||||
minus(used_ids(B), used_ids(A));
|
|
||||||
%% Declarations
|
|
||||||
used_ids({contract, _, _, Decls}) -> used_ids(Decls);
|
|
||||||
used_ids({type_decl, _, _, _}) -> none();
|
|
||||||
used_ids({type_def, _, _, _, _}) -> none();
|
|
||||||
used_ids({fun_decl, _, _, _}) -> none();
|
|
||||||
used_ids({letval, _, _, _, E}) -> used_ids(E);
|
|
||||||
used_ids({letfun, _, _, Args, _, E}) -> used_ids({bind, Args, E});
|
|
||||||
used_ids({letrec, _, Decls}) -> used_ids(Decls);
|
|
||||||
%% Args
|
|
||||||
used_ids({arg, _, X, _}) -> used_ids(X);
|
|
||||||
used_ids({named_arg, _, _, E}) -> used_ids(E);
|
|
||||||
%% Constants
|
|
||||||
used_ids({int, _, _}) -> none();
|
|
||||||
used_ids({bool, _, _}) -> none();
|
|
||||||
used_ids({hash, _, _}) -> none();
|
|
||||||
used_ids({unit, _}) -> none();
|
|
||||||
used_ids({string, _, _}) -> none();
|
|
||||||
used_ids({char, _, _}) -> none();
|
|
||||||
%% Expressions
|
|
||||||
used_ids({lam, _, Args, E}) -> used_ids({bind, Args, E});
|
|
||||||
used_ids({'if', _, A, B, C}) -> used_ids([A, B, C]);
|
|
||||||
used_ids({switch, _, E, Bs}) -> used_ids([E, Bs]);
|
|
||||||
used_ids({app, _, E, Es}) -> used_ids([E | Es]);
|
|
||||||
used_ids({proj, _, E, _}) -> used_ids(E);
|
|
||||||
used_ids({tuple, _, Es}) -> used_ids(Es);
|
|
||||||
used_ids({list, _, Es}) -> used_ids(Es);
|
|
||||||
used_ids({typed, _, E, _}) -> used_ids(E);
|
|
||||||
used_ids({record, _, Fs}) -> used_ids(Fs);
|
|
||||||
used_ids({record, _, E, Fs}) -> used_ids([E, Fs]);
|
|
||||||
used_ids({map, _, E, Fs}) -> used_ids([E, Fs]);
|
|
||||||
used_ids({map, _, KVs}) -> used_ids([ [K, V] || {K, V} <- KVs ]);
|
|
||||||
used_ids({map_get, _, M, K}) -> used_ids([M, K]);
|
|
||||||
used_ids({map_get, _, M, K, V}) -> used_ids([M, K, V]);
|
|
||||||
used_ids({block, _, Ss}) -> used_ids_s(Ss);
|
|
||||||
used_ids({Op, _}) when is_atom(Op) -> none();
|
|
||||||
used_ids({id, _, X}) -> [X];
|
|
||||||
used_ids({qid, _, _}) -> none();
|
|
||||||
used_ids({con, _, _}) -> none();
|
|
||||||
used_ids({qcon, _, _}) -> none();
|
|
||||||
%% Switch branches
|
|
||||||
used_ids({'case', _, P, E}) -> used_ids({bind, P, E});
|
|
||||||
%% Fields
|
|
||||||
used_ids({field, _, LV, E}) -> used_ids([LV, E]);
|
|
||||||
used_ids({field, _, LV, X, E}) -> used_ids([LV, {bind, X, E}]);
|
|
||||||
used_ids({proj, _, _}) -> none();
|
|
||||||
used_ids({map_get, _, E}) -> used_ids(E).
|
|
||||||
|
|
||||||
%% Statements
|
-type kind() :: decl | type | bind_type | expr | bind_expr.
|
||||||
used_ids_s([]) -> none();
|
|
||||||
used_ids_s([S | Ss]) ->
|
|
||||||
used_ids([S, {bind, bound_ids(S), {block, [], Ss}}]).
|
|
||||||
|
|
||||||
bound_ids({letval, _, X, _, _}) -> one(X);
|
-spec fold(alg(A), fun((kind(), _) -> A), kind(), E | [E]) -> A
|
||||||
bound_ids({letfun, _, X, _, _, _}) -> one(X);
|
when E :: aeso_syntax:decl()
|
||||||
bound_ids({letrec, _, Decls}) -> union_map(fun bound_ids/1, Decls);
|
| aeso_syntax:typedef()
|
||||||
bound_ids(_) -> none().
|
| aeso_syntax:field_t()
|
||||||
|
| aeso_syntax:constructor_t()
|
||||||
|
| aeso_syntax:type()
|
||||||
|
| aeso_syntax:expr()
|
||||||
|
| aeso_syntax:pat()
|
||||||
|
| aeso_syntax:arg()
|
||||||
|
| aeso_syntax:alt()
|
||||||
|
| aeso_syntax:elim()
|
||||||
|
| aeso_syntax:arg_expr()
|
||||||
|
| aeso_syntax:field(aeso_syntax:expr())
|
||||||
|
| aeso_syntax:stmt().
|
||||||
|
fold(Alg = #alg{zero = Zero, plus = Plus, scoped = Scoped}, Fun, K, X) ->
|
||||||
|
Sum = fun(Xs) -> lists:foldl(Plus, Zero, Xs) end,
|
||||||
|
Same = fun(A) -> fold(Alg, Fun, K, A) end,
|
||||||
|
Decl = fun(D) -> fold(Alg, Fun, decl, D) end,
|
||||||
|
Type = fun(T) -> fold(Alg, Fun, type, T) end,
|
||||||
|
Expr = fun(E) -> fold(Alg, Fun, expr, E) end,
|
||||||
|
BindExpr = fun(P) -> fold(Alg, Fun, bind_expr, P) end,
|
||||||
|
BindType = fun(T) -> fold(Alg, Fun, bind_type, T) end,
|
||||||
|
Top = Fun(K, X),
|
||||||
|
Bound = fun LB ({letval, _, Y, _, _}) -> BindExpr(Y);
|
||||||
|
LB ({letfun, _, F, _, _, _}) -> BindExpr(F);
|
||||||
|
LB ({letrec, _, Ds}) -> Sum(lists:map(LB, Ds));
|
||||||
|
LB (_) -> Zero
|
||||||
|
end,
|
||||||
|
Rec = case X of
|
||||||
|
%% lists (bound things in head scope over tail)
|
||||||
|
[A | As] -> Scoped(Same(A), Same(As));
|
||||||
|
%% decl()
|
||||||
|
{contract, _, _, Ds} -> Decl(Ds);
|
||||||
|
{namespace, _, _, Ds} -> Decl(Ds);
|
||||||
|
{type_decl, _, I, _} -> BindType(I);
|
||||||
|
{type_def, _, I, _, D} -> Plus(BindType(I), Decl(D));
|
||||||
|
{fun_decl, _, _, T} -> Type(T);
|
||||||
|
{letval, _, F, T, E} -> Sum([BindExpr(F), Type(T), Expr(E)]);
|
||||||
|
{letfun, _, F, Xs, T, E} -> Sum([BindExpr(F), Type(T), Scoped(BindExpr(Xs), Expr(E))]);
|
||||||
|
{letrec, _, Ds} -> Plus(Bound(Ds), Decl(Ds));
|
||||||
|
%% typedef()
|
||||||
|
{alias_t, T} -> Type(T);
|
||||||
|
{record_t, Fs} -> Type(Fs);
|
||||||
|
{variant_t, Cs} -> Type(Cs);
|
||||||
|
%% field_t() and constructor_t()
|
||||||
|
{field_t, _, _, T} -> Type(T);
|
||||||
|
{constr_t, _, _, Ts} -> Type(Ts);
|
||||||
|
%% type()
|
||||||
|
{fun_t, _, Named, Args, Ret} -> Type([Named, Args, Ret]);
|
||||||
|
{app_t, _, T, Ts} -> Type([T | Ts]);
|
||||||
|
{tuple_t, _, Ts} -> Type(Ts);
|
||||||
|
%% named_arg_t()
|
||||||
|
{named_arg_t, _, _, T, E} -> Plus(Type(T), Expr(E));
|
||||||
|
%% expr()
|
||||||
|
{lam, _, Args, E} -> Scoped(BindExpr(Args), Expr(E));
|
||||||
|
{'if', _, A, B, C} -> Expr([A, B, C]);
|
||||||
|
{switch, _, E, Alts} -> Expr([E, Alts]);
|
||||||
|
{app, _, A, As} -> Expr([A | As]);
|
||||||
|
{proj, _, E, _} -> Expr(E);
|
||||||
|
{tuple, _, As} -> Expr(As);
|
||||||
|
{list, _, As} -> Expr(As);
|
||||||
|
{typed, _, E, T} -> Plus(Expr(E), Type(T));
|
||||||
|
{record, _, Fs} -> Expr(Fs);
|
||||||
|
{record, _, E, Fs} -> Expr([E | Fs]);
|
||||||
|
{map, _, E, Fs} -> Expr([E | Fs]);
|
||||||
|
{map, _, KVs} -> Sum([Expr([Key, Val]) || {Key, Val} <- KVs]);
|
||||||
|
{map_get, _, A, B} -> Expr([A, B]);
|
||||||
|
{map_get, _, A, B, C} -> Expr([A, B, C]);
|
||||||
|
{block, _, Ss} -> Expr(Ss);
|
||||||
|
%% field()
|
||||||
|
{field, _, LV, E} -> Expr([LV, E]);
|
||||||
|
{field, _, LV, _, E} -> Expr([LV, E]);
|
||||||
|
%% arg()
|
||||||
|
{arg, _, X, T} -> Plus(Expr(X), Type(T));
|
||||||
|
%% alt()
|
||||||
|
{'case', _, P, E} -> Scoped(BindExpr(P), Expr(E));
|
||||||
|
%% elim()
|
||||||
|
{proj, _, _} -> Zero;
|
||||||
|
{map_get, _, E} -> Expr(E);
|
||||||
|
%% arg_expr()
|
||||||
|
{named_arg, _, _, E} -> Expr(E);
|
||||||
|
_ -> Alg#alg.zero
|
||||||
|
end,
|
||||||
|
(Alg#alg.plus)(Top, Rec).
|
||||||
|
|
||||||
|
%% Name dependencies
|
||||||
|
|
||||||
|
used_ids(E) ->
|
||||||
|
[ X || {term, [X]} <- used(E) ].
|
||||||
|
|
||||||
|
used_types(T) ->
|
||||||
|
[ X || {type, [X]} <- used(T) ].
|
||||||
|
|
||||||
|
-type entity() :: {term, [string()]}
|
||||||
|
| {type, [string()]}
|
||||||
|
| {namespace, [string()]}.
|
||||||
|
|
||||||
|
-spec entity_alg() -> alg([entity()]).
|
||||||
|
entity_alg() ->
|
||||||
|
IsBound = fun({K, _}) -> lists:member(K, [bound_term, bound_type]) end,
|
||||||
|
Unbind = fun(bound_term) -> term; (bound_type) -> type end,
|
||||||
|
Scoped = fun(Xs, Ys) ->
|
||||||
|
{Bound, Others} = lists:partition(IsBound, Ys),
|
||||||
|
Bound1 = [ {Unbind(Tag), X} || {Tag, X} <- Bound ],
|
||||||
|
lists:umerge(Xs -- Bound1, Others)
|
||||||
|
end,
|
||||||
|
#alg{ zero = []
|
||||||
|
, plus = fun lists:umerge/2
|
||||||
|
, scoped = Scoped }.
|
||||||
|
|
||||||
|
-spec used(_) -> [entity()].
|
||||||
|
used(D) ->
|
||||||
|
Kind = fun(expr) -> term;
|
||||||
|
(bind_expr) -> bound_term;
|
||||||
|
(type) -> type;
|
||||||
|
(bind_type) -> bound_type
|
||||||
|
end,
|
||||||
|
NS = fun(Xs) -> {namespace, lists:droplast(Xs)} end,
|
||||||
|
NotBound = fun({Tag, _}) -> not lists:member(Tag, [bound_term, bound_type]) end,
|
||||||
|
Xs =
|
||||||
|
fold(entity_alg(),
|
||||||
|
fun(K, {id, _, X}) -> [{Kind(K), [X]}];
|
||||||
|
(K, {qid, _, Xs}) -> [{Kind(K), Xs}, NS(Xs)];
|
||||||
|
(K, {con, _, X}) -> [{Kind(K), [X]}];
|
||||||
|
(K, {qcon, _, Xs}) -> [{Kind(K), Xs}, NS(Xs)];
|
||||||
|
(_, _) -> []
|
||||||
|
end, decl, D),
|
||||||
|
lists:filter(NotBound, Xs).
|
||||||
|
|
||||||
used_types(Ts) when is_list(Ts) -> union_map(fun used_types/1, Ts);
|
|
||||||
used_types({type_def, _, _, _, T}) -> used_types(T);
|
|
||||||
used_types({alias_t, T}) -> used_types(T);
|
|
||||||
used_types({record_t, Fs}) -> used_types(Fs);
|
|
||||||
used_types({variant_t, Cs}) -> used_types(Cs);
|
|
||||||
used_types({field_t, _, _, T}) -> used_types(T);
|
|
||||||
used_types({constr_t, _, _, Ts}) -> used_types(Ts);
|
|
||||||
used_types({fun_t, _, Named, Args, T}) -> used_types([T | Named ++ Args]);
|
|
||||||
used_types({named_arg_t, _, _, T, _}) -> used_types(T);
|
|
||||||
used_types({app_t, _, T, Ts}) -> used_types([T | Ts]);
|
|
||||||
used_types({tuple_t, _, Ts}) -> used_types(Ts);
|
|
||||||
used_types({id, _, X}) -> one(X);
|
|
||||||
used_types({qid, _, _}) -> none();
|
|
||||||
used_types({con, _, _}) -> none();
|
|
||||||
used_types({qcon, _, _}) -> none();
|
|
||||||
used_types({tvar, _, _}) -> none().
|
|
||||||
|
|||||||
@@ -1,10 +1,11 @@
|
|||||||
{application, aesophia,
|
{application, aesophia,
|
||||||
[{description, "Contract Language for Aethernity"},
|
[{description, "Contract Language for Aethernity"},
|
||||||
{vsn, "1.2.0"},
|
{vsn, {cmd, "cat VERSION | tr -d '[:space:]'"}},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications,
|
{applications,
|
||||||
[kernel,
|
[kernel,
|
||||||
stdlib,
|
stdlib,
|
||||||
|
jsx,
|
||||||
syntax_tools,
|
syntax_tools,
|
||||||
getopt,
|
getopt,
|
||||||
aebytecode
|
aebytecode
|
||||||
|
|||||||
+12
-5
@@ -4,6 +4,7 @@
|
|||||||
|
|
||||||
-define(OPT_SPEC,
|
-define(OPT_SPEC,
|
||||||
[ {src_file, undefined, undefined, string, "Sophia source code file"}
|
[ {src_file, undefined, undefined, string, "Sophia source code file"}
|
||||||
|
, {version, $V, "version", undefined, "Print compiler version"}
|
||||||
, {verbose, $v, "verbose", undefined, "Verbose output"}
|
, {verbose, $v, "verbose", undefined, "Verbose output"}
|
||||||
, {help, $h, "help", undefined, "Show this message"}
|
, {help, $h, "help", undefined, "Show this message"}
|
||||||
, {outfile, $o, "out", string, "Output file (experimental)"} ]).
|
, {outfile, $o, "out", string, "Output file (experimental)"} ]).
|
||||||
@@ -14,11 +15,13 @@ usage() ->
|
|||||||
main(Args) ->
|
main(Args) ->
|
||||||
case getopt:parse(?OPT_SPEC, Args) of
|
case getopt:parse(?OPT_SPEC, Args) of
|
||||||
{ok, {Opts, []}} ->
|
{ok, {Opts, []}} ->
|
||||||
case proplists:get_value(help, Opts, false) of
|
case Opts of
|
||||||
false ->
|
[version] ->
|
||||||
compile(Opts);
|
print_vsn();
|
||||||
true ->
|
[help] ->
|
||||||
usage()
|
usage();
|
||||||
|
_ ->
|
||||||
|
compile(Opts)
|
||||||
end;
|
end;
|
||||||
|
|
||||||
{ok, {_, NonOpts}} ->
|
{ok, {_, NonOpts}} ->
|
||||||
@@ -69,3 +72,7 @@ write_outfile(Out, ResMap) ->
|
|||||||
%% Lazy approach
|
%% Lazy approach
|
||||||
file:write_file(Out, term_to_binary(ResMap)),
|
file:write_file(Out, term_to_binary(ResMap)),
|
||||||
io:format("Output written to: ~s\n", [Out]).
|
io:format("Output written to: ~s\n", [Out]).
|
||||||
|
|
||||||
|
print_vsn() ->
|
||||||
|
{ok, Vsn} = aeso_compiler:version(),
|
||||||
|
io:format("Compiler version: ~s\n", [Vsn]).
|
||||||
|
|||||||
+105
-17
@@ -54,27 +54,115 @@ encode_decode_test() ->
|
|||||||
ok.
|
ok.
|
||||||
|
|
||||||
encode_decode_sophia_test() ->
|
encode_decode_sophia_test() ->
|
||||||
{42} = encode_decode_sophia_string("int", "42"),
|
Check = fun(Type, Str) -> case {encode_decode_sophia_string(Type, Str), Str} of
|
||||||
{1} = encode_decode_sophia_string("bool", "true"),
|
{X, X} -> ok;
|
||||||
{0} = encode_decode_sophia_string("bool", "false"),
|
Other -> Other
|
||||||
{<<"Hello">>} = encode_decode_sophia_string("string", "\"Hello\""),
|
end end,
|
||||||
{<<"Hello">>, [1,2,3], {variant, 1, [1]}} =
|
ok = Check("int", "42"),
|
||||||
encode_decode_sophia_string(
|
ok = Check("bool", "true"),
|
||||||
"(string, list(int), option(bool))",
|
ok = Check("bool", "false"),
|
||||||
"\"Hello\", [1,2,3], Some(true)"),
|
ok = Check("string", "\"Hello\""),
|
||||||
|
ok = Check("(string, list(int), option(bool))",
|
||||||
|
"(\"Hello\", [1, 2, 3], Some(true))"),
|
||||||
|
ok = Check("variant", "Blue({[\"x\"] = 1})"),
|
||||||
|
ok = Check("r", "{x = (\"foo\", 0), y = Red}"),
|
||||||
ok.
|
ok.
|
||||||
|
|
||||||
encode_decode_sophia_string(SophiaType, String) ->
|
encode_decode_sophia_string(SophiaType, String) ->
|
||||||
io:format("String ~p~n", [String]),
|
io:format("String ~p~n", [String]),
|
||||||
Code = [ "contract Call =\n"
|
Code = [ "contract MakeCall =\n"
|
||||||
, " function foo : ", SophiaType, " => _\n"
|
, " type arg_type = ", SophiaType, "\n"
|
||||||
, " function __call() = foo(", String, ")\n" ],
|
, " type an_alias('a) = (string, 'a)\n"
|
||||||
{ok, _, {Types, _}, Args} = aeso_compiler:check_call(lists:flatten(Code), []),
|
, " record r = {x : an_alias(int), y : variant}\n"
|
||||||
Arg = list_to_tuple(Args),
|
, " datatype variant = Red | Blue(map(string, int))\n"
|
||||||
Type = {tuple, Types},
|
, " function foo : arg_type => arg_type\n" ],
|
||||||
io:format("Type ~p~n", [Type]),
|
case aeso_compiler:check_call(lists:flatten(Code), "foo", [String], []) of
|
||||||
Data = encode(Arg),
|
{ok, _, {[Type], _}, [Arg]} ->
|
||||||
decode(Type, Data).
|
io:format("Type ~p~n", [Type]),
|
||||||
|
Data = encode(Arg),
|
||||||
|
case aeso_compiler:to_sophia_value(Code, "foo", ok, Data) of
|
||||||
|
{ok, Sophia} ->
|
||||||
|
lists:flatten(io_lib:format("~s", [prettypr:format(aeso_pretty:expr(Sophia))]));
|
||||||
|
{error, Err} ->
|
||||||
|
io:format("~s\n", [Err]),
|
||||||
|
{error, Err}
|
||||||
|
end;
|
||||||
|
{error, Err} ->
|
||||||
|
io:format("~s\n", [Err]),
|
||||||
|
{error, Err}
|
||||||
|
end.
|
||||||
|
|
||||||
|
calldata_test() ->
|
||||||
|
[42, <<"foobar">>] = encode_decode_calldata("foo", ["int", "string"], ["42", "\"foobar\""]),
|
||||||
|
Map = #{ <<"a">> => 4 },
|
||||||
|
[{variant, 1, [Map]}, {{<<"b">>, 5}, {variant, 0, []}}] =
|
||||||
|
encode_decode_calldata("foo", ["variant", "r"], ["Blue({[\"a\"] = 4})", "{x = (\"b\", 5), y = Red}"]),
|
||||||
|
[16#123, 16#456] = encode_decode_calldata("foo", ["hash", "address"], ["#123", "#456"]),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
calldata_init_test() ->
|
||||||
|
encode_decode_calldata("init", ["int"], ["42"], {tuple, [typerep, word]}),
|
||||||
|
|
||||||
|
Code = parameterized_contract("foo", ["int"]),
|
||||||
|
encode_decode_calldata_(Code, "init", [], {tuple, [typerep, {tuple, []}]}).
|
||||||
|
|
||||||
|
calldata_indent_test() ->
|
||||||
|
Test = fun(Extra) ->
|
||||||
|
encode_decode_calldata_(
|
||||||
|
parameterized_contract(Extra, "foo", ["int"]),
|
||||||
|
"foo", ["42"], word)
|
||||||
|
end,
|
||||||
|
Test(" stateful function bla() = ()"),
|
||||||
|
Test(" type x = int"),
|
||||||
|
Test(" private function bla : int => int"),
|
||||||
|
Test(" public stateful function bla(x : int) =\n"
|
||||||
|
" x + 1"),
|
||||||
|
Test(" stateful private function bla(x : int) : int =\n"
|
||||||
|
" x + 1"),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
parameterized_contract(FunName, Types) ->
|
||||||
|
parameterized_contract([], FunName, Types).
|
||||||
|
|
||||||
|
parameterized_contract(ExtraCode, FunName, Types) ->
|
||||||
|
lists:flatten(
|
||||||
|
["contract Dummy =\n",
|
||||||
|
ExtraCode, "\n",
|
||||||
|
" type an_alias('a) = (string, 'a)\n"
|
||||||
|
" record r = {x : an_alias(int), y : variant}\n"
|
||||||
|
" datatype variant = Red | Blue(map(string, int))\n"
|
||||||
|
" function ", FunName, " : (", string:join(Types, ", "), ") => int\n" ]).
|
||||||
|
|
||||||
|
oracle_test() ->
|
||||||
|
Contract =
|
||||||
|
"contract OracleTest =\n"
|
||||||
|
" function question(o, q : oracle_query(list(string), option(int))) =\n"
|
||||||
|
" Oracle.get_question(o, q)\n",
|
||||||
|
{ok, _, {[word, word], {list, string}}, [16#123, 16#456]} =
|
||||||
|
aeso_compiler:check_call(Contract, "question", ["#123", "#456"], []),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
permissive_literals_fail_test() ->
|
||||||
|
Contract =
|
||||||
|
"contract OracleTest =\n"
|
||||||
|
" function haxx(o : oracle(list(string), option(int))) =\n"
|
||||||
|
" Chain.spend(o, 1000000)\n",
|
||||||
|
{error, <<"Type errors\nCannot unify", _/binary>>} =
|
||||||
|
aeso_compiler:check_call(Contract, "haxx", ["#123"], []),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
encode_decode_calldata(FunName, Types, Args) ->
|
||||||
|
encode_decode_calldata(FunName, Types, Args, word).
|
||||||
|
|
||||||
|
encode_decode_calldata(FunName, Types, Args, RetType) ->
|
||||||
|
Code = parameterized_contract(FunName, Types),
|
||||||
|
encode_decode_calldata_(Code, FunName, Args, RetType).
|
||||||
|
|
||||||
|
encode_decode_calldata_(Code, FunName, Args, RetVMType) ->
|
||||||
|
{ok, Calldata, CalldataType, RetVMType1} = aeso_compiler:create_calldata(Code, FunName, Args),
|
||||||
|
?assertEqual(RetVMType1, RetVMType),
|
||||||
|
{ok, {_Hash, ArgTuple}} = aeso_heap:from_binary(CalldataType, Calldata),
|
||||||
|
tuple_to_list(ArgTuple).
|
||||||
|
|
||||||
encode_decode(T, D) ->
|
encode_decode(T, D) ->
|
||||||
?assertEqual(D, decode(T, encode(D))),
|
?assertEqual(D, decode(T, encode(D))),
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
-module(aeso_aci_tests).
|
||||||
|
|
||||||
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
|
|
||||||
|
do_test() ->
|
||||||
|
test_contract(1),
|
||||||
|
test_contract(2).
|
||||||
|
|
||||||
|
test_contract(N) ->
|
||||||
|
{Contract,DecACI} = test_cases(N),
|
||||||
|
{ok,Enc} = aeso_aci:encode(Contract),
|
||||||
|
?assertEqual(DecACI, jsx:decode(Enc)).
|
||||||
|
|
||||||
|
test_cases(1) ->
|
||||||
|
Contract = <<"contract C =\n"
|
||||||
|
" function a(i : int) = i+1\n">>,
|
||||||
|
DecodedACI = [{<<"contract">>,
|
||||||
|
[{<<"name">>,<<"C">>},
|
||||||
|
{<<"type_defs">>,[]},
|
||||||
|
{<<"functions">>,
|
||||||
|
[[{<<"name">>,<<"a">>},
|
||||||
|
{<<"arguments">>,
|
||||||
|
[[{<<"name">>,<<"i">>},{<<"type">>,<<"int">>}]]},
|
||||||
|
{<<"type">>,<<"int">>},
|
||||||
|
{<<"stateful">>,false}]]}]}],
|
||||||
|
{Contract,DecodedACI};
|
||||||
|
|
||||||
|
test_cases(2) ->
|
||||||
|
Contract = <<"contract C =\n"
|
||||||
|
" type allan = int\n"
|
||||||
|
" function a(i : allan) = i+1\n">>,
|
||||||
|
DecodedACI = [{<<"contract">>,
|
||||||
|
[{<<"name">>,<<"C">>},
|
||||||
|
{<<"type_defs">>,
|
||||||
|
[[{<<"name">>,<<"allan">>},
|
||||||
|
{<<"vars">>,[]},
|
||||||
|
{<<"typedef">>,<<"int">>}]]},
|
||||||
|
{<<"functions">>,
|
||||||
|
[[{<<"name">>,<<"a">>},
|
||||||
|
{<<"arguments">>,
|
||||||
|
[[{<<"name">>,<<"i">>},{<<"type">>,<<"int">>}]]},
|
||||||
|
{<<"type">>,<<"int">>},
|
||||||
|
{<<"stateful">>,false}]]}]}],
|
||||||
|
{Contract,DecodedACI}.
|
||||||
@@ -10,15 +10,10 @@
|
|||||||
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
-include_lib("eunit/include/eunit.hrl").
|
||||||
|
|
||||||
%% simple_compile_test_() -> ok.
|
|
||||||
%% Very simply test compile the given contracts. Only basic checks
|
%% Very simply test compile the given contracts. Only basic checks
|
||||||
%% are made on the output, just that it is a binary which indicates
|
%% are made on the output, just that it is a binary which indicates
|
||||||
%% that the compilation worked.
|
%% that the compilation worked.
|
||||||
|
|
||||||
simple_compile_test_() ->
|
simple_compile_test_() ->
|
||||||
{setup,
|
|
||||||
fun () -> ok end, %Setup
|
|
||||||
fun (_) -> ok end, %Cleanup
|
|
||||||
[ {"Testing the " ++ ContractName ++ " contract",
|
[ {"Testing the " ++ ContractName ++ " contract",
|
||||||
fun() ->
|
fun() ->
|
||||||
#{byte_code := ByteCode,
|
#{byte_code := ByteCode,
|
||||||
@@ -28,11 +23,34 @@ simple_compile_test_() ->
|
|||||||
end} || ContractName <- compilable_contracts() ] ++
|
end} || ContractName <- compilable_contracts() ] ++
|
||||||
[ {"Testing error messages of " ++ ContractName,
|
[ {"Testing error messages of " ++ ContractName,
|
||||||
fun() ->
|
fun() ->
|
||||||
<<"Type errors\n",ErrorString/binary>> = compile(ContractName),
|
case compile(ContractName) of
|
||||||
check_errors(lists:sort(ExpectedErrors), ErrorString)
|
<<"Type errors\n", ErrorString/binary>> ->
|
||||||
|
check_errors(lists:sort(ExpectedErrors), ErrorString);
|
||||||
|
<<"Parse errors\n", ErrorString/binary>> ->
|
||||||
|
check_errors(lists:sort(ExpectedErrors), ErrorString)
|
||||||
|
end
|
||||||
end} ||
|
end} ||
|
||||||
{ContractName, ExpectedErrors} <- failing_contracts() ]
|
{ContractName, ExpectedErrors} <- failing_contracts() ] ++
|
||||||
}.
|
[ {"Testing include with explicit files",
|
||||||
|
fun() ->
|
||||||
|
FileSystem = maps:from_list(
|
||||||
|
[ begin
|
||||||
|
{ok, Bin} = file:read_file(filename:join([aeso_test_utils:contract_path(), File])),
|
||||||
|
{File, Bin}
|
||||||
|
end || File <- ["included.aes", "../contracts/included2.aes"] ]),
|
||||||
|
#{byte_code := Code1} = compile("include", [{include, {explicit_files, FileSystem}}]),
|
||||||
|
#{byte_code := Code2} = compile("include"),
|
||||||
|
?assertMatch(true, Code1 == Code2)
|
||||||
|
end} ] ++
|
||||||
|
[ {"Testing deadcode elimination",
|
||||||
|
fun() ->
|
||||||
|
#{ byte_code := NoDeadCode } = compile("nodeadcode"),
|
||||||
|
#{ byte_code := DeadCode } = compile("deadcode"),
|
||||||
|
SizeNoDeadCode = byte_size(NoDeadCode),
|
||||||
|
SizeDeadCode = byte_size(DeadCode),
|
||||||
|
?assertMatch({_, _, true}, {SizeDeadCode, SizeNoDeadCode, SizeDeadCode + 40 < SizeNoDeadCode}),
|
||||||
|
ok
|
||||||
|
end} ].
|
||||||
|
|
||||||
check_errors(Expect, ErrorString) ->
|
check_errors(Expect, ErrorString) ->
|
||||||
%% This removes the final single \n as well.
|
%% This removes the final single \n as well.
|
||||||
@@ -44,10 +62,13 @@ check_errors(Expect, ErrorString) ->
|
|||||||
end.
|
end.
|
||||||
|
|
||||||
compile(Name) ->
|
compile(Name) ->
|
||||||
|
compile(Name, [{include, {file_system, [aeso_test_utils:contract_path()]}}]).
|
||||||
|
|
||||||
|
compile(Name, Options) ->
|
||||||
String = aeso_test_utils:read_contract(Name),
|
String = aeso_test_utils:read_contract(Name),
|
||||||
case aeso_compiler:from_string(String, []) of
|
case aeso_compiler:from_string(String, [{src_file, Name} | Options]) of
|
||||||
{ok,Map} -> Map;
|
{ok, Map} -> Map;
|
||||||
{error,ErrorString} -> ErrorString
|
{error, ErrorString} -> ErrorString
|
||||||
end.
|
end.
|
||||||
|
|
||||||
%% compilable_contracts() -> [ContractName].
|
%% compilable_contracts() -> [ContractName].
|
||||||
@@ -70,7 +91,13 @@ compilable_contracts() ->
|
|||||||
"stack",
|
"stack",
|
||||||
"test",
|
"test",
|
||||||
"builtin_bug",
|
"builtin_bug",
|
||||||
"builtin_map_get_bug"
|
"builtin_map_get_bug",
|
||||||
|
"nodeadcode",
|
||||||
|
"deadcode",
|
||||||
|
"variant_types",
|
||||||
|
"state_handling",
|
||||||
|
"events",
|
||||||
|
"include"
|
||||||
].
|
].
|
||||||
|
|
||||||
%% Contracts that should produce type errors
|
%% Contracts that should produce type errors
|
||||||
@@ -150,11 +177,11 @@ failing_contracts() ->
|
|||||||
<<"Ambiguous record type with field y (at line 13, column 25) could be one of\n"
|
<<"Ambiguous record type with field y (at line 13, column 25) could be one of\n"
|
||||||
" - r (at line 4, column 10)\n"
|
" - r (at line 4, column 10)\n"
|
||||||
" - r' (at line 5, column 10)">>,
|
" - r' (at line 5, column 10)">>,
|
||||||
<<"Record type r2 does not have field y (at line 15, column 22)">>,
|
|
||||||
<<"The field z is missing when constructing an element of type r2 (at line 15, column 24)">>,
|
|
||||||
<<"Repeated name x in pattern\n"
|
<<"Repeated name x in pattern\n"
|
||||||
" x :: x (at line 26, column 7)">>,
|
" x :: x (at line 26, column 7)">>,
|
||||||
<<"No record type with fields y, z (at line 14, column 22)">>]}
|
<<"No record type with fields y, z (at line 14, column 22)">>,
|
||||||
|
<<"The field z is missing when constructing an element of type r2 (at line 15, column 24)">>,
|
||||||
|
<<"Record type r2 does not have field y (at line 15, column 22)">>]}
|
||||||
, {"init_type_error",
|
, {"init_type_error",
|
||||||
[<<"Cannot unify string\n"
|
[<<"Cannot unify string\n"
|
||||||
" and map(int, int)\n"
|
" and map(int, int)\n"
|
||||||
@@ -167,4 +194,27 @@ failing_contracts() ->
|
|||||||
[<<"The field x is missing when constructing an element of type r('a) (at line 7, column 40)">>,
|
[<<"The field x is missing when constructing an element of type r('a) (at line 7, column 40)">>,
|
||||||
<<"The field y is missing when constructing an element of type r(int) (at line 8, column 40)">>,
|
<<"The field y is missing when constructing an element of type r(int) (at line 8, column 40)">>,
|
||||||
<<"The fields y, z are missing when constructing an element of type r('1) (at line 6, column 40)">>]}
|
<<"The fields y, z are missing when constructing an element of type r('1) (at line 6, column 40)">>]}
|
||||||
|
, {"namespace_clash",
|
||||||
|
[<<"The contract Call (at line 4, column 10) has the same name as a namespace at (builtin location)">>]}
|
||||||
|
, {"bad_events",
|
||||||
|
[<<"The payload type int (at line 10, column 30) should be string">>,
|
||||||
|
<<"The payload type alias_address (at line 12, column 30) equals address but it should be string">>,
|
||||||
|
<<"The indexed type string (at line 9, column 25) is not a word type">>,
|
||||||
|
<<"The indexed type alias_string (at line 11, column 25) equals string which is not a word type">>]}
|
||||||
|
, {"bad_events2",
|
||||||
|
[<<"The event constructor BadEvent1 (at line 9, column 7) has too many non-indexed values (max 1)">>,
|
||||||
|
<<"The event constructor BadEvent2 (at line 10, column 7) has too many indexed values (max 3)">>,
|
||||||
|
<<"The event constructor BadEvent3 (at line 11, column 7) has too many non-indexed values (max 1)">>,
|
||||||
|
<<"The payload type address (at line 11, column 17) should be string">>,
|
||||||
|
<<"The payload type int (at line 11, column 26) should be string">>]}
|
||||||
|
, {"type_clash",
|
||||||
|
[<<"Cannot unify int\n"
|
||||||
|
" and string\n"
|
||||||
|
"when checking the record projection at line 12, column 40\n"
|
||||||
|
" r.foo : (gas : int, value : int) => Remote.themap\n"
|
||||||
|
"against the expected type\n"
|
||||||
|
" (gas : int, value : int) => map(string, int)">>]}
|
||||||
|
, {"bad_include_and_ns",
|
||||||
|
[<<"Include of 'included.aes' at line 2, column 11\nnot allowed, include only allowed at top level.">>,
|
||||||
|
<<"Nested namespace not allowed\nNamespace 'Foo' at line 3, column 13 not defined at top level.">>]}
|
||||||
].
|
].
|
||||||
|
|||||||
@@ -12,9 +12,11 @@ groups() ->
|
|||||||
, aeso_parser_tests
|
, aeso_parser_tests
|
||||||
, aeso_compiler_tests
|
, aeso_compiler_tests
|
||||||
, aeso_abi_tests
|
, aeso_abi_tests
|
||||||
|
, aeso_aci_tests
|
||||||
]}].
|
]}].
|
||||||
|
|
||||||
aeso_scan_tests(_Config) -> ok = eunit:test(aeso_scan_tests).
|
aeso_scan_tests(_Config) -> ok = eunit:test(aeso_scan_tests).
|
||||||
aeso_parser_tests(_Config) -> ok = eunit:test(aeso_parser_tests).
|
aeso_parser_tests(_Config) -> ok = eunit:test(aeso_parser_tests).
|
||||||
aeso_compiler_tests(_Config) -> ok = eunit:test(aeso_compiler_tests).
|
aeso_compiler_tests(_Config) -> ok = eunit:test(aeso_compiler_tests).
|
||||||
aeso_abi_tests(_Config) -> ok = eunit:test(aeso_abi_tests).
|
aeso_abi_tests(_Config) -> ok = eunit:test(aeso_abi_tests).
|
||||||
|
aeso_aci_tests(_Config) -> ok = eunit:test(aeso_aci_tests).
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
-module(contract_tests).
|
|
||||||
|
|
||||||
-include_lib("eunit/include/eunit.hrl").
|
|
||||||
|
|
||||||
make_cmd() -> "make -C " ++ aeso_test_utils:contract_path().
|
|
||||||
|
|
||||||
contracts_test_() ->
|
|
||||||
{setup,
|
|
||||||
fun() -> os:cmd(make_cmd()) end,
|
|
||||||
fun(_) -> os:cmd(make_cmd() ++ " clean") end,
|
|
||||||
[ {"Testing the " ++ Contract ++ " contract",
|
|
||||||
fun() ->
|
|
||||||
?assertCmdOutput(Expected, filename:join(aeso_test_utils:contract_path(), Contract ++ "_test"))
|
|
||||||
end} || {Contract, Expected} <- contracts() ]}.
|
|
||||||
|
|
||||||
contracts() ->
|
|
||||||
[].
|
|
||||||
%% [{"voting",
|
|
||||||
%% "Delegate before vote\n"
|
|
||||||
%% "Cake: 1\n"
|
|
||||||
%% "Beer: 2\n"
|
|
||||||
%% "Winner: Beer\n"
|
|
||||||
%% "Delegate after vote\n"
|
|
||||||
%% "Cake: 1\n"
|
|
||||||
%% "Beer: 2\n"
|
|
||||||
%% "Winner: Beer\n"
|
|
||||||
%% }].
|
|
||||||
|
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
contract Events =
|
||||||
|
type alias_int = int
|
||||||
|
type alias_address = address
|
||||||
|
type alias_string = string
|
||||||
|
|
||||||
|
datatype event =
|
||||||
|
Event1(indexed alias_int, indexed int, string)
|
||||||
|
| Event2(alias_string, indexed alias_address)
|
||||||
|
| BadEvent1(indexed string, string)
|
||||||
|
| BadEvent2(indexed int, int)
|
||||||
|
| BadEvent3(indexed alias_string, string)
|
||||||
|
| BadEvent4(indexed int, alias_address)
|
||||||
|
|
||||||
|
function f1(x : int, y : string) =
|
||||||
|
Chain.event(Event1(x, x+1, y))
|
||||||
|
|
||||||
|
function f2(s : string) =
|
||||||
|
Chain.event(Event2(s, Call.caller))
|
||||||
|
|
||||||
|
function f3(x : int) =
|
||||||
|
Chain.event(Event1(x, x + 2, Int.to_str(x + 7)))
|
||||||
|
|
||||||
|
function i2s(i : int) = Int.to_str(i)
|
||||||
|
function a2s(a : address) = Address.to_str(a)
|
||||||
|
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
contract Events =
|
||||||
|
type alias_int = int
|
||||||
|
type alias_address = address
|
||||||
|
type alias_string = string
|
||||||
|
|
||||||
|
datatype event =
|
||||||
|
Event1(indexed alias_int, indexed int, string)
|
||||||
|
| Event2(alias_string, indexed alias_address)
|
||||||
|
| BadEvent1(string, string)
|
||||||
|
| BadEvent2(indexed int, indexed int, indexed int, indexed address)
|
||||||
|
| BadEvent3(address, int)
|
||||||
|
|
||||||
|
function f1(x : int, y : string) =
|
||||||
|
Chain.event(Event1(x, x+1, y))
|
||||||
|
|
||||||
|
function f2(s : string) =
|
||||||
|
Chain.event(Event2(s, Call.caller))
|
||||||
|
|
||||||
|
function f3(x : int) =
|
||||||
|
Chain.event(Event1(x, x + 2, Int.to_str(x + 7)))
|
||||||
|
|
||||||
|
function i2s(i : int) = Int.to_str(i)
|
||||||
|
function a2s(a : address) = Address.to_str(a)
|
||||||
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
contract Bad =
|
||||||
|
include "included.aes"
|
||||||
|
namespace Foo =
|
||||||
|
function foo() = 42
|
||||||
|
|
||||||
|
function foo() = 43
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
// Test more advanced chain interactions
|
// Test more advanced chain interactions
|
||||||
|
|
||||||
contract Chain =
|
contract ChainTest =
|
||||||
|
|
||||||
record state = { last_bf : address }
|
record state = { last_bf : address }
|
||||||
|
|
||||||
@@ -10,4 +10,4 @@ contract Chain =
|
|||||||
function miner() = Chain.coinbase
|
function miner() = Chain.coinbase
|
||||||
|
|
||||||
function save_coinbase() =
|
function save_coinbase() =
|
||||||
put(state{last_bf = Chain.coinbase})
|
put(state{last_bf = Chain.coinbase})
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
namespace List =
|
||||||
|
|
||||||
|
function map1(f : 'a => 'b, xs : list('a)) =
|
||||||
|
switch(xs)
|
||||||
|
[] => []
|
||||||
|
x :: xs => f(x) :: map1(f, xs)
|
||||||
|
|
||||||
|
function map2(f : 'a => 'b, xs : list('a)) =
|
||||||
|
switch(xs)
|
||||||
|
[] => []
|
||||||
|
x :: xs => f(x) :: map2(f, xs)
|
||||||
|
|
||||||
|
contract Deadcode =
|
||||||
|
|
||||||
|
function inc1(xs : list(int)) : list(int) =
|
||||||
|
List.map1((x) => x + 1, xs)
|
||||||
|
|
||||||
|
function inc2(xs : list(int)) : list(int) =
|
||||||
|
List.map1((x) => x + 1, xs)
|
||||||
|
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
include "included.aes"
|
||||||
|
include "../contracts/included2.aes"
|
||||||
|
|
||||||
|
contract Include =
|
||||||
|
function foo() =
|
||||||
|
Included.foo() < Included2a.bar()
|
||||||
|
|
||||||
|
function bar() =
|
||||||
|
Included2b.foo() > Included.foo()
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
namespace Included =
|
||||||
|
function foo() = 42
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
namespace Included2a =
|
||||||
|
function bar() = 43
|
||||||
|
|
||||||
|
namespace Included2b =
|
||||||
|
function foo() = 44
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
// You can't shadow existing contracts or namespaces.
|
||||||
|
|
||||||
|
contract Call =
|
||||||
|
function whatever() = ()
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
namespace Lib =
|
||||||
|
|
||||||
|
private function rev(xs, ys) =
|
||||||
|
switch(xs)
|
||||||
|
[] => ys
|
||||||
|
x :: xs => rev(xs, x :: ys)
|
||||||
|
|
||||||
|
function reverse(xs : list('a)) : list('a) = rev(xs, [])
|
||||||
|
|
||||||
|
function eqlist(xs : list(int), ys : list(int)) =
|
||||||
|
switch((xs, ys))
|
||||||
|
([], []) => true
|
||||||
|
(x :: xs, y :: ys) => x == y && eqlist(xs, ys)
|
||||||
|
_ => false
|
||||||
|
|
||||||
|
contract TestNamespaces =
|
||||||
|
|
||||||
|
function palindrome(xs : list(int)) : bool =
|
||||||
|
Lib.eqlist(xs, Lib.reverse(xs))
|
||||||
|
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
|
||||||
|
namespace List =
|
||||||
|
|
||||||
|
function map1(f : 'a => 'b, xs : list('a)) =
|
||||||
|
switch(xs)
|
||||||
|
[] => []
|
||||||
|
x :: xs => f(x) :: map1(f, xs)
|
||||||
|
|
||||||
|
function map2(f : 'a => 'b, xs : list('a)) =
|
||||||
|
switch(xs)
|
||||||
|
[] => []
|
||||||
|
x :: xs => f(x) :: map2(f, xs)
|
||||||
|
|
||||||
|
contract Deadcode =
|
||||||
|
|
||||||
|
function inc1(xs : list(int)) : list(int) =
|
||||||
|
List.map1((x) => x + 1, xs)
|
||||||
|
|
||||||
|
function inc2(xs : list(int)) : list(int) =
|
||||||
|
List.map2((x) => x + 1, xs)
|
||||||
|
|
||||||
@@ -18,7 +18,6 @@
|
|||||||
|
|
||||||
contract SimpleStorage =
|
contract SimpleStorage =
|
||||||
|
|
||||||
type event = int
|
|
||||||
record state = { data : int }
|
record state = { data : int }
|
||||||
|
|
||||||
function init(value : int) : state = { data = value }
|
function init(value : int) : state = { data = value }
|
||||||
|
|||||||
@@ -1,19 +1,22 @@
|
|||||||
contract Remote =
|
contract Remote =
|
||||||
function look_at : (state) => ()
|
record rstate = { i : int, s : string, m : map(int, int) }
|
||||||
|
|
||||||
|
function look_at : (rstate) => ()
|
||||||
function return_s : (bool) => string
|
function return_s : (bool) => string
|
||||||
function return_m : (bool) => map(int, int)
|
function return_m : (bool) => map(int, int)
|
||||||
function get : (state) => state
|
function get : (rstate) => rstate
|
||||||
function get_i : (state) => int
|
function get_i : (rstate) => int
|
||||||
function get_s : (state) => string
|
function get_s : (rstate) => string
|
||||||
function get_m : (state) => map(int, int)
|
function get_m : (rstate) => map(int, int)
|
||||||
|
|
||||||
function fun_update_i : (state, int) => state
|
function fun_update_i : (rstate, int) => rstate
|
||||||
function fun_update_s : (state, string) => state
|
function fun_update_s : (rstate, string) => rstate
|
||||||
function fun_update_m : (state, map(int, int)) => state
|
function fun_update_m : (rstate, map(int, int)) => rstate
|
||||||
function fun_update_mk : (state, int, int) => state
|
function fun_update_mk : (rstate, int, int) => rstate
|
||||||
|
|
||||||
contract StateHandling =
|
contract StateHandling =
|
||||||
record state = { i : int, s : string, m : map(int, int) }
|
|
||||||
|
type state = Remote.rstate
|
||||||
|
|
||||||
function init(r : Remote, i : int) =
|
function init(r : Remote, i : int) =
|
||||||
let state0 = { i = 0, s = "undefined", m = {} }
|
let state0 = { i = 0, s = "undefined", m = {} }
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
|
||||||
|
contract Remote =
|
||||||
|
|
||||||
|
type themap = map(int, string)
|
||||||
|
function foo : () => themap
|
||||||
|
|
||||||
|
contract Main =
|
||||||
|
|
||||||
|
type themap = map(string, int)
|
||||||
|
|
||||||
|
// Should fail
|
||||||
|
function foo(r : Remote) : themap = r.foo()
|
||||||
|
|
||||||
Reference in New Issue
Block a user