-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsix-convert.scm
387 lines (322 loc) · 11.1 KB
/
six-convert.scm
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
;;;============================================================================
;;; File: "six-convert.scm"
;;; Copyright (c) 2020-2021 by Marc Feeley, All Rights Reserved.
;;;============================================================================
(define-type conversion-ctx
operators
parameters
globals
literal
unsupported)
(define python-operators
(list->table
'(
;; _____precedence
;; / ____associativity (0=LR, 1=RL, 2=not assoc.)
;; / / ___operand count (-1 for postfix unary operator)
;; / / / __target operator
;; / / / /
(six.prefix 0 0)
(six.identifier 0 0)
(six.literal 0 0)
(six.list 0 0)
(six.null 0 0)
(six.index 1 0)
(six.call 1 0)
(six.dot 1 0)
(six.x**y 3 1 2 "**") ;; note: RL associative
(six.+x 4 1 1 "+") ;; note: RL associative
(six.-x 4 1 1 "-")
(six.~x 4 1 1 "~")
(six.x*y 5 0 2 "*")
(six.x@y 5 0 2 "@")
(six.x/y 5 0 2 "/")
(six.x//y 5 0 2 "//")
(six.x%y 5 0 2 "%")
(six.x+y 6 0 2 "+")
(six.x-y 6 0 2 "-")
(six.x<<y 7 0 2 "<<")
(six.x>>y 7 0 2 ">>")
(six.x&y 8 0 2 "&")
(six.x^y 9 0 2 "^")
(|six.x\|y| 10 0 2 "|")
(six.x<y 11 2 2 "<") ;; note: not associative
(six.x<=y 11 2 2 "<=")
(six.x>y 11 2 2 ">")
(six.x>=y 11 2 2 ">=")
(six.x==y 11 2 2 "==")
(six.x!=y 11 2 2 "!=")
(six.xiny 11 2 2 " in ")
(six.xisy 11 2 2 " is ")
(six.notx 12 1 1 "not ") ;; note: RL associative
(six.xandy 13 0 2 " and ")
(six.xory 14 0 2 " or "))))
(define javascript-operators
(list->table
'(
;; _____precedence
;; / ____associativity (0=LR, 1=RL, 2=not assoc.)
;; / / ___operand count (-1 for postfix unary operator)
;; / / / __target operator
;; / / / /
(six.prefix 0 0)
(six.identifier 0 0)
(six.literal 0 0)
(six.list 0 0)
(six.null 0 0)
(six.index 1 0)
(six.call 1 0)
(six.dot 1 0)
;; (six.x++ 3 0 -1 "++")
;; (six.x-- 3 0 -1 "--")
(six.+x 4 1 1 "+") ;; note: RL associative
(six.-x 4 1 1 "-")
(six.~x 4 1 1 "~")
(six.++x 4 1 1 "++")
(six.--x 4 1 1 "--")
(six.x**y 5 1 2 "**") ;; note: RL associative
(six.x*y 6 0 2 "*")
(six.x/y 6 0 2 "/")
(six.x%y 6 0 2 "%")
(six.x+y 7 0 2 "+")
(six.x-y 7 0 2 "-")
(six.x<<y 8 0 2 "<<")
(six.x>>y 8 0 2 ">>")
(six.x>>>y 8 0 2 ">>>")
(six.x<y 9 0 2 "<")
(six.x<=y 9 0 2 "<=")
(six.x>y 9 0 2 ">")
(six.x>=y 9 0 2 ">=")
(six.xiny 9 0 2 " in ")
(six.x==y 10 0 2 "==")
(six.x!=y 10 0 2 "!=")
(six.x===y 10 0 2 "===")
(six.x!==y 10 0 2 "!==")
(six.x&y 11 0 2 "&")
(six.x^y 12 0 2 "^")
(|six.x\|y| 13 0 2 "|")
(six.x&&y 14 0 2 "&&")
(|six.x\|\|y| 15 0 2 "||")
;; (six.x?y:z 17 1) ;; note: RL associative
(six.x=y 18 1 2 "=") ;; note: RL associative
(six.x+=y 18 1 2 "+=")
(six.x-=y 18 1 2 "-=")
(six.x**=y 18 1 2 "**=")
(six.x*=y 18 1 2 "*=")
(six.x/=y 18 1 2 "/=")
(six.x%=y 18 1 2 "%=")
(six.x<<=y 18 1 2 "<<=")
(six.x>>=y 18 1 2 ">>=")
(six.x>>>=y 18 1 2 ">>>=")
(six.x&=y 18 1 2 "&=")
(six.x^=y 18 1 2 "^=")
(|six.x\|=y| 18 1 2 "|=")
(six.x&&=y 18 1 2 "&&=")
(six.x||=y 18 1 2 "||=")
;; (six.x,=y 20 0 2 ",")
)))
(define (six->python ast-src)
(define (convert-literal cctx src)
(##deconstruct-call
src
2
(lambda (val-src)
(let ((val (##source-strip val-src)))
(cond ((number? val)
(number->string val)) ;; TODO: use Python number syntax
((boolean? val)
(if val "True" "False"))
((string? val)
(object->string val)) ;; TODO: use Python string syntax
(else
(unsupported cctx src)))))))
(define (unsupported cctx src)
(##raise-expression-parsing-exception
'ill-formed-expression
src))
(define cctx
(make-conversion-ctx
python-operators
'()
(make-table)
convert-literal
unsupported))
(let ((target-expr (six-expression-to-infix cctx ast-src)))
(list (reverse (conversion-ctx-parameters cctx))
(flatten-string (list "return " target-expr)))))
(define (six->javascript ast-src)
(define (convert-literal cctx src)
(##deconstruct-call
src
2
(lambda (val-src)
(let ((val (##source-strip val-src)))
(cond ((number? val)
(number->string val)) ;; TODO: use Python number syntax
((boolean? val)
(if val "true" "false"))
((string? val)
(object->string val)) ;; TODO: use Python string syntax
(else
(unsupported cctx src)))))))
(define (unsupported cctx src)
(##raise-expression-parsing-exception
'ill-formed-expression
src))
(define cctx
(make-conversion-ctx
javascript-operators
'()
(make-table)
convert-literal
unsupported))
(let ((target-expr (six-expression-to-infix cctx ast-src)))
(list "function ___fn("
(append-strings (map car (conversion-ctx-parameters cctx)) ",")
") { return "
target-expr
"; }")))
(define (six-expression-to-infix cctx ast-src)
(define (unsupported ast-src)
((conversion-ctx-unsupported cctx) cctx ast-src))
(define (precedence op) (car op))
(define (associativity op) (cadr op))
(define (parens-optional? pos inner-op outer-op)
(let ((inner-prec (precedence inner-op))
(outer-prec (precedence outer-op)))
(or (< inner-prec outer-prec)
(and (= inner-prec outer-prec)
(let ((inner-assoc (associativity inner-op)))
(and (< inner-assoc 2) ;; 2 = non associative
(= inner-assoc pos)))))))
(define (infix ast-src pos outer-op)
(let ((ast (##source-strip ast-src)))
(if (not (pair? ast))
(unsupported ast-src)
(let* ((head
(##source-strip (car ast)))
(rest
(cdr ast))
(inner-op
(table-ref (conversion-ctx-operators cctx) head #f)))
(if (not inner-op)
(unsupported ast-src)
(let* ((x
(cddr inner-op))
(expr
(if (not (pair? x))
(infix* ast-src pos inner-op)
(let ((operand-count (car x))
(target-op (cadr x)))
(case (length rest)
((0)
target-op)
((1)
(list target-op
(infix (car rest) 1 inner-op)))
((2)
(list (infix (car rest) 0 inner-op)
target-op
(infix (cadr rest) 1 inner-op)))
((3)
...) ;; TODO: ternary operator
(else
(unsupported ast)))))))
(if (parens-optional? pos inner-op outer-op)
expr
(list "(" expr ")"))))))))
(define (infix* ast-src pos inner-op)
(let ((ast (##source-strip ast-src)))
(case (##source-strip (car ast))
((six.prefix)
(##deconstruct-call
ast-src
2
(lambda (expr-src)
(let* ((params
(conversion-ctx-parameters cctx))
(param-id
(string-append "___"
(number->string (+ 1 (length params))))))
(conversion-ctx-parameters-set!
cctx
(cons (cons param-id expr-src)
params))
param-id))))
((six.identifier)
(##deconstruct-call
ast-src
2
(lambda (ident-src)
(let* ((ident-sym (##source-strip ident-src))
(ident (symbol->string ident-sym)))
(table-set! (conversion-ctx-globals cctx) ident-sym ident)
ident))))
((six.literal)
((conversion-ctx-literal cctx) cctx ast-src))
((six.list six.null)
(let loop ((ast-src ast-src) (rev-elems '()))
(let ((ast (##source-strip ast-src)))
(case (##source-strip (car ast))
((six.list)
(##deconstruct-call
ast-src
3
(lambda (head-src tail-src)
(loop tail-src (cons (cvt head-src) rev-elems)))))
((six.null)
(##deconstruct-call
ast-src
0
(lambda ()
(list "["
(comma-separated (reverse rev-elems))
"]"))))
(else
(unsupported ast-src))))))
((six.index)
(##deconstruct-call
ast-src
3
(lambda (obj-src index-src)
(list (infix obj-src 0 inner-op)
"["
(cvt index-src)
"]"))))
((six.call)
(##deconstruct-call
ast-src
-2
(lambda (fn-src . args-src)
(let ((args (map cvt args-src)))
(list (infix fn-src 0 inner-op)
"("
(comma-separated args)
")")))))
((six.dot)
(##deconstruct-call
ast-src
3
(lambda (obj-src attr-src)
;; TODO: check that attr-src is (six.identifier ...)
(##deconstruct-call
attr-src
2
(lambda (ident-src)
(list (infix obj-src 0 inner-op)
"."
(symbol->string (##source-strip ident-src))))))))
(else
(unsupported ast-src)))))
(define (cvt ast-src)
;; pretend wrapped in infinitely low precedence operator to prevent
;; outer level of parentheses
(infix ast-src 0 '(9999 0)))
(cvt ast-src))
(define (comma-separated lst)
(if (pair? lst)
(cons (car lst)
(map (lambda (x) (list "," x)) (cdr lst)))
""))
(define (flatten-string x)
(call-with-output-string (lambda (p) (print port: p x))))