forked from bldl/magnolisp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.rkt
507 lines (419 loc) · 14.8 KB
/
parse.rkt
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
#lang racket/base
#|
|#
(require "ir-ast.rkt" "app-util.rkt" "util.rkt"
racket/contract racket/dict racket/function racket/list
racket/match racket/pretty
syntax/id-table syntax/kerncase syntax/parse syntax/stx
(for-template racket/base "core.rkt"))
;;;
;;; debugging utilities
;;;
(define (print-stx-with-bindings stx)
(define lst (syntax->list stx))
(cond
(lst (for-each print-stx-with-bindings lst))
((identifier? stx) (writeln (list stx (identifier-binding stx 0))))
(else (writeln stx))))
(define (find-id-stx find-stx stx (msg #f))
(define (f stx)
(define lst (syntax->list stx))
(cond
(lst
(for-each f lst))
((identifier? stx)
(when (free-identifier=? find-stx stx)
(writeln (list (or msg (format "~a" (syntax->datum stx)))
stx (identifier-binding stx 0)))))))
(f stx))
(define (stx-binding-info stx)
(define p (syntax-e stx))
(and (pair? p)
(let ((id (car p)))
(and (identifier? id)
(identifier-binding id 0)))))
;;;
;;; annotation parsing
;;;
(define (parse-type-expr t-e)
(let loop ([stx t-e])
(kernel-syntax-case*/phase stx 0 (#%magnolisp)
[id
(identifier? #'id)
(syntaxed stx NameT #'id)]
[(if _ (#%plain-app #%magnolisp (quote f) p ... r) _)
(eq? 'fn (syntax-e #'f))
(let ()
(define p-stx-lst (syntax->list #'(p ...)))
(define p-ast-lst (map loop p-stx-lst))
(define r-ast (loop #'r))
(syntaxed stx FunT p-ast-lst r-ast))]
[(if _ (#%plain-app #%magnolisp (quote q)
(let-values (((tp) _) ...)
sub-t)) _)
(or (eq? 'exists (syntax-e #'q))
(eq? 'for-all (syntax-e #'q)))
(let ()
(define for-all? (eq? 'for-all (syntax-e #'q)))
(define tp-id-lst (syntax->list #'(tp ...)))
(define tp-ast-lst (for/list ((id tp-id-lst))
(syntaxed id NameT id)))
(define sub-ast (loop #'sub-t))
(syntaxed stx (if for-all? ForAllT ExistsT)
tp-ast-lst sub-ast))]
[(if _ (#%plain-app #%magnolisp (quote f) t p ...) _)
(and (eq? 'parameterized (syntax-e #'f))
(identifier? #'t))
(let ()
(define t-ast (loop #'t))
(define p-stx-lst (syntax->list #'(p ...)))
(define p-ast-lst (map loop p-stx-lst))
(syntaxed stx ParamT t-ast p-ast-lst))]
[(if _ (#%plain-app #%magnolisp (quote f)) _)
(eq? 'auto (syntax-e #'f))
(syntaxed stx AnyT)]
[_
(raise-language-error
#f "illegal type expression"
t-e stx)])))
(define (parse-anno-value anno-stx kind dat-stx)
(case kind
[(type)
(syntaxed anno-stx TypeAnno (parse-type-expr dat-stx))]
[else
;;(writeln dat-stx)
(kernel-syntax-case/phase dat-stx 0
[(quote dat)
(syntaxed anno-stx GenericAnno kind (syntax->datum #'dat))]
[(quote-syntax stx)
(syntaxed anno-stx GenericAnno kind #'stx)]
[_
(raise-language-error
#f "unsupported data in generic annotation"
anno-stx dat-stx)])]))
(define (parse-anno-expr let-stx anno-stx)
(kernel-syntax-case*/phase anno-stx 0 (#%magnolisp)
[(if _ (#%plain-app #%magnolisp (quote a) (quote k) dat) _)
(and (eq? 'anno (syntax-e #'a)) (identifier? #'k))
(let ((kind (syntax-e #'k)))
(parse-anno-value anno-stx (syntax-e #'k) #'dat))]
[_
(raise-language-error
#f "illegal syntax in annotation expression context"
let-stx anno-stx)]))
;;;
;;; parsing
;;;
;; Returns top-level defs in module.
(define-with-contract*
(-> syntax? immutable-id-table?)
(parse-defs-from-module modbeg-stx)
(define defs-in-mod (make-immutable-free-id-table))
(define (get-def-in-mod id)
(dict-ref defs-in-mod id #f))
(define (set-def-in-mod! id def)
(set! defs-in-mod (dict-set defs-in-mod id def)))
(define (redefinition id old-def new-stx)
(error 'parse-defs-from-module
"redefinition of ~a: ~a and ~a"
id (ast-anno-must old-def 'stx) new-stx))
(define (check-redefinition id new-stx)
(when-let old-def (get-def-in-mod id)
(redefinition id old-def new-stx)))
;; Returns initial annotations for definition 'stx' (binding
;; 'id-stx'). Returns AST node annotations as a (hash/c symbol?
;; any/c).
(define (mk-annos stx id-stx)
(hasheq 'stx stx))
(define (make-DefVar stx id-stx e-stx #:top? [top? #f])
(check-redefinition id-stx stx)
(define ast (parse-expr e-stx))
(define ann-h (mk-annos stx id-stx))
(define def (DefVar ann-h id-stx the-AnyT ast))
(when top?
(set-def-in-mod! id-stx def))
def)
(define (make-Param stx id-stx)
(check-redefinition id-stx stx)
(define ann-h (mk-annos stx id-stx))
(define def (Param ann-h id-stx the-AnyT))
def)
(define (make-Literal stx datum-stx)
(syntaxed stx Literal (syntax->datum datum-stx)))
;; Parses all the different kinds of Let expressions (`let`,
;; `letrec`, etc.). Any number of definition clauses, any number of
;; variables each.
(define (parse-let-expr stx binds-stx exprs-stx)
(define kind (syntax-e (car (syntax-e stx))))
(define body-stx-lst (syntax->list exprs-stx))
(define body-ast-lst (map parse-expr body-stx-lst))
(define bind-stx-lst (syntax->list binds-stx))
(define (wrap-in-let dv ast-lst)
(define as (hasheq 'stx stx 'let-kind kind))
(list (LetExpr as dv ast-lst)))
(define (wrap-in-let* dv-lst ast-lst)
(for/fold ([ast-lst ast-lst]) ([dv (reverse dv-lst)])
(wrap-in-let dv ast-lst)))
(define ast-lst
(for/fold ([ast-lst body-ast-lst]) ([i-e (reverse bind-stx-lst)])
;;(pretty-print `(BINDING ,(syntax->datum i-e)))
(kernel-syntax-case*/phase i-e 0 (values)
[((id ...) (#%plain-app values v ...))
(let ()
(define id-lst (syntax->list #'(id ...)))
(assert (andmap identifier? id-lst))
(define v-lst (syntax->list #'(v ...)))
(unless (= (length v-lst) (length id-lst))
(raise-language-error
#f
(format "expected ~a values" (length id-lst))
stx))
(define dv-lst
(map
(lambda (id-stx v-stx)
(make-DefVar i-e id-stx v-stx))
id-lst v-lst))
(wrap-in-let* dv-lst ast-lst))]
[(() stat)
(cons (parse-expr #'stat) ast-lst)]
[((id) v)
(identifier? #'id)
(let ()
(define dv (make-DefVar i-e #'id #'v))
(wrap-in-let dv ast-lst))]
[_
(raise-language-error
#f
(format "illegal syntax for a `~a` binding form" kind)
stx i-e
#:fields `(("binding"
,(or (stx-binding-info i-e) 'unbound))))])))
(if (= (length ast-lst) 1)
(first ast-lst)
(syntaxed stx SeqExpr ast-lst)))
(define (parse-module-begin stx)
(kernel-syntax-case/phase stx 0
((#%module-begin . bs)
(for-each
parse-module-level
(syntax->list #'bs)))
(_ (raise-language-error
#f "illegal syntax for #%module-begin" stx
#:fields `(("binding"
,(or (stx-binding-info stx) 'unbound)))))))
(define (for-non-cxx-target stx)
(define v (syntax-property stx 'for-target))
(and v (not (eq? v 'cxx))))
(define (parse-module-level stx)
(kernel-syntax-case*/phase stx 0 (#%magnolisp values)
(_
(for-non-cxx-target stx)
(void))
((begin-for-syntax . _)
(void))
;; This form binds nothing in Racket, but for C++ it does have
;; the effect of binding something, in the same way as the form
;; (define-values (id) e) would.
((define-values ()
(begin
(if _ (#%plain-app #%magnolisp (quote d) id e) _)
(#%plain-app values)))
(and (eq? 'declare (syntax-e #'d)) (identifier? #'id))
(make-DefVar stx #'id #'e #:top? #t))
;; We support (values v ...) in an expression position only
;; directly in binding form value expressions.
((define-values (id ...) (#%plain-app values v ...))
(let ()
(define id-lst (syntax->list #'(id ...)))
(assert (andmap identifier? id-lst))
(define v-lst (syntax->list #'(v ...)))
(unless (= (length v-lst) (length id-lst))
(raise-language-error
#f
(format "expected ~a values" (length id-lst))
stx (third (syntax->list stx))))
(for-each
(lambda (id-stx e-stx)
(make-DefVar stx id-stx e-stx #:top? #t))
id-lst v-lst)))
;; Must come after the ((define-values (id ...) (#%plain-app
;; values v ...)) pattern.
((define-values (id) e)
(identifier? #'id)
(make-DefVar stx #'id #'e #:top? #t))
((define-syntaxes . _)
(void))
((#%provide . _)
(void))
((#%require . _)
(void))
((module . _)
(void))
((module* . _)
(void))
;; Any module-level Racket expression is ignored. (These are as
;; for the Racket 'expr' non-terminal, and mostly in the same
;; order, too.) Note that we should only have (begin expr ...+)
;; forms, and not anything containing non-expressions, since
;; those should have gotten spliced into the module body.
((#%expression e)
(void))
(id
(identifier? #'id)
(void))
((#%plain-lambda . _)
(void))
((case-lambda . _)
(void))
((if c t e)
(void))
((begin . _)
(void))
((begin0 . _)
(void))
((let-values . _)
(void))
((letrec-values . _)
(void))
;; Not Racket core language, but may appear here.
((letrec-syntaxes+values . _)
(void))
((set! id expr)
(identifier? #'id)
(void))
((quote _)
(void))
((quote-syntax _)
(void))
((with-continuation-mark . _)
(void))
((#%plain-app . _)
(void))
((#%top . id) ;; should not appear in a module body
(identifier? #'id)
(void))
((#%variable-reference id)
(identifier? #'id)
(void))
((#%variable-reference (#%top . id))
(identifier? #'id)
(void))
((#%variable-reference)
(void))
(_
(raise-language-error
#f "illegal syntax at module level" stx
#:fields `(("binding"
,(or (stx-binding-info stx) 'unbound)))))))
(define (parse-expr stx)
(kernel-syntax-case*/phase stx 0 (call/ec values void #%magnolisp)
(_
(for-non-cxx-target stx)
(syntaxed stx RacketExpr))
((begin . bs)
(syntaxed stx SeqExpr
(map parse-expr
(syntax->list #'bs))))
((begin0 e . bs)
(let ()
(define e-ast (parse-expr #'e))
(define b-ast-lst (map parse-expr (syntax->list #'bs)))
(cond
((null? b-ast-lst)
e-ast)
(else
(syntaxed stx Begin0 (cons e-ast b-ast-lst))))))
;; These do appear as well.
((#%expression e)
(parse-expr #'e))
((#%plain-lambda (par ...) . exprs)
(let ()
(define par-id-lst (syntax->list #'(par ...)))
(define e-stx-lst (syntax->list #'exprs))
(define par-ast-lst
(map (lambda (id)
(make-Param stx id))
par-id-lst))
(define e-ast-lst (map parse-expr e-stx-lst))
(define e-ast
(case (length e-ast-lst)
((0) (assert #f))
((1) (first e-ast-lst))
(else (annoless SeqExpr e-ast-lst))))
(syntaxed stx Lambda par-ast-lst e-ast)))
((if _ t e)
(syntax-property stx 'if-target)
(let ()
(define name (syntax-property stx 'if-target))
(define ast
(if (eq? name 'cxx)
(parse-expr #'t)
(parse-expr #'e)))
ast))
((if _ (#%plain-app #%magnolisp (quote k)) _)
(eq? 'foreign-type (syntax-e #'k))
(syntaxed stx ForeignTypeExpr))
((if c t e)
(syntaxed stx IfExpr
(parse-expr #'c)
(parse-expr #'t)
(parse-expr #'e)))
((#%plain-app void . exprs)
(let ()
(define e-lst (map parse-expr (syntax->list #'exprs)))
(if (null? e-lst)
(syntaxed stx VoidExpr)
(syntaxed stx SeqExpr
(append e-lst (list (annoless VoidExpr)))))))
((#%plain-app values e)
(parse-expr #'e))
((#%plain-app values)
(syntaxed stx SeqExpr '()))
((#%plain-app p-expr . a-expr)
(let ()
(define f-ast (parse-expr #'p-expr))
(unless (Var? f-ast)
;; No first-class functions in Magnolisp.
(raise-syntax-error #f "expected identifier"
stx #'p-expr))
(ApplyExpr
(hasheq 'stx stx 'uid (gensym 'apply))
f-ast
(map
parse-expr
(syntax->list #'a-expr)))))
((let-values ([() (begin a (#%plain-app values))] ...) e)
(syntax-property stx 'annotate)
(let ()
(define a-stx-lst (syntax->list #'(a ...)))
(define a-ast-lst (for/list ((a-stx a-stx-lst))
(parse-anno-expr stx a-stx)))
(define e-ast (parse-expr #'e))
(syntaxed stx AnnoExpr a-ast-lst e-ast)))
((let-values binds . exprs)
(parse-let-expr stx #'binds #'exprs))
((letrec-values binds . exprs)
(parse-let-expr stx #'binds #'exprs))
((letrec-syntaxes+values _ binds . exprs)
(parse-let-expr stx #'binds #'exprs))
((set! id expr)
(identifier? #'id)
(syntaxed stx AssignExpr (parse-expr #'id) (parse-expr #'expr)))
((quote lit)
(make-Literal stx #'lit))
((#%top . id) ;; module-level variable (can prevent shadowing)
(identifier? #'id)
(syntaxed stx Var #'id))
(id
(identifier? #'id)
(syntaxed stx Var #'id))
((quote-syntax _) ;; syntax literal
;; Be permissive, as these sometimes appear without being referenced.
(syntaxed stx RacketExpr))
(_ (raise-language-error
#f "illegal syntax in expression context" stx
#:fields `(("binding"
,(or (stx-binding-info stx) 'unbound)))))))
;;(print-with-select-syntax-properties '(in-racket local-ec) modbeg-stx)
(parse-module-begin modbeg-stx)
defs-in-mod)