-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ss
288 lines (274 loc) · 9.25 KB
/
parser.ss
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
(define scheme-value?
(lambda (v)
#t))
(define-datatype expression expression?
(var-exp
(id symbol?))
(informal-lambda-exp
(id symbol?)
(body expression?))
(lambda-exp
(id list?)
(body expression?))
(app-exp
(rator expression?)
(rand list?))
(lit-exp
(datum (lambda (x)
(or (number? x) (string? x) (pair? x) (vector? x) (boolean? x) (symbol? x) (null? x)))))
(if-exp
(test-exp expression?)
(true-exp expression?)
(false-exp expression?))
(if-exp2
(test-exp expression?)
(true-exp expression?))
(let-exp
(vars (list-of symbol?))
(vals (list-of expression?))
(exprs (list-of expression?)))
(let*-exp
(vars (list-of symbol?))
(vals (list-of expression?))
(exprs (list-of expression?)))
(letrec-exp
(vars (list-of symbol?))
(vals (list-of expression?))
(exprs (list-of expression?)))
(set!-exp
(vars (list-of symbol?))
(vals (list-of expression?))
(exprs (list-of expression?)))
(begin-exp
(exps (list-of expression?)))
(dotted-lambda-exp
(id list?)
(sym symbol?)
(body expression?))
(cond-exp
(body (list-of list?)))
(and-exp
(body (list-of expression?)))
(or-exp
(body (list-of expression?)))
(case-exp
(test-val scheme-value?)
(bodies list?))
(while-exp
(test expression?)
(bodys (list-of expression?)))
(set-exp
(var symbol?)
(val expression?))
(define-exp
(sym symbol?)
(val expression?))
(call/cc-exp
(reciever expression?))
)
(define parse-expression
(lambda (datum)
(cond
[(symbol? datum) (var-exp datum)]
[(or (null? datum) (number? datum) (string? datum) (vector? datum) (boolean? datum) (null? datum)) (lit-exp datum)]
[(pair? datum)
(cond
[(not (list? datum))
(eopl:error 'parse-expression "Invalid list structure in ~s" datum)]
[(eqv? (car datum) 'quote)
(if (= (length datum) 2)
(lit-exp (cadr datum))
(eopl:error 'parse-expression "Invalid quote in ~s" datum))]
[(eqv? (car datum) 'let)
(if (validApplyingProdcedure? datum)
(let-exp (map car (cadr datum))
(map (lambda (expr)
(parse-expression (cadr expr)))
(cadr datum))
(map parse-expression (cddr datum))))]
[(eqv? (car datum) 'define) (define-exp (cadr datum) (parse-expression (caddr datum)))]
[(eqv? (car datum) 'let*)
(if (validApplyingProdcedure? datum)
(let*-exp (map car (cadr datum))
(map (lambda (expr)
(parse-expression (cadr expr)))
(cadr datum))
(map parse-expression (cddr datum))))]
[(eqv? (car datum) 'set!)
(set-exp (cadr datum)
(parse-expression (caddr datum)))]
[(eqv? (car datum) 'letrec)
(if (validApplyingProdcedure? datum)
(letrec-exp (map car (cadr datum))
(map (lambda (expr)
(parse-expression (cadr expr)))
(cadr datum))
(map parse-expression (cddr datum))))]
[(eqv? (car datum) 'if)
(cond
[(= (length datum) 3)
(apply if-exp2 (map parse-expression (cdr datum)))]
[(= (length datum) 4)
(apply if-exp (map parse-expression (cdr datum)))]
[else (eopl:error 'parse-expression "Invalid if in ~s" datum)])]
[(eqv? (car datum) 'cond)
(set! b datum)
(cond-exp (map list (map (lambda (e) (parse-expression (car e))) (cdr datum))
(map (lambda (e) (parse-expression (cadr e))) (cdr datum))))]
[(eqv? (car datum) 'lambda)
(if (validLambda? datum)
(cond
[(symbol? (cadr datum)) (informal-lambda-exp(cadr datum)
(begin-exp (map parse-expression (cddr datum))))]
[(list? (cadr datum)) (lambda-exp(cadr datum)
(begin-exp (map parse-expression (cddr datum))))]
[(pair? (cadr datum))
(let ([t (parse-parms (cadr datum))])
(dotted-lambda-exp (car t) (caadr t) (begin-exp (map parse-expression (cddr datum)))))]))]
[(eqv? (car datum) 'begin)
(begin-exp (map parse-expression (cdr datum)))]
[(eqv? (car datum) 'and)
(and-exp (map parse-expression (cdr datum)))]
[(eqv? (car datum) 'or)
(or-exp (map parse-expression (cdr datum)))]
[(eq? (car datum) 'case)
(case-exp (cadr datum)
(letrec ([helper (lambda (ls)
(if (null? (cdr ls))
(if (eq? (caar ls) 'else)
(list (list (caar ls) (parse-expression (cadar ls))))
(list (list (caar ls) (parse-expression (cadar ls)))))
(cons (list (caar ls) (parse-expression (cadar ls))) (helper (cdr ls)))))])
(helper (cddr datum))))]
[(eqv? (car datum) 'while)
(while-exp (parse-expression (cadr datum))
(map parse-expression (cddr datum)))]
[(eqv? (car datum) 'call/cc)
(call/cc-exp (parse-expression (cadr datum)))]
[else (app-exp
(parse-expression (car datum))
(map parse-expression (cdr datum)))])]
(else (eopl:error 'parse-expression
"Invalid concrete syntax ~s" datum)))))
(define unparse-expression
(lambda (exp)
(cases expression exp
[var-exp (id) id]
[informal-lambda-exp (id body)
(cons 'lambda
(cons id (map unparse-expression body)))]
[lambda-exp (id body)
(cons 'lambda
(cons id (map unparse-expression body)))]
[dotted-lambda-exp (id body)
(cons 'lambda
(cons id (map unparse-expression body)))]
[app-exp (rator rand)
(cons (unparse-expression rator)
(map unparse-expression rand))]
[lit-exp (datum)
(cond
((or (number? datum)
(string? datum)
(vector? datum)
(boolean? datum)) datum)
((or (pair? datum)
(symbol? datum)) (list 'quote datum))
((null? datum) ''()))]
[if-exp (test-exp true-exp false-exp)
(cons 'if
(map unparse-expression (list test-exp true-exp false-exp)))]
[if-exp2 (test-exp true-exp)
(cons 'if (map unparse-expression (list test-exp true-exp)))]
[let-exp (vars vals exprs)
(cons 'let (unparse-applying-exp vars vals exprs))]
[let*-exp (vars vals exprs)
(cons 'let* (unparse-applying-exp vars vals exprs))]
[letrec-exp (vars vals exprs)
(cons 'letrec (unparse-applying-exp vars vals exprs))]
[set!-exp (vars vals exprs)
(cons 'set! (unparse-applying-exp vars vals exprs))]
[begin-exp (exps)
(cons 'begin (map unparse-expression exps))]
[cond-exp (body)
(cons 'cond (map unparse-expression exps))]
[and-exp(body)]
[or-exp(body)]
[case-exp(test-val bodies)]
[while-exp(test bodys )]
[set-exp(var val)])))
(define unparse-applying-exp
(lambda (vars vals exprs)
(cons (map (lambda (var val)
(list var (unparse-expression val)))
vars vals)
(map unparse-expression exprs))))
; Validation Functios
(define validApplyingProdcedure?
(lambda (expr)
(set! a expr)
(cond
; Check to see that we have at least some form of let, assignments, and expressions
[(< (length expr) 3)
(eopl:error 'parse-expression "Incorrect length in ~s" expr)]
; Check that the argument list is a valid list
[(not (list? (cadr expr)))
(eopl:error 'parse-expression "Bad assignment list in ~s" expr)]
; Check that each assignment is a valid list
[(not (andmap list? (cadr expr)))
(eopl:error 'parse-expression "Bad assignment in list ~s" expr)]
; Check that each assignment is a valid list
[(not (andmap (lambda (assignment)
(= (length assignment) 2)) (cadr expr)))
(eopl:error 'parse-expression "An assignment has invalid length in ~s" expr)]
; Check to see if each assignment is a symbol
[(not (andmap (lambda (itm)
(symbol? (car itm))) (cadr expr)))
(eopl:error 'parse-expression "Each assignment must be to a symbol in ~s" expr)]
; Check for uniqueness
[(not (set? (map car (cadr expr))))
(eopl:error 'parse-expresssion "Each assignment must be to a unique symbol in ~s" expr)]
[else #t])))
(define validLambda?
(lambda (expr)
(cond
; Check to see that we have at least lambda, an argument, and expressions
[(< (length expr) 3)
(eopl:error 'parse-expression "Incorrect length in ~s" expr)]
; Make sure the arguments are valid
[(not (or (symbol? (cadr expr))
(null? (cadr expr))
(and (or (list? (cadr expr)) (pair? (cadr expr)))
(or (pair? (cadr expr)) (andmap symbol? (cadr expr))))))
(eopl:error 'parse-expression "Incorrect argument list in ~s" expr)]
; Make sure each variable only occurs once
[(if (list? (cadr expr))
(not (set? (cadr expr)))
#f)
(eopl:error 'parse-expression "Each variable may only occur once in ~s" expr)]
[else #t])))
; Helper Functions
(define parse-parms
(lambda (ls)
(if (symbol? ls)
ls
(let ([t (parse-parms (cdr ls))])
(if (symbol? t)
(cons (list (car ls)) (list (list t)))
(cons (cons (car ls) (car t)) (cdr t)))))))
(define set?
(lambda (l)
(if (null? l)
#t
(let set?? ((l1 (list (car l))) (l2 (cdr l)))
(cond
((null? l2) #t)
((contains? l1 (car l2)) #f)
(else (set?? (cons (car l2) l1) (cdr l2))))))))
(define contains?
(lambda (l n)
(if (null? l)
#f
(if (equal? (car l) n)
#t
(contains? (cdr l) n)))))