compile list literals

This commit is contained in:
Ulf Norell 2019-04-08 14:29:18 +02:00
parent 42c7fde413
commit e597a3780a
2 changed files with 17 additions and 9 deletions

View File

@ -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)};

View File

@ -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