forked from bengioe/rogue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpand.scm
553 lines (474 loc) · 17.5 KB
/
expand.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
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
;;
;; Expand handle the compilation phase of macro expansion.
;; It recursively descend through the expression tree, evaluating macro on the
;; way. During the descend, it expands macro definition it encounters.
;;
;; Syntax analysis is being done during expansion. Since every bit of source
;; code pass through expansion and that expansion conserves data about
;; currently reachable definitions, it has been considered to be an ideal
;; phase to implement syntax analysis.
;; Analysis is being done inside each special-forms.
;;
(define (expand-error s . R)
(error_ s R))
(define (expand expression env)
(if (not (pair? expression))
;;(if (symbol? expression) ;; production version
(if #f ;; devel only version; comment accordingly
(let ((matching-def (assoc expression env)))
(cond
((not matching-def)
(expand-error (string-append "Undefined symbol "
(symbol->string expression) expression)))
((eq? 'macro (env-record-type matching-def))
;; If matching-def is a macro, it isn't at the beginning
;; of a list or it would have been expanded earlier. We
;; concluded that it must be a macro that is used as a
;; variable.
(expand-error (string-append "Macro "
(symbol->string expression)
" cannot be used as a variable") expression))
((eq? 'var (env-record-type matching-def))
expression)))
expression)
(let ((matching-macro (get-env-macro (car expression) env)))
;; If expression is a user-defined macro provided it hasn't been
;; overided by a local definition.
(if matching-macro
;;(if (matching-macro expression)
(expand (matching-macro expression) env);;)
;; Propagate expansion through primitive-forms.
(match expression
(('%toplevel . expr)
(expand-toplevel expr env))
(('%body expr)
;`(expanded-body ,expr))
;;(expand (expand-body `(%expanded-body ,@expr) env) env))
(expand-body expr env))
(('%lambda parameters ('%body exps))
(let ((new-env (foldl (flip push-env-var)
env
(if (symbol? parameters)
(list parameters)
(to-clean-list parameters)))))
`(%lambda
,parameters
,@(expand-body exps new-env))))
(('%lambda parameters . exps)
(let ((new-env (foldl (flip push-env-var)
env (if (symbol? parameters)
(list parameters)
(to-clean-list parameters)))))
`(%lambda
,parameters
,@(expand-body exps new-env))))
(('%set! ref) `(%set! ,ref))
(('%quote exp) `(%quote ,exp))
(('quote exp) `(quote ,exp))
(('%unquote symbol) `(%unquote ,symbol))
;((head . tail) where (member head ('let))
; ())
(exp-list
(begin
`(,@(map
(lambda (expression)
(expand expression env))
exp-list)))))))))
(define (expand-toplevel expression env)
(define (fetch-expressions expressions
defines-stack
expression-queue
env)
(if (null? expressions)
(make-toplevel expression-queue
defines-stack
env)
(match expressions
((('define (name . args) . expressions) . remaining-expressions)
(fetch-expressions
remaining-expressions
(cons `(define ,name (lambda ,args ,@expressions))
defines-stack)
expression-queue
env))
((('define name expression) . remaining-expressions)
(fetch-expressions
remaining-expressions
(cons (car expressions) defines-stack)
expression-queue
env))
((('define-type name . fields) . remaining-expressions)
(begin
(fetch-expressions
remaining-expressions
(append (expand-define-type name fields) defines-stack)
expression-queue
env)))
((('include path) . remaining-expressions)
(fetch-expressions
(append (read-whole-file path) remaining-expressions)
defines-stack
expression-queue
env))
((('define-macro (name . args) . expressions) . remaining-expressions)
(fetch-expressions
remaining-expressions
defines-stack
expression-queue
(push-env-macro
name
(eval `(lambda (expression)
(apply (lambda ,args ,@expressions) (cdr expression))))
env)))
((('define-macro name
('lambda args . expressions))
. remaining-expressions)
(fetch-expressions
remaining-expressions
defines-stack
expression-queue
(push-env-macro
name
(eval `(lambda (expression)
(apply (lambda ,args ,@expressions) (cdr expression))))
env)))
((('define-macro . _) . _) (expand-error "ill-formed macro" expressions))
((e . remaining-expressions )
(fetch-expressions
remaining-expressions
defines-stack
(append expression-queue (list e))
env))
(_ (expand-error "unexpected expression" expressions)))))
(define (make-toplevel expressions defines env)
`(%program
,@(if (null? defines)
`(,@(expand expressions env))
(let ((bindings
(reverse (map (lambda (e)
`(%set! ,(cadr e) ,(caddr e)))
defines)))
(declarations (map (lambda (e)
`(define-global ,(cadr e)))
defines)))
;; letrec expansion handles env defs.
(expand `(,@declarations ,@bindings ,@expressions) env)))))
(let ((res (fetch-expressions expression '() '() env)))
;;(pp res)
res))
(define (expand-define-type name fields)
(define symbol-append
(lambda syms
(string->symbol (apply string-append (map symbol->string syms)))))
(let ((type-sym (gensym 'user-type)))
`((define ,(symbol-append 'make- name) (lambda P (cons ',type-sym P)))
(define ,(symbol-append name '?) (lambda (x) (and (list? x)
(not (eq? (cdr x) '()))
(eq? (car x) ',type-sym))))
,@(map (lambda (field)
`(define ,(symbol-append name '- field)
(lambda (obj)
(list-ref obj
,(- (length fields)
-1
(length (member field fields)))))))
fields)
,@(map (lambda (field)
`(define ,(symbol-append name '- field '-set!)
(lambda (obj val)
(list-set! obj
,(- (length fields)
-1
(length (member field fields)))
val))))
fields))))
(define (expand-body expression env)
(define (fetch-defines expressions defines env)
(if (null? expressions)
(make-base-defines expressions defines env)
(match expressions
((('define (name . args) . expressions) . remaining-expressions)
(fetch-defines
remaining-expressions
(cons `(define ,name (lambda ,args ,@expressions))
defines)
env))
((('define name expression) . remaining-expressions)
(fetch-defines
remaining-expressions
(cons (car expressions) defines)
env))
((('define-macro (name . args) . expressions) . remaining-expressions)
(fetch-defines
remaining-expressions
defines
(push-env-macro
name
(eval `(lambda (expression)
(apply (lambda ,args ,@expressions) (cdr expression))))
env)))
((('define-macro name
('lambda args . expressions))
. remaining-expressions)
(fetch-defines
remaining-expressions
defines
(push-env-macro
name
(eval `(lambda (expression)
(apply (lambda ,args ,@expressions) (cdr expression))))
env)))
((('define-macro . _) . _) (expand-error "ill-formed macro" expressions))
(_ (make-base-defines expressions defines env)))))
(define (make-base-defines expressions defines env)
;;(display "base exp: ")(pp expressions)
;;(display "base defines:")(pp defines)
(if (null? defines)
(expand expressions env)
(let ((bindings (reverse (map (lambda (e)
`(,(cadr e) ,(caddr e)))
defines))))
;; return ((letrec...)) because expand-body must return a list of expressions
;; and not just a single expression
(expand `((letrec ,bindings ,@expressions)) env)))) ;; letrec expansion
;; handles env defs.
;;(trace make-base-defines)
;;(trace fetch-defines)
(fetch-defines expression '() env))
(define (expand-qq-u u)
(if (pair? u)
(expand-qq (car u) (cdr u))
(if (null? u)
'()
(expand-qq u '()))))
(define (expand-qq head rest)
(cond
((eq? head 'quasiquote)
(expand-qq (caar rest) (cdar rest)))
;;(cons 'list (expand-qq (caar rest) (cdar rest))))
((pair? head)
(cond
((eq? (car head) 'unquote)
`(%cons ,(cadr head) ,(expand-qq-u rest)))
((eq? (car head) 'unquote-splicing)
`(append ,(cadr head) ,(expand-qq-u rest)))
(else
(list '%cons
(expand-qq (car head) (cdr head))
(expand-qq-u rest)))))
(else
(cond
((symbol? head)
(if (eq? head 'unquote)
(car rest)
(if (pair? rest)
(list '%cons (list 'quote head)
(expand-qq-u rest))
(list '%cons (list 'quote head)
(if (symbol? rest)
(list 'quote rest)
rest)))))
(else
(list '%cons head (expand-qq-u rest)))))))
;; Base macro environment
;; |> lambda
;; |> let
;; |> let*
;; |> letrec
;; |> and
;; |> or
;; |> cond
;; |> quasiquote
(define environment
(list
(cons 'lambda
(make-env-record
'macro
(lambda (expression)
(match expression
((_ params . expressions)
where (or (parameter-list? params)
(expand-error "ill-formed parameters in lambda expression" expression))
`(%lambda ,params (%body ,expressions)))
(_ (expand-error "ill-formed lambda" expression))))))
(cons 'begin
(make-env-record
'macro
(lambda (expression) ;; must bypass lambda expansion
`((%lambda () ,@(cdr expression)))))) ;; to prevent the addition of
;; a %body statement
(cons 'case
(make-env-record
'macro
(lambda (expression)
(match expression
(('case operand ('else . block))
(let ((sym (gensym 'case-operand)))
`(begin ,@block)))
(('case operand (options . block) . rest)
(let ((sym (gensym 'case-operand)))
`(let ((,sym ,operand))
(if (member ,sym ',options)
(begin ,@block)
(%case ,sym ,@rest)))))
(_ (expand-error "ill-formed case" expression))))))
(cons '%case
(make-env-record
'macro
(lambda (expression)
(match expression
((_ operand ('else . block))
`(begin ,@block))
((_ operand (options . block) . rest)
`(if (member ,operand ',options)
(begin ,@block)
(%case ,operand ,@rest)))
((_ operand)
'())
(_ (expand-error "ill-formed case" expression))))))
(cons 'let
(make-env-record
'macro
(lambda (expression)
(match expression
((_ bindings . expressions)
where (or (bindings? bindings)
(expand-error "ill-formed bindings in let" expression))
(let ((binding-names (map car bindings))
(binding-expressions (map cadr bindings)))
`((lambda ,binding-names
%body ,expressions)
,@binding-expressions)))
(_ (expand-error "ill-formed let" expression))))))
(cons 'letrec
(make-env-record
'macro
(lambda (expression)
(match expression
((_ bindings . expressions)
where (or (bindings? bindings)
(expand-error "ill-formed bindings in letrec" expression))
(let ((new-bindings (map (lambda (x)
`(,(car x) 0))
bindings))
(new-values (map (lambda (x)
`(%set! ,(car x) ,(cadr x)))
bindings)))
`(let ,new-bindings ,@new-values ,@expressions)))
(_ (expand-error "ill-formed letrec" expression))))))
(cons 'let*
(make-env-record
'macro
(lambda (expression)
(match expression
((_ bindings . expressions)
where (or (bindings? bindings)
(expand-error "ill-formed bindings in let*" expression))
`(let (,(car bindings))
,@(if (null? (cdr bindings))
expressions
`((let* ,(cdr bindings) ,@expressions)))))
(_ (expand-error "ill-formed let*" expression))))))
(cons 'if
(make-env-record
'macro
(lambda (expression)
(match expression
((_ cond true)
`(%if ,cond ,true))
((_ cond true false)
`(%if ,cond ,true ,false))
(_ (expand-error "ill-formed if" expression))))))
(cons 'set!
(make-env-record
'macro
(lambda (expression)
(match expression
((_ sym value) where (or (symbol? sym)
(expand-error "set! should set a symbol not an expression" expression))
`(%set! ,sym ,value))
(_ (expand-error "ill-formed set!" expression))))))
(cons 'and
(make-env-record
'macro
(lambda (expression)
(match expression
((_) '#t)
((_ x) x)
((_ x . xs)
`(if ,x (and ,@xs) #f))
(_ (expand-error "ill-formed and" expression))))))
(cons 'or
(make-env-record
'macro
(lambda (expression)
(match expression
((_) '#f)
((_ x) x)
((_ x . xs)
(let ((temp (gensym)))
`(let ((,temp ,x))
(if ,temp ,temp (or ,@xs)))))
(_ (expand-error "ill-formed or" expression))))))
(cons 'cond
(make-env-record
'macro
(lambda (expression)
(match expression
((_ ('else . clauses))
`(begin ,@clauses))
((_ (test . clauses) . rest)
`(if ,test
(begin ,@clauses)
,@(if (= 0 (length rest))
'()
`((cond ,@rest)))))
(_ (expand-error "ill formed cond" expression))))))
(cons 'if
(make-env-record
'macro
(lambda (expression)
(match expression
((_ cnd then-clause else-clause)
`(%if ,cnd ,then-clause ,else-clause))
((_ cnd then-clause)
`(%if ,cnd ,then-clause))
(_ (expand-error "ill-formed if expression" expression))))))
(cons 'quasiquote
(make-env-record
'macro
(lambda (expression)
(if (pair? expression)
(expand-qq (car expression) (cdr expression))
(expand-qq expression '())))))
(cons 'set!
(make-env-record
'macro
(lambda (expression)
(match expression
((_ id val )
`(%set! ,id ,val))
(_ (expand-error "ill-formed set! expression" expression))))))
;; Default macro pattern
; (cons '
; (make-environment-record
; 'macro
; (lambda () ))
)) ;; closing environment definition
;; Syntax analysis function
(define (binding? binding)
(match binding
((var expr) (symbol? var))
(_ #f)))
(define (bindings? expressions)
(foldl (lambda (acc x)
(and (binding? x) acc))
#t
expressions))
(define (parameter-list? params)
(or (symbol? params)
(and (or (null? params) (pair? params))
(foldl (lambda (acc x)
(and (symbol? x) acc))
#t
(to-clean-list params)))))