Improve the interface to the compiler
It is now more consistent though we can still discuss how we want the interface to look.
This commit is contained in:
parent
267fef3a5b
commit
3137bc4d4a
@ -21,8 +21,8 @@
|
|||||||
-include("aeso_icode.hrl").
|
-include("aeso_icode.hrl").
|
||||||
|
|
||||||
|
|
||||||
-type option() :: pp_sophia_code | pp_ast | pp_icode | pp_assembler |
|
-type option() :: pp_sophia_code | pp_ast | pp_types | pp_typed_ast |
|
||||||
pp_bytecode.
|
pp_icode| pp_assembler | pp_bytecode.
|
||||||
-type options() :: [option()].
|
-type options() :: [option()].
|
||||||
|
|
||||||
-export_type([ option/0
|
-export_type([ option/0
|
||||||
@ -43,12 +43,17 @@ file(Filename) ->
|
|||||||
file(Filename, []).
|
file(Filename, []).
|
||||||
|
|
||||||
-spec file(string(), options()) -> map().
|
-spec file(string(), options()) -> map().
|
||||||
file(Filename, Options) ->
|
file(File, Options) ->
|
||||||
C = read_contract(Filename),
|
case read_contract(File) of
|
||||||
from_string(C, Options).
|
{ok, Bin} -> from_string(Bin, Options);
|
||||||
|
{error, Error} -> {error, {File, Error}}
|
||||||
|
end.
|
||||||
|
|
||||||
-spec from_string(string(), options()) -> map().
|
-spec from_string(binary() | string(), options()) -> map().
|
||||||
|
from_string(ContractBin, Options) when is_binary(ContractBin) ->
|
||||||
|
from_string(binary_to_list(ContractBin), Options);
|
||||||
from_string(ContractString, Options) ->
|
from_string(ContractString, Options) ->
|
||||||
|
try
|
||||||
Ast = parse(ContractString, Options),
|
Ast = parse(ContractString, Options),
|
||||||
ok = pp_sophia_code(Ast, Options),
|
ok = pp_sophia_code(Ast, Options),
|
||||||
ok = pp_ast(Ast, Options),
|
ok = pp_ast(Ast, Options),
|
||||||
@ -63,9 +68,26 @@ from_string(ContractString, 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),
|
ok = pp_bytecode(ByteCode, Options),
|
||||||
#{byte_code => ByteCode, type_info => TypeInfo,
|
{ok,#{byte_code => ByteCode,
|
||||||
|
compiler_version => version(),
|
||||||
contract_source => ContractString,
|
contract_source => ContractString,
|
||||||
compiler_version => version()}.
|
type_info => TypeInfo
|
||||||
|
}}
|
||||||
|
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")).
|
||||||
|
|
||||||
-define(CALL_NAME, "__call").
|
-define(CALL_NAME, "__call").
|
||||||
|
|
||||||
@ -76,6 +98,7 @@ from_string(ContractString, Options) ->
|
|||||||
-spec check_call(string(), options()) -> {ok, string(), {[Type], Type | any}, [term()]} | {error, term()}
|
-spec check_call(string(), options()) -> {ok, string(), {[Type], Type | any}, [term()]} | {error, term()}
|
||||||
when Type :: term().
|
when Type :: term().
|
||||||
check_call(ContractString, Options) ->
|
check_call(ContractString, Options) ->
|
||||||
|
try
|
||||||
Ast = parse(ContractString, Options),
|
Ast = parse(ContractString, Options),
|
||||||
ok = pp_sophia_code(Ast, Options),
|
ok = pp_sophia_code(Ast, Options),
|
||||||
ok = pp_ast(Ast, Options),
|
ok = pp_ast(Ast, Options),
|
||||||
@ -91,11 +114,20 @@ check_call(ContractString, Options) ->
|
|||||||
ok = pp_icode(Icode, Options),
|
ok = pp_icode(Icode, Options),
|
||||||
#{ functions := Funs } = Icode,
|
#{ functions := Funs } = Icode,
|
||||||
ArgIcode = get_arg_icode(Funs),
|
ArgIcode = get_arg_icode(Funs),
|
||||||
try [ icode_to_term(T, Arg) || {T, Arg} <- lists:zip(ArgVMTypes, ArgIcode) ] of
|
ArgTerms = [ icode_to_term(T, Arg) ||
|
||||||
ArgTerms ->
|
{T, Arg} <- lists:zip(ArgVMTypes, ArgIcode) ],
|
||||||
{ok, FunName, {ArgVMTypes, RetVMType}, ArgTerms}
|
{ok, FunName, {ArgVMTypes, RetVMType}, ArgTerms}
|
||||||
catch throw:Err ->
|
catch
|
||||||
{error, Err}
|
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_call_function}} ->
|
||||||
|
{error,join_errors("Type errors", ["missing __call function"],
|
||||||
|
fun (E) -> E end)};
|
||||||
|
throw:Error -> %Don't ask
|
||||||
|
{error,join_errors("Code errors", [Error],
|
||||||
|
fun (E) -> io_lib:format("~p", [E]) end)}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
-spec create_calldata(map(), string(), string()) ->
|
-spec create_calldata(map(), string(), string()) ->
|
||||||
@ -252,6 +284,5 @@ parse_error({Line,Pos}, ErrorString) ->
|
|||||||
error({parse_errors,[Error]}).
|
error({parse_errors,[Error]}).
|
||||||
|
|
||||||
read_contract(Name) ->
|
read_contract(Name) ->
|
||||||
{ok, Bin} = file:read_file(Name),
|
file:read_file(Name).
|
||||||
binary_to_list(Bin).
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user