Saving stuff, definitely WIP

This commit is contained in:
Robert Virding 2019-04-17 18:26:37 +02:00
parent 51b63f9559
commit 5719730d8c

View File

@ -10,7 +10,8 @@
-module(aeso_aci). -module(aeso_aci).
-export([encode/1,encode/2,decode/1]). -export([encode/1,encode/2,decode/1]).
-export([encode_type/1,encode_stmt/1,encode_expr/1]). -export([encode_type/1,encode_types/1,
encode_stmt/1,encode_expr/1,encode_exprs/1]).
%% Define records for the various typed syntactic forms. These make %% Define records for the various typed syntactic forms. These make
%% the code easier but don't seem to exist elsewhere. %% the code easier but don't seem to exist elsewhere.
@ -47,6 +48,9 @@
-record(bytes, {ann,bin}). -record(bytes, {ann,bin}).
-record(tuple, {ann,args}). -record(tuple, {ann,args}).
-record(list, {ann,args}). -record(list, {ann,args}).
-record(record, {ann,fields}).
-record(field, {ann,name,value}). %A record field
-record(proj, {ann,value}). %?
-record(app, {ann,func,args}). -record(app, {ann,func,args}).
-record(typed, {ann,expr,type}). -record(typed, {ann,expr,type}).
@ -62,10 +66,10 @@ encode(ContractString, Options) when is_binary(ContractString) ->
encode(ContractString, Options) -> encode(ContractString, Options) ->
try try
Ast = parse(ContractString, Options), Ast = parse(ContractString, Options),
%% io:format("~p\n", [Ast]), io:format("Ast\n~p\n", [Ast]),
%% aeso_ast:pp(Ast), %% aeso_ast:pp(Ast),
TypedAst = aeso_ast_infer_types:infer(Ast, Options), TypedAst = aeso_ast_infer_types:infer(Ast, Options),
%% io:format("~p\n", [TypedAst]), io:format("Typed ast\n~p\n", [TypedAst]),
%% aeso_ast:pp_typed(TypedAst), %% aeso_ast:pp_typed(TypedAst),
%% We find and look at the last contract. %% We find and look at the last contract.
Contract = lists:last(TypedAst), Contract = lists:last(TypedAst),
@ -137,7 +141,7 @@ encode_type(#tuple_t{args=As}) ->
encode_type(#bytes_t{len=Len}) -> encode_type(#bytes_t{len=Len}) ->
{<<"bytes">>, Len}; {<<"bytes">>, Len};
encode_type(#record_t{fields=Fs}) -> encode_type(#record_t{fields=Fs}) ->
Efs = encode_fields(Fs), Efs = encode_field_types(Fs),
[{<<"record">>,Efs}]; [{<<"record">>,Efs}];
encode_type(#app_t{id=Id,fields=Fs}) -> encode_type(#app_t{id=Id,fields=Fs}) ->
Name = encode_type(Id), Name = encode_type(Id),
@ -158,14 +162,14 @@ encode_type(#fun_t{args=As,type=T}) ->
encode_name(Name) -> encode_name(Name) ->
list_to_binary(Name). list_to_binary(Name).
%% encode_fields(Fields) -> [JSON]. %% encode_field_types(Fields) -> [JSON].
%% encode_field(Field) -> JSON. %% encode_field_type(Field) -> JSON.
%% Encode a record field. %% Encode a record field type.
encode_fields(Fs) -> encode_field_types(Fs) ->
[ encode_field(F) || F <- Fs ]. [ encode_field_type(F) || F <- Fs ].
encode_field(#field_t{id=Id,type=T}) -> encode_field_type(#field_t{id=Id,type=T}) ->
[{<<"name">>,encode_type(Id)}, [{<<"name">>,encode_type(Id)},
{<<"type">>,[encode_type(T)]}]. {<<"type">>,[encode_type(T)]}].
@ -218,6 +222,11 @@ encode_expr(#tuple{args=As}) ->
encode_expr(#list{args=As}) -> encode_expr(#list{args=As}) ->
Eas = encode_exprs(As), Eas = encode_exprs(As),
[{<<"list">>,Eas}]; [{<<"list">>,Eas}];
encode_expr(#record{fields=Fs}) ->
Efs = encode_field_exprs(Fs),
[{<<"record">>,Efs}];
encode_expr(#proj{value=V}) ->
encode_expr(V);
encode_expr(#app{func=F,args=As}) -> encode_expr(#app{func=F,args=As}) ->
Ef = encode_expr(F), Ef = encode_expr(F),
Eas = encode_exprs(As), Eas = encode_exprs(As),
@ -226,6 +235,17 @@ encode_expr(#app{func=F,args=As}) ->
encode_expr({Op,_Ann}) -> encode_expr({Op,_Ann}) ->
list_to_binary(atom_to_list(Op)). list_to_binary(atom_to_list(Op)).
%% encode_field_exprs(Fields) -> [JSON].
%% encode_field_expr(Field) -> JSON.
%% Encode a record field expression.
encode_field_exprs(Fs) ->
[ encode_field_expr(F) || F <- Fs ].
encode_field_expr(#field{name=[N],value=V}) ->
[{<<"name">>,encode_expr(N)},
{<<"value">>,[encode_expr(V)]}].
%% decode(JSON) -> ContractString. %% decode(JSON) -> ContractString.
%% Decode a JSON string and generate a suitable contract string which %% Decode a JSON string and generate a suitable contract string which
%% can be included in a contract definition. We decode into a map %% can be included in a contract definition. We decode into a map