Skip to content

Commit e3684b8

Browse files
committed
Add: yield
1 parent 5915f9a commit e3684b8

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

.vscode/tasks.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"src/storage.c",
3737
"src/strbldr.c",
3838
//
39-
"-O1", // Optimization Level. Tail Call Optimization seem to need >= 2
39+
"-O3", // 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",
@@ -76,4 +76,4 @@
7676
],
7777
},
7878
]
79-
}
79+
}

src/eceval.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
#include "eceval.h"
55

6+
#include <stdbool.h>
67
#include <stdlib.h>
8+
79
#include "error.h"
810
#include "list.h"
911
#include "mceval.h"
@@ -81,7 +83,7 @@ static void set_proc_name(struct core *cr)
8183
of_string("<unknown>");
8284
}
8385

84-
static obj ecevalgoto(struct core *cr)
86+
static obj ecevalgoto(struct core *cr, bool yield)
8587
{
8688
obj go_obj = restore(cr);
8789
goto go_obj;
@@ -422,6 +424,10 @@ static obj ecevalgoto(struct core *cr)
422424
return error_eval(AREA, "Unknown procedure type: %s", errstr(cr->proc));
423425

424426
go_cont:
427+
if (yield) {
428+
save(cr->cont, cr);
429+
return yielded;
430+
}
425431
go_obj = cr->cont;
426432
go_obj:
427433
if (is_eq(go_obj, ev_return_caller))
@@ -456,10 +462,14 @@ static obj ecevalgoto(struct core *cr)
456462

457463
obj eceval(obj expression, obj _environment)
458464
{
465+
obj rslt;
466+
459467
struct core *cr = dfltcore();
460468
cr->expr = expression;
461469
cr->env = _environment;
462470
cr->cont = ev_return_caller;
463471
save_nogc(ev_eval_dispatch, cr);
464-
return ecevalgoto(cr);
472+
while (is_yielded(rslt = ecevalgoto(cr, false)))
473+
;
474+
return rslt;
465475
}

src/obj.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,11 @@ bool is_void(obj dat)
302302
return type(dat) == TYPE_VOID;
303303
}
304304
const obj void_o = OBJ_2(TYPE_VOID, SUBTYPE_NOT_SET);
305+
const obj yielded = OBJ_2(TYPE_YIELDED, SUBTYPE_NOT_SET);
306+
bool is_yielded(obj dat)
307+
{
308+
return type(dat) == TYPE_YIELDED;
309+
}
305310

306311
// BROKEN HEART
307312

src/obj.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ enum type {
2929
TYPE_BROKEN_HEART, // 11
3030
TYPE_BITMAP, // 12
3131
TYPE_UNASSIGNED, // 13
32-
TYPE_ERROR // 14
32+
TYPE_YIELDED, // 14
33+
TYPE_ERROR // 15
3334
};
3435

3536
enum { SUBTYPE_NOT_SET = 0 };
@@ -172,6 +173,8 @@ extern const obj ok;
172173
extern const obj unspecified;
173174
bool is_void(obj dat);
174175
extern const obj void_o;
176+
extern const obj yielded;
177+
bool is_yielded(obj dat);
175178

176179
// BROKEN HEART
177180

0 commit comments

Comments
 (0)