-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtmp
More file actions
305 lines (289 loc) · 10 KB
/
Copy pathtmp
File metadata and controls
305 lines (289 loc) · 10 KB
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
#lang racket
;(provide void? lambda? prim? define? begin? if? set? let? <? >? =? eq-exp?)
; tagged-list? : symbol value -> boolean
(define (tagged-list? tag l)
(and (pair? l)
(eq? tag (car l))))
(define (void? exp)
(eq? 'void (car exp)))
(define (lambda? exp)
(or (tagged-list? 'lambda exp) (tagged-list? 'λ exp)))
(define (prim? exp)
(or (tagged-list? '+ exp)
(tagged-list? '- exp)
(tagged-list? '* exp)
(tagged-list? '/ exp)))
(define (define? exp)
(tagged-list? 'define exp))
(define (begin? exp)
(tagged-list? 'begin exp))
(define (if? exp)
(tagged-list? 'if exp))
(define (set? exp)
(tagged-list? 'set exp))
(define (let? exp)
(tagged-list? 'let exp))
(define (<? exp)
(tagged-list? '< exp))
(define (>? exp)
(tagged-list? '> exp))
(define (=? exp)
(tagged-list? '= exp))
(define (eq-exp? exp)
(tagged-list? 'equal exp))
(define main-list (list))
(define var-list (list))
(define func-list (list))
;构造函数参数
(define (gen-parm exp)
(let ((ret ""))
(if (= (length exp) 1)
(string-replace ret ret (string-append ret (deduce (car exp)) ")"))
(string-replace ret ret (string-append ret
(deduce (car exp))
", "
(gen-parm (cdr exp)))))))
;F tmp;[]{tmp();}();
(define (constructor-run exp)
(list (string-append "[] {"
(symbol->string (car exp))
" tmp("
(gen-parm (cdr exp))
";"
"return tmp();"
"}();")))
;f/(f a b)
(define (gen-exec exp)
(deduce exp))
;todo
(define (emit line)
(display line)
(newline))
(define (tap) (display " "))
(define (emit4 line)
(begin
(tap)
(display line)
(newline)))
(define (func? exp)
(pair? (cadr exp)))
(define (func-parm-length exp)
(length exp))
;所有define的结束点********************************************
(define (get-ret exp)
(cond
((lambda? exp) (string-append "return [=]("
(gen-lambda-parm (cadr exp))
"{"
(get-ret (caddr exp))
"};"
))
((boolean? exp) (if exp "return true;" "return false;"))
((char? exp) (string-append "return " (~a exp) ";"))
((symbol? exp) (string-append "return " (~a exp) ";"))
((number? exp) (string-append "return " (~a exp) ";"))
((string? exp) (string-append "return " (~a exp) ";"))
((prim? exp)
(string-append
"return "
(cond
((tagged-list? '+ exp) "sum(")
((tagged-list? '- exp) "sub(")
((tagged-list? '* exp) "mul(")
((tagged-list? '/ exp) "div("))
(gen-parm (cdr exp))
";"))
((if? exp)
(string-append
(cond
((or (<? (cadr exp)) (>? (cadr exp)))
(string-append "if("
(~a (if (or (symbol? (list-ref (cadr exp) 1)) (number? (list-ref (cadr exp) 1)))
(list-ref (cadr exp) 1)
(deduce (list-ref (cadr exp) 1))))
(~a (list-ref (cadr exp) 0))
(~a (if (or (symbol? (list-ref (cadr exp) 2)) (number? (list-ref (cadr exp) 2)))
(list-ref (cadr exp) 2)
(deduce (list-ref (cadr exp) 2))))
")"))
((=? (cadr exp))
(string-append "if("
(~a (if (or (symbol? (list-ref (cadr exp) 1)) (number? (list-ref (cadr exp) 1)))
(list-ref (cadr exp) 1)
(deduce (list-ref (cadr exp) 1))))
"=="
(~a (if (or (symbol? (list-ref (cadr exp) 2)) (number? (list-ref (cadr exp) 2)))
(list-ref (cadr exp) 2)
(deduce (list-ref (cadr exp) 2))))
")"))
(else
(string-append "if(" (~a (deduce (cadr exp))) ")" )))
"\n{\n"
(get-ret (list-ref exp 2)) "\n}\n"
"else\n{\n"
(get-ret (list-ref exp 3)) "\n}\n"))
((void? exp) "return;")
((pair? exp)
(string-append
"return "
(deduce exp)
";"))
(else
(error "ret ERROR"))
))
(define (gen-lambda-parm exp)
(let ((ret ""))
(if (= (length exp) 1)
(string-replace ret ret (string-append ret "auto " (symbol->string (car exp)) ")"))
(string-replace ret ret (string-append ret
"auto "
(symbol->string (car exp))
", "
(gen-lambda-parm (cdr exp)))))))
;生成表达式
(define (deduce exp)
(cond
((if? exp)
(string-append
(cond
((or (<? (cadr exp)) (>? (cadr exp)))
(string-append "("
(~a (if (or (symbol? (list-ref (cadr exp) 1)) (number? (list-ref (cadr exp) 1)))
(list-ref (cadr exp) 1)
(deduce (list-ref (cadr exp) 1))))
(~a (list-ref (cadr exp) 0))
(~a (if (or (symbol? (list-ref (cadr exp) 2)) (number? (list-ref (cadr exp) 2)))
(list-ref (cadr exp) 2)
(deduce (list-ref (cadr exp) 2))))
")?"))
((=? (cadr exp))
(string-append "("
(~a (if (or (symbol? (list-ref (cadr exp) 1)) (number? (list-ref (cadr exp) 1)))
(list-ref (cadr exp) 1)
(deduce (list-ref (cadr exp) 1))))
"=="
(~a (if (or (symbol? (list-ref (cadr exp) 2)) (number? (list-ref (cadr exp) 2)))
(list-ref (cadr exp) 2)
(deduce (list-ref (cadr exp) 2))))
")?"))
(else
(string-append "(" (~a (deduce (cadr exp))) ")?" )))
"\n{\n("
(get-ret (list-ref exp 2)) ")\n}\n:("
(get-ret (list-ref exp 3)) ")\n}\n"))
((lambda? exp) (string-append "[=]("
(gen-lambda-parm (cadr exp))
"{"
(get-ret (caddr exp))
"}"
))
((prim? exp)
(string-append
(cond
((tagged-list? '+ exp) "sum(")
((tagged-list? '- exp) "sub(")
((tagged-list? '* exp) "mul(")
((tagged-list? '/ exp) "div("))
(gen-parm (cdr exp))))
((pair? exp)
(string-append (deduce (car exp))
"("
(gen-parm (cdr exp))))
((boolean? exp) (if exp "true" "false"))
((char? exp) (~a exp))
((symbol? exp)
(if (member exp var-list)
(string-append (~a exp) "()")
(~a exp)))
((number? exp) (~a exp))
((string? exp) (~a exp))
))
(define (gen-template-line x y);default x 0 生成template<...>
(if (= x 0)
(void)
(cond
((= y 0)
(begin (display "template<") (gen-template-line x 1)))
((= x y)
(begin (display "typename T") (printf "~a" y) (display ">")) (newline))
(else
(begin (display "typename T") (printf "~a" y) (display ", ") (gen-template-line x (+ y 1)))))))
(define (gen-parm-val exp y);default x 1生成 T1 x;T2 y;(原函数的参数)
(cond
((= (func-parm-length exp) 0)
(void))
((= (func-parm-length exp) y)
(begin (tap) (printf "Rkt_Data ~a;" (car (list-ref exp y))) (newline)))
(else
(begin (tap) (printf "Rkt_Data ~a;" (car (list-ref exp y))) (newline) (gen-parm-val exp (+ 1 y))))))
(define (gen-constructor exp);生成构造函数
(begin
(display " ")
(printf "~a()" (car (car exp)))
(if (tagged-list? 'let (cadr exp))
(begin
(emit "{")
(for ([i (cadr (cadr exp))])
(for ([i (func-parm-length exp)])
(if (= i (- (func-parm-length exp) 1))
(printf "T~a _t~a):" (+ i 1) (+ i 1))
(printf "T~a _t~a, " (+ i 1) (+ i 1))))
(if (= (func-parm-length exp) 0)
(emit "){}")
(void))
(for ([i (func-parm-length exp)])
(if (= i (- (func-parm-length exp) 1))
(printf "~a(_t~a){}" (list-ref exp (+ i 1)) (+ i 1))
(printf "~a(_t~a), " (list-ref exp (+ i 1)) (+ i 1))))
(newline)))
;(define (gen-closure exp)
; (if (tagged-list? 'let exp)
; (begin
(define (gen-func-content exp);生成函数体
(begin
(emit4 "auto operator()()")
(emit4 "{")
(emit (get-ret exp))
(emit4 "}")))
; define->class
(define (gen-func exp)
(begin
(display "class ")
(printf "~a" (car (car exp)))(newline)
(emit "{")
(gen-parm-val (cadr (cadr exp)) 1)
(emit "public:")
(gen-constructor exp)
(define (gen-symbol exp)
(begin
(display "class ")
(printf "~a" (car exp))(newline)
(emit "{")
(emit "public:")
(gen-func-content (cadr exp))
(printf "}~a;" (car exp))
(newline)))
(define (put-main)
(begin
(emit "int main()")
(emit "{")
(for/list ([i main-list])
(printf "if (!is_void<decltype(~a)>::value) cout << ~a << '\\n';\n" i i))
(emit "return 0;")
(emit "}")))
; 只支持两类:define和执行define过的过程
(define (trans-core exp)
(cond
((define? exp)
(if (func? exp)
(begin (set! func-list (append func-list (list (car (cadr exp))))) (gen-func (cdr exp)))
(begin (set! var-list (append var-list (list (cadr exp))))(gen-symbol (cdr exp)))))
(else
(set! main-list (append main-list (list (gen-exec exp)))))))
(define (rkt2cpp)
(let ((prog (read)))
(if (eq? prog eof)
(put-main)
(begin (trans-core prog) (rkt2cpp)))))
(emit "#include \"rkt2cpp.h\"")
(rkt2cpp)