Skip to content

Commit 5915f9a

Browse files
committed
Mod: ecevalgoto
1 parent 6caff1e commit 5915f9a

File tree

4 files changed

+34
-20
lines changed

4 files changed

+34
-20
lines changed

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"src/storage.c",
3737
"src/strbldr.c",
3838
//
39-
"-O3", // Optimization Level. Tail Call Optimization seem to need >= 2
39+
"-O1", // Optimization Level. Tail Call Optimization seem to need >= 2
4040
"-std=c17", // 'ISO C 2017' standard
4141
"-o",
4242
"${workspaceFolder}/bin/sicp", // output e.g., "${fileDirname}/sicp",

src/eceval.c

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ static void save(obj dat, struct core *cr)
5252
eprintf(AREA, "Halting - Reached Memory Limit");
5353
exit(1);
5454
}
55-
return;
55+
}
56+
57+
static void save_nogc(obj dat, struct core *cr)
58+
{
59+
cr->stack = cons(dat, cr->stack);
5660
}
5761

5862
static obj restore(struct core *cr)
@@ -77,11 +81,10 @@ static void set_proc_name(struct core *cr)
7781
of_string("<unknown>");
7882
}
7983

80-
static obj ecevalcr(obj expression, obj _environment, struct core *cr)
84+
static obj ecevalgoto(struct core *cr)
8185
{
82-
cr->expr = expression;
83-
cr->env = _environment;
84-
cr->cont = ev_return_caller;
86+
obj go_obj = restore(cr);
87+
goto go_obj;
8588

8689
// 5.4.1 The Core of the Evaluator
8790

@@ -419,35 +422,44 @@ static obj ecevalcr(obj expression, obj _environment, struct core *cr)
419422
return error_eval(AREA, "Unknown procedure type: %s", errstr(cr->proc));
420423

421424
go_cont:
422-
if (is_eq(cr->cont, ev_return_caller))
425+
go_obj = cr->cont;
426+
go_obj:
427+
if (is_eq(go_obj, ev_return_caller))
423428
return cr->val;
424-
if (is_eq(cr->cont, ev_appl_accum_last_arg))
429+
if (is_eq(go_obj, ev_appl_accum_last_arg))
425430
goto ev_appl_accum_last_arg;
426-
if (is_eq(cr->cont, ev_appl_accumulate_arg))
431+
if (is_eq(go_obj, ev_appl_accumulate_arg))
427432
goto ev_appl_accumulate_arg;
428-
if (is_eq(cr->cont, ev_appl_did_operator))
433+
if (is_eq(go_obj, ev_appl_did_operator))
429434
goto ev_appl_did_operator;
430-
if (is_eq(cr->cont, ev_apply_2))
435+
if (is_eq(go_obj, ev_apply_2))
431436
goto ev_apply_2;
432-
if (is_eq(cr->cont, ev_apply_3))
437+
if (is_eq(go_obj, ev_apply_3))
433438
goto ev_apply_3;
434-
if (is_eq(cr->cont, ev_assignment_1))
439+
if (is_eq(go_obj, ev_assignment_1))
435440
goto ev_assignment_1;
436-
if (is_eq(cr->cont, ev_definition_1))
441+
if (is_eq(go_obj, ev_definition_1))
437442
goto ev_definition_1;
438-
if (is_eq(cr->cont, ev_if_decide))
443+
if (is_eq(go_obj, ev_eval_dispatch))
444+
goto eval_dispatch;
445+
if (is_eq(go_obj, ev_if_decide))
439446
goto ev_if_decide;
440-
if (is_eq(cr->cont, ev_quoted))
447+
if (is_eq(go_obj, ev_quoted))
441448
goto ev_quoted;
442-
if (is_eq(cr->cont, ev_sequence_continue))
449+
if (is_eq(go_obj, ev_sequence_continue))
443450
goto ev_sequence_continue;
444-
if (is_eq(cr->cont, ev_timed_done))
451+
if (is_eq(go_obj, ev_timed_done))
445452
goto ev_timed_done;
446453
return error_internal(AREA, "BUG! Goto... where?: %s",
447-
to_string(cr->cont));
454+
to_string(go_obj));
448455
}
449456

450457
obj eceval(obj expression, obj _environment)
451458
{
452-
return ecevalcr(expression, _environment, dfltcore());
459+
struct core *cr = dfltcore();
460+
cr->expr = expression;
461+
cr->env = _environment;
462+
cr->cont = ev_return_caller;
463+
save_nogc(ev_eval_dispatch, cr);
464+
return ecevalgoto(cr);
453465
}

src/obj.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ SYMBOL_VAR(ev_apply_2)
274274
SYMBOL_VAR(ev_apply_3)
275275
SYMBOL_VAR(ev_assignment_1)
276276
SYMBOL_VAR(ev_definition_1)
277+
SYMBOL_VAR(ev_eval_dispatch)
277278
SYMBOL_VAR(ev_if_decide)
278279
SYMBOL_VAR(ev_quoted)
279280
SYMBOL_VAR(ev_sequence_continue)

src/obj.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ extern const obj ev_apply_2;
156156
extern const obj ev_apply_3;
157157
extern const obj ev_assignment_1;
158158
extern const obj ev_definition_1;
159+
extern const obj ev_eval_dispatch;
159160
extern const obj ev_if_decide;
160161
extern const obj ev_quoted;
161162
extern const obj ev_sequence_continue;

0 commit comments

Comments
 (0)