-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreflection11.js
769 lines (667 loc) · 22.7 KB
/
reflection11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
/*
CS1101S Lecture L11: CSE Machine
This program contains a CSE machine for a simple
functional programming language, including:
* literal values: numbers, booleans, strings
* conditional statements and expressions
* sequences and blocks
* simple functions (body consists of a single
return statement)
Examples at the end of the program
*/
//Question 1
function evaluate(program) {
let C = list(make_block(program));
let S = null;
let E = the_global_environment;
while (! is_null(C)) {
display_control(C);
display_stash(S);
display_environment(E);
const command = head(C);
C = tail(C);
// expressions and statements
if (is_literal(command)) {
S = pair(literal_value(command), S);
} else if (is_binary_operator_combination(command)) {
C = pair(first_operand(command),
pair(second_operand(command),
pair(make_binary_operator_instruction(
operator_symbol(command)),
C)));
} else if (is_unary_operator_combination(command)) {
C = pair(first_operand(command),
pair(make_unary_operator_instruction(
operator_symbol(command)),
C));
} else if (is_sequence(command)) {
const body = sequence_statements(command);
C = is_null(body)
? pair(make_literal(undefined), C)
: is_null(tail(body))
? pair(head(body), C)
: pair(head(body),
pair(make_pop_instruction(),
pair(make_sequence(tail(body)),
C)));
} else if (is_conditional(command)) {
C = pair(conditional_predicate(command),
pair(make_branch_instruction(
conditional_consequent(command),
conditional_alternative(command)),
C));
} else if (is_block(command)) {
const locals = scan_out_declarations(
block_body(command));
const unassigneds = list_of_unassigned(locals);
C = pair(block_body(command),
pair(make_env_instruction(E),
C));
E = extend_environment(locals, unassigneds, E);
} else if (is_function_declaration(command)) {
C = pair(function_decl_to_constant_decl(command),
C);
} else if (is_declaration(command)) {
C = pair(make_assignment(
declaration_symbol(command),
declaration_value_expression(command)),
C);
} else if (is_name(command)) {
S = pair(lookup_symbol_value(
symbol_of_name(command),
E),
S);
} else if (is_assignment(command)) {
C = pair(assignment_value_expression(command),
pair(make_assign_instruction(
assignment_symbol(command)),
C));
} else if (is_lambda_expression(command)) {
if (is_return_statement(
lambda_body(command))) {
S = pair(make_simple_function(
lambda_parameter_symbols(command),
return_expression(lambda_body(command)),
E),
S);
} else {
error(command, "body must be return statement");
}
} else if (is_application(command)) {
const arg_exprs = arg_expressions(command);
C = pair(function_expression(command),
append(arg_exprs,
pair(make_call_instruction(
length(arg_exprs)), C)));
// machine instructions
} else if (is_pop_instruction(command)) {
S = tail(S);
} else if (is_binary_operator_instruction(command)) {
S = pair(apply_binary(operator_instruction_symbol(command),
head(tail(S)), head(S)),
tail(tail(S)));
} else if (is_unary_operator_instruction(command)) {
S = pair(apply_unary(operator_instruction_symbol(command),
head(S)),
tail(S));
} else if (is_branch_instruction(command)) {
C = pair(is_truthy(head(S))
? branch_instruction_consequent(command)
: branch_instruction_alternative(command),
C);
S = tail(S);
} else if (is_assign_instruction(command)) {
assign_symbol_value(
assign_instruction_symbol(command),
head(S),
E);
} else if (is_env_instruction(command)) {
E = env_instruction_environment(command);
} else if (is_call_instruction(command)) {
const arity = call_instruction_arity(command);
let args = null;
let n = arity;
while (n > 0) {
args = pair(head(S), args);
S = tail(S);
n = n - 1;
}
const fun = head(S);
S = tail(S);
if (is_primitive_function(fun)) {
S = pair(apply_primitive_function(fun, args),
S);
} else if (is_simple_function(fun)) {
C = pair(function_body(fun),
pair(make_env_instruction(E),
C));
E = extend_environment(
function_parameters(fun),
args,
function_environment(fun));
} else {
error(fun, "unknown function type -- call");
}
} else if (is_logical_composition(command)){
return logical_composition_to_conditional_expression(command);
}
else {
error(command, "unknown command:");
}
}
return head(S);
}
// scan_out_declarations and list_of_unassigned
// (SICP JS 4.1.1)
function scan_out_declarations(component) {
return is_sequence(component)
? accumulate(append,
null,
map(scan_out_declarations,
sequence_statements(component)))
: is_declaration(component)
? list(declaration_symbol(component))
: null;
}
function list_of_unassigned(symbols) {
return map(symbol => "*unassigned*", symbols);
}
function apply_binary(operator, op1, op2) {
return operator === "+"
? op1 + op2
: operator === "-"
? op1 - op2
: operator === "*"
? op1 * op2
: operator === "/"
? op1 / op2
: operator === "%"
? math_hypot(op1, op2)
: operator === "<"
? op1 < op2
: operator === ">"
? op1 > op2
: operator === "<="
? op1 <= op2
: operator === ">="
? op1 >= op2
: operator === "==="
? op1 === op2
: operator === "!=="
? op1 !== op2
: error(operator, "Unknown operator");
}
function apply_unary(operator, op) {
return operator === "-unary"
? - op
: operator === "!"
? ! op
: error(operator, "Unknown operator");
}
//
// syntax functions (SICP JS 4.1.2)
//
function is_tagged_list(component, the_tag) {
return is_pair(component) && head(component) === the_tag;
}
// literals
function is_literal(component) {
return is_tagged_list(component, "literal");
}
function literal_value(component) {
return head(tail(component));
}
function make_literal(val) {
return list("literal", val);
}
// names
function is_name(component) {
return is_tagged_list(component, "name");
}
function make_name(symbol) {
return list("name", symbol);
}
function symbol_of_name(component) {
return head(tail(component));
}
// operator combinations
function is_operator_combination(component) {
return is_unary_operator_combination(component) ||
is_binary_operator_combination(component);
}
function is_unary_operator_combination(component) {
return is_tagged_list(component, "unary_operator_combination");
}
function is_binary_operator_combination(component) {
return is_tagged_list(component, "binary_operator_combination");
}
function operator_symbol(component) {
return list_ref(component, 1);
}
function first_operand(component) {
return list_ref(component, 2);
}
function second_operand(component) {
return list_ref(component, 3);
}
// sequences
function is_sequence(stmt) {
return is_tagged_list(stmt, "sequence");
}
function sequence_statements(stmt) {
return head(tail(stmt));
}
function make_sequence(stmts) {
return list("sequence", stmts);
}
// conditionals
function is_conditional(component) {
return is_tagged_list(component, "conditional_expression") ||
is_tagged_list(component, "conditional_statement");
}
function conditional_predicate(component) {
return list_ref(component, 1);
}
function conditional_consequent(component) {
return list_ref(component, 2);
}
function conditional_alternative(component) {
return list_ref(component, 3);
}
// blocks
function is_block(component) {
return is_tagged_list(component, "block");
}
function block_body(component) {
return head(tail(component));
}
function make_block(statement) {
return list("block", statement);
}
// declarations
function is_declaration(component) {
return is_tagged_list(component, "constant_declaration") ||
is_tagged_list(component, "variable_declaration") ||
is_tagged_list(component, "function_declaration");
}
function declaration_symbol(component) {
return symbol_of_name(head(tail(component)));
}
function declaration_value_expression(component) {
return head(tail(tail(component)));
}
function make_constant_declaration(name, value_expression) {
return list("constant_declaration", name, value_expression);
}
// assignments
function is_assignment(component) {
return is_tagged_list(component, "assignment");
}
function assignment_symbol(component) {
return head(tail(head(tail(component))));
}
function assignment_value_expression(component) {
return head(tail(tail(component)));
}
function make_assignment(symbol, expression) {
return list("assignment",
make_name(symbol),
expression);
}
// lambda expressions
function is_lambda_expression(component) {
return is_tagged_list(component, "lambda_expression");
}
function lambda_parameter_symbols(component) {
return map(symbol_of_name, head(tail(component)));
}
function lambda_body(component) {
return head(tail(tail(component)));
}
function make_lambda_expression(parameters, body) {
return list("lambda_expression", parameters, body);
}
// function declarations
function is_function_declaration(component) {
return is_tagged_list(component, "function_declaration");
}
function function_declaration_name(component) {
return list_ref(component, 1);
}
function function_declaration_parameters(component) {
return list_ref(component, 2);
}
function function_declaration_body(component) {
return list_ref(component, 3);
}
function function_decl_to_constant_decl(component) {
return make_constant_declaration(
function_declaration_name(component),
make_lambda_expression(
function_declaration_parameters(component),
function_declaration_body(component)));
}
// applications
function is_application(component) {
return is_tagged_list(component, "application");
}
function function_expression(component) {
return head(tail(component));
}
function arg_expressions(component) {
return head(tail(tail(component)));
}
// return statements
function is_return_statement(component) {
return is_tagged_list(component, "return_statement");
}
function return_expression(component) {
return head(tail(component));
}
//
// CSE machine instructions
//
// operators instructions
function is_binary_operator_instruction(component) {
return is_tagged_list(component, "binop");
}
function is_unary_operator_instruction(component) {
return is_tagged_list(component, "unop");
}
function operator_instruction_symbol(component) {
return list_ref(component, 1);
}
function make_binary_operator_instruction(symbol) {
return list("binop", symbol);
}
function make_unary_operator_instruction(symbol) {
return list("unop", symbol);
}
// assign instructions
function is_assign_instruction(component) {
return is_tagged_list(component, "asgn");
}
function assign_instruction_symbol(component) {
return head(tail(component));
}
function make_assign_instruction(symbol) {
return list("asgn", symbol);
}
// pop instructions
function is_pop_instruction(component) {
return is_tagged_list(component, "pop");
}
function make_pop_instruction() {
return list("pop");
}
// branch instructions
function is_branch_instruction(component) {
return is_tagged_list(component, "branch");
}
function branch_instruction_consequent(component) {
return list_ref(component, 1);
}
function branch_instruction_alternative(component) {
return list_ref(component, 2);
}
function make_branch_instruction(consequent, alternative) {
return list("branch", consequent, alternative);
}
// environment instructions
function is_env_instruction(component) {
return is_tagged_list(component, "env");
}
function env_instruction_environment(component) {
return list_ref(component, 1);
}
function make_env_instruction(environment) {
return list("env", environment);
}
// call instructions
function is_call_instruction(component) {
return is_tagged_list(component, "call");
}
function call_instruction_arity(component) {
return list_ref(component, 1);
}
function make_call_instruction(arity) {
return list("call", arity);
}
//
// evaluator data structures (SICP JS 4.1.3)
//
// Booleans
function is_truthy(x) {
return is_boolean(x)
? x
: error(x, "boolean expected, received");
}
// function objects
function make_simple_function(parameters, body, env) {
return list("simple_function", parameters, body, env);
}
function is_simple_function(f) {
return is_tagged_list(f, "simple_function");
}
function function_parameters(f) { return list_ref(f, 1); }
function function_body(f) { return list_ref(f, 2); }
function function_environment(f) { return list_ref(f, 3); }
//
// environments
//
function enclosing_environment(env) { return tail(env); }
function first_frame(env) { return head(env); }
const the_empty_environment = null;
function make_frame(symbols, values) { return pair(symbols, values); }
function frame_symbols(frame) { return head(frame); }
function frame_values(frame) { return tail(frame); }
function extend_environment(symbols, vals, base_env) {
return length(symbols) === length(vals)
? pair(make_frame(symbols, vals), base_env)
: length(symbols) < length(vals)
? error("too many arguments supplied: " +
stringify(symbols) + ", " +
stringify(vals))
: error("too few arguments supplied: " +
stringify(symbols) + ", " +
stringify(vals));
}
function lookup_symbol_value(symbol, env) {
function env_loop(env) {
function scan(symbols, vals) {
return is_null(symbols)
? env_loop(enclosing_environment(env))
: symbol === head(symbols)
? head(vals)
: scan(tail(symbols), tail(vals));
}
if (env === the_empty_environment) {
error(symbol, "unbound name");
} else {
const frame = first_frame(env);
return scan(frame_symbols(frame), frame_values(frame));
}
}
return env_loop(env);
}
function assign_symbol_value(symbol, val, env) {
function env_loop(env) {
function scan(symbols, vals) {
return is_null(symbols)
? env_loop(enclosing_environment(env))
: symbol === head(symbols)
? set_head(vals, val)
: scan(tail(symbols), tail(vals));
}
if (env === the_empty_environment) {
error(symbol, "unbound name -- assignment");
} else {
const frame = first_frame(env);
return scan(frame_symbols(frame), frame_values(frame));
}
}
return env_loop(env);
}
// global environment (SICP JS 4.1.4)
function is_primitive_function(fun) {
return is_tagged_list(fun, "primitive");
}
function primitive_implementation(fun) { return head(tail(fun)); }
const primitive_functions = list(
list("head", head ),
list("tail", tail ),
list("pair", pair ),
list("list", list ),
list("is_null", is_null ),
list("display", display ),
list("error", error ),
list("math_pow",math_pow )
);
const primitive_function_symbols =
map(head, primitive_functions);
const primitive_function_objects =
map(fun => list("primitive", head(tail(fun))),
primitive_functions);
const primitive_constants = list(list("undefined", undefined),
list("Infinity", Infinity),
list("math_PI", math_PI),
list("math_E", math_E),
list("NaN", NaN)
);
const primitive_constant_symbols =
map(c => head(c), primitive_constants);
const primitive_constant_values =
map(c => head(tail(c)), primitive_constants);
function apply_primitive_function(fun, arglist) {
return apply_in_underlying_javascript(
primitive_implementation(fun), arglist);
}
function setup_environment() {
return extend_environment(append(primitive_function_symbols,
primitive_constant_symbols),
append(primitive_function_objects,
primitive_constant_values),
the_empty_environment);
}
const the_global_environment = setup_environment();
const the_global_frame = head(the_global_environment);
//
// testing
//
function display_control(C) {
display("", "*** control *** ");
for_each(command =>
is_literal(command) ||
is_call_instruction(command) ||
is_pop_instruction(command) ||
is_binary_operator_instruction(command) ||
is_unary_operator_instruction(command) ||
is_assign_instruction(command)
? display_list(command)
: display_list(list(head(command), "...")),
C);
display("", " ");
}
function display_stash(S) {
display("", "*** stash *** ");
for_each(value => {
if (is_simple_function(value)) {
display_list(list("simple_function",
function_parameters(value),
"<body not displayed>",
"<environment not displayed>"));
} else {
display_list(value);
}
},
S);
display("", " ");
}
function display_environment(E) {
display("", "*** environment *** ");
for_each(frame => {
if (frame === the_global_frame) {
display("", "<global frame not displayed> ");
} else {
display_list(head(frame), "names: ");
display_list(
map(value =>
is_simple_function(value)
? list("simple_function",
function_parameters(value),
"<body not displayed>",
"<environment not displayed>")
: value,
tail(frame)),
"values:");
}
}, E);
display("", " ");
}
function parse_and_evaluate(string) {
return evaluate(parse(string));
}
//parse_and_evaluate("1;");
//parse_and_evaluate("undefined;");
//parse_and_evaluate("'hello';");
//parse_and_evaluate("1 + 2;");
//parse_and_evaluate("- 4;");
//parse_and_evaluate("! true;");
//parse_and_evaluate("'hello' + 'world';");
//parse_and_evaluate("1; 2; 3;");
//parse_and_evaluate("if (true) { 1; } else { 2; }");
//parse_and_evaluate("true ? 1 : 2;");
//parse_and_evaluate("math_PI;");
//parse_and_evaluate("const x = 1 + 2; x;");
//parse_and_evaluate("const x = 1; { const x = 2; x; } x;");
//parse_and_evaluate("math_pow(2, 3);");
/*
parse_and_evaluate(`
function factorial(n) {
return n === 1
? 1
: n * factorial(n - 1);
}
factorial(5);`);
*/
/*
parse_and_evaluate(`
const n = 1000000;
function factorial(n) {
return n === 1
? 1
: factorial(n - 1) * n;
}
factorial(5) + n;`);
*/
// Question 2
// the syntax predicate
function is_logical_composition(component) {
return is_tagged_list(component, "logical_composition");
}
// selectors
function logical_symbol(comp) {
return list_ref(comp, 1);
}
function logical_composition_first_component(comp) {
return list_ref(comp, 2);
}
function logical_composition_second_component(comp) {
return list_ref(comp, 3);
}
// helper to make a conditional expression
function make_conditional_expression(pred, cons, alt) {
return list("conditional_expression", pred, cons, alt);
}
// helper to convert logical_composition to conditional expression
function logical_composition_to_conditional_expression(command){
return logical_symbol(command) === "&&"
? make_conditional_expression(logical_composition_first_command(command),
logical_composition_second_command(command),
make_literal(false))
: make_conditional_expression(logical_composition_first_command(command),
make_literal(true),
logical_composition_second_command(command));
}
// Question 3
// just use the inbuilt parse function in source