Add loop operator in fcode #889

Open
zxq9 wants to merge 27 commits from loop-op into master
2 changed files with 14 additions and 2 deletions
Showing only changes of commit 0f5daafc29 - Show all commits

View File

@ -653,6 +653,18 @@ expr_to_fcode(Env, _Type, {list, _, Es}) ->
lists:foldr(fun(E, L) -> {op, '::', [expr_to_fcode(Env, E), L]} end, lists:foldr(fun(E, L) -> {op, '::', [expr_to_fcode(Env, E), L]} end,
nil, Es); nil, Es);
expr_to_fcode(Env, _Type, {app, _, {'..', _}, [A, B]}) ->
AV = fresh_name(),
BV = fresh_name(),
WithA = fun(X) -> {'let', AV, expr_to_fcode(Env, A), X} end,
WithB = fun(X) -> {'let', BV, expr_to_fcode(Env, B), X} end,
St = fresh_name(),
Init = {var, AV},
Loop = {loop, Init, St, make_if({op, '<', [{var, St}, {var, BV}]},
{continue, {op, '+', [{var, St}, {lit, {int, 1}}]}},
{break, nil}
)},
WithA(WithB(Loop));
expr_to_fcode(Env, _Type, {app, _, {'..', _}, [A, B]}) -> expr_to_fcode(Env, _Type, {app, _, {'..', _}, [A, B]}) ->
AV = fresh_name(), AV = fresh_name(),
BV = fresh_name(), BV = fresh_name(),

View File

@ -378,10 +378,10 @@ to_scode1(Env, {loop, Init, It, Expr}) ->
InitS = to_scode(Env, Init) ++ [{jump, ContRef}], InitS = to_scode(Env, Init) ++ [{jump, ContRef}],
{ItV, Env1} = bind_local(It, Env), {ItV, Env1} = bind_local(It, Env),
ExprS = to_scode(bind_loop(ContRef, BreakRef, ItV, Env1), Expr) ++ [{jumpif, ?a, ContRef}, {jump, BreakRef}], ExprS = to_scode(bind_loop(ContRef, BreakRef, ItV, Env1), Expr) ++ [{jumpif, ?a, ContRef}, {jump, BreakRef}],
[{loop, InitS, It, ExprS, ContRef, BreakRef}]; [{loop, InitS, ItV, ExprS, ContRef, BreakRef}];
to_scode1(Env = #env{cont_ref = ContRef, loop_it = It}, {continue, Expr}) -> to_scode1(Env = #env{cont_ref = ContRef, loop_it = It}, {continue, Expr}) ->
ExprS = to_scode1(Env, Expr), ExprS = to_scode1(Env, Expr),
ExprS ++ [{'POP', It}, push(?i(1))]; ExprS ++ [{'STORE', It, ?a}, push(?i(1))];
to_scode1(Env = #env{break_ref = BreakRef}, {break, Expr}) -> to_scode1(Env = #env{break_ref = BreakRef}, {break, Expr}) ->
ExprS = to_scode1(Env, Expr), ExprS = to_scode1(Env, Expr),
ExprS ++ [push(?i(0))]; ExprS ++ [push(?i(0))];