sibilant-rails 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/sibilant-rails.rb +7 -0
- data/lib/sibilant.rb +37 -0
- data/lib/sibilant/haml/filter.rb +16 -0
- data/lib/sibilant/tilt/template.rb +20 -0
- data/lib/source/include/functional.sibilant +411 -0
- data/lib/source/include/macros.sibilant +411 -0
- data/lib/source/lib/browser.js +644 -0
- metadata +106 -0
@@ -0,0 +1,411 @@
|
|
1
|
+
(macro join (glue arr)
|
2
|
+
(concat "(" (translate arr) ").join(" (translate glue) ")"))
|
3
|
+
|
4
|
+
|
5
|
+
(macro send (object method &rest args)
|
6
|
+
(concat (translate object) "." (translate method)
|
7
|
+
"(" (join ", " (map args translate)) ")"))
|
8
|
+
|
9
|
+
(macro apply (fn arglist)
|
10
|
+
(macros.send fn 'apply 'undefined arglist))
|
11
|
+
|
12
|
+
(macro list (&rest args)
|
13
|
+
(concat "[ " (join ", " (map args translate)) " ]"))
|
14
|
+
|
15
|
+
(macro cons (first rest)
|
16
|
+
(macros.send (macros.list first) 'concat rest))
|
17
|
+
|
18
|
+
(macro append (list &rest additional)
|
19
|
+
(macros.send list 'concat (apply macros.list additional)))
|
20
|
+
|
21
|
+
(macro get (obj &rest keys)
|
22
|
+
(concat "(" (translate obj) ")"
|
23
|
+
(join "" (map keys (lambda (key)
|
24
|
+
(concat "[" (translate key) "]"))))))
|
25
|
+
|
26
|
+
(macro + (&rest args)
|
27
|
+
(concat "(" (join " + " (map args translate)) ")"))
|
28
|
+
(macro - (&rest args)
|
29
|
+
(concat "(" (join " - " (map args translate)) ")"))
|
30
|
+
(macro * (&rest args)
|
31
|
+
(concat "(" (join " * " (map args translate)) ")"))
|
32
|
+
(macro / (&rest args)
|
33
|
+
(concat "(" (join " / " (map args translate)) ")"))
|
34
|
+
(macro or (&rest args)
|
35
|
+
(concat "(" (join " || " (map args translate)) ")"))
|
36
|
+
(macro and (&rest args)
|
37
|
+
(concat "(" (join " && " (map args translate)) ")"))
|
38
|
+
(macro mod (&rest args)
|
39
|
+
(concat "(" (join " % " (map args translate)) ")"))
|
40
|
+
|
41
|
+
(macro join (&optional glue arr) ;redefined because join and or are mutually dependent
|
42
|
+
(concat "(" (translate arr) ").join(" (or (translate glue) "\"\"") ")"))
|
43
|
+
|
44
|
+
(macro infix-comparator (comparator args)
|
45
|
+
(concat "("
|
46
|
+
(join " && "
|
47
|
+
(map (args.slice 0 -1)
|
48
|
+
(lambda (item index)
|
49
|
+
(concat item
|
50
|
+
" " comparator " "
|
51
|
+
(get args (+ 1 index))))))
|
52
|
+
")"))
|
53
|
+
|
54
|
+
(macro > (&rest args) (macros.infix-comparator ">" (map args translate)))
|
55
|
+
(macro < (&rest args) (macros.infix-comparator "<" (map args translate)))
|
56
|
+
(macro <= (&rest args) (macros.infix-comparator "<=" (map args translate)))
|
57
|
+
(macro >= (&rest args) (macros.infix-comparator ">=" (map args translate)))
|
58
|
+
(macro != (&rest args) (macros.infix-comparator "!==" (map args translate)))
|
59
|
+
|
60
|
+
(macro pow (base exponent)
|
61
|
+
(macros.call "Math.pow" base exponent))
|
62
|
+
|
63
|
+
(macro incr-by (item increment)
|
64
|
+
(concat (translate item) " += " (translate increment)))
|
65
|
+
|
66
|
+
(macro incr (item)
|
67
|
+
(concat "((" (translate item) ")++)"))
|
68
|
+
|
69
|
+
(macro decr (item)
|
70
|
+
(concat "((" (translate item) ")--)"))
|
71
|
+
|
72
|
+
(macro set (arr &rest kv-pairs)
|
73
|
+
(join "\n" (bulk-map kv-pairs
|
74
|
+
(lambda (k v)
|
75
|
+
(concat "(" (translate arr) ")"
|
76
|
+
"[" (translate k) "] = " (translate v) ";")))))
|
77
|
+
|
78
|
+
(macro new (&rest args)
|
79
|
+
(concat "(new " (apply macros.call args) ")"))
|
80
|
+
|
81
|
+
(macro regex (string &optional glim)
|
82
|
+
(call (get macros 'new) 'RegExp string (or glim 'undefined)))
|
83
|
+
|
84
|
+
(macro timestamp ()
|
85
|
+
(concat "\"" (send (new Date) to-string) "\""))
|
86
|
+
|
87
|
+
(macro comment (&rest contents)
|
88
|
+
(map contents
|
89
|
+
(lambda (item)
|
90
|
+
(join "\n" (map (send (translate item) split "\n")
|
91
|
+
(lambda (line) (concat "// " line)))))))
|
92
|
+
|
93
|
+
(macro meta (body)
|
94
|
+
(eval (translate body)))
|
95
|
+
|
96
|
+
(macro zero? (item)
|
97
|
+
((get macros "=") (translate item) 0))
|
98
|
+
|
99
|
+
(macro empty? (arr)
|
100
|
+
(concat "((" (translate arr) ").length === 0)"))
|
101
|
+
|
102
|
+
(macro odd? (number)
|
103
|
+
((get macros "!=") 0
|
104
|
+
(macros.mod (translate number) 2)))
|
105
|
+
|
106
|
+
(macro even? (number)
|
107
|
+
((get macros "=") 0
|
108
|
+
(macros.mod (translate number) 2)))
|
109
|
+
|
110
|
+
|
111
|
+
(macro typeof (thing) (+ "typeof " (translate thing)))
|
112
|
+
|
113
|
+
(macro function? (thing)
|
114
|
+
((get macros "=") (macros.typeof thing) "'function'"))
|
115
|
+
|
116
|
+
(macro undefined? (thing)
|
117
|
+
((get macros "=") (macros.typeof thing) "'undefined'"))
|
118
|
+
|
119
|
+
(macro defined? (thing)
|
120
|
+
((get macros "!=") (macros.typeof thing) "'undefined'"))
|
121
|
+
|
122
|
+
(macro number? (thing)
|
123
|
+
((get macros "=") (macros.typeof thing) "'number'"))
|
124
|
+
|
125
|
+
(macro first (arr) (macros.get arr 0))
|
126
|
+
(macro second (arr) (macros.get arr 1))
|
127
|
+
(macro third (arr) (macros.get arr 2))
|
128
|
+
(macro fourth (arr) (macros.get arr 3))
|
129
|
+
(macro fifth (arr) (macros.get arr 4))
|
130
|
+
(macro sixth (arr) (macros.get arr 5))
|
131
|
+
(macro seventh (arr) (macros.get arr 6))
|
132
|
+
(macro eighth (arr) (macros.get arr 7))
|
133
|
+
(macro ninth (arr) (macros.get arr 8))
|
134
|
+
|
135
|
+
(macro rest (arr)
|
136
|
+
(macros.send arr 'slice 1))
|
137
|
+
|
138
|
+
(macro length (arr)
|
139
|
+
(macros.get arr "\"length\""))
|
140
|
+
|
141
|
+
(macro last (arr)
|
142
|
+
(macros.get (macros.send arr 'slice -1) 0))
|
143
|
+
|
144
|
+
(macro if (arg truebody falsebody)
|
145
|
+
(concat
|
146
|
+
"(function() {"
|
147
|
+
(indent (concat
|
148
|
+
"if (" (translate arg) ") {"
|
149
|
+
(indent (macros.progn truebody))
|
150
|
+
"} else {"
|
151
|
+
(indent (macros.progn falsebody))
|
152
|
+
"}"))
|
153
|
+
"})()"))
|
154
|
+
|
155
|
+
(macro defvar (&rest pairs)
|
156
|
+
(as-statement
|
157
|
+
(concat "var "
|
158
|
+
(join ",\n "
|
159
|
+
(bulk-map pairs
|
160
|
+
(lambda (name value)
|
161
|
+
(concat (translate name) " = "
|
162
|
+
(translate value)))))
|
163
|
+
";")))
|
164
|
+
|
165
|
+
(macro = (first-thing &rest other-things)
|
166
|
+
(defvar translated-first-thing (translate first-thing))
|
167
|
+
(concat "("
|
168
|
+
(join " &&\n "
|
169
|
+
(map other-things
|
170
|
+
(lambda (thing)
|
171
|
+
(concat translated-first-thing
|
172
|
+
" === "
|
173
|
+
(translate thing)))))
|
174
|
+
")"))
|
175
|
+
|
176
|
+
|
177
|
+
(macro string? (thing)
|
178
|
+
(concat "typeof(" (translate thing) ") === \"string\""))
|
179
|
+
|
180
|
+
(macro list? (thing)
|
181
|
+
(defvar translated (concat "(" (translate thing) ")"))
|
182
|
+
(concat translated " && "
|
183
|
+
translated ".constructor.name === \"Array\""))
|
184
|
+
|
185
|
+
|
186
|
+
(macro when (arg &rest body)
|
187
|
+
(concat
|
188
|
+
"(function() {"
|
189
|
+
(indent (concat
|
190
|
+
"if (" (translate arg) ") {"
|
191
|
+
(indent (apply macros.progn body))
|
192
|
+
"}"))
|
193
|
+
"})()"))
|
194
|
+
|
195
|
+
(macro unless (condition &rest body)
|
196
|
+
(apply macros.when (cons ['not condition] body)))
|
197
|
+
|
198
|
+
(macro not (exp)
|
199
|
+
(concat "(!" (translate exp) ")"))
|
200
|
+
|
201
|
+
(macro slice (arr start &optional end)
|
202
|
+
(macros.send (translate arr) "slice" start end))
|
203
|
+
|
204
|
+
(macro inspect (&rest args)
|
205
|
+
(join " + \"\\n\" + "
|
206
|
+
(map args
|
207
|
+
(lambda (arg)
|
208
|
+
(concat "\"" arg ":\" + " (translate arg))))))
|
209
|
+
|
210
|
+
(macro each (item array &rest body)
|
211
|
+
(macros.send (translate array) 'for-each
|
212
|
+
(apply macros.lambda (cons item body))))
|
213
|
+
|
214
|
+
|
215
|
+
(macro setf (&rest args)
|
216
|
+
(join "\n"
|
217
|
+
(bulk-map args (lambda (name value)
|
218
|
+
(as-statement (concat (translate name) " = "
|
219
|
+
(translate value)))))))
|
220
|
+
|
221
|
+
|
222
|
+
(macro macro-list ()
|
223
|
+
(concat "["
|
224
|
+
(indent (join ",\n"
|
225
|
+
(map (-object.keys macros)
|
226
|
+
macros.quote)))
|
227
|
+
"]"))
|
228
|
+
|
229
|
+
(macro macroexpand (name)
|
230
|
+
(defvar macro (get macros (translate name)))
|
231
|
+
(if macro
|
232
|
+
(concat "// macro: " name "\n" (send macro to-string))
|
233
|
+
"undefined"))
|
234
|
+
|
235
|
+
(macro throw (&rest string)
|
236
|
+
(concat "throw new Error (" (join " " (map string translate)) ")"))
|
237
|
+
|
238
|
+
(macro as-boolean (expr)
|
239
|
+
(concat "(!!(" (translate expr) "))"))
|
240
|
+
|
241
|
+
(macro chain (object &rest calls)
|
242
|
+
(if (= 0 calls.length) (translate object)
|
243
|
+
(if (= 1 calls.length) (apply macros.send (cons object (first calls)))
|
244
|
+
(concat (translate object)
|
245
|
+
(indent (join "\n"
|
246
|
+
(map calls (lambda (call index)
|
247
|
+
(concat "." (translate (first call))
|
248
|
+
"(" (join ", " (map (rest call) translate)) ")")))))))))
|
249
|
+
|
250
|
+
(macro chainable (&rest names)
|
251
|
+
(each (name) names
|
252
|
+
(macros.macro name ['target "&rest" 'calls]
|
253
|
+
['apply 'macros.chain ['cons ['macros.call ['quote name] 'target] 'calls]])))
|
254
|
+
|
255
|
+
(macro try (tryblock catchblock)
|
256
|
+
(concat
|
257
|
+
"(function() {"
|
258
|
+
(indent (concat
|
259
|
+
"try {"
|
260
|
+
(indent (macros.progn tryblock))
|
261
|
+
"} catch (e) {"
|
262
|
+
(indent (macros.progn catchblock))
|
263
|
+
"}"))
|
264
|
+
"})()"))
|
265
|
+
|
266
|
+
|
267
|
+
(macro while (condition &rest block)
|
268
|
+
(macros.scoped
|
269
|
+
(macros.defvar '**return-value**)
|
270
|
+
(concat "while (" (translate condition) ") {"
|
271
|
+
(indent (macros.setf '**return-value**
|
272
|
+
(apply macros.scoped block))))
|
273
|
+
"}"
|
274
|
+
'**return-value**))
|
275
|
+
|
276
|
+
(macro until (condition &rest block)
|
277
|
+
(apply (get macros 'while)
|
278
|
+
(cons ['not condition] block)))
|
279
|
+
|
280
|
+
|
281
|
+
(macro thunk (&rest args)
|
282
|
+
(apply macros.lambda (cons [] args)))
|
283
|
+
|
284
|
+
(macro keys (obj)
|
285
|
+
(macros.call "Object.keys" (translate obj)))
|
286
|
+
|
287
|
+
(macro delete (&rest objects)
|
288
|
+
(join "\n" (map objects (lambda (obj)
|
289
|
+
(concat "delete " (translate obj) ";")))))
|
290
|
+
|
291
|
+
(macro delmacro (macro-name)
|
292
|
+
(delete (get macros (translate macro-name)))
|
293
|
+
"")
|
294
|
+
|
295
|
+
(macro alias-macro (current-macro-name desired-macro-name)
|
296
|
+
(defvar current-macro-name (translate current-macro-name)
|
297
|
+
desired-macro-name (translate desired-macro-name))
|
298
|
+
(set macros desired-macro-name (get macros current-macro-name))
|
299
|
+
"")
|
300
|
+
|
301
|
+
(macro rename-macro (current-macro-name desired-macro-name)
|
302
|
+
(macros.alias-macro current-macro-name desired-macro-name)
|
303
|
+
(macros.delmacro current-macro-name)
|
304
|
+
"")
|
305
|
+
|
306
|
+
(macro defhash (name &rest pairs)
|
307
|
+
(macros.defvar name (apply macros.hash pairs)))
|
308
|
+
|
309
|
+
(macro arguments ()
|
310
|
+
"(Array.prototype.slice.apply(arguments))")
|
311
|
+
|
312
|
+
(macro scoped (&rest body)
|
313
|
+
(macros.call (apply macros.thunk body)))
|
314
|
+
|
315
|
+
(macro each-key (as obj &rest body)
|
316
|
+
(concat "(function() {"
|
317
|
+
(indent
|
318
|
+
(concat "for (var " (translate as) " in " (translate obj) ") "
|
319
|
+
(apply macros.scoped body)
|
320
|
+
";"))
|
321
|
+
"})();"))
|
322
|
+
|
323
|
+
(macro match? (regexp string)
|
324
|
+
(macros.send string 'match regexp))
|
325
|
+
|
326
|
+
(macro switch (obj &rest cases)
|
327
|
+
|
328
|
+
;; the complexity of this macro indicates there's a problem
|
329
|
+
;; I'm not quite sure where to fix this, but it has to do with quoting.
|
330
|
+
(defvar lines (list (concat "switch(" (translate obj) ") {")))
|
331
|
+
(each (case-def) cases
|
332
|
+
(defvar case-name (first case-def))
|
333
|
+
(when (and (list? case-name)
|
334
|
+
(= (first case-name) 'quote))
|
335
|
+
(defvar second (second case-name))
|
336
|
+
(setf case-name (if (list? second)
|
337
|
+
(map second macros.quote)
|
338
|
+
(macros.quote second))))
|
339
|
+
|
340
|
+
(defvar case-string
|
341
|
+
(if (list? case-name)
|
342
|
+
(join "\n" (map case-name (lambda (c)
|
343
|
+
(concat "case " (translate c) ":"))))
|
344
|
+
(if (= 'default case-name) "default:"
|
345
|
+
(concat "case " (translate case-name) ":"))))
|
346
|
+
|
347
|
+
(lines.push (concat case-string
|
348
|
+
(indent (apply macros.progn (case-def.slice 1))))))
|
349
|
+
|
350
|
+
;; the following two lines are to get the whitespace right
|
351
|
+
;; this is necessary because switches are indented weird
|
352
|
+
(set lines (- lines.length 1)
|
353
|
+
(chain (get lines (- lines.length 1)) (concat "}")))
|
354
|
+
|
355
|
+
(concat "(function() {" (apply indent lines) "})()"))
|
356
|
+
|
357
|
+
|
358
|
+
(macro if-else (&rest args)
|
359
|
+
(concat "(function() {"
|
360
|
+
(indent
|
361
|
+
(join " else "
|
362
|
+
(bulk-map args
|
363
|
+
(lambda (cond val)
|
364
|
+
(if (undefined? val)
|
365
|
+
(concat "{" (indent (macros.progn cond)) "}")
|
366
|
+
(concat "if (" (translate cond) ") {"
|
367
|
+
(indent (macros.progn val))
|
368
|
+
"}"))))))
|
369
|
+
"})()"))
|
370
|
+
|
371
|
+
(macro let (args &optional body)
|
372
|
+
(concat "let (" (bulk-map args
|
373
|
+
(lambda (h k v)
|
374
|
+
(concat (translate k) " = " (translate v))))
|
375
|
+
(if (undefined? body) ");"
|
376
|
+
(concat ") {" (indent (translate body)) "}"))))
|
377
|
+
|
378
|
+
(macro instance-of? (item type)
|
379
|
+
(concat "(" (translate item) " instanceof " (translate type) ")"))
|
380
|
+
|
381
|
+
(macro slice (list &optional begin &optional end)
|
382
|
+
(concat "Array.prototype.slice.call(" (translate list)
|
383
|
+
", " (or (translate begin) 0)
|
384
|
+
(if (defined? end) (concat ", " (translate end) ")") ")")))
|
385
|
+
|
386
|
+
(macro ternary (cond if-true if-false)
|
387
|
+
(concat "(" (translate cond) ") ? "
|
388
|
+
(translate if-true) " : "
|
389
|
+
(translate if-false)))
|
390
|
+
|
391
|
+
|
392
|
+
(macro includes? (list item)
|
393
|
+
(call (get macros "!=") -1 (macros.send list 'index-of item)))
|
394
|
+
|
395
|
+
(macro excludes? (list item)
|
396
|
+
(call (get macros "=") -1 (macros.send list 'index-of item)))
|
397
|
+
|
398
|
+
(macro exists? (thing)
|
399
|
+
(call (get macros "and")
|
400
|
+
(macros.defined? thing)
|
401
|
+
(call (get macros "!=") thing 'null)))
|
402
|
+
|
403
|
+
;;new style
|
404
|
+
(alias-macro lambda #)
|
405
|
+
(alias-macro thunk #>)
|
406
|
+
(alias-macro defvar var)
|
407
|
+
(alias-macro progn do)
|
408
|
+
(alias-macro setf assign)
|
409
|
+
(alias-macro defun def)
|
410
|
+
(alias-macro delmacro delete-macro)
|
411
|
+
|
@@ -0,0 +1,644 @@
|
|
1
|
+
(function(root) {
|
2
|
+
var sibilant = { },
|
3
|
+
exports = { };
|
4
|
+
var error = (function(str) {
|
5
|
+
throw new Error (str);
|
6
|
+
});
|
7
|
+
;
|
8
|
+
var inspect = (function(item) {
|
9
|
+
return (function() {
|
10
|
+
if (item.toSource) {
|
11
|
+
return item.toSource();
|
12
|
+
} else {
|
13
|
+
return item.toString();
|
14
|
+
}
|
15
|
+
})();
|
16
|
+
});
|
17
|
+
;
|
18
|
+
(root)["sibilant"] = sibilant;
|
19
|
+
var bulkMap = (function(arr, fn) {
|
20
|
+
var index = 0,
|
21
|
+
groupSize = fn.length,
|
22
|
+
retArr = [ ];
|
23
|
+
(function() {
|
24
|
+
var __returnValue__ = undefined;
|
25
|
+
while ((index < arr.length)) {
|
26
|
+
__returnValue__ = (function() {
|
27
|
+
retArr.push(fn.apply(undefined, arr.slice(index, (index + groupSize))));
|
28
|
+
return index += groupSize;
|
29
|
+
})();
|
30
|
+
};
|
31
|
+
return __returnValue__;
|
32
|
+
})();
|
33
|
+
return retArr;
|
34
|
+
});
|
35
|
+
|
36
|
+
var inject = (function(start, items, fn) {
|
37
|
+
var value = start;
|
38
|
+
(function() {
|
39
|
+
if ((items) && (items).constructor.name === "Array") {
|
40
|
+
return items.forEach((function(item, index) {
|
41
|
+
return value = fn(value, item, index);
|
42
|
+
}));
|
43
|
+
}
|
44
|
+
})();
|
45
|
+
return value;
|
46
|
+
});
|
47
|
+
|
48
|
+
var map = (function(items, fn) {
|
49
|
+
return inject([ ], items, (function(collector, item, index) {
|
50
|
+
collector.push(fn(item, index));
|
51
|
+
return collector;
|
52
|
+
}));
|
53
|
+
});
|
54
|
+
|
55
|
+
var select = (function(items, fn) {
|
56
|
+
return inject([ ], items, (function(collector, item, index) {
|
57
|
+
(function() {
|
58
|
+
if (fn(item, index)) {
|
59
|
+
return collector.push(item);
|
60
|
+
}
|
61
|
+
})();
|
62
|
+
return collector;
|
63
|
+
}));
|
64
|
+
});
|
65
|
+
|
66
|
+
var detect = (function(items, fn) {
|
67
|
+
var returnItem = undefined,
|
68
|
+
index = 0,
|
69
|
+
items = items;
|
70
|
+
return (function() {
|
71
|
+
var __returnValue__ = undefined;
|
72
|
+
while ((!((items.length === index) || returnItem))) {
|
73
|
+
__returnValue__ = (function() {
|
74
|
+
(function() {
|
75
|
+
if (fn((items)[index], index)) {
|
76
|
+
return returnItem = (items)[index];
|
77
|
+
}
|
78
|
+
})();
|
79
|
+
return ((index)++);
|
80
|
+
})();
|
81
|
+
};
|
82
|
+
return __returnValue__;
|
83
|
+
})();
|
84
|
+
});
|
85
|
+
|
86
|
+
var reject = (function(items, fn) {
|
87
|
+
var args = [ items, fn ];
|
88
|
+
return select(items, (function() {
|
89
|
+
return (!fn.apply(undefined, arguments));
|
90
|
+
}));
|
91
|
+
});
|
92
|
+
|
93
|
+
var compact = (function(arr) {
|
94
|
+
return select(arr, (function(item) {
|
95
|
+
return (!!(item));
|
96
|
+
}));
|
97
|
+
});
|
98
|
+
|
99
|
+
var flatten = (function(items) {
|
100
|
+
var items = Array.prototype.slice.call(arguments, 0);
|
101
|
+
|
102
|
+
return inject([ ], items, (function(collector, item) {
|
103
|
+
return collector.concat((function() {
|
104
|
+
if ((item) && (item).constructor.name === "Array") {
|
105
|
+
return flatten.apply(undefined, item);
|
106
|
+
} else {
|
107
|
+
return item;
|
108
|
+
}
|
109
|
+
})());
|
110
|
+
}));
|
111
|
+
});
|
112
|
+
|
113
|
+
;
|
114
|
+
(sibilant)["tokens"] = { };
|
115
|
+
(sibilant.tokens)["regex"] = "(\\/(\\\\\\\/|[^\\/\\n])+\\/[glim]*)";
|
116
|
+
(sibilant.tokens)["comment"] = "(;.*)";
|
117
|
+
(sibilant.tokens)["string"] = "(\"(([^\"]|(\\\\\"))*[^\\\\])?\")";
|
118
|
+
(sibilant.tokens)["number"] = "(-?[0-9][0-9.,]*)";
|
119
|
+
(sibilant.tokens)["literal"] = "(-?[*.$a-zA-Z][*.a-zA-Z0-9-]*(\\?|!)?)";
|
120
|
+
(sibilant.tokens)["special"] = "([&']?)";
|
121
|
+
(sibilant.tokens)["otherChar"] = "([#><=!\\+\\/\\*-]+)";
|
122
|
+
(sibilant.tokens)["openParen"] = "(\\()";
|
123
|
+
(sibilant.tokens)["specialOpenParen"] = "('?\\()";
|
124
|
+
(sibilant.tokens)["closeParen"] = "(\\))";
|
125
|
+
(sibilant.tokens)["alternativeParens"] = "\\{|\\[|\\}|\\]";
|
126
|
+
(sibilant.tokens)["specialLiteral"] = (sibilant.tokens.special + sibilant.tokens.literal);
|
127
|
+
(sibilant)["tokenPrecedence"] = [ "regex", "comment", "string", "number", "specialLiteral", "otherChar", "specialOpenParen", "closeParen", "alternativeParens" ];
|
128
|
+
var tokenize = sibilant.tokenize = (function(string) {
|
129
|
+
var tokens = [ ],
|
130
|
+
parseStack = [ tokens ],
|
131
|
+
specials = [ ];
|
132
|
+
var acceptToken = (function(token) {
|
133
|
+
return (parseStack)[0].push(token);
|
134
|
+
});
|
135
|
+
;
|
136
|
+
var increaseNesting = (function() {
|
137
|
+
var newArr = [ ];
|
138
|
+
acceptToken(newArr);
|
139
|
+
return parseStack.unshift(newArr);
|
140
|
+
});
|
141
|
+
;
|
142
|
+
var decreaseNesting = (function() {
|
143
|
+
specials.shift();
|
144
|
+
parseStack.shift();
|
145
|
+
return (function() {
|
146
|
+
if ((parseStack.length === 0)) {
|
147
|
+
throw new Error (("unbalanced parens:\n" + inspect(parseStack)));
|
148
|
+
}
|
149
|
+
})();
|
150
|
+
});
|
151
|
+
;
|
152
|
+
var handleToken = (function(token) {
|
153
|
+
var special = (token)[0],
|
154
|
+
token = token;
|
155
|
+
(function() {
|
156
|
+
if ((special === "'")) {
|
157
|
+
token = token.slice(1);
|
158
|
+
increaseNesting();
|
159
|
+
return acceptToken("quote");
|
160
|
+
} else {
|
161
|
+
return special = false;
|
162
|
+
}
|
163
|
+
})();
|
164
|
+
specials.unshift((!!(special)));
|
165
|
+
(function() {
|
166
|
+
switch(token) {
|
167
|
+
case "(":
|
168
|
+
return increaseNesting();
|
169
|
+
|
170
|
+
case "]":
|
171
|
+
case "}":
|
172
|
+
case ")":
|
173
|
+
return decreaseNesting();
|
174
|
+
|
175
|
+
case "{":
|
176
|
+
increaseNesting();
|
177
|
+
return acceptToken("hash");
|
178
|
+
|
179
|
+
case "[":
|
180
|
+
increaseNesting();
|
181
|
+
return acceptToken("list");
|
182
|
+
|
183
|
+
default:
|
184
|
+
return (function() {
|
185
|
+
if (token.match((new RegExp(("^" + sibilant.tokens.number + "$"), undefined)))) {
|
186
|
+
return acceptToken(parseFloat(token.replace((new RegExp(",", "g")), "")));
|
187
|
+
} else {
|
188
|
+
return acceptToken(token);
|
189
|
+
}
|
190
|
+
})();
|
191
|
+
}
|
192
|
+
})();
|
193
|
+
return (function() {
|
194
|
+
if (((token !== "(") && specials.shift())) {
|
195
|
+
return decreaseNesting();
|
196
|
+
}
|
197
|
+
})();
|
198
|
+
});
|
199
|
+
;
|
200
|
+
var orderedRegexen = map(sibilant.tokenPrecedence, (function(x) {
|
201
|
+
return (sibilant.tokens)[x];
|
202
|
+
})),
|
203
|
+
masterRegex = (new RegExp((orderedRegexen).join("|"), "g"));
|
204
|
+
string
|
205
|
+
.match(masterRegex)
|
206
|
+
.forEach(handleToken)
|
207
|
+
;
|
208
|
+
(function() {
|
209
|
+
if ((parseStack.length > 1)) {
|
210
|
+
return error("unexpected EOF, probably missing a )\n", inspect((parseStack)[0]));
|
211
|
+
}
|
212
|
+
})();
|
213
|
+
return tokens;
|
214
|
+
});
|
215
|
+
var indent = (function(args) {
|
216
|
+
var args = Array.prototype.slice.call(arguments, 0);
|
217
|
+
|
218
|
+
return (compact(args)
|
219
|
+
.join("\n")
|
220
|
+
.replace(/^/, "\n")
|
221
|
+
.replace(/\n/g, "\n ")
|
222
|
+
+ "\n");
|
223
|
+
});
|
224
|
+
|
225
|
+
var constructHash = (function(arrayOfArrays) {
|
226
|
+
return inject({ }, arrayOfArrays, (function(object, item) {
|
227
|
+
(object)[(item)[0]] = (object)[(item)[1]];
|
228
|
+
return object;
|
229
|
+
}));
|
230
|
+
});
|
231
|
+
|
232
|
+
var macros = { };
|
233
|
+
(sibilant)["macros"] = macros;
|
234
|
+
(macros)["return"] = (function(token) {
|
235
|
+
var defaultReturn = ("return " + translate(token));
|
236
|
+
return (function() {
|
237
|
+
if ((token) && (token).constructor.name === "Array") {
|
238
|
+
return (function() {
|
239
|
+
switch((token)[0]) {
|
240
|
+
case "return":
|
241
|
+
case "throw":
|
242
|
+
case "progn":
|
243
|
+
case "do":
|
244
|
+
return translate(token);
|
245
|
+
|
246
|
+
case "delete":
|
247
|
+
var deleteMacro = (macros)["delete"];
|
248
|
+
return (function() {
|
249
|
+
if ((token.length < 3)) {
|
250
|
+
return defaultReturn;
|
251
|
+
} else {
|
252
|
+
return (deleteMacro.apply(undefined, token.slice(1, -1)) + "\nreturn " + deleteMacro((token.slice(-1))[0]));
|
253
|
+
}
|
254
|
+
})();
|
255
|
+
|
256
|
+
case "setf":
|
257
|
+
case "assign":
|
258
|
+
return (function() {
|
259
|
+
if ((token.length < 4)) {
|
260
|
+
return defaultReturn;
|
261
|
+
} else {
|
262
|
+
return (macros.setf.apply(undefined, token.slice(1, (token.length - 2))) + "\nreturn " + macros.setf.apply(undefined, token.slice(-2)));
|
263
|
+
}
|
264
|
+
})();
|
265
|
+
|
266
|
+
case "set":
|
267
|
+
return (function() {
|
268
|
+
if ((token.length < 5)) {
|
269
|
+
return defaultReturn;
|
270
|
+
} else {
|
271
|
+
var obj = (token)[1],
|
272
|
+
nonReturnPart = token.slice(2, (token.length - 2)),
|
273
|
+
returnPart = token.slice(-2);
|
274
|
+
nonReturnPart.unshift(obj);
|
275
|
+
returnPart.unshift(obj);
|
276
|
+
return (macros.set.apply(undefined, nonReturnPart) + "\nreturn " + macros.set.apply(undefined, returnPart));
|
277
|
+
}
|
278
|
+
})();
|
279
|
+
|
280
|
+
default:
|
281
|
+
return defaultReturn;
|
282
|
+
}
|
283
|
+
})();
|
284
|
+
} else {
|
285
|
+
return defaultReturn;
|
286
|
+
}
|
287
|
+
})();
|
288
|
+
});
|
289
|
+
var asStatement = (function(string) {
|
290
|
+
return string
|
291
|
+
.toString()
|
292
|
+
.replace(/;*\s*$/, ";")
|
293
|
+
;
|
294
|
+
});
|
295
|
+
|
296
|
+
macros.statement = (function(args) {
|
297
|
+
var args = Array.prototype.slice.call(arguments, 0);
|
298
|
+
|
299
|
+
return (macros.call.apply(undefined, args) + ";\n");
|
300
|
+
});
|
301
|
+
|
302
|
+
macros.progn = (function(body) {
|
303
|
+
var body = Array.prototype.slice.call(arguments, 0);
|
304
|
+
|
305
|
+
var lastIndex = Math.max(0, (body.length - 1));
|
306
|
+
(body)[lastIndex] = [ "return", (body)[lastIndex] ];
|
307
|
+
return (map(body, (function(arg) {
|
308
|
+
return (asStatement(translate(arg)));
|
309
|
+
}))).join("\n");
|
310
|
+
});
|
311
|
+
|
312
|
+
macros.emptyList = (function() {
|
313
|
+
return "null";
|
314
|
+
});
|
315
|
+
|
316
|
+
macros.call = (function(fnName, args) {
|
317
|
+
var args = Array.prototype.slice.call(arguments, 1);
|
318
|
+
|
319
|
+
return (translate(fnName) + "(" + (map(args, translate)).join(", ") + ")");
|
320
|
+
});
|
321
|
+
|
322
|
+
macros.defun = (function(fnName, argsAndBody) {
|
323
|
+
var argsAndBody = Array.prototype.slice.call(arguments, 1);
|
324
|
+
|
325
|
+
var fnNameTr = translate(fnName),
|
326
|
+
start = (function() {
|
327
|
+
if (fnNameTr.match(/\./)) {
|
328
|
+
return "";
|
329
|
+
} else {
|
330
|
+
return "var ";
|
331
|
+
}
|
332
|
+
})();
|
333
|
+
return (start + fnNameTr + " = " + macros.lambda.apply(undefined, argsAndBody) + ";\n");
|
334
|
+
});
|
335
|
+
|
336
|
+
macros.defmacro = (function(name, argsAndBody) {
|
337
|
+
var argsAndBody = Array.prototype.slice.call(arguments, 1);
|
338
|
+
|
339
|
+
var js = macros.lambda.apply(undefined, argsAndBody),
|
340
|
+
name = translate(name);
|
341
|
+
(function() {
|
342
|
+
try {
|
343
|
+
return (macros)[name] = eval(js);
|
344
|
+
} catch (e) {
|
345
|
+
return error(("error in parsing macro " + name + ":\n" + indent(js)));
|
346
|
+
}
|
347
|
+
})();
|
348
|
+
return undefined;
|
349
|
+
});
|
350
|
+
|
351
|
+
(macros)["macro"] = macros.defmacro;
|
352
|
+
macros.concat = (function(args) {
|
353
|
+
var args = Array.prototype.slice.call(arguments, 0);
|
354
|
+
|
355
|
+
return ("(" + (map(args, translate)).join(" + ") + ")");
|
356
|
+
});
|
357
|
+
|
358
|
+
var transformArgs = (function(arglist) {
|
359
|
+
var last = undefined,
|
360
|
+
args = [ ];
|
361
|
+
arglist.forEach((function(arg) {
|
362
|
+
return (function() {
|
363
|
+
if (((arg)[0] === "&")) {
|
364
|
+
return last = arg.slice(1);
|
365
|
+
} else {
|
366
|
+
args.push([ (last || "required"), arg ]);
|
367
|
+
return last = null;
|
368
|
+
}
|
369
|
+
})();
|
370
|
+
}));
|
371
|
+
(function() {
|
372
|
+
if (last) {
|
373
|
+
return error(("unexpected argument modifier: " + last));
|
374
|
+
}
|
375
|
+
})();
|
376
|
+
return args;
|
377
|
+
});
|
378
|
+
|
379
|
+
macros.reverse = (function(arr) {
|
380
|
+
var reversed = [ ];
|
381
|
+
arr.forEach((function(item) {
|
382
|
+
return reversed.unshift(item);
|
383
|
+
}));
|
384
|
+
return reversed;
|
385
|
+
});
|
386
|
+
|
387
|
+
var reverse = macros.reverse;
|
388
|
+
var buildArgsString = (function(args, rest) {
|
389
|
+
var argsString = "",
|
390
|
+
optionalCount = 0;
|
391
|
+
args.forEach((function(arg, optionIndex) {
|
392
|
+
return (function() {
|
393
|
+
if (((arg)[0] === "optional")) {
|
394
|
+
argsString = (argsString + "if (arguments.length < " + (args.length - optionalCount) + ")" + indent(("var " + map(args.slice((optionIndex + 1)), (function(arg, argIndex) {
|
395
|
+
return (translate((arg)[1]) + " = " + translate(((args)[(optionIndex + argIndex)])[1]));
|
396
|
+
}))
|
397
|
+
.reverse()
|
398
|
+
.concat((translate((arg)[1]) + " = undefined"))
|
399
|
+
.join(", ")
|
400
|
+
+ ";")));
|
401
|
+
return ((optionalCount)++);
|
402
|
+
}
|
403
|
+
})();
|
404
|
+
}));
|
405
|
+
return (function() {
|
406
|
+
if ((typeof rest !== 'undefined')) {
|
407
|
+
return (argsString + "var " + translate((rest)[1]) + " = Array.prototype.slice.call(arguments, " + args.length + ");\n");
|
408
|
+
} else {
|
409
|
+
return argsString;
|
410
|
+
}
|
411
|
+
})();
|
412
|
+
});
|
413
|
+
|
414
|
+
macros.lambda = (function(arglist, body) {
|
415
|
+
var body = Array.prototype.slice.call(arguments, 1);
|
416
|
+
|
417
|
+
var args = transformArgs(arglist),
|
418
|
+
rest = (select(args, (function(arg) {
|
419
|
+
return ("rest" === (arg)[0]);
|
420
|
+
})))[0],
|
421
|
+
docString = undefined;
|
422
|
+
(body)[(body.length - 1)] = [ "return", (body)[(body.length - 1)] ];
|
423
|
+
(function() {
|
424
|
+
if (((typeof (body)[0] === "string") && (body)[0].match(/^".*"$/))) {
|
425
|
+
return docString = ("/* " + eval(body.shift()) + " */\n");
|
426
|
+
}
|
427
|
+
})();
|
428
|
+
var noRestArgs = (function() {
|
429
|
+
if (rest) {
|
430
|
+
return args.slice(0, -1);
|
431
|
+
} else {
|
432
|
+
return args;
|
433
|
+
}
|
434
|
+
})(),
|
435
|
+
argsString = buildArgsString(noRestArgs, rest);
|
436
|
+
return ("(function(" + (map(args, (function(arg) {
|
437
|
+
return translate((arg)[1]);
|
438
|
+
}))).join(", ") + ") {" + indent(docString, argsString, (map(body, (function(stmt) {
|
439
|
+
var tstmt = translate(stmt);
|
440
|
+
return (tstmt + (function() {
|
441
|
+
if (((tstmt.slice(-1))[0] === ";")) {
|
442
|
+
return "";
|
443
|
+
} else {
|
444
|
+
return ";";
|
445
|
+
}
|
446
|
+
})());
|
447
|
+
}))).join("\n")) + "})");
|
448
|
+
});
|
449
|
+
|
450
|
+
macros.quote = (function(item) {
|
451
|
+
return (function() {
|
452
|
+
if (("Array" === item.constructor.name)) {
|
453
|
+
return ("[ " + (map(item, macros.quote)).join(", ") + " ]");
|
454
|
+
} else {
|
455
|
+
return (function() {
|
456
|
+
if (("number" === typeof item)) {
|
457
|
+
return item;
|
458
|
+
} else {
|
459
|
+
return ("\"" + literal(item) + "\"");
|
460
|
+
}
|
461
|
+
})();
|
462
|
+
}
|
463
|
+
})();
|
464
|
+
});
|
465
|
+
|
466
|
+
macros.hash = (function(pairs) {
|
467
|
+
var pairs = Array.prototype.slice.call(arguments, 0);
|
468
|
+
|
469
|
+
(function() {
|
470
|
+
if ((0 !== (pairs.length % 2))) {
|
471
|
+
return error(("odd number of key-value pairs in hash: " + inspect(pairs)));
|
472
|
+
}
|
473
|
+
})();
|
474
|
+
var pairStrings = bulkMap(pairs, (function(key, value) {
|
475
|
+
return (translate(key) + ": " + translate(value));
|
476
|
+
}));
|
477
|
+
return (function() {
|
478
|
+
if ((1 >= pairStrings.length)) {
|
479
|
+
return ("{ " + (pairStrings).join(", ") + " }");
|
480
|
+
} else {
|
481
|
+
return ("{" + indent((pairStrings).join(",\n")) + "}");
|
482
|
+
}
|
483
|
+
})();
|
484
|
+
});
|
485
|
+
|
486
|
+
var literal = (function(string) {
|
487
|
+
return inject(string
|
488
|
+
.replace(/\*/g, "_")
|
489
|
+
.replace(/\?$/, "__QUERY")
|
490
|
+
.replace(/!$/, "__BANG")
|
491
|
+
, string.match(/-(.)/g), (function(returnString, match) {
|
492
|
+
return returnString.replace(match, (match)[1].toUpperCase());
|
493
|
+
}));
|
494
|
+
});
|
495
|
+
|
496
|
+
var translateListToken = (function(token, hint) {
|
497
|
+
return (function() {
|
498
|
+
if (((token).length === 0)) {
|
499
|
+
return macros.emptyList();
|
500
|
+
} else {
|
501
|
+
return (function() {
|
502
|
+
if ((typeof (macros)[translate((token)[0])] !== 'undefined')) {
|
503
|
+
return (macros)[translate((token)[0])].apply(undefined, token.slice(1));
|
504
|
+
} else {
|
505
|
+
return (macros)[(hint || "call")].apply(undefined, token);
|
506
|
+
}
|
507
|
+
})();
|
508
|
+
}
|
509
|
+
})();
|
510
|
+
});
|
511
|
+
|
512
|
+
var translateStringToken = (function(token, hint) {
|
513
|
+
return (function() {
|
514
|
+
if (token.match((new RegExp(("^" + sibilant.tokens.literal + "$"), undefined)))) {
|
515
|
+
return literal(token);
|
516
|
+
} else {
|
517
|
+
return (function() {
|
518
|
+
if (token.match((new RegExp("^;", undefined)))) {
|
519
|
+
return token.replace((new RegExp("^;+", undefined)), "//");
|
520
|
+
} else {
|
521
|
+
return (function() {
|
522
|
+
if (("\"" === (token)[0] &&
|
523
|
+
"\"" === (token.slice(-1))[0])) {
|
524
|
+
return token
|
525
|
+
.split("\n")
|
526
|
+
.join("\\n\" +\n\"");
|
527
|
+
} else {
|
528
|
+
return token;
|
529
|
+
}
|
530
|
+
})();
|
531
|
+
}
|
532
|
+
})();
|
533
|
+
}
|
534
|
+
})();
|
535
|
+
});
|
536
|
+
|
537
|
+
var translate = (function(token, hint) {
|
538
|
+
var hint = hint;
|
539
|
+
(function() {
|
540
|
+
if ((hint && (typeof (macros)[hint] === 'undefined'))) {
|
541
|
+
return hint = undefined;
|
542
|
+
}
|
543
|
+
})();
|
544
|
+
return (function() {
|
545
|
+
if ((typeof token !== 'undefined')) {
|
546
|
+
(function() {
|
547
|
+
if (typeof(token) === "string") {
|
548
|
+
return token = token.trim();
|
549
|
+
}
|
550
|
+
})();
|
551
|
+
return (function() {
|
552
|
+
try {
|
553
|
+
return (function() {
|
554
|
+
if ((token) && (token).constructor.name === "Array") {
|
555
|
+
return translateListToken(token, hint);
|
556
|
+
} else {
|
557
|
+
return (function() {
|
558
|
+
if (typeof(token) === "string") {
|
559
|
+
return translateStringToken(token, hint);
|
560
|
+
} else {
|
561
|
+
return token;
|
562
|
+
}
|
563
|
+
})();
|
564
|
+
}
|
565
|
+
})();
|
566
|
+
} catch (e) {
|
567
|
+
return error((e.stack + "\n" + "Encountered when attempting to process:\n" + indent(inspect(token))));
|
568
|
+
}
|
569
|
+
})();
|
570
|
+
}
|
571
|
+
})();
|
572
|
+
});
|
573
|
+
|
574
|
+
(sibilant)["translate"] = translate;
|
575
|
+
var translateAll = (function(contents) {
|
576
|
+
var buffer = "";
|
577
|
+
tokenize(contents).forEach((function(token) {
|
578
|
+
var line = translate(token, "statement");
|
579
|
+
return (function() {
|
580
|
+
if (line) {
|
581
|
+
return buffer = (buffer + line + "\n");
|
582
|
+
}
|
583
|
+
})();
|
584
|
+
}));
|
585
|
+
return buffer;
|
586
|
+
});
|
587
|
+
|
588
|
+
(sibilant)["translateAll"] = translateAll;
|
589
|
+
;
|
590
|
+
return (function() {
|
591
|
+
if ((typeof $ !== "undefined")) {
|
592
|
+
return $((function() {
|
593
|
+
var sibilant = root.sibilant,
|
594
|
+
scripts = [ ];
|
595
|
+
var evalWithTryCatch = (function(js) {
|
596
|
+
return (function() {
|
597
|
+
try {
|
598
|
+
return eval(js);
|
599
|
+
} catch (e) {
|
600
|
+
console.log(js);
|
601
|
+
throw new Error (e);
|
602
|
+
}
|
603
|
+
})();
|
604
|
+
});
|
605
|
+
;
|
606
|
+
sibilant.scriptLoaded = (function() {
|
607
|
+
var lisp = null,
|
608
|
+
js = null;
|
609
|
+
return (function() {
|
610
|
+
if ((!sibilant.loadNextScript())) {
|
611
|
+
return $("script[type=\"text/sibilant\"]:not([src])").each((function() {
|
612
|
+
lisp = $(this)
|
613
|
+
.text()
|
614
|
+
.replace(/(^\s*\/\/\<!\[CDATA\[)|(\/\/\]\]>\s*$)/g, "");
|
615
|
+
js = sibilant.translateAll(lisp);
|
616
|
+
$(this).data("js", js);
|
617
|
+
return evalWithTryCatch(js);
|
618
|
+
}));
|
619
|
+
}
|
620
|
+
})();
|
621
|
+
});
|
622
|
+
;
|
623
|
+
scripts = $.makeArray($("script[type=\"text/sibilant\"][src]").map((function() {
|
624
|
+
return this.src;
|
625
|
+
})));
|
626
|
+
sibilant.loadNextScript = (function() {
|
627
|
+
var nextScript = scripts.shift();
|
628
|
+
return (function() {
|
629
|
+
if ((typeof nextScript !== 'undefined')) {
|
630
|
+
$.get(nextScript, (function(data) {
|
631
|
+
evalWithTryCatch(sibilant.translateAll(data));
|
632
|
+
return sibilant.scriptLoaded();
|
633
|
+
}));
|
634
|
+
return true;
|
635
|
+
}
|
636
|
+
})();
|
637
|
+
});
|
638
|
+
;
|
639
|
+
return sibilant.loadNextScript();
|
640
|
+
}));
|
641
|
+
}
|
642
|
+
})();
|
643
|
+
})(this);
|
644
|
+
|