Skip to content

Commit 298c88b

Browse files
Updates to §5.4 sources and working program
1 parent 1e29dfc commit 298c88b

File tree

6 files changed

+1877
-158
lines changed

6 files changed

+1877
-158
lines changed

xml/chapter5/section3/subsection2.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ accumulate((x, y) => x + y, 0, filter(is_odd, enumerate_interval(0, n)))
447447
branch(label("already_moved")),
448448
assign("new", reg("free")), // new location for pair
449449
// Update "free" pointer.
450-
assign("free", list((op("+"), reg(free), constant(1))),
450+
assign("free", list((op("+"), reg("free"), constant(1))),
451451
// Copy the head and tail to new memory
452452
perform(list(op("vector_set"),
453453
reg("new_heads"), reg("new"), reg("oldhr"))),

xml/chapter5/section4/section4.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
operations to be used in our register machine. We described the
5353
metacircular evaluator in terms of abstract syntax, using
5454
<SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE>
55-
such as <SCHEMEINLINE>quoted?</SCHEMEINLINE> and <SCHEMEINLINE>make-procedure</SCHEMEINLINE>. In implementing the
55+
such as <SPLITINLINE><SCHEME><SCHEMEINLINE>quoted?</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>is_self_evaluating</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> and <SPLITINLINE><SCHEME><SCHEMEINLINE>make-procedure</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>make_compound_function</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>. In implementing the
5656
register machine, we could expand these
5757
<SPLITINLINE><SCHEME>procedures</SCHEME><JAVASCRIPT>functions</JAVASCRIPT></SPLITINLINE>
5858
into sequences of

xml/chapter5/section4/subsection1.xml

Lines changed: 94 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,30 @@
5151
</SCHEME>
5252
<JAVASCRIPT>
5353
<!-- \indcode*{eval-dispatch} -->
54-
"eval_dispatch",
55-
test(op("is_self_evaluating"), reg("exp")),
56-
branch(label("ev_self_eval")),
57-
test(op("is_variable"), reg("exp")),
58-
branch(label("ev_variable")),
59-
test(op("is_quoted"), reg("exp")),
60-
branch(label("ev_quoted")), /// FIXME
61-
test(op("is_assignment"), reg("exp")),
62-
branch(label("ev_assignment")),
63-
test(op("is_definition"), reg("exp")),
64-
branch(label("ev_definition")),
65-
test(op("is_conditional_statement"), reg("exp")),
66-
branch(label("ev_if")),
67-
test(op("is_function_expression"), reg("exp")),
68-
branch(label("ev_lambda")),
69-
test(op("is_block"), reg("exp")),
70-
branch(label("ev_begin")),
71-
test(op("is_application"), reg("exp")),
72-
branch(label("ev_application")),
73-
go_to(label("unknown_expression_type")),
54+
"eval_dispatch",
55+
test(op("is_self_evaluating"), reg("exp")),
56+
branch(label("ev_self_eval")),
57+
test(op("is_name"), reg("exp")),
58+
branch(label("ev_name")),
59+
60+
test(op("is_variable_declaration"), reg("exp")),
61+
branch(label("ev_variable_declaration")),
62+
test(op("is_constant_declaration"), reg("exp")),
63+
branch(label("ev_constant_declaration")),
64+
test(op("is_assignment"), reg("exp")),
65+
branch(label("ev_assignment")),
66+
67+
test(op("is_conditional_expression"), reg("exp")),
68+
branch(label("ev_if")),
69+
test(op("is_function_definition"), reg("exp")),
70+
branch(label("ev_lambda")),
71+
test(op("is_sequence"), reg("exp")),
72+
branch(label("ev_seq")),
73+
test(op("is_block"), reg("exp")),
74+
branch(label("ev_block")),
75+
test(op("is_application"), reg("exp")),
76+
branch(label("ev_application")),
77+
go_to(label("unknown_expression_type")),
7478
</JAVASCRIPT>
7579
</SNIPPET>
7680
</TEXT>
@@ -111,23 +115,20 @@
111115
(goto (reg continue))
112116
</SCHEME>
113117
<JAVASCRIPT>
114-
<!-- \indcode*{ev-self-eval} -->
115118
"ev_self_eval",
116-
assign("val", reg("exp")),
119+
assign("val", list(reg("exp"))),
117120
go_to(reg("continue")),
118-
<!-- \indcode*{ev-variable} -->
119-
"ev_variable",
120-
assign("val", op("lookup_variable_value"), reg("exp"), reg("env")),
121+
122+
"ev_name",
123+
assign("val", list(op("lookup_name_value"), reg("exp"), reg("env"))),
121124
go_to(reg("continue")),
122-
<!-- \indcode*{ev-quoted} -->
123-
"ev_quoted",
124-
assign("val", op("text_of_quotation"), reg("exp")),
125-
go_to(reg("continue")),
126-
<!-- \indcode*{ev-lambda} -->
125+
127126
"ev_lambda",
128-
assign("unev", op("lambda_parameters"), reg("exp")),
129-
assign("exp", op("lambda_body"), reg("exp")),
130-
assign("val", op("make_procedure"), reg("unev"), reg("exp"), reg("env")),
127+
assign("unev", list(op("function_definition_parameters"), reg("exp"))),
128+
assign("exp", list(op("function_definition_body"), reg("exp"))),
129+
assign("val",
130+
list(op("make_compound_function"),
131+
reg("unev"), reg("exp"), reg("env"))),
131132
go_to(reg("continue")),
132133
</JAVASCRIPT>
133134
</SNIPPET>
@@ -214,13 +215,12 @@
214215
(goto (label eval-dispatch))
215216
</SCHEME>
216217
<JAVASCRIPT>
217-
<!-- \indcode*{ev-application} -->
218218
"ev_application",
219219
save("continue"),
220220
save("env"),
221-
assign("unev", op("operands"), reg("exp")),
221+
assign("unev", list(op("operands"), reg("exp"))),
222222
save("unev"),
223-
assign("exp", op("operator"), reg("exp")),
223+
assign("exp", list(op("operator"), reg("exp"))),
224224
assign("continue", label("ev_appl_did_operator")),
225225
go_to(label("eval_dispatch")),
226226
</JAVASCRIPT>
@@ -295,13 +295,13 @@ function is_last_operand(ops) {
295295
</SCHEME>
296296
<JAVASCRIPT>
297297
"ev_appl_did_operator",
298-
restore("unev"), // the operands
299-
restore("env"),
300-
assign("argl", op("empty_arglist")),
301-
assign("fun", reg("val")), // the operator
302-
test(op("has_no_operands"), reg("unev")),
303-
branch(label("apply_dispatch")),
304-
save("fun"),
298+
restore("unev"), // the operands
299+
restore("env"),
300+
assign("argl", list(op("empty_arglist"))),
301+
assign("fun", list(reg("val"))), // the operator
302+
test(op("has_no_operands"), reg("unev")),
303+
branch(label("apply_dispatch")),
304+
save("fun"),
305305
</JAVASCRIPT>
306306
</SNIPPET>
307307
</TEXT>
@@ -331,14 +331,14 @@ function is_last_operand(ops) {
331331
</SCHEME>
332332
<JAVASCRIPT>
333333
"ev_appl_operand_loop",
334-
save("argl"),
335-
assign("exp", (op("first_operand"), reg("unev")),
336-
test(op("is_last_operand"), reg("unev")),
337-
branch(label("ev_appl_last_arg")),
338-
save("env"),
339-
save("unev"),
340-
assign("continue", (label("ev_appl_accumulate_arg"))),
341-
go_to(label("eval_dispatch")),
334+
save("argl"),
335+
assign("exp", list(op("first_operand"), reg("unev"))),
336+
test(op("is_last_operand"), reg("unev")),
337+
branch(label("ev_appl_last_arg")),
338+
save("env"),
339+
save("unev"),
340+
assign("continue", label("ev_appl_accumulate_arg")),
341+
go_to(label("eval_dispatch")),
342342
</JAVASCRIPT>
343343
</SNIPPET>
344344
</TEXT>
@@ -363,8 +363,8 @@ function is_last_operand(ops) {
363363
restore("unev"),
364364
restore("env"),
365365
restore("argl"),
366-
assign("argl", op("adjoin_arg"), reg("val"), reg("argl")),
367-
assign("unev", op("rest_operands"), reg("unev")),
366+
assign("argl", list(op("adjoin_arg"), reg("val"), reg("argl"))),
367+
assign("unev", list(op("rest_operands"), reg("unev"))),
368368
go_to(label("ev_appl_operand_loop")),
369369
</JAVASCRIPT>
370370
</SNIPPET>
@@ -375,7 +375,7 @@ function is_last_operand(ops) {
375375
need to save the environment or the list of unevaluated operands
376376
before going to <SPLITINLINE><SCHEMEINLINE>eval-dispatch</SCHEMEINLINE><JAVASCRIPTINLINE>eval_dispatch</JAVASCRIPTINLINE></SPLITINLINE>,
377377
since they will not be required after the last operand is evaluated.
378-
Thus, we return from the evaluation to a special entry point <SPLITINLINE><SCHEMEINLINE>ev-appl-accum-last-arg</SCHEMEINLINE><JAVASCRIPTINLINE>ev_appl_accum_last_arg</JAVASCRIPTINLINE></SPLITINLINE>, which restores the argument list, accumulates
378+
Thus, we return from the evaluation to a special entry point <SPLITINLINE><SCHEME><SCHEMEINLINE>ev-appl-accum-last-arg</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>ev_appl_accum_last_arg</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>, which restores the argument list, accumulates
379379
the new argument, restores the saved
380380
<SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>, and goes off to
381381
perform the
@@ -407,15 +407,15 @@ function is_last_operand(ops) {
407407
(goto (label apply-dispatch))
408408
</SCHEME>
409409
<JAVASCRIPT>
410-
"ev_appl_last_arg",
411-
assign("continue", label("ev_appl_accum_last_arg")),
412-
go_to(label("eval_dispatch")),
413-
414-
"ev_appl_accum_last_arg",
415-
restore(argl),
416-
assign("argl", op("adjoin_arg"), reg("val"), reg("argl")),
417-
restore("fun"),
418-
go_to(label("apply_dispatch")),
410+
"ev_appl_last_arg",
411+
assign("continue", label("ev_appl_accum_last_arg")),
412+
go_to(label("eval_dispatch")),
413+
414+
"ev_appl_accum_last_arg",
415+
restore("argl"),
416+
assign("argl", list(op("adjoin_arg"), reg("val"), reg("argl"))),
417+
restore("fun"),
418+
go_to(label("apply_dispatch")),
419419
</JAVASCRIPT>
420420
</SNIPPET>
421421
</TEXT>
@@ -433,7 +433,7 @@ function is_last_operand(ops) {
433433
in the
434434
<SPLITINLINE><SCHEME>procedure</SCHEME><JAVASCRIPT>function</JAVASCRIPT></SPLITINLINE>
435435
<SPLITINLINE><SCHEME><SCHEMEINLINE>list-of-values</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>list_of_values</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> of section<SPACE/><REF NAME="sec:core-of-evaluator"/>
436-
(see exercise<SPACE/><REF NAME="ex:arg-eval-order"/>).</FOOTNOTE> Because the <SPLITINLINE><SCHEMEINLINE>first-operand</SCHEMEINLINE><JAVASCRIPTINLINE>first_operand</JAVASCRIPTINLINE></SPLITINLINE>
436+
(see exercise<SPACE/><REF NAME="ex:arg-eval-order"/>).</FOOTNOTE> Because the <SPLITINLINE><SCHEME><SCHEMEINLINE>first-operand</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>first_operand</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>
437437
selector (used in <SPLITINLINE><SCHEME><SCHEMEINLINE>ev-appl-operand-loop</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>ev_appl_operand_loop</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> to extract successive operands
438438
from <SCHEMEINLINE>unev</SCHEMEINLINE>) is implemented as <SPLITINLINE><SCHEME><SCHEMEINLINE>car</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>head</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> and the <SPLITINLINE><SCHEME><SCHEMEINLINE>rest-operands</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>rest_operands</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE> selector is implemented as <SPLITINLINE><SCHEME><SCHEMEINLINE>cdr</SCHEMEINLINE></SCHEME><JAVASCRIPT><JAVASCRIPTINLINE>tail</JAVASCRIPTINLINE></JAVASCRIPT></SPLITINLINE>, the
439439
explicit-control evaluator will evaluate the operands of a combination
@@ -477,13 +477,12 @@ function is_last_operand(ops) {
477477
(goto (label unknown-procedure-type))
478478
</SCHEME>
479479
<JAVASCRIPT>
480-
<!-- \indcode*{apply-dispatch} -->
481-
"apply_dispatch",
482-
test(op("is_primitive_procedure"), reg("fun")),
483-
branch(label("primitive_apply")),
484-
test(op("is_compound_procedure"), reg("fun")),
485-
branch(label("compound_apply")),
486-
go_to(label("unknown_procedure_type")),
480+
"apply_dispatch",
481+
test(op("is_primitive_function"), reg("fun")),
482+
branch(label("primitive_apply")),
483+
test(op("is_compound_function"), reg("fun")),
484+
branch(label("compound_apply")),
485+
go_to(label("unknown_procedure_type")),
487486
</JAVASCRIPT>
488487
</SNIPPET>
489488
</TEXT>
@@ -534,11 +533,13 @@ function is_last_operand(ops) {
534533
(goto (reg continue))
535534
</SCHEME>
536535
<JAVASCRIPT>
537-
<!-- \indcode*{primitive-apply} -->
538536
"primitive_apply",
539-
assign("val", op("apply_primitive_procedure"), reg("fun"), reg("argl")),
540-
restore("continue"),
541-
go_to(reg("continue")),
537+
assign(
538+
"val",
539+
list(op("apply_primitive_function"), reg("fun"), reg("argl"))
540+
),
541+
restore("continue"),
542+
go_to(reg("continue")),
542543
</JAVASCRIPT>
543544
</SNIPPET>
544545
</TEXT>
@@ -570,12 +571,25 @@ function is_last_operand(ops) {
570571
(goto (label ev-sequence))
571572
</SCHEME>
572573
<JAVASCRIPT>
573-
<!-- \indcode*{compound-apply} -->
574574
"compound_apply",
575-
assign("unev", op("procedure_parameters"), reg("fun")),
576-
assign("env", op("procedure_environment") reg("fun")),
577-
assign("env", op("extend_environment"), reg("unev"), reg("argl"), reg("env")),
578-
assign("unev", op("procedure_body"), reg("fun")),
575+
assign("unev", list(op("function_parameters"), reg("fun"))), // params
576+
// FIXME: A QUICK HACK HERE, UNSURE WHY IT'S NEEDED
577+
assign("unev", list(op("all_names_of_names"), reg("unev"))), // params destructured
578+
579+
assign("temp", list(op("function_body"), reg("fun"))), // body
580+
assign("temp", list(op("local_names"), reg("unev"))), // locals
581+
582+
assign("unev", list(op("insert_all"), reg("unev"), reg("temp"))), //names
583+
assign("temp", list(op("get_temp_block_values"), reg("temp"))), // temp_values
584+
585+
assign("temp", list(op("append"), reg("argl"), reg("temp"))), // values
586+
assign("env", list(op("function_environment"), reg("fun"))),
587+
588+
assign(
589+
"env",
590+
list(op("extend_environment"), reg("unev"), reg("temp"), reg("env"))
591+
),
592+
assign("unev", list(op("function_body"), reg("fun"))),
579593
go_to(label("ev_sequence")),
580594
</JAVASCRIPT>
581595
</SNIPPET>

xml/chapter5/section4/subsection2.xml

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,28 @@
8686
(goto (label eval-dispatch))
8787
</SCHEME>
8888
<JAVASCRIPT>
89-
<!-- \indcode*{ev-sequence}[with tail recursion] -->
90-
"ev_sequence",
91-
assign(exp(op("first_exp"), reg("unev"))),
92-
test(op("is_last_exp"), reg("unev")),
93-
branch(label("ev_sequence_last_exp")),
94-
save("unev"),
95-
save("env"),
96-
assign("continue", label("ev_sequence_continue")),
97-
go_to(label("eval_dispatch")),
98-
"ev_sequence_continue",
99-
restore("env"),
100-
restore("unev"),
101-
assign("unev", op("rest_exps"), reg("unev")),
102-
go_to(label("ev_sequence")),
103-
"ev_sequence_last_exp",
104-
restore("continue"),
105-
go_to(label("eval_dispatch")),
89+
"ev_seq", //FIXME: not part of original book -- explain why needed
90+
save("continue"),
91+
assign("unev", list(op("sequence_statements"), reg("exp"))),
92+
93+
"ev_sequence",
94+
assign("exp", list(op("first_statement"), reg("unev"))),
95+
test(op("is_last_statement"), reg("unev")),
96+
branch(label("ev_sequence_last_exp")),
97+
save("unev"),
98+
save("env"),
99+
assign("continue", label("ev_sequence_continue")),
100+
go_to(label("eval_dispatch")),
101+
102+
"ev_sequence_continue",
103+
restore("env"),
104+
restore("unev"),
105+
assign("unev", list(op("rest_statements"), reg("unev"))),
106+
go_to(label("ev_sequence")),
107+
108+
"ev_sequence_last_exp",
109+
restore("continue"),
110+
go_to(label("eval_dispatch")),
106111
</JAVASCRIPT>
107112
</SNIPPET>
108113
</TEXT>
@@ -201,9 +206,9 @@
201206
(define (no-more-exps? seq) (null? seq))
202207
</SCHEME>
203208
<JAVASCRIPT>
204-
function has_no_more_exps(seq) {
205-
return is_null(seq);
206-
}
209+
function has_no_more_exps(seq) {
210+
return is_null(seq);
211+
}
207212
</JAVASCRIPT>
208213
</SNIPPET>
209214
</FOOTNOTE>
@@ -278,10 +283,10 @@
278283
(count (+ n 1)))
279284
</SCHEME>
280285
<JAVASCRIPT>
281-
function count(n) {
282-
display(n, "\n");
283-
count(n + 1);
284-
}
286+
function count(n) {
287+
display(n, "\n");
288+
count(n + 1);
289+
}
285290
</JAVASCRIPT>
286291
</SNIPPET>
287292
Without tail recursion, such a

0 commit comments

Comments
 (0)