-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammars.scm
477 lines (376 loc) · 14.8 KB
/
grammars.scm
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
;: Rewrite eval so that the dispatch is done in data-directed style.
;; (load "../chap2/ata_directed_package.scm")
(define (error-no-exception msg . others)
(display-debug msg)
(display-debug " ")
(display-debug others)
(newline-debug)
-1)
;:
;: We can rewrite "list-of-values" to implement the "simultaneous" scope rule.
;: But we have to consider the procedure, which includes mutually recursive internal
;: definitions.
(define (list-of-values-new exprs env) ())
(define (list-of-values exprs env)
(if (no-operands? exprs)
'()
(cons (eval (first-operand exprs) env)
(list-of-values (rest-operands exprs) env))))
;: A version of "list-of-values" that evaluates operands from left to right regardless of the
;: order of evalution in the underlying Lisp.
(define (list-of-values-left-to-right exprs env)
(define (iter e results)
(if (no-operands? e)
results
(let ((left-value (eval (first-operand e) env)))
(iter (rest-operands e) (append results (list left-value))))))
(iter exprs '()))
(define (list-of-values-left-to-right2 exprs env)
(if (no-operands? exprs)
'()
(let ((left-value (eval (first-operand exprs) env)))
(cons left-value (list-of-values-left-to-right2
(rest-operands exprs)
env)))))
;: A version of list-of-values that evaluates operands from right to left regardless of the
;: order of evalution in the underlying Lisp.
(define (list-of-values-right-to-left exprs env)
(if (no-operands? exprs)
'()
(let ((right-value (list-of-values-right-to-left (rest-operands exprs) env)))
(append right-value (list (eval (first-operand exprs) env))))))
;: The semantics of "true?" can be defined by ourself instead of
;: underlying scheme.
(define (eval-if expr env)
(if (true? (eval (if-predicate expr) env))
(eval (if-consequent expr) env)
(eval (if-alternative expr) env)))
(define (eval-sequence exprs env)
(cond ((last-exp? exprs) (eval (first-exp exprs) env))
(else (eval (first-exp exprs) env)
(eval-sequence (rest-exps exprs) env))))
(define (eval-assignment expr env)
(set-variable-value! (assignment-variable expr)
(eval (assignment-value expr) env)
env)
'ok)
(define (eval-definition expr env)
(define-variable! (definition-variable expr)
(eval (definition-value expr) env)
env)
'ok)
(define (self-evaluating? expr)
(cond ((number? expr) true)
((string? expr) true)
((eq? expr '*unassigned*) '*unassigned*)
(else false)))
;: (quote <text-of-quotation>) is equal to '<text--of-quotation> .
(define (quoted? expr)
(tagged-list? expr 'quote))
(define (text-of-quotation expr) (cadr expr))
;:
(define (install-quoted-package)
(define (text-of-quotation-env expr env)
(text-of-quotation expr))
(put 'eval 'quote text-of-quotation-env))
;:
(define (tagged-list? expr tag)
(if (pair? expr)
(eq? (car expr) tag)
false))
(define (variable? expr) (symbol? expr))
;: Assignment: (set! <var> <value>)
(define (make-assignment var val)
(cons 'set! (list var val)))
(define (assignment? expr)
(tagged-list? expr 'set!))
(define (assignment-variable expr) (cadr expr))
(define (assignment-value expr) (caddr expr))
(define (install-assignment-package)
(put 'eval 'set! eval-assignment))
;: Definition statement:
(define (definition? expr)
(tagged-list? expr 'define))
;: Example: (define VAR "hello")
;: or (define (func x) (* x 2))
(define (definition-variable expr)
(if (symbol? (cadr expr))
(cadr expr)
(caadr expr)))
(define (definition-value expr)
(if (symbol? (cadr expr))
(caddr expr)
;: function definition.
(make-lambda (cdadr expr) ;: format parameters
(cddr expr)))) ;: body
(define (make-definition variable parameters body)
(cons 'define (cons (cons variable parameters)
body)))
(define (install-definition-package)
(put 'eval 'define eval-definition))
(define (install-lambda-package)
(define (make-procedure-from-lambda expr env)
(make-procedure (lambda-parameters expr) (lambda-body expr) env))
(put 'eval 'lambda make-procedure-from-lambda))
;: Lambda expression.
(define (lambda? expr) (tagged-list? expr 'lambda))
(define (lambda-parameters expr) (cadr expr))
(define (lambda-body expr) (cddr expr))
(define (make-lambda parameters body)
(cons 'lambda (cons parameters body)))
(define (install-if-package)
(put 'eval 'if eval-if))
;: if expression:
(define (if? expr) (tagged-list? expr 'if))
(define (if-predicate expr) (cadr expr))
(define (if-consequent expr) (caddr expr))
(define (if-alternative expr)
(if (not (null? (cdddr expr)))
(cadddr expr)
'false))
(define (make-if predicate consequent alternative)
(list 'if predicate consequent alternative))
(define (install-begin-package)
(define (my-begin expr env)
(eval-sequence (begin-actions expr) env))
(put 'eval 'begin my-begin))
;: begin statement:
(define (begin? expr) (tagged-list? expr 'begin))
(define (begin-actions expr) (cdr expr))
(define (last-exp? seq) (null? (cdr seq)))
(define (first-exp seq) (car seq))
(define (rest-exps seq) (cdr seq))
;: convert sequence to expression:
;: for example:
(define (sequence->exp seq)
(cond ((null? seq) seq)
((last-exp? seq) (first-exp seq))
(else (make-begin seq))))
(define (make-begin seq) (cons 'begin seq))
;: (call <operator> <operands>)
;: (define (application? expr) (tagged-list? expr 'call))
;: (define (operator expr) (cadr expr))
;: (define (operands expr) (cddr expr))
;: "application" is the compound-expressions which are
;: not belong to any above expressions.
(define (install-application-package)
(define (my-application expr env)
(apply (eval (operator expr) env) (list-of-values (operands expr) env)))
(put 'eval 'call my-application))
(define (application? expr) (pair? expr))
(define (operator expr) (car expr))
(define (operands expr) (cdr expr))
(define (no-operands? ops) (null? ops))
(define (first-operand ops) (car ops))
(define (rest-operands ops) (cdr ops))
(define (install-cond-package)
(define (my-cond expr env)
(eval (cond->if expr) env))
(put 'eval 'cond my-cond))
;: Cond expression is a "derived expression" as syntax suger
;: which can be derived by "if" expressions .
(define (cond? expr) (tagged-list? expr 'cond))
(define (cond-clauses expr) (cdr expr))
(define (cond-else-clause? clause)
(eq? (cond-predicate clause) 'else))
;: The extend syntax: (<test> => <recipient>)
(define (cond-form2-clause? clause)
(eq? (car (cond-actions clause)) '=>))
;; only one action which has only one parameter.
(define (cond-form2-action clause)
(cadr (cond-actions clause)))
(define (cond-predicate clause) (car clause))
(define (cond-actions clause) (cdr clause))
(define (cond->if expr)
(expand-clauses (cond-clauses expr)))
(define (expand-clauses clauses)
(if (null? clauses)
'false ; no else clause
(let ((first (car clauses))
(rest (cdr clauses)))
(if (cond-else-clause? first)
(if (null? rest)
(sequence->exp (cond-actions first))
(error-report "ELSE clause isn't last -- COND->IF"
clauses))
(if (and (cond-form2-clause? first)
(cond-predicate first))
(list (cond-form2-action first) (cond-predicate first));
;(eval (cond-form2-action first) (cond-predicate first))
(make-if (cond-predicate first)
(sequence->exp (cond-actions first))
(expand-clauses rest)))))))
;: And statement: (and <expr1> <expr2>...)
;: Or statement: (or <expr1> <expr2>...)
(define (and? expr) (tagged-list? expr 'and))
(define (or? expr) (tagged-list? expr 'or))
(define (eval-and expr env)
(define (eval-and-caluses caluses)
(if (null? caluses)
'true
(if (eval (car caluses) env)
(eval-and-caluses (cdr caluses))
'false)))
(eval-and-caluses (cdr expr)))
(define (eval-or expr env)
(define (eval-or-caluses caluses)
(if (null? caluses)
'false
(if (eval (car caluses) env)
'true
(eval-or-caluses (cdr caluses)))))
(eval-or-caluses (cdr expr)))
;: "and" or "or" statement can alse be derived by "if" statement
;: like "cond".
(define (and->if expr)
(define (expand-and-caluses caluses)
(if (null? caluses)
'true
(make-if (car caluses)
(expand-and-caluses (cdr caluses))
'false)))
(expand-and-caluses (cdr expr)))
(define (or->if expr)
(define (expand-or-caluses caluses)
(if (null? caluses)
'false
(make-if (car caluses)
'true
(expand-or-caluses (cdr caluses)))))
(expand-or-caluses (cdr expr)))
(define (install-or-package)
(put 'eval 'or eval-or))
(define (install-and-package)
(put 'eval 'and eval-and))
;: let statement:
;: it can be derived by "lambda" statement .
;: "let*" is similar with "let" but it's variable-constraint
;: finished by the order from left to right.
(define (let*? expr) (tagged-list? expr 'let*))
(define (make-let parameters-and-exps body)
(cons 'let (cons parameters-and-exps body)))
(define (let*->nested-lets expr)
(define (translate caluses)
(if (null? caluses)
(let-body expr)
(make-let (list (car caluses))
(translate (cdr caluses)))))
(translate (let-vars-and-exps expr)))
(define (let? expr) (tagged-list? expr 'let))
(define (let-body expr) (cddr expr))
(define (let-vars-and-exps expr) (cadr expr))
(define (let-vars caluses)
(if (null? caluses)
'()
(cons (car (car caluses))
(let-vars (cdr caluses)))))
(define (let-exps caluses)
(if (null? caluses)
'()
(cons (cadr (car caluses))
(let-exps (cdr caluses)))))
(define (named-let? expr) (variable? (cadr expr)))
(define (let->combination expr)
(if (named-let? expr)
(make-begin (list (make-definition (cadr expr)
(let-vars (caddr expr))
(cdddr expr)))
(cons (cadr expr) (let-exps (caddr expr)))))
(cons (make-lambda (let-vars (let-vars-and-exps expr))
(let-body expr))
(let-exps (let-vars-and-exps expr))))
(define (install-let-package)
(define (my-let expr env)
(eval (let->combination expr) env))
(put 'eval 'let my-let))
;: while statement:
(define (while? expr) (tagged-list? expr 'while))
(define (while-predicate expr) (cadr expr))
(define (while-body expr) (cddr expr))
;(define (make-while predicate body)
; (cons 'while (cons predicate body)))
(define (while->combination expr)
(make-if (while-predicate expr)
(make-begin (append (while-body expr) (list expr)))
'true))
;: unless statement:
(define (unless? expr) (tagged-list? expr 'unless))
(define (unless-condition expr) (cadr expr))
(define (unless-usual-value expr) (caddr expr))
(define (unless-exceptional-value expr) (cadddr expr))
(define (unless->if expr)
(make-if (unless-condition expr)
(unless-exceptional-value expr)
(unless-usual-value expr)))
;: evaluator data structures
(define (true? x)
(not (eq? x false)))
(define (false? x)
(eq? x false))
;: scan-out-defines constructes a extra frame.
;: Design a way without constructing it:
;: To translate all internal definitations to lambda exprressions will create new problem
;: that which breaks the order of set!.
(define (scan-out-defines-optim proc-body)
(define (iter body define-caluses set-caluses expr-calues)
(if (null? body)
(cons define-caluses (append set-caluses expr-calues))
(let ((first-exp (car body)))
(if (definition? first-exp)
(iter (cdr body)
(append define-caluses (list (list 'define
(definition-variable first-exp)
'*unassigned*)))
(append set-caluses (list (make-assignment (definition-variable first-exp)
(definition-value first-exp))))
expr-calues)
(iter (cdr body)
define-caluses
set-caluses
(append expr-calues (list first-exp)))))))
(let ((items (iter proc-body '() '() '())))
(if (null? (car items))
proc-body
(append (car items) (cdr items)))))
(define (scan-out-defines proc-body)
(define (iter body let-caluses set-caluses expr-calues)
(if (null? body)
(cons let-caluses (append set-caluses expr-calues))
(let ((first-exp (car body)))
(if (definition? first-exp)
(iter (cdr body)
(append let-caluses (list (list (definition-variable first-exp)
'*unassigned*)))
(append set-caluses (list (make-assignment (definition-variable first-exp)
(definition-value first-exp))))
expr-calues)
(iter (cdr body)
let-caluses
set-caluses
(append expr-calues (list first-exp)))))))
(let ((items (iter proc-body '() '() '())))
(if (null? (car items))
proc-body
(list (make-let (car items) (cdr items))))))
;:
;: scan-out-defines can't match the analyzing_mceval.scm
;:
(define (make-procedure parameters body env)
(display-debug "make-procedure scan-out-defines: ")
(display-debug body)
(newline-debug)
(display-debug " after: ")
;(display-debug (scan-out-defines-optim body))
(newline-debug)
;(list 'procedure parameters (scan-out-defines-optim body) env))
;(list 'procedure parameters (scan-out-defines body) env))
(list 'procedure parameters body env))
;: compound-procedure:
(define (compound-procedure? p)
(tagged-list? p 'procedure))
(define (procedure-parameters p) (cadr p))
(define (procedure-body p) (caddr p))
(define (procedure-environment p) (cadddr p))
(define (primitive-procedure? proc)
(tagged-list? proc 'primitive))
(define (primitive-implementation proc) (cadr proc))