-
Notifications
You must be signed in to change notification settings - Fork 4
/
javascript.ralph
255 lines (245 loc) · 9.65 KB
/
javascript.ralph
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
(define-module ralph/compiler/javascript
import: (ralph/regexp
ralph/compiler/utilities
(escodegen rename: (generate compile-tree)))
export: (compile-js))
(define *reserved*
["break" "case" "catch" "continue" "default" "delete" "do" "else"
"finally" "for" "function" "if" "in" "instanceof" "new" "return"
"switch" "this" "throw" "try" "typeof" "var" "void" "while"
"with" "abstract" "boolean" "byte" "char" "class" "const"
"debugger" "double" "enum" "export" "extends" "final" "float"
"goto" "implements" "import" "int" "interface" "long" "native"
"package" "private" "protected" "public" "short" "static" "super"
"synchronized" "throws" "transient" "volatile" "null" "true" "false"
"arguments" "object" "number" "string" "array" "let" "yield"])
(define *symbol-escapes*
(make-object
"-" "_"
"_" "__"
"&" "A"
"$" "B"
":" "C"
"." "D"
"=" "E"
"^" "F"
">" "G"
"#" "H"
"@" "I"
"~" "J"
;; K: keyword
"<" "L"
"%" "M"
"!" "N"
;; O
"+" "P"
"?" "Q"
;; R: reserved
"/" "S"
"*" "T"
;; U, V, W, X, Y
;; Z: any other
))
(define-function escape-name (name)
(if (member? name *reserved*)
(concatenate "R" name)
(bind ((chars
(map (method (char)
(cond
((has? *symbol-escapes* char)
(get *symbol-escapes* char))
((match "\\w" char)
char)
(else:
(concatenate
"Z" (as-string (char-code char))))))
(as-array name))))
(join chars ""))))
(define-function compile-js (exp env)
(compile-tree (as-statement
(compile exp env))
(%object "verbatim" "x-verbatim")))
(define-function as-statement (exp)
(if (or (position (get exp "type")
"Statement")
(position (get exp "type")
"Declaration"))
exp
(%object "type" "ExpressionStatement"
"expression" exp)))
(define-function make-block (statements)
(%object "type" "BlockStatement"
"body" statements))
(define-function compile (exp env)
(select exp instance?
((<array>)
(if (empty? exp)
(signal (make <error> message: "empty expression"))
(compile-exp exp env)))
((<hash-symbol>)
(%object "type" "Identifier"
"name" (concatenate
"$" (as-uppercase (symbol-name exp)))))
((<keyword>)
(%object "type" "CallExpression"
"callee" (%object "type" "Identifier"
"name" "$K")
"arguments" [(compile (symbol-name exp) env)]))
((<symbol>)
(select (symbol-name exp) ==
(("%this-method")
(compile (get env "current-method") env))
(("%all-arguments")
(%object "type" "Identifier"
"name" "arguments"))
(else:
(%object "type" "Identifier"
"name" (escape-name
(if (== (get exp "module")
(get env "module" "name"))
(symbol-name exp)
(description exp)))))))
((<number>)
(if (< exp 0)
(%object "type" "UnaryExpression"
"operator" "-"
"prefix" #t
"argument" (%object "type" "Literal"
"value" (abs exp)))
(%object "type" "Literal"
"value" exp)))
(else:
(%object "type" "Literal"
"value" (or exp #f)))))
(define-function compile-exp (exp env)
(bind ((name (symbol-name (first exp))))
(select name ==
(("%quote")
(destructuring-bind (name module-name)
(destructure-symbol (second exp))
(compile
`(%native-call "$S" ,name
,@(if module-name
[module-name]
[]))
env)))
(("%method")
(destructuring-bind (_ name parameters form) exp
(bind ((previous-method (get env "current-method")))
(%object "type" "FunctionExpression"
"id" (compile name env)
"params" (map (rcurry compile env)
parameters)
"body" (begin
(set! (get env "current-method")
name)
(bind ((form* (compile (if (op? "%begin" form)
form
`(%begin ,form))
env)))
(set! (get env "current-method")
previous-method)
form*))))))
(("%set")
(destructuring-bind (_ identifier value) exp
(%object "type" "AssignmentExpression"
"operator" "="
"left" (compile identifier env)
"right" (compile value env))))
(("%if")
(destructuring-bind (_ test consequent alternate) exp
(%object "type" "IfStatement"
"test" (compile test env)
"consequent" (as-statement
(compile consequent env))
"alternate" (when alternate
(as-statement
(compile alternate env))))))
(("%begin")
(make-block (map (method (exp)
(as-statement
(compile exp env)))
(rest exp))))
(("%while")
(destructuring-bind (_ test form) exp
(%object "type" "WhileStatement"
"test" (compile test env)
"body" (as-statement
(compile form env)))))
(("%try")
(destructuring-bind (_ protected-form identifier handling-form) exp
(%object "type" "TryStatement"
"block" (make-block [(as-statement
(compile protected-form env))])
"handler" (%object "type" "CatchClause"
"param" (compile identifier env)
"body" (make-block
[(as-statement
(compile handling-form env))])))))
(("%var")
(destructuring-bind (_ identifier value) exp
;; NOTE: always initialize, might reset state
(%object "type" "VariableDeclaration"
"kind" "var"
"declarations" [(%object "type" "VariableDeclarator"
"id" (compile identifier env)
"init" (compile (or value #f) env))])))
(("%native-call")
(destructuring-bind (_ operator #rest arguments) exp
(%object "type" "CallExpression"
"callee" (%object "type" "Identifier"
"name" operator)
"arguments" (map (rcurry compile env)
arguments))))
(("%infix")
(destructuring-bind (_ operator #rest arguments) exp
(reduce1 (method (result exp)
(%object "type" "BinaryExpression"
"operator" operator
"left" result
"right" exp))
(map (rcurry compile env)
arguments))))
(("%native")
(%object "type" "Expression"
"x-verbatim"
(reduce1 concatenate
(map (method (e)
(select e instance?
((<symbol> <array>)
;; returns statement, strip semicolon
(but-last
(compile-js e env)))
(else:
(as-string e))))
(rest exp)))))
(("%object")
(%object "type" "ObjectExpression"
"properties" (map (method (property/value)
(destructuring-bind (property value)
property/value
(%object "type" "Property"
"key" (compile property env)
"value" (compile value env)
"kind" "init")))
(partition 2 (rest exp)))))
(("%array")
(destructuring-bind (_ #rest elements) exp
(%object "type" "ArrayExpression"
"elements" (map (rcurry compile env)
elements))))
(("%get-property")
(destructuring-bind (_ object #rest properties) exp
(reduce (method (object property)
(%object "type" "MemberExpression"
"object" object
"property" (compile property env)
"computed" #t))
(compile object env)
properties)))
(else:
(destructuring-bind (function #rest arguments) exp
(%object "type" "CallExpression"
"callee" (compile function env)
"arguments" (map (rcurry compile env)
arguments)))))))