From e597a3780ab1d93167548cd61e2029f9def21056 Mon Sep 17 00:00:00 2001 From: Ulf Norell Date: Mon, 8 Apr 2019 14:29:18 +0200 Subject: [PATCH] compile list literals --- src/aeso_ast_to_fcode.erl | 10 +++++++++- src/aeso_fcode_to_fate.erl | 16 ++++++++-------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/aeso_ast_to_fcode.erl b/src/aeso_ast_to_fcode.erl index d2190ed..04431ab 100644 --- a/src/aeso_ast_to_fcode.erl +++ b/src/aeso_ast_to_fcode.erl @@ -24,10 +24,11 @@ -type var_name() :: string(). -type sophia_name() :: [string()]. --type binop() :: '+' | '-' | '=='. +-type binop() :: '+' | '-' | '==' | '::'. -type fexpr() :: {integer, integer()} | {bool, false | true} + | nil | {var, var_name()} | {tuple, [fexpr()]} | {binop, ftype(), binop(), fexpr(), fexpr()} @@ -196,6 +197,12 @@ expr_to_fcode(_Env, _Type, {id, _, X}) -> {var, X}; expr_to_fcode(Env, _Type, {tuple, _, Es}) -> {tuple, [expr_to_fcode(Env, E) || E <- Es]}; +%% Lists +expr_to_fcode(Env, Type, {list, _, Es}) -> + FType = type_to_fcode(Env, Type), + lists:foldr(fun(E, L) -> {binop, FType, '::', expr_to_fcode(Env, E), L} end, + nil, Es); + %% Conditionals expr_to_fcode(Env, _Type, {'if', _, Cond, Then, Else}) -> {'if', expr_to_fcode(Env, Cond), @@ -315,6 +322,7 @@ rename(Ren, Expr) -> case Expr of {integer, _} -> Expr; {bool, _} -> Expr; + nil -> nil; {var, X} -> {var, rename_var(Ren, X)}; {tuple, Es} -> {tuple, [rename(Ren, E) || E <- Es]}; {binop, T, Op, E1, E2} -> {binop, T, Op, rename(Ren, E1), rename(Ren, E2)}; diff --git a/src/aeso_fcode_to_fate.erl b/src/aeso_fcode_to_fate.erl index b6208d5..a61900b 100644 --- a/src/aeso_fcode_to_fate.erl +++ b/src/aeso_fcode_to_fate.erl @@ -41,7 +41,8 @@ Op =:= 'NEQ' orelse Op =:= 'AND' orelse Op =:= 'OR' orelse - Op =:= 'ELEMENT')). + Op =:= 'ELEMENT' orelse + Op =:= 'CONS')). -record(env, { vars = [], locals = [], tailpos = true }). @@ -124,6 +125,8 @@ to_scode(_Env, {integer, N}) -> to_scode(_Env, {bool, B}) -> [aeb_fate_code:push(?i(B))]; +to_scode(_Env, nil) -> aeb_fate_code:nil(?a); + to_scode(Env, {var, X}) -> [aeb_fate_code:push(lookup_var(Env, X))]; @@ -210,15 +213,12 @@ match_tuple(Env, _, []) -> %% -- Operators -- -binop_to_scode('+') -> add_a_a_a(); %% Optimization introduces other variants -binop_to_scode('-') -> sub_a_a_a(); -binop_to_scode('==') -> eq_a_a_a(). +binop_to_scode('+') -> aeb_fate_code:add(?a, ?a, ?a); +binop_to_scode('-') -> aeb_fate_code:sub(?a, ?a, ?a); +binop_to_scode('==') -> aeb_fate_code:eq(?a, ?a, ?a); +binop_to_scode('::') -> aeb_fate_code:cons(?a, ?a, ?a). % binop_to_scode(Op) -> ?TODO(Op). -add_a_a_a() -> aeb_fate_code:add(?a, ?a, ?a). -sub_a_a_a() -> aeb_fate_code:sub(?a, ?a, ?a). -eq_a_a_a() -> aeb_fate_code:eq(?a, ?a, ?a). - %% -- Phase II --------------------------------------------------------------- %% Optimize