Saving more stuff, definitely WIP
This commit is contained in:
parent
5719730d8c
commit
a38afe7693
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
-export([encode/1,encode/2,decode/1]).
|
-export([encode/1,encode/2,decode/1]).
|
||||||
-export([encode_type/1,encode_types/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
|
%% 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.
|
||||||
@ -49,8 +49,9 @@
|
|||||||
-record(tuple, {ann,args}).
|
-record(tuple, {ann,args}).
|
||||||
-record(list, {ann,args}).
|
-record(list, {ann,args}).
|
||||||
-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,29 +224,43 @@ 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}) ->
|
||||||
Ef = encode_expr(F),
|
Ef = encode_expr(F),
|
||||||
Eas = encode_exprs(As),
|
Eas = encode_exprs(As),
|
||||||
[{<<"apply">>,[{<<"function">>,Ef},
|
[{<<"apply">>,[{<<"function">>,Ef},
|
||||||
{<<"arguments">>,Eas}]}];
|
{<<"arguments">>,Eas}]}];
|
||||||
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
|
||||||
@ -295,12 +310,12 @@ decode_type(#{<<"map">> := Ets}) ->
|
|||||||
decode_type(#{<<"variant">> := Ets}) ->
|
decode_type(#{<<"variant">> := Ets}) ->
|
||||||
Ts = decode_types(Ets),
|
Ts = decode_types(Ets),
|
||||||
lists:join(" | ", Ts);
|
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),
|
[{Ec,Ets}] = maps:to_list(Econs),
|
||||||
C = decode_name(Ec),
|
C = decode_name(Ec),
|
||||||
Ts = decode_types(Ets),
|
Ts = decode_types(Ets),
|
||||||
[C,$(,lists:join(",", Ts),$)];
|
[C,$(,lists:join(",", Ts),$)];
|
||||||
decode_type(T) -> %Just raw names.
|
decode_type(T) -> %Just raw names.
|
||||||
decode_name(T).
|
decode_name(T).
|
||||||
|
|
||||||
decode_name(En) ->
|
decode_name(En) ->
|
||||||
@ -319,8 +334,8 @@ decode_field(#{<<"name">> := En,<<"type">> := [Et]}) ->
|
|||||||
%% aliases. We find them as they always have variants.
|
%% aliases. We find them as they always have variants.
|
||||||
|
|
||||||
decode_tdefs(Ts) -> [ decode_tdef(T) ||
|
decode_tdefs(Ts) -> [ decode_tdef(T) ||
|
||||||
#{<<"typedef">> := #{<<"variant">> := _}} = T <- Ts
|
#{<<"typedef">> := #{<<"variant">> := _}} = T <- Ts
|
||||||
].
|
].
|
||||||
|
|
||||||
decode_tdef(#{<<"name">> := Name,<<"vars">> := Vs,<<"typedef">> := T}) ->
|
decode_tdef(#{<<"name">> := Name,<<"vars">> := Vs,<<"typedef">> := T}) ->
|
||||||
[" datatype"," ",decode_name(Name),decode_tvars(Vs),
|
[" datatype"," ",decode_name(Name),decode_tvars(Vs),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user