-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcompiler.sc
221 lines (193 loc) · 7.07 KB
/
compiler.sc
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
; MIT License
; Copyright guenchi (c) 2018 - 2019
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
(library (compiler)
(export
p1
p2
p3
p4
p5
p6
js->scheme
)
(import
(chezscheme)
(match match)
(core string)
(core condition))
(define p1
(lambda (lst)
(split*
(list->string
(let loop ((l lst))
(if (null? l)
'()
(let ((x (car l))(y (cdr l)))
(case x
((#\( #\) #\[ #\] #\{ #\} #\. #\; #\: #\+ #\- #\* #\/ #\= #\> #\<)
(cons #\space (cons x (cons #\space (loop y)))))
(#\, (cons #\space (loop y)))
(#\xA (loop y))
(else (cons x (loop y)))))))) #\space)))
(define p2
(lambda (lst)
(define f
(lambda (str)
(let ((x (string-ref str 0)))
(cond
((or/equal? x #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\0) (string->number str))
((> (string-length str) 1)(string->symbol str))
((or/equal? x #\( #\) #\[ #\] #\{ #\} #\. #\; #\: #\+ #\- #\* #\/ #\= #\> #\<)
x)
(else (string->symbol str))))))
(if (null? lst)
'()
(cons (f (car lst)) (p2 (cdr lst))))))
(define p3
(lambda (lst i j)
(let loop ((lst lst)(n i))
(if (> n j)
'()
(cons (list-ref lst n) (loop lst (+ 1 n)))))))
(define p4
(lambda (lst)
(let loop ((lst lst)(k '()))
(if (not (null? lst))
(let ((x (car lst))
(y (cdr lst))
(len (length lst)))
(cond
((char? x)
(cond
((equal? x #\;)
(let l ((n 1))
(if (or (= n len)
(or/equal? (list-ref lst n) #\} #\{ #\;))
(loop
(list-tail lst n)
(cons (p4 (p3 lst 1 (- n 1))) k))
(l (+ 1 n)))))
((equal? x #\})
(let l ((n 0)(m 0))
(if (equal? (list-ref lst n) #\{)
(if (or (= m len)
(or/equal? (list-ref lst m) #\} #\;))
(loop
(list-tail lst m)
(cons
(append
(p4 (p3 lst n (- m 1)))
(list (p4 (p3 lst 1 (- n 1))))
(cons x '()))
k))
(l n (+ 1 m)))
(l (+ 1 n)(+ 1 m)))))
(else (loop y (cons x k)))))
((symbol? x)
(cond
((equal? x 'else)
(loop y (cons x k)))
((equal? x 'return)
(loop y (cons x k)))
(else (loop y (cons x k)))))
(else (loop y (cons x k)))))
k))))
(define p5
(lambda (lst)
(define Var
(lambda (x)
(match x
(,v (guard (symbol? v)) v)
(,n (guard (number? n)) n)
((,(Expr -> e)) e))))
(define Expr
(lambda (x)
(match x
((,(Var -> x) #\+ ,(Var -> y)) `(+ ,x ,y))
((,(Var -> x) #\- ,(Var -> y)) `(- ,x ,y))
((,(Var -> x) #\* ,(Var -> y)) `(* ,x ,y))
((,(Var -> x) #\/ ,(Var -> y)) `(/ ,x ,y))
((,i #\= ,x)`(set! ,i ,x)))))
(define Exprs
(lambda (x)
(match x
((,(Expr -> e)) e)
((,(Expr -> e1) ,(Expr -> e2) ...)
`(begin ,e1 ,e2 ...)))))
(define Arg
(lambda (x)
(match x
(,v (guard (symbol? v)) v)
((,(Func -> f)) f))))
(define Test
(lambda (x)
(match x
(ture #t)
(false #f)
((,(Var -> v1) #\< ,(Var -> v2))
(< v1 v2))
((,(Var -> v1) #\> ,(Var -> v2))
(< v1 v2))
((,(Var -> v1) #\= ,(Var -> v2))
(equal? v1 v2)))))
(define Cond
(lambda (x)
(match x
((if #\( ,(Test -> t) #\) ,(Expr -> e))
`(if ,t ,e))
((if #\( ,(Test -> t) #\) #\{ ,(Exprs -> e) #\})
`(if ,t ,e))
((if #\( ,(Test -> t) #\) ,(Expr -> e1) else ,(Expr -> e2))
`(if ,t ,e1 ,e2))
((if #\( ,(Test -> t) #\) #\{ ,(Exprs -> e1) #\} else #\{ ,(Exprs -> e2) #\})
`(if ,t ,e1 ,e2)))))
(define Func
(lambda (x)
(match x
((function ,f #\( ,(Arg -> a) ... #\) #\{ ,(Exprs -> e) #\})
`(define (,f ,a ...) ,e))
((function #\( ,(Arg -> a) ... #\) #\{ ,(Exprs -> e) #\})
`(lambda (,a ...) ,e))
((,(Arg -> a) #\= #\> ,(Expr -> e))
`(lambda (,a) ,e))
((,(Arg -> a) #\= #\> #\{ ,(Exprs -> e) #\})
`(lambda (,a) ,e))
((#\( ,(Arg -> a ...) #\) #\= #\> ,(Expr -> e))
`(lambda (,a ...) ,e))
((#\( ,(Arg -> a ...) #\) #\= #\> #\{ ,(Exprs -> e) #\})
`(lambda (,a ...) ,e)))))
(match lst
((,i #\= ,x) `(set! ,i ,x))
((print #\( ,x #\)) `(display ,x))
((var ,i #\= ,x) `(define ,i ,x))
((let ,i #\= ,x) `(define ,i ,x))
((const ,i #\= ,x) `(define ,i ,x))
((,f #\( ,x ... #\)) `(,f ,x ...))
(,(Func -> f) f)
(,(Expr -> e) e))))
(define p6
(lambda (lst)
(let ((x (car lst))
(y (cdr lst)))
(if (null? y)
(cons (p5 x) '())
(cons (p5 x)(p6 y))))))
(define js->scheme
(lambda (lst)
(p6 (p4 (reverse (p2 (p1 lst)))))))
)