forked from LuKuangChen/stacker-on-racket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.rkt
156 lines (154 loc) · 5.13 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
#lang racket
(provide parse)
(require "io.rkt")
(require syntax/parse)
(require (only-in plait pair some none))
(define-syntax-class constant
(pattern x:string)
(pattern x:number)
(pattern x:boolean)
(pattern x:char)
(pattern #(c:literal ...))
(pattern ((~datum quote) #(c:literal ...)))
(pattern ((~datum quote) (c:literal ...))))
(define-syntax-class literal
(pattern x:string)
(pattern x:number)
(pattern x:boolean)
(pattern x:char)
(pattern #(c:literal ...))
(pattern #(c:literal ...))
(pattern (c:literal ...)))
(define-syntax-class d
(pattern ((~datum define) x:identifier e:e))
(pattern ((~datum define) (x1:identifier x2:identifier ...) d1:d ... e1:e ... e2:e)))
(define-syntax-class e
(pattern x:identifier)
(pattern ((~datum lambda) (x:identifier ...) d:d ... e1:e ... e2:e))
(pattern ((~datum λ) (x:identifier ...) d:d ... e1:e ... e2:e))
(pattern (e1:e e2:e ...))
(pattern ((~datum let) ([x:identifier e1:e] ...) d:d ... e2:e ... e3:e))
(pattern ((~datum let*) ([x:identifier e1:e] ...) d:d ... e2:e ... e3:e))
(pattern ((~datum letrec) ([x:identifier e1:e] ...) d:d ... e2:e ... e3:e))
(pattern ((~datum begin) e1:e ... e2:e))
(pattern ((~datum set!) x:identifier e))
(pattern ((~datum if) e1:e e2:e e3:e))
(pattern ((~datum cond) [cnd:e when-cond:e] ... [(~datum else) when-else:e]))
(pattern ((~datum cond) [cnd:e when-cond:e] ...))
(pattern c:constant))
(define-syntax-class p
(pattern (d:d ... e:e ...)))
(define (parse prog)
(syntax-parse prog
[(d:d ... e:e ...)
(program (parse-d* #'(d ...))
(parse-e* #'(e ...)))]))
(define (parse-x* x*) (map parse-x (syntax-e x*)))
(define (parse-x x) (syntax->datum x))
(define (parse-e* expr*)
(map parse-e (syntax-e expr*)))
(define (parse-x&e* x&e*)
(map parse-x&e (syntax-e x&e*)))
(define (parse-e&e* e&e*)
(map parse-e&e (syntax-e e&e*)))
(define (parse-x&e x&e)
(syntax-parse x&e
[[x:id e:e]
(bind (parse-x #'x)
(parse-e #'e))]))
(define (parse-e&e e&e)
(syntax-parse e&e
[[e1:e e2:e]
(pair (parse-e #'e1)
(parse-e #'e2))]))
(define (parse-d* d)
(map parse-d (syntax-e d)))
(define (parse-d def)
(syntax-parse def
[((~datum define) x:id e:e)
(d-var (parse-x #'x) (parse-e #'e))]
[((~datum define) (x1:identifier x2:identifier ...) d:d ... e1:e ... e2:e)
(d-fun (parse-x #'x1)
(parse-x* #'(x2 ...))
(parse-d* #'(d ...))
(parse-e* #'(e1 ...))
(parse-e #'e2))]))
(define (parse-e expr)
(syntax-parse expr
[((~datum lambda) (x:identifier ...) d:d ... e1:e ... e2:e)
(e-fun (parse-x* #'(x ...))
(parse-d* #'(d ...))
(parse-e* #'(e1 ...))
(parse-e #'e2))]
[((~datum λ) (x:identifier ...) d:d ... e1:e ... e2:e)
(e-fun (parse-x* #'(x ...))
(parse-d* #'(d ...))
(parse-e* #'(e1 ...))
(parse-e #'e2))]
[((~datum let) ([x:identifier e1:e] ...) d:d ... e2:e ... e3:e)
(e-let (parse-x&e* #'([x e1] ...))
(parse-d* #'(d ...))
(parse-e* #'(e2 ...))
(parse-e #'e3))]
[((~datum let*) ([x:identifier e1:e] ...) d:d ... e2:e ... e3:e)
(e-let* (parse-x&e* #'([x e1] ...))
(parse-d* #'(d ...))
(parse-e* #'(e2 ...))
(parse-e #'e3))]
[((~datum letrec) ([x:identifier e1:e] ...) d:d ... e2:e ... e3:e)
(e-letrec (parse-x&e* #'([x e1] ...))
(parse-d* #'(d ...))
(parse-e* #'(e2 ...))
(parse-e #'e3))]
[((~datum begin) e1:e ... e2:e)
(e-begin (parse-e* #'(e1 ...))
(parse-e #'e2))]
[((~datum set!) x:identifier e1:e)
(e-set! (parse-x #'x) (parse-e #'e1))]
[((~datum if) e1:e e2:e e3:e)
(e-if (parse-e #'e1) (parse-e #'e2) (parse-e #'e3))]
[((~datum cond) [cnd:e when-cond:e] ... [(~datum else) when-else:e])
(e-cond (parse-e&e* #'([cnd when-cond] ...))
(some (parse-e #'when-else)))]
[((~datum cond) [cnd:e when-cond:e] ...)
(e-cond (parse-e&e* #'([cnd when-cond] ...))
(none))]
[((~datum quote) x:id)
(e-con (parse-con #''x))]
[c:constant
(e-con (parse-con #'c))]
[(e1:e e2:e ...)
(e-app (parse-e #'e1)
(parse-e* #'(e2 ...)))]
[x:identifier
(e-var (syntax->datum #'x))]))
(define (parse-con con)
(syntax-parse con
[x:number
(c-num (syntax-e #'x))]
[x:boolean
(c-bool (syntax-e #'x))]
[x:char
(c-char (syntax-e #'x))]
[x:string
(c-str (syntax-e #'x))]
[((~datum quote) (x:literal ...))
(c-list (map parse-literal (syntax-e #'(x ...))))]
[((~datum quote) #(x:literal ...))
(c-vec (map parse-literal (syntax-e #'(x ...))))]
[#(x:literal ...)
(c-vec (map parse-literal (syntax-e #'(x ...))))]))
(define (parse-literal con)
(syntax-parse con
[x:number
(c-num (syntax-e #'x))]
[x:boolean
(c-bool (syntax-e #'x))]
[x:char
(c-char (syntax-e #'x))]
[x:string
(c-str (syntax-e #'x))]
[#(x:literal ...)
(c-vec (map parse-literal (syntax-e #'(x ...))))]
[(x:literal ...)
(c-list (map parse-literal (syntax-e #'(x ...))))]))