Repository navigation
Expand file tree
/
Copy pathjisp.jisp
More file actions
1098 lines (1023 loc) · 47.4 KB
/
Copy pathjisp.jisp
File metadata and controls
1098 lines (1023 loc) · 47.4 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; Version
(= exports.version "0.3.3")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Dependencies ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; External dependencies
(= vm (require "vm")
fs (require "fs")
path (require "path")
beautify (require "js-beautify"))
; Jisp dependencies
(= utils (require "./utils")
ops (require "./operators")
operators ops.operators
opFuncs ops.opFuncs
tokenise (require "./tokenise")
lex (require "./lex")
parse (require "./parse")
Uniq (require "./uniq"))
; Util
(= pr utils.pr ; this must be applied to EVERY form we render or print to console
spr utils.spr ; or this, depending on task
render utils.render
isAtom utils.isAtom
isHash utils.isHash
isList utils.isList
isVarName utils.isVarName
isIdentifier utils.isIdentifier
isService utils.isService
getServicePart utils.getServicePart
assertExp utils.assertExp
plusname utils.plusname)
; This file has gotten insanely big and repetitive, todo rearchitect and deduplicate
; ToDo move exports to a `jisp` object and export just that object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Globals ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Lists of built-in functions whose names have been overridden by an early `=` or `def`
(= functionsRedeclare `()
functionsRedefine `())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Helper Functions ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Checks if user-defined name in scope, puts name in declaration if not, otherwise drops it
(def declareVar name scope
(if (in name scope.hoist)
scope
(do (scope.hoist.push name)
scope)))
; Usage when declaring variable:
; (= scope (declareVar name scope))
; Checks if service name in scope, modifies name until it's not, puts into scope service
(def declareService name scope (do
(while (or (in name scope.hoist)
(in name scope.service))
(= name (plusname name)))
(scope.service.push name)
`(name scope)))
; Usage when declaring service variable:
; (= (name scope) (declareService candidate scope))
; Checks list of args, returns true if any of them are "spread" forms
(def hasSpread form
(and (isList form) (is (car form) "spread")))
; Compiles form, resolves naming conflicts, returns compiled, buffer, and modified scope
(def compileResolve form buffer scope opts nested (do
; Compile new form and modify scope
(= (compiled scope) (compileForm form scope opts nested))
; Check if newly hoisted vars overlap service vars in scope + buffer and rename accordingly
(over name i scope.service
(if (in name scope.hoist) (do
(= newname name)
(while (in newname scope.hoist)
(= newname (plusname newname)))
(= scope.service[i] newname
re (RegExp (+ "(?=(?:[^$#_A-Za-z0-9]{1}|^)" name "(?:[^$#_A-Za-z0-9]{1}|$))([^$#_A-Za-z0-9]|^)" name) "g") ; matches old name; probably also inside strings and regexes, todo check and fix
subst (+ "$1" newname))
(for str i buffer
(if (and (? str) (is (typeof str) "string")) (= buffer[i] (str.replace re subst)))))))
`(compiled buffer scope)))
; Compiles form and adds result to passed buffer and scope
(def compileAdd form buffer scope opts nested (do
(= (compiled buffer scope) (compileResolve form buffer scope opts nested))
(buffer.push ...compiled)
`(buffer scope)))
; Compiles given form, modifying buffer and scope, and splits off last expression
(def compileGetLast form buffer scope opts nested
(do (= (buffer scope) (compileAdd form buffer scope opts nested)
lastItem (buffer.pop))
`(lastItem buffer scope)))
; Prepends `return` to expression or form
(def returnify form
(if (or (isAtom form) (isHash form))
`("return" form)
(elif (and (isList form) (utils.isBlankObject form)) ; []
form)
(elif (and (isList form) (is form.length 1) (utils.isBlankObject (car form))) ; [[]]
(car form))
(elif (and (isList form) (isnt (car form) "return"))
`("return" form))
form))
(def getArgNames args
(do (= arr `())
(for arg args
(if (and (isAtom arg) (isVarName arg))
(arr.push arg)
(elif (and (isList arg) (isVarName (car arg))
(not (in (car arg) (Object.keys specials)))
(not (in (car arg) (Object.keys macros)))
(not (is (car arg) "mac")))
(arr.push (car arg)))))
arr))
; Checks if a functions built-in name has been redefined
(def notRedefined name
(and (not (in name functionsRedeclare))
(not (in name functionsRedefine))))
; Checks if a form begins with a property reference (for chaining)
(def isPropertyExp form
(and (isList form)
(or (and (isList (car form))
(is (car form).length 2)
(is (car (car form)) "get"))
(utils.isPropSyntax (car form)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Helper Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Shorter version of compileAdd
(mac macCompileAdd form (nested "nested")
`(= (buffer scope) (compileAdd ,form buffer scope opts ,nested)))
; Shorter version of compileGetLast
(mac macCompileGetLast form name (nested "nested")
`(= (,name buffer scope) (compileGetLast ,form buffer scope opts ,nested)))
; Shorter version of compileResolve
(mac macCompileResolve form container (nested "nested")
`(= (,container buffer scope) (compileResolve ,form buffer scope opts ,nested)))
; Wrapper for declareVar with checks to override built-in functions where relevant
(mac macDeclareVar name (override yes) (declare yes)
`(if (isService ,name) ; if service name like #ref, compile it; this declares it in service scope
(macCompileGetLast ,name ,name)
(do (assertExp ,name isVarName "valid identifier")
,(if override
`(if (and opts.topScope
(in ,name (Object.keys functions))
(not (in ,name scope.hoist))
(notRedefined ,name))
,(if declare
`(functionsRedeclare.push ,name)
`(functionsRedefine.push ,name))))
,(if declare
`(= scope (declareVar ,name scope))))))
; Shorter version of declareService
(mac macDeclareService name candidate
`(= (,name scope) (declareService ,candidate scope (if opts.function args))))
(mac macForkScope
; If this form declares variables, fork scope
`(do (= outerScope scope
scope (JSON.parse (JSON.stringify outerScope)))
(delete opts.topScope)))
(mac macDeclareOrHoist (dest "buffer") (do
; Declare vars and funcs or hoist funcs as appropriate
`(do
(= vars ``()
funcs ``()
dec "var ")
; Deal with new names: declare, drop, or bubble up if funcs
(if (?! args) (= args ``()))
(for name scope.hoist
(if (and (not (in name outerScope.hoist))
(not (in name args)))
(if (in name (Object.keys functions))
(if (and opts.topScope (in name functionsRedeclare))
(vars.push name)
(if (notRedefined name)
(funcs.push name)))
(elif (or (in name (Object.keys opFuncs))
(in name (Object.keys macros)))
(funcs.push name))
(if (not (is name "this"))
(vars.push name)))))
(for name scope.service
(if (not (in name outerScope.service))
(vars.push name)))
; Declare vars
(while (> vars.length 0) (do
(= name (vars.shift))
(if (in name vars)
(throw (Error (+ "compiler error: duplicate var in declarations:" name)))) ; no pr, expecting string
(+= dec (+ name ", "))))
(if (> dec.length 4)
(do (= dec (dec.slice 0 (- dec.length 2)))
(do ,dest (.unshift dec))))
; Declare funcs if functions, otherwise bubble all the way up
(if (and (? isTopLevel) isTopLevel)
(while (> funcs.length 0) (do
(= func (funcs.pop))
(if (in func funcs)
(throw (Error (+ "compiler error: duplicate func in declarations:" func)))) ; no pr, expecting string
(if (in func (Object.keys functions))
; embed only if not redeclared or redefined
(if (notRedefined func)
(if (and (? functions[func].name) (isnt functions[func].name ""))
; embed like named function
(do ,dest (.unshift (functions[func].toString)))
; embed like lambda
(if (isVarName func)
(do ,dest (.unshift (+ "var " func " = " (functions[func].toString) ";"))))))
(elif (and (in func (Object.keys macros)) (isVarName func))
(do ,dest (.unshift (+ "var " func " = " (macros[func].toString) ";"))))
; ToDo embedding of opFuncs with .toString and renaming of references in code when not first in list
(elif (and (in func (Object.keys opFuncs)) (isVarName func))
(do ,dest (.unshift (+ "var " opFuncs[func].name " = " (opFuncs[func].func.toString) ";")))) ; no pr, expecting strings
(throw (Error (+ "unrecognised func: " (pr func)))))))
(for func funcs
(if (not (in func outerScope.hoist))
(outerScope.hoist.push func))))
; Will return outer scope: nothing gets out except for funcs
(= scope outerScope)) ; / macro output
)) ; /macro
(mac macCompileSpecial opts (do
`(fn form scope (opts (:)) nested (do
(= buffer ``()
form (form.slice) ; duplicate object to avoid changing it for callers
formName (form.shift))
; Check args number if relevant
,(if opts.argsMin
`(if (< form.length ,opts.argsMin)
(throw (Error (+ (pr formName) " expects no less than " (pr ,opts.argsMin) " arguments")))))
,(if opts.argsMax
`(if (> form.length ,opts.argsMax)
(throw (Error (+ (pr formName) " expects no more than " (pr ,opts.argsMax) " arguments")))))
; Unset nested if passed, move value under another name to avoid propagating it further (compile macros pass it automatically)
(= nestedLocal (if (? nested) nested true)
nested undefined)
; Put in code from caller
,opts.code
; Return compiled
(Array buffer scope)
)) ; /lambda
)) ; /macro
(mac macMacroCheck form
; Check if a form is a macro call and expand it
`(if (and (isList ,form) (in (car ,form) (Object.keys macros)))
(macCompileGetLast ,form ,form)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Main Compiler Parts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Compiles form, switching between types
; Takes: <form to compile> <scope> <options>
; Returns: <array of compiled strings> <modified scope>
(def compileForm form scope (opts (:)) nested (do
; Switch between form types and compile accordingly
(if (and (isList form) (utils.isBlankObject form))
`(("") scope)
(elif (isAtom form) (do
; ToDo: check for reserved and forbidden words right here
; If name of toprange jisp func, hoist if not redefined earlier
; If name of operator, hoist appropriate func and replace atom with func name
(if (or (and (in form (Object.keys functions))
(notRedefined form))
(in form (Object.keys macros)))
(macDeclareVar form no)
; ToDo only do this when not first element in list
(elif (in form (Object.keys opFuncs))
(do
(macDeclareVar form no)
(= form opFuncs[form].name))))
; If name is a service name like #ref, declare it as a service var and replace the name
(if (isService form) (do
; Get service part of name
(= serv (getServicePart form)
re (RegExp (+ "^" serv)))
; Define corresponding service var, get replacement name
(if (not (in serv (Object.keys scope.replace)))
(macDeclareService scope.replace[serv] (cdr serv)))
; Replace service part of this name
(= form (form.replace re scope.replace[serv]))))
`((form) scope)))
(elif (isHash form) (do
(= buffer `())
; Unset nested
(= nested undefined)
(over val key form
(macCompileGetLast val form[key]))
(buffer.push form)
`(buffer scope)))
(do ; Assume list, either special form or function call
(if (not (isList form)) (throw (Error (+ "expecting list, got: " (pr form)))))
(= buffer `())
(= form (form.slice)) ; duplicate object to avoid changing it for callers
(if (in (car form) (Object.keys specials)) ; special forms have their own rules
(= (buffer scope) (specials[(car form)] form scope opts nested))
; if a macro definition, define it and revise the result
(elif (is (car form) "mac")
(macCompileAdd (parseMacros form)))
; if a known macro, expand it and revise the result
(elif (in (car form) (Object.keys macros))
(do
; fork the `replace` part of the scope to trigger new service declarations
; this ensures no overlaps in service names like #res between outer and inner code
(= oldReplace scope.replace
scope.replace (:))
(macCompileAdd (expandMacros form))
; revert to the outer `replace` store, dropping the inner store (which should be no longer relevant)
(= scope.replace oldReplace)))
(do ; Not a special form? -> function call
; Unset nested (callees assume true)
(= nestedLocal nested
nested undefined)
; Compile first element (compiles to itself if atom)
(macCompileGetLast (form.shift) first)
; If jisp-top-level func, hoist it
(if (and (in first (Object.keys functions))
(notRedefined first))
(macDeclareVar first no))
; If operator, check if we're inside compile call to another operator; if not, put this into options
(if (in first (Object.keys operators))
(do (if (not opts.compilingOperator) (= isOuterOperator yes))
(= innerType (or nestedLocal (is opts.compilingOperator))
opts.compilingOperator yes))
; If not operator, unset operator option for inner calls
(do (= opts (JSON.parse (JSON.stringify opts)))
(delete opts.compilingOperator)))
; Compile each element and test for spread
(for arg i form (do
(if (hasSpread arg)
(do
(= argsSpread true)
(macCompileGetLast arg arg)
(= form[i] `("spread" arg)))
(do
(macCompileGetLast arg arg)
(= form[i] arg)))))
; Compile: simple or with spread
(if (?! argsSpread)
(if (in first (Object.keys operators))
(buffer.push (operators[first] form innerType))
(buffer.push (+ (pr first) "(" (spr form) ")")))
(do ; Compile as spread
; Compile args into expression that produces single list with elements spread into it, like `(1 2 ...`(3 4)) -> `(1 2 3 4)
(= form `("quote" ,form))
(macCompileGetLast form form)
; Embed as function and replace `first`
; Only operators that take multiple arguments allow spread; others give a compile error
(if (in first (Object.keys operators))
; ToDo implement embedding as .toString and renaming of references which are not first element in list
(if (and (in first (Object.keys opFuncs))
(spr opFuncs[first]))
(do (macDeclareVar first no)
(= first opFuncs[first].name))
(throw (Error (+ (pr first) " can't spread arguments (yet)")))))
; Split object name and method for applying
(= split (utils.splitName first))
(if (> split.length 1)
(= method (split.pop)
name (split.join ""))
(= method ""
name (car split)))
; Apply, passing self and list of spread args
(if (isIdentifier name)
(buffer.push (+ name method ".apply(" name ", " (pr form) ")"))
(do (= (collector scope) (declareService "_ref" scope))
(buffer.push (+ "(" collector " = " name ")" method ".apply(" collector ", " (pr form) ")"))))))))
; Unset operator option for outer calls
(if (? isOuterOperator)
(delete opts.compilingOperator))
; Return compiled
`(buffer scope)))))
;;;;;;;;;;;;;;;;;;;;;;; Special Logic For Special Forms ;;;;;;;;;;;;;;;;;;;;;;;
(= specials (:))
(= specials.do (macCompileSpecial
(code: (do
(if opts.isTopLevel
(do (= isTopLevel true)
(delete opts.isTopLevel)))
(if isTopLevel (macForkScope))
(for exp i form (do
; `nested` tells the compiler whether the compiled form is going to be on its own line or merged with something
(= nested (or (and (not isTopLevel)
(is i (- form.length 1))
nestedLocal)
(isPropertyExp form[(+ i 1)])))
(if (?! exp)
(buffer.push exp)
(if (isPropertyExp exp)
; .dot or [bracket] notation: implicit reference to last object for method chaining
(do (= ref (buffer.pop))
(if (?! ref) (= ref ""))
(macCompileAdd exp)
(buffer.push (+ ref "\n" (buffer.pop))))
; simple sequence element
(macCompileAdd exp)))))
(if isTopLevel (macDeclareOrHoist))))))
(= specials.quote (macCompileSpecial (argsMin: 1 argsMax: 1
code: (do
(= form (car form))
(if (and (isAtom form) (not (utils.isPrimitive form)) (not (utils.isSpecialValue form)))
(buffer.push (JSON.stringify form)) ; identifiers and strings get additional quotes that soak up the additional rendering done when expanding a macro
(elif (isAtom form)
(buffer.push form))
(elif (isHash form)
(if (not opts.macro)
(do (over exp key form
(macCompileGetLast exp form[key]))
(buffer.push form))
; in macro: quote all elements in hash
(do (= newform (:))
(over exp key form (do
(= key (JSON.stringify key))
(macCompileGetLast `("quote" exp) newform[key])))
(buffer.push newform))))
; assume list
(do
; todo no concat if single exp
(= arr `() ; collector array literal
res "[]") ; collector string for .concat
(for exp form (do
(if (and (isList exp) (is (car exp) "quote")
(isList exp[1]) (is exp[1].length 0))
(arr.push `()) ; quoted empty list becomes empty array literal
(elif (and (isList exp) (is (car exp) "unquote")
(isList exp[1]) (is (car exp[1]) "spread"))
(do ; explicit unquote
; if someone puts more than a single element into a spread list, all but the first will be lost; todo assert number
(macCompileGetLast (car (cdr exp)) exp)
(if (? exp)
(do
(if (> arr.length 0)
(do (+= res (+ ".concat(" (pr arr) ")"))
(= arr `())))
(+= res (+ ".concat(" (pr exp) ")"))))))
(elif (and (isList exp) (is (car exp) "quote"))
(do (macCompileGetLast exp exp)
(if (? exp) (arr.push exp))))
(elif (and (isList exp) (is (car exp) "unquote"))
(do (macCompileGetLast exp exp)
(if (and (? exp) opts.macro)
(if (isList exp)
(for item i exp
; atoms need to be re-quoted after compilation
(if (isAtom item)
(macCompileGetLast `("quote" item) exp[i])))))
(if (? exp) (arr.push exp))))
(elif (and (isList exp) (is (car exp) "spread")
(not opts.macro))
(do ; implicit unquote outside macro
(macCompileGetLast exp exp)
(if (? exp)
(do
(if (> arr.length 0)
(do (+= res (+ ".concat(" (pr arr) ")"))
(= arr `())))
(+= res (+ ".concat(" (pr exp) ")"))))))
(do (if (and (isAtom exp) (not opts.macro))
(macCompileGetLast exp exp)
(macCompileGetLast `("quote" exp) exp))
(if (? exp) (arr.push exp))))))
(if (> arr.length 0)
(if (is res "[]") (= res (pr arr))
(+= res (+ ".concat(" (pr arr) ")"))))
(buffer.push res)))
))))
(= specials.unquote (macCompileSpecial (argsMin: 1 argsMax: 1
code: (do
(= form (car form))
(if (and (isList form) (is (car form) "quote"))
(macCompileGetLast form form))
(macCompileAdd form)))))
(= specials["="] (macCompileSpecial (argsMin: 1
; (= <name> <form> <name> <form> ...)
; (= <name>)
code: (do
(if (is form.length 1)
(do (macDeclareVar (car form))
(macCompileAdd (car form)))
(do
(assertExp form {is (% #0.length 2) 0} "an even number of arguments")
(while (> form.length 0) (do
(= left (form.shift)
right (form.shift))
(= lastAssign (if (is form.length 0) true))
(macCompileGetLast right right)
; Check and expand if has macros
(macMacroCheck left)
(if (and (isList left) (is (car left) "get")) ; property access
(do (macCompileGetLast left left)
(= res (+ (pr left) " = " (pr right)))
(if (and lastAssign nestedLocal (isnt nestedLocal "parens"))
(= res (+ "(" res ")")))
(buffer.push res))
(elif (isList left) (do ; destructuring assignment
(macDeclareService ref "_ref")
(macDeclareService ind "_i")
(buffer.push (+ ref " = " (pr right)))
(= spreads 0)
(for name i left
(if (is (car name) "spread")
(do
(if (> (++ spreads) 1) (throw (Error "an assignment can only have one spread")))
(macCompileGetLast name name)
(macDeclareVar name)
(= spreadname name
spreadind i)
(buffer.push (+ "var " spreadname " = " left.length " <= " ref ".length ? [].slice.call(" ref ", " spreadind ", " ind " = " ref ".length - " (- left.length spreadind 1) ") : (" ind " = " spreadind ", [])")))
(elif (?! spreadname)
(do (macCompileGetLast name name)
(if (isVarName name) (macDeclareVar name))
(buffer.push (+ (pr name) " = " ref "[" i "]"))))
(do (macCompileGetLast name name)
(if (isVarName name) (macDeclareVar name))
(buffer.push (+ (pr name) " = " ref "[" ind "++]")))))))
(do (if (isVarName left) (macDeclareVar left)) ; normal assignment
(assertExp left isIdentifier)
(= res (+ (pr left) " = " (pr right)))
(if (and (isHash right) (not nestedLocal))
(+= res ";")) ; a hash table assignment needs to end with a semicolon, otherwise the next line is considered a part of this one
(if (and lastAssign nestedLocal (isnt nestedLocal "parens"))
(= res (+ "(" res ")")))
(buffer.push res)))))))))))
; Mostly the same code for `fn` and `def`
(mac macFunctionDefinition (type "fn")
`(macCompileSpecial
(code: (do
; Fork scope in accordance with JS function scoping
(macForkScope)
,(if (is type "fn") ; different arg splitting for `fn` and `def`
`(= (...args body) form)
`(do
(= (fname ...args body) form)
(macDeclareVar fname yes no)))
(scope.hoist.push ...(getArgNames args))
(if (?! body) (= body ``()))
(= optionals ``()
spreads 0)
(for arg i args (do
; Expand if macro
(macMacroCheck arg)
(if (isList arg)
(do
(assertExp arg {is #0.length 2} "optional or rest parameter")
(if (is (car arg) "spread")
(do
(if (> (++ spreads) 1) (throw (Error "cannot define more than one rest parameter")))
(macDeclareService ind "_i")
(macCompileGetLast arg name)
(assertExp name isVarName "valid identifier")
(= restname name
restind i
args[i] restname)
(= rest (list (+ "var " name " = " args.length " <= arguments.length ? [].slice.call(arguments, " i ", " ind " = arguments.length - " (- args.length i 1) ") : (" ind " = " restind ", [])"))))
(do ; assume optional parameter
(assertExp (= name (car arg)) isVarName "valid parameter name")
(= args[i] name)
(optionals.push ``("if" ("?!" name) ("=" name arg[1]))))))
(elif restname
(rest.push (+ (pr arg) " = arguments[" ind "++]")))))) ; when there's a restname, `ind` is defined
(if (? restind) (= args (args.slice 0 restind))) ; drop restarg and all following args from arg list; prevents bug with empty rest
(if (> optionals.length 0) (= body ``("do" ...optionals body)))
(= body (returnify body)) ; put form into `("return" ...) special form
(macCompileResolve body body)
(if rest (body.unshift ...rest))
; Declare vars
(macDeclareOrHoist body)
,(if (is type "fn") ; different template for `fn` and `def`
`(buffer.push (+ "(function(" (spr args) ") {" (render body) " })"))
`(do
(buffer.push (+ "function " fname "(" (spr args) ") {" (render body) " }"))
(buffer.push fname)))))))
(= specials.fn (macFunctionDefinition))
; (fn <args> (<body>))
; (fn (<body>))
; (fn)
(= specials.def (macFunctionDefinition def))
; (def <name> <args> (<body>))
; (def <name> (<body>))
; (def <name>)
(= specials.mac (fn form
; (mac <args> (<body>))
; macro definition: makes a macro and returns `("")
(makeMacro form)))
; Puts collector variable in end of given branch
(def collect compiled collector (isCase no) (nestedLocal yes) (do
(if (and (isList compiled) (> compiled.length 0)) (do
; when I wrote this, only God and I knew what this /\{$/ logic was for
; now, God only knows
(if (/\{$/.test (last compiled))
(= plug (compiled.pop)))
(= lastItem (compiled.pop))
; collectify if we're nested
(if nestedLocal
(if (/^return\s/.test lastItem)
(= lastItem (lastItem.replace /^return\s/ (+ "return " collector " = ")))
(elif (utils.kwtest lastItem)
(= lastItem (+ collector " = undefined; " lastItem)))
(= lastItem (+ collector " = " (pr lastItem)))))
(compiled.push lastItem)
(if isCase (compiled.push "break"))
(if (? plug) (compiled.push plug))))
compiled))
(= specials.if (macCompileSpecial (code: (do
; (if <test> <then-branch> (elif <test> <elif-branch>)... <else-branch>)
; (if <test> <then-branch>) ;; <else-branch> = undefined
; todo if last form in function and no returns inside, end each branch with return instead of collector variable
; what arguments mean:
; predicate prebranch ...midcases postbranch
; test then-branch ...elifs else-branch
(= (predicate prebranch ...midcases postbranch) form)
(if (and (isList postbranch) (is (car postbranch) "elif"))
(do (midcases.push postbranch)
(= postbranch undefined)))
; prepare forms
; for ternary safety: compiled expressions could contain operators with a lower precedence than the ternary `?` or `:`
(= nested yes)
(macCompileGetLast predicate predicate)
(if (?! predicate) (= predicate "false")) ; JS requires something in that field
; if this isn't nested, further compiled branches should be aware of that
(= nested nestedLocal)
(macCompileResolve prebranch prebranch)
(macCompileResolve postbranch postbranch)
; choose between binary, ternary and full form
(if (and (isnt nestedLocal) ; binary form not an expression!
(<= prebranch.length 1)
(is midcases.length 0)
(or (is postbranch.length 0)
(?! (car postbranch))
(is (car postbranch) "")))
; binary
(do (= res (+ "if (" (pr predicate) ")" (pr (car prebranch)) ";")) ; print `undefined` if nothing
(buffer.push res "")) ; extra "" signals this is a multi-line statement
(elif (and (is prebranch.length 1) (not (utils.kwtest (car prebranch)))
(is midcases.length 0)
(is postbranch.length 1) (not (utils.kwtest (car postbranch))))
; ternary
(do (= res (+ (pr predicate) " ? "
(or (pr (car prebranch)) undefined) " : " ; print `undefined` if nothing
(or (pr (car postbranch)) undefined))) ; print `undefined` if nothing
(if (and nestedLocal (isnt nestedLocal "parens"))
(= res (+ "(" res ")")))
(buffer.push res)))
(do ; full form
(if nestedLocal (macDeclareService collector "_ref"))
(= prebranch (collect prebranch collector no nestedLocal))
(= postbranch (collect postbranch collector no nestedLocal))
(for mid i midcases (do
(assertExp mid (fn x (is (x.shift) "elif")) "elif")
(= (midtest midbranch) mid)
(macCompileResolve midtest midtest "parens")
(if (?! midtest) (= midtest "false")) ; JS requires something in that field
; temporary ban on more-than-single-expression tests in midcases
; todo implement later (check if can use multiple exps with commas)
(if (> midtest.length 1) (throw (Error (+ (pr "elif") " must compile to single expression (todo fix later); got:" (pr midtest)))))
(macCompileResolve midbranch midbranch)
(= midcases[i] (test: midtest branch: (collect midbranch collector no nestedLocal)))))
; compile full form
(= comp (+ "if (" (pr predicate) ") { " (render prebranch) " } "))
(for mid midcases
(+= comp (+ " else if (" (spr mid.test) ") { " (render mid.branch) " }")))
(if (and (? postbranch)
(or (> postbranch.length 1)
(? (car postbranch))))
(+= comp (+ " else { " (render postbranch) " }")))
(buffer.push comp)
(if nestedLocal
(buffer.push collector)
; more than 1 element in buffer signals to caller that this is not an expression
(buffer.push ""))))))))
(= specials.switch (macCompileSpecial (code: (do
; (switch <exp> (case <test> ... <test> <case-branch>) ... <def-branch>)
; switch <exp> (case <test> <case-branch) ... ) ;; <def-branch> = undefined
; todo multiple tests per case
; todo if last form in function and no returns inside, end each branch with return instead of collector variable
; what arguments mean
; predicate ...midcases postbranch
; test cases default
(= (predicate ...midcases postbranch) form)
(if (and (? postbranch) (is (car postbranch) "case"))
(do (midcases.push postbranch)
(= postbranch undefined)))
(if nestedLocal (macDeclareService collector "_ref"))
; prepare forms
(macCompileGetLast predicate predicate "parens")
(if (?! predicate) (= predicate "false")) ; JS requires something in that field
; if this isn't nested, further compiled branches should be aware of that
(= nested nestedLocal)
(for mid i midcases (do
(assertExp mid (fn x (is (x.shift) "case")) "case")
(= (midtest midbranch) mid)
(macCompileResolve midtest midtest)
(if (?! midtest) (= midtest "false")) ; JS requires something in that field
; temporary ban on more-than-single-expression tests in midcases
; todo implement later (check if can use multiple exps with commas)
(if (> midtest.length 1) (throw (Error (+ (pr "case") " must compile to single expression (todo fix later); got: " (pr midtest)))))
(macCompileResolve midbranch midbranch)
(= midcases[i] (test: midtest branch: (collect midbranch collector yes nestedLocal)))))
(macCompileResolve postbranch postbranch)
(= postbranch (collect postbranch collector no nestedLocal))
; compile
(= comp (+ "switch (" (pr predicate) ") { "))
(for mid midcases
(+= comp (+ " case " (spr mid.test) ": " (render mid.branch))))
(+= comp (+ " default: " (render postbranch) " }"))
(buffer.push comp)
(if nestedLocal
(buffer.push collector)
; more than 1 element in buffer signals to caller that this is not an expression
(buffer.push ""))))))
; Shared collector code for all loops
(mac macCollect
`(do
(= rear (body.pop))
(if (or (utils.isPrimitive rear)
(utils.isString rear)
(utils.isSpecialValue rear)
(utils.isSpecialValueStr rear))
; a literal; just push it
(body.push (+ collector ".push(" (pr rear) ")"))
; identifier, can be safely repeated in code, test it with the `?` macro, and push it
(elif (isIdentifier rear)
(do (macCompileGetLast ``("?" rear) tested "parens")
(body.push (+ "if (" tested ") " collector ".push(" (pr rear) ")"))))
; might contain side effects, reference it, test it, and push it
(do (macDeclareService subst "_ref")
(body.push (+ "if (typeof (" subst " = " (pr rear) ") !== 'undefined') "
collector ".push(" subst ")" ))))))
; Mostly the same code for `for` and `over`
(mac macLoopDefinition (type "for") (ind `"_i")
`(macCompileSpecial (argsMin: 2 argsMax: 4
code: (do
(= (value key iterable body) form)
,(if (is type "for")
`(if (?! body) ; assume <value> <iterable> <body>
(if (?! iterable) ; assume <integer> <body>
(do
(if (or (isNaN (Number value))
(not (> (parseInt value) 0)))
(throw (Error (+ "expecting integer, got " (pr value)))))
(= body key
iterable ``("quote" (range 1 (parseInt value))))
(macDeclareService key ,ind)
(macDeclareService value "_val"))
(do
(= body iterable
iterable key)
(macDeclareService key ,ind)
(macDeclareVar value)))
(do
(macDeclareVar key)
(macDeclareVar value)))
(elif (is type "over")
`(if (?! body) ; assume <value> <iterable> <body>
(do
(= body iterable
iterable key)
(macDeclareService key ,ind)
(macDeclareVar value))
(elif (?! iterable)
(do
(= body key
iterable value)
(macDeclareService key ,ind)
(macDeclareService value "_val")))
(do
(macDeclareVar key)
(macDeclareVar value)))))
(assertExp key isVarName "valid identifier")
(assertExp value isVarName "valid identifier")
(if nestedLocal
(do (macDeclareService collector "_res") ; array for iteration results
(buffer.push (+ collector " = []"))))
(macDeclareService ref "_ref") ; iterable
(macCompileGetLast iterable iterable)
(buffer.push (+ ref " = " (pr iterable)))
; if this isn't nested, further compiled body should be aware of that
(= nested nestedLocal)
(macCompileResolve body body)
; only collect if the form is nested, if the last expression isn't a keyword, and it's not undefined, unless it's a literal, in which case it's allowed to be undefined
(if (and nestedLocal (not (utils.kwtest (pr (last body)))))
(macCollect))
,(if (is type "for")
`(buffer.push (+ "for (" key " = 0; " key " < " ref ".length; ++" key ") { " value " = " ref "[" key "]; " (render body) " }"))
(elif (is type "over")
`(buffer.push (+ "for (" key " in " ref ") { " value " = " ref "[" key "]; " (render body) " }"))))
(if nestedLocal
(buffer.push collector)
; more than 1 element in buffer signals to caller that this is not an expression
(buffer.push ""))))))
(= specials.for (macLoopDefinition))
; (for <value> <index> <iterable> <body>)
; (for <value> <iterable> <body>)
; (for <integer> <body>) ;? todo change to <iterable> <body> for comprehended arrays
(= specials.over (macLoopDefinition over "_key"))
; (over <value> <key> <iterable> <body>)
; (over <value> <iterable> <body>)
; (over <iterable> <body>)
(= specials.while (macCompileSpecial (argsMin: 2 argsMax: 3
; (while <test> <body> <return_value>) -- returns return_value
; (while <test> <body>) -- returns array of body values
code: (do
(= (test body rvalue) form)
(if (is form.length 2) ; no rvalue: array mode
; checking by form length to allow user to pass `undefined` and `null` as rvalue
(if nestedLocal (do (macDeclareService collector "_res")
(buffer.push (+ collector " = []"))))
(= comp "")) ; rvalue: no array mode
(macCompileGetLast test test "parens")
; if this isn't nested, further compiled body should be aware of that
(= nested nestedLocal)
(macCompileResolve body body)
(if (and nestedLocal (is form.length 2) (not (utils.kwtest (pr (last body)))))
(macCollect))
(buffer.push (+ "while (" (pr test) ") { " (render body) " }"))
(if (is form.length 2)
(if nestedLocal
(buffer.push collector)
; more than 1 element in buffer signals to caller that this is not an expression
(buffer.push ""))
(do (macCompileResolve rvalue rvalue)
(buffer.push (render rvalue))))))))
(= specials.try (macCompileSpecial (argsMin: 1 argsMax: 3
; (try <try> (catch err <catch>) <finally>)
; (try <try> (catch err <catch>))
; (try <try> <catch> <finally>)
; (try <try> <catch>)
; (try <try>)
code: (do
(= (tryForm catchForm finalForm) form)
(macCompileResolve tryForm tryForm "parens")
(if nestedLocal (do (macDeclareService collector "_ref")
(tryForm.push (+ collector " = " (pr (tryForm.pop))))))
(if (and (isList catchForm) (is (car catchForm) "catch"))
(do
(assertExp catchForm {is #0.length 2 3} "valid catch form")
(= (catchForm err catchForm) catchForm)
(assertExp err isVarName "valid identifier"))
(macDeclareService err "_err")) ; shouldn't be declared, it's like an argument, todo fix
(if (?! catchForm) (= catchForm undefined))
; if this isn't nested, further compiled parts should be aware of that
(= nested nestedLocal)
(macCompileResolve catchForm catchForm)
(if (and nestedLocal (not (utils.kwtest (pr (last catchForm)))))
(catchForm.push (+ collector " = " (pr (catchForm.pop)))))
(if (? finalForm)
(do
(if (and (isList finalForm) (is (car finalForm) "finally"))
(do (assertExp finalForm {is #0.length 2})
(= finalForm (last finalForm))))
(macCompileResolve finalForm finalForm)
(if (and nestedLocal (not (utils.kwtest (pr (last finalForm)))))
(finalForm.push (+ collector " = " (pr (finalForm.pop)))))))
(= res (+ "try { " (render tryForm) " } catch (" (pr err) ") { " (render catchForm) " }"))
(if (? finalForm) (+= res (+ " finally { " (render finalForm) " }")))
(buffer.push res)
(if nestedLocal
(buffer.push collector)
; more than 1 element in buffer signals to caller that this is not an expression
(buffer.push ""))))))
(= specials.get (macCompileSpecial (argsMin: 1 argsMax: 2
; (get <object> <property>)
; (get <property>)
code: (do
(= (object property) form)
(if (?! property)
(= property object
object ""))
(macCompileGetLast object object)
(macCompileGetLast property property)
(assertExp object {? #0} "valid object")
(if (utils.isDotName property)
(buffer.push (+ (pr object) property))
(buffer.push (+ (pr object) "[" (pr property) "]")))))))
(= specials.spread (macCompileSpecial (argsMin: 1 argsMax: 1
code: (do
(= form (car form))
(if (isList form)
(macCompileAdd form)
(elif (isAtom form)
(buffer.push form))
(throw (Error (+ "spread requires atom, got: " (pr form)))))))))
(= specials.return (macCompileSpecial (argsMin: 0 argsMax: 1
code: (do
(if (isnt form.length 0)
(do
(macCompileGetLast (car form) form yes)
(if (not (utils.kwtest form))
(= form (+ "return " (pr form))))
(buffer.push form)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Macros ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Macro store
(= macros (:))
; Imports macros from given stores, overriding defaults
; Each store must be a hash table where keys are macro names and values are macro functions
(= exports.importMacros (def importMacros ...stores
(do (for store stores
(over val key store
(= macros[key] val)))
macros)))
; Import and merge macros
(importMacros (require "./macros"))
; Parses form for macro definitions and makes macros, removing definitions from source
(def parseMacros form
(do (if (utils.isHash form)
(over val key form
(= form[key] (parseMacros val)))
(elif (utils.isList form)
; standard definition: (mac x) -> ()
(if (is (car form) "mac")
(= form (makeMacro (cdr form)))
; self-expanding definition: ((mac x)) -> (x)
(elif (and (>= form.length 1)
(utils.isList (car form))
(is (car (car form)) "mac"))
(= (car form) (makeMacro (cdr (car form)) yes)))
; other cases; check recursively
(for val i form
(= form[i] (parseMacros val))))))
form))
(def makeMacro form selfExpand (do
(= (name ...body) form)
(if (?! name) (throw (Error "a macro requires a name")))
(if (?! body) (throw (Error "a macro requires a body")))
(= body `("do" ("fn" ,...body)))
(= (compiled scope) (compileForm body (hoist:`() service:`() replace:(:)) (macro: yes isTopLevel: yes)))
(= rendered (render compiled))
(= macros[name] (jispEval rendered))
(if selfExpand name `())))
; Parses form for known macros to expand, replacing macro calls with macro results
; Doesn't reparse after expansion
(def expandMacros form (do
(if (utils.isHash form) (do
(over val key form
(= form[key] (expandMacros val))))
(elif (utils.isList form)
(if (is (car form) "mac")
(= form (parseMacros form))
(elif (in (car form) (Object.keys macros)) (do
(= args (cdr form))
; (= args (concat ...(for element (cdr form)
; (if (and (isList element) (is (car element) "spread"))
; (if (isnt element.length 2)
; (throw Error (+ "expecting valid spread form, got:" (pr element)))
; element[1])
; `(element)))))
(= form (macros[(car form)] ...args))
(if (is (typeof form) "undefined") (= form `())))) ; no render
(for val i form
(= form[i] (expandMacros val))))))
form))
; Recursively travels code and expands all macros, resolving uniq name conflicts
(def macroexpand form (uniq (new Uniq undefined)) (do
(if (isAtom form)