-
Notifications
You must be signed in to change notification settings - Fork 0
/
lecture11_conditional_etc
368 lines (299 loc) · 10.3 KB
/
lecture11_conditional_etc
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
/*
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
Examples at the end of the program
*/
/*
stmt::= ...
| stmt1 .... stmtn (statement sequence)
expr::= ...
| true | false (boolean literals)
| expr1 ? expr2 : expr3 (conditional expression)
*/
// parsing of conditionals return:
/*
parse("true ? 42 : 17;");
returns
list(" conditional_expression ",
list(" literal ", true),
list(" literal ", 42),
list(" literal ", 17))
Syntax predicate: is_conditional
Selectors: cond_predicate, cond_consequent,
cond_alternative
*/
/*
Parsing of sequences return
parse("1; 2; 3;");
returns
list(" sequence ",
list(list(" literal ", 1),
list(" literal ", 2),
list(" literal ", 3))
Syntax predicate: is_sequence
Selector: sequence_statements
*/
function evaluate(program) {
let C = list(program);
let S = null;
while (! is_null(C)) {
display_control(C);
display_stash(S);
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) // if sequence is empty - undefined
? pair(make_literal(undefined), C)
// put the literal value undefined
// back to control
// if given sequence empty
: is_null(tail(body))
// if sequence has one element
// put that element in control
? pair(head(body), C)
// finally if sequence has more than one element
// first evaluate the first component of sequence
// that is put it first on the control
// then put the pop instruction on the control
// to get rid of its value on the stash
// and then put remaining components of the
// sequence on the control
// sequence one lesser
// recursive
: pair(head(body),
pair(make_pop_instruction(),
pair(make_sequence(tail(body)), // resursive nature, putting remaining elements of sequence back on control
C)));
} else if (is_conditional(command)) {
// not evaluate the components of the conditional
// until the predicate is evaluated
// and then based on that
// evaluate one of the consequent or the
// alternative
// put the predicate first on the control
// since that is the next thing we need to evaluate
// then we put special instruction known as branch
// branch instruction has consequent and alternative component of the condition
// get the result of the predicate from the stash
// then take action accordingly from branch instruction
// and put it back in the control
C = pair(conditional_predicate(command),
pair(make_branch_instruction(
conditional_consequent(command),
conditional_alternative(command)),
C));
// machine instructions
} else if (is_pop_instruction(command)) {
// simply removes an element from the stash
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)) {
// when this is encountered, we assume that the predicate
// has already been evaluated (from the is_conditional(command))
// and assume that the result is already on the stash
C = pair(is_truthy(head(S)) // check our stash and see if the predicate is true
// is_truthy comes from javascript
// to convey what is considered to be true in the language
// declared later in the language
? branch_instruction_consequent(command) // if predicate true
// put the consequent back on control
: branch_instruction_alternative(command),
C);
// otherwise put alternate back on control
S = tail(S);
} else {
error(command, "unknown command:");
}
}
return head(S);
}
function apply_binary(operator, op1, op2) {
return operator === "+"
? op1 + op2
: operator === "-"
? op1 - op2
: operator === "*"
? op1 * op2
: operator === "/"
? op1 / op2
: operator === "%"
? 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);
}
// 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);
}
//
// 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);
}
// 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);
}
//
// evaluator data structures (SICP JS 4.1.3)
//
// Booleans
function is_truthy(x) {
return is_boolean(x)
? x
: error(x, "boolean expected, received");
}
//
// testing
//
function display_control(C) {
display("", "*** control *** ");
for_each(command =>
is_literal(command) ||
is_pop_instruction(command) ||
is_binary_operator_instruction(command) ||
is_unary_operator_instruction(command)
? display_list(command)
: display_list(list(head(command), "...")),
C);
display("", " ");
}
function display_stash(S) {
display("", "*** stash *** ");
for_each(value => display_list(value), S);
display("", " ");
}
function parse_and_evaluate(string) {
return evaluate(parse(string));
}
parse_and_evaluate("true ? 42 : 17;");