-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharcscript.arc
More file actions
298 lines (259 loc) · 7.86 KB
/
arcscript.arc
File metadata and controls
298 lines (259 loc) · 7.86 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
;;;
; utils
;;;
; heuristics to generate valid JS identifier
(defmemo symbol-name-to-js-string (symb)
(with (name (string symb)
lowercase t
all-uppercase nil)
(if (and (len> name 1) (endmatch "*" name))
(= all-uppercase t
name (butlast name)))
(tostring
(each c name
(aif (is c #\-)
(= lowercase (no lowercase))
(posmatch (string c) "!?#@%+*/=:<>")
(pr ('("bang" "what" "hash" "at" "percent" "plus" "star" "slash" "equals" "colon" "lessthan" "greaterthan") it))
(do
(pr (if (and lowercase (no all-uppercase))
(downcase c)
(upcase c)))
(= lowercase t)))))))
;;;
; compiler
;;;
(= as-reserved-symbol-names*
(list "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"))
(def add-as-reserved-symbol (name)
(pushnew (symbol-name-to-js-string name) as-reserved-symbol-names*))
(def is-as-reserved-symbol (symb)
(if (isa symb 'sym)
(mem (sym-to-js-string symb) as-reserved-symbol-names*)))
(= as-special-forms* {})
(def get-as-special-form (name)
(as-special-forms* name))
; XXX what is the point of the dstructuring bind in the original?
(mac define-as-special-form (name lambda-list . body)
`(= (as-special-forms* ',name)
(fn ,lambda-list
,@body)))
(def is-as-special-form (form)
(and (acons form)
(isa (car form) 'sym)
(as-special-forms* (car form))))
(def is-comparison-form (form)
(mem (car form) '(< > <= >= is isnt)))
(def is-funcall-form (form)
(and form
(acons form)
(no (is-op-form form))
(no (is-as-special-form form))))
(= as-macro-toplevel* {}
as-macro-env* (list as-macro-toplevel*)
as-symbol-macro-toplevel {}
as-symbol-macro-env* (list as-symbol-macro-toplevel)
as-special-forms* {}
as-local-function-names* nil
as-enclosing-lexicals nil
)
(def lookup-macro-def (name env)
(aif (find [_ name] env) (it name)))
(def as-macroexpand (form)
(aif (or (and (isa form 'sym) (lookup-macro-def form as-symbol-macro-env*))
(and (acons form) (lookup-macro-def (car form) as-macro-env*)))
(list (car (as-macroexpand (apply it form))) t)
(list form nil)))
;;; operators
; XXX fix pipe symbol
(with (precedence-table {} i 0)
(each level '((js:new js:getprop js:aref)
(postfix++ postfix--)
(delete void typeof ++ -- unary+ unary- ~ !)
(* / %)
(+ -)
(<< >> >>>)
(< > <= >= js:instanceof js:in)
(== != === !==)
(&)
(^)
(\|)
(&& and)
(\|\| or)
(js:?)
(= *= /= %= += -= <<= >>= >>>= &= ^= \|=)
(comma))
(map
(fn (op)
(= (precedence-table op) i))
level)
(++ i))
(def op-precedence (op)
(precedence-table op)))
;;;
(def as-convert-op-name (op)
(case op
and '&&
or '\|\|
no '!
is '==
isnt '!=
op))
(def maybe-fix-nary-comparison-form (form)
(if (len> (cdr form) 2)
(list
(withs (operator (car form)
tmp-var-forms (butlast (cddr form))
tmp-vars (accum collect (repeat (len tmp-var-forms) (collect (as-gensym "_cmp"))))
all-comparisons (+ (list (cadr form)) tmp-vars (list (last form))))
`(with (,@(accum collect (each pair (zip tmp-vars tmp-var-forms) (collect (car pair)) (collect (cadr pair)))))
(and ,@(accum collect
(each (x1 x2) (zip (butlast all-comparisons) (cdr all-comparisons))
(collect (list operator x1 x2)))))))
t)
(list
form
nil)))
(def compile-op-form (form)
(prn "compiling op form: " form)
`(js:operator ,(as-convert-op-name (as-compile-symbol (car form)))
,@(map [as-compile-expression (car (as-macroexpand _))] (cdr form))))
(def is-op-form (form)
(and (or (acons form) (no form))
(no (is-as-special-form form))
(op-precedence (form 0))))
(def adjust-as-compilation-level (form level)
;(if (or (and (acons form) (mem (car form) '(
(if (and (isa form 'sym) (is 'toplevel level))
level
(is 'toplevel level)
'inside-toplevel))
(def as-compile-symbol (form)
(let exp (as-compile-expression form)
(when (is (exp 0) 'js:variable)
(= exp (exp 1)))
(assert (isa exp 'sym))
exp))
(= as-compilation-level* 'toplevel)
(def compile-funcall-form (form)
(prn "compiling funcall form: " form)
`(js:funcall
,(if (isa (car form) 'sym)
`(js:variable ,(maybe-rename-local-function (car form)))
(as-compile-expression (car (as-macroexpand (car form)))))
,@(map as-compile-expression (cdr form))))
(def as-compile (expr)
(prn "compiling: " expr)
(case (type expr)
int expr
num expr
string expr
char (as-compile (string expr))
sym
(let (expansion did-expand) (as-macroexpand expr)
(if did-expand
(as-compile expansion)
(is-as-special-form (list expr))
(if (is-as-reserved-symbol expr)
((get-as-aspecial-form expr))
(err "Attempting to use ArcScript special form " expr " as variable"))
`(js:variable ,expr)))
cons
(let (expansion did-expand) (as-macroexpand expr)
(let as-compilation-level*
(if did-expand
as-compilation-level*
(adjust-as-compilation-level expr as-compilation-level*))
(if did-expand
(as-compile expr)
(is-as-special-form expr)
(apply (get-as-special-form (car expr)) (cdr expr))
(is-comparison-form expr)
(let (form fixed) (maybe-fix-nary-comparison-form expr)
(prn "is comparison!")
(if fixed
(as-compile form)
(compile-op-form form)))
(is-op-form expr)
(compile-op-form expr)
(is-funcall-form expr)
(compile-funcall-form expr)
(err "Cannot compile " expr " to an ArcScript form.")
)))))
(def as-compile-statement (form)
(let compiling-expression nil
(as-compile form)))
(def as-compile-expression (form)
(prn "compiling expression: " form)
(let compiling-expression t
(as-compile form)))
(def as-gensym ((o prefix "_js"))
(let prefix (if (isa prefix 'string) prefix (symbol-to-js-string prefix nil))
; XXX original is more complex
(uniq prefix)))
;;;
; printer
;;;
(def as-print (expr)
(case (type expr)
sym (asw (symbol-to-js-string expr))
)
)
;;;
; special forms
;;;
(mac defasliteral (name string)
`(do
(add-as-reserved-symbol ',name)
(define-as-special-form ,name ()
(list 'js:literal ,string))))
(def maybe-rename-local-function (fun-name)
(prn "maybe-rename-local-function: " fun-name)
(aif (assoc fun-name as-local-function-names*)
it
fun-name))
(defasliteral this "this")
(defasliteral t "true")
(defasliteral true "true")
(defasliteral false "false")
(defasliteral f "false")
(defasliteral nil "null")
(defasliteral undefined "undefined")
; XXX this was a macrolet in original version
(mac def-for-literal (name printer)
`(do
(add-as-reserved-symbol ',name)
(define-as-special-form ,name ((o label))
(list ',printer label))))
(def-for-literal break js:break)
(def-for-literal continue js:continue)
(define-as-special-form quote (x)
(let quote% (fn (expr) (when expr `',expr))
(as-compile-expression
(case (type x)
cons `(list ,@(map quote% x))
nil '(list)
sym (symbol-to-js-string x)
num x
int x
string x))))
; XXX was macrolet
(mac def-unary-ops ops
`(do
,@(map
(fn (op)
(with (op (if (acons op) (car op) op)
is-space (if (acons op) (op 1)))
`(define-as-special-form ,op (x)
(list 'js:unary-operator ',op
(as-compile-expression (as-macroexpand x))
t ,is-space))))
ops)))
(def-unary-ops ~ ! (new t) (delete t) (void t) (typeof t))