Skip to content

Commit 9a5fa25

Browse files
committed
fix expander: optimize lazily
1 parent 212e84b commit 9a5fa25

File tree

2 files changed

+37
-31
lines changed

2 files changed

+37
-31
lines changed

src/eval.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,7 @@ obj eval_expr(obj e) {
860860
obj tc, k, env, v, ab;
861861
uptr aa, ac;
862862

863-
// check and expand
863+
// check and expand expression
864864
check_expr(e);
865865
e = expand_expr(e);
866866

@@ -892,16 +892,25 @@ obj eval_expr(obj e) {
892892
}
893893

894894
void eval_module(obj mod) {
895-
obj tc, k, env;
895+
obj tc, k, env, ab;
896+
uptr aa, ac;
896897

897-
// stash old continuation and environment
898+
// check and expand module
899+
check_module(mod);
900+
expand_module(mod);
901+
902+
// stash mutable thread context info
898903
tc = Mcurr_tc();
899904
k = Mtc_cc(tc);
900905
env = Mtc_env(tc);
901-
902-
// check module syntax
903-
check_module(mod);
904-
expand_module(mod);
906+
ab = Mtc_ab(tc);
907+
aa = Mtc_aa(tc);
908+
ac = Mtc_ac(tc);
909+
910+
// create new argument buffer
911+
Mtc_aa(tc) = INIT_ARGS_BUFFER_LEN;
912+
Mtc_ab(tc) = GC_malloc(Mtc_aa(tc) * sizeof(obj));
913+
Mtc_ac(tc) = 0;
905914

906915
// set up thread context
907916
Mtc_env(tc) = empty_env();
@@ -913,7 +922,10 @@ void eval_module(obj mod) {
913922
do_module_body(mod);
914923
do_exports(tc, mod);
915924

916-
// restore old continuation and environment
925+
// restore mutable thread context info
917926
Mtc_cc(tc) = k;
918927
Mtc_env(tc) = env;
928+
Mtc_ab(tc) = ab;
929+
Mtc_aa(tc) = aa;
930+
Mtc_ac(tc) = ac;
919931
}

src/expand.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
static obj condense_body(obj es) {
66
if (Mnullp(Mcdr(es))) {
77
// single expression => just use expression
8-
return expand_expr(Mcar(es));
8+
return Mcar(es);
99
} else {
1010
// multiple expressions => wrap in begin
11-
return expand_expr(Mcons(Mbegin_symbol, es));
11+
return Mcons(Mbegin_symbol, es);
1212
}
1313
}
1414

@@ -46,7 +46,7 @@ static obj expand_let_values_expr(obj e) {
4646
tl = Mcdr(tl);
4747
}
4848

49-
return Mlist3(Mlet_values_symbol, hd, condense_body(Mcddr(e)));
49+
return Mlist3(Mlet_values_symbol, hd, expand_expr(condense_body(Mcddr(e))));
5050
}
5151

5252
// ([(<id> ...) <expr>] ...)
@@ -265,29 +265,31 @@ obj expand_expr(obj e) {
265265
if (Msymbolp(Mcadr(e))) {
266266
// named let => convert to letrec
267267
e = expand_let_loop(e);
268-
goto loop;
269268
} else if (Mnullp(Mcadr(e))) {
270269
// empty bindings => just use body
271-
return condense_body(Mcddr(e));
270+
e = condense_body(Mcddr(e));
272271
} else {
273272
// at least one binding
274-
e = expand_let_expr(e);
275-
goto loop;
273+
e = expand_let_expr(e);
276274
}
275+
276+
goto loop;
277277
} else if (hd == Mletrec_symbol) {
278278
// letrec => transforms to let
279279
if (Mnullp(Mcadr(e))) {
280280
// empty bindings => just use body
281-
return condense_body(Mcddr(e));
281+
e = condense_body(Mcddr(e));
282282
} else {
283283
// at least one binding
284284
e = expand_letrec_expr(e);
285-
goto loop;
286285
}
286+
287+
goto loop;
287288
} else if (hd == Mlet_values_symbol) {
288289
if (Mnullp(Mcadr(e))) {
289290
// empty bindings => just use body
290-
return condense_body(Mcddr(e));
291+
e = condense_body(Mcddr(e));
292+
goto loop;
291293
} else {
292294
// at least one binding
293295
return expand_let_values_expr(e);
@@ -327,19 +329,11 @@ obj expand_expr(obj e) {
327329
expand_expr(Mcar(Mcdddr(e)))
328330
);
329331
} else if (hd == Mwhen_symbol) {
330-
return Mlist4(
331-
Mif_symbol,
332-
expand_expr(Mcadr(e)),
333-
expand_expr(Mcons(Mbegin_symbol, Mcddr(e))),
334-
Mlist2(Mquote_symbol, Mvoid)
335-
);
332+
e = Mlist4(Mif_symbol, Mcadr(e), Mcons(Mbegin_symbol, Mcddr(e)), Mlist2(Mquote_symbol, Mvoid));
333+
goto loop;
336334
} else if (hd == Munless_symbol) {
337-
return Mlist4(
338-
Mif_symbol,
339-
expand_expr(Mcadr(e)),
340-
Mlist2(Mquote_symbol, Mvoid),
341-
expand_expr(Mcons(Mbegin_symbol, Mcddr(e)))
342-
);
335+
e = Mlist4(Mif_symbol, Mcadr(e), Mlist2(Mquote_symbol, Mvoid), Mcons(Mbegin_symbol, Mcddr(e)));
336+
goto loop;
343337
} else if (hd == Mcond_symbol) {
344338
if (Mnullp(Mcdr(e))) {
345339
// empty cond => void
@@ -374,7 +368,7 @@ obj expand_expr(obj e) {
374368
goto loop;
375369
}
376370
} else if (hd == Mlambda_symbol) {
377-
return Mlist3(Mlambda_symbol, Mcadr(e), condense_body(Mcddr(e)));
371+
return Mlist3(Mlambda_symbol, Mcadr(e), expand_expr(condense_body(Mcddr(e))));
378372
} else if (hd == Msetb_symbol) {
379373
return Mlist3(Msetb_symbol, Mcadr(e), expand_expr(Mcaddr(e)));
380374
} else if (hd == Mquote_symbol) {

0 commit comments

Comments
 (0)