Saving more stuff, definitely WIP

This commit is contained in:
Robert Virding 2019-04-18 01:16:59 +02:00
parent 5719730d8c
commit a38afe7693

View File

@ -51,6 +51,7 @@
-record(record, {ann,fields}). -record(record, {ann,fields}).
-record(field, {ann,name,value}). %A record field -record(field, {ann,name,value}). %A record field
-record(proj, {ann,value}). %? -record(proj, {ann,value}). %?
-record(map, {ann,fields}).
-record(app, {ann,func,args}). -record(app, {ann,func,args}).
-record(typed, {ann,expr,type}). -record(typed, {ann,expr,type}).
@ -66,10 +67,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("Ast\n~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("Typed ast\n~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),
@ -141,7 +142,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_field_types(Fs), Efs = encode_rec_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),
@ -162,14 +163,14 @@ encode_type(#fun_t{args=As,type=T}) ->
encode_name(Name) -> encode_name(Name) ->
list_to_binary(Name). list_to_binary(Name).
%% encode_field_types(Fields) -> [JSON]. %% encode_rec_field_types(Fields) -> [JSON].
%% encode_field_type(Field) -> JSON. %% encode_rec_field_type(Field) -> JSON.
%% Encode a record field type. %% Encode a record field type.
encode_field_types(Fs) -> encode_rec_field_types(Fs) ->
[ encode_field_type(F) || F <- Fs ]. [ encode_rec_field_type(F) || F <- Fs ].
encode_field_type(#field_t{id=Id,type=T}) -> encode_rec_field_type(#field_t{id=Id,type=T}) ->
[{<<"name">>,encode_type(Id)}, [{<<"name">>,encode_type(Id)},
{<<"type">>,[encode_type(T)]}]. {<<"type">>,[encode_type(T)]}].
@ -210,7 +211,7 @@ encode_expr(#qid{names=Ns}) ->
encode_name(lists:join(".", Ns)); encode_name(lists:join(".", Ns));
encode_expr(#qcon{names=Ns}) -> encode_expr(#qcon{names=Ns}) ->
encode_name(lists:join(".", Ns)); %? encode_name(lists:join(".", Ns)); %?
encode_expr(#typed{expr=E}) -> encode_expr(#typed{expr=E}) -> %Ignore the type
encode_expr(E); encode_expr(E);
encode_expr(#bool{bool=B}) -> B; encode_expr(#bool{bool=B}) -> B;
encode_expr(#int{value=V}) -> V; encode_expr(#int{value=V}) -> V;
@ -223,8 +224,11 @@ encode_expr(#list{args=As}) ->
Eas = encode_exprs(As), Eas = encode_exprs(As),
[{<<"list">>,Eas}]; [{<<"list">>,Eas}];
encode_expr(#record{fields=Fs}) -> encode_expr(#record{fields=Fs}) ->
Efs = encode_field_exprs(Fs), Efs = encode_rec_field_exprs(Fs),
[{<<"record">>,Efs}]; [{<<"record">>,Efs}];
encode_expr(#map{fields=Fs}) ->
Efs = encode_map_field_exprs(Fs),
[{<<"map">>,Efs}];
encode_expr(#proj{value=V}) -> encode_expr(#proj{value=V}) ->
encode_expr(V); encode_expr(V);
encode_expr(#app{func=F,args=As}) -> encode_expr(#app{func=F,args=As}) ->
@ -235,17 +239,28 @@ 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_rec_field_exprs(Fields) -> [JSON].
%% encode_field_expr(Field) -> JSON. %% encode_rec_field_expr(Field) -> JSON.
%% Encode a record field expression. %% Encode a record field expression.
encode_field_exprs(Fs) -> encode_rec_field_exprs(Fs) ->
[ encode_field_expr(F) || F <- Fs ]. [ encode_rec_field_expr(F) || F <- Fs ].
encode_field_expr(#field{name=[N],value=V}) -> encode_rec_field_expr(#field{name=[N],value=V}) ->
[{<<"name">>,encode_expr(N)}, [{<<"name">>,encode_expr(N)},
{<<"value">>,[encode_expr(V)]}]. {<<"value">>,[encode_expr(V)]}].
%% encode_map_field_exprs(Fields) -> [JSON].
%% encode_map_field_expr(Field) -> JSON.
%% Encode a map field expression.
encode_map_field_exprs(Fs) ->
[ encode_map_field_expr(F) || F <- Fs ].
encode_map_field_expr({K,V}) ->
[{<<"key">>,encode_expr(K)},
{<<"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