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

@ -11,7 +11,7 @@
-export([encode/1,encode/2,decode/1]).
-export([encode_type/1,encode_types/1,
encode_stmt/1,encode_expr/1,encode_exprs/1]).
encode_stmt/1,encode_expr/1,encode_exprs/1]).
%% Define records for the various typed syntactic forms. These make
%% the code easier but don't seem to exist elsewhere.
@ -49,8 +49,9 @@
-record(tuple, {ann,args}).
-record(list, {ann,args}).
-record(record, {ann,fields}).
-record(field, {ann,name,value}). %A record field
-record(proj, {ann,value}). %?
-record(field, {ann,name,value}). %A record field
-record(proj, {ann,value}). %?
-record(map, {ann,fields}).
-record(app, {ann,func,args}).
-record(typed, {ann,expr,type}).
@ -66,10 +67,10 @@ encode(ContractString, Options) when is_binary(ContractString) ->
encode(ContractString, Options) ->
try
Ast = parse(ContractString, Options),
io:format("Ast\n~p\n", [Ast]),
%% io:format("Ast\n~p\n", [Ast]),
%% aeso_ast:pp(Ast),
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),
%% We find and look at the last contract.
Contract = lists:last(TypedAst),
@ -141,7 +142,7 @@ encode_type(#tuple_t{args=As}) ->
encode_type(#bytes_t{len=Len}) ->
{<<"bytes">>, Len};
encode_type(#record_t{fields=Fs}) ->
Efs = encode_field_types(Fs),
Efs = encode_rec_field_types(Fs),
[{<<"record">>,Efs}];
encode_type(#app_t{id=Id,fields=Fs}) ->
Name = encode_type(Id),
@ -162,14 +163,14 @@ encode_type(#fun_t{args=As,type=T}) ->
encode_name(Name) ->
list_to_binary(Name).
%% encode_field_types(Fields) -> [JSON].
%% encode_field_type(Field) -> JSON.
%% encode_rec_field_types(Fields) -> [JSON].
%% encode_rec_field_type(Field) -> JSON.
%% Encode a record field type.
encode_field_types(Fs) ->
[ encode_field_type(F) || F <- Fs ].
encode_rec_field_types(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)},
{<<"type">>,[encode_type(T)]}].
@ -210,7 +211,7 @@ encode_expr(#qid{names=Ns}) ->
encode_name(lists:join(".", Ns));
encode_expr(#qcon{names=Ns}) ->
encode_name(lists:join(".", Ns)); %?
encode_expr(#typed{expr=E}) ->
encode_expr(#typed{expr=E}) -> %Ignore the type
encode_expr(E);
encode_expr(#bool{bool=B}) -> B;
encode_expr(#int{value=V}) -> V;
@ -223,29 +224,43 @@ encode_expr(#list{args=As}) ->
Eas = encode_exprs(As),
[{<<"list">>,Eas}];
encode_expr(#record{fields=Fs}) ->
Efs = encode_field_exprs(Fs),
Efs = encode_rec_field_exprs(Fs),
[{<<"record">>,Efs}];
encode_expr(#map{fields=Fs}) ->
Efs = encode_map_field_exprs(Fs),
[{<<"map">>,Efs}];
encode_expr(#proj{value=V}) ->
encode_expr(V);
encode_expr(#app{func=F,args=As}) ->
Ef = encode_expr(F),
Eas = encode_exprs(As),
[{<<"apply">>,[{<<"function">>,Ef},
{<<"arguments">>,Eas}]}];
{<<"arguments">>,Eas}]}];
encode_expr({Op,_Ann}) ->
list_to_binary(atom_to_list(Op)).
%% encode_field_exprs(Fields) -> [JSON].
%% encode_field_expr(Field) -> JSON.
%% encode_rec_field_exprs(Fields) -> [JSON].
%% encode_rec_field_expr(Field) -> JSON.
%% Encode a record field expression.
encode_field_exprs(Fs) ->
[ encode_field_expr(F) || F <- Fs ].
encode_rec_field_exprs(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)},
{<<"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 a JSON string and generate a suitable contract string which
%% can be included in a contract definition. We decode into a map
@ -295,12 +310,12 @@ decode_type(#{<<"map">> := Ets}) ->
decode_type(#{<<"variant">> := Ets}) ->
Ts = decode_types(Ets),
lists:join(" | ", Ts);
decode_type(Econs) when is_map(Econs) -> %General constructor
decode_type(Econs) when is_map(Econs) -> %General constructor
[{Ec,Ets}] = maps:to_list(Econs),
C = decode_name(Ec),
Ts = decode_types(Ets),
[C,$(,lists:join(",", Ts),$)];
decode_type(T) -> %Just raw names.
decode_type(T) -> %Just raw names.
decode_name(T).
decode_name(En) ->
@ -319,8 +334,8 @@ decode_field(#{<<"name">> := En,<<"type">> := [Et]}) ->
%% aliases. We find them as they always have variants.
decode_tdefs(Ts) -> [ decode_tdef(T) ||
#{<<"typedef">> := #{<<"variant">> := _}} = T <- Ts
].
#{<<"typedef">> := #{<<"variant">> := _}} = T <- Ts
].
decode_tdef(#{<<"name">> := Name,<<"vars">> := Vs,<<"typedef">> := T}) ->
[" datatype"," ",decode_name(Name),decode_tvars(Vs),