-
Notifications
You must be signed in to change notification settings - Fork 11
/
JavascriptGeneration.ns
1061 lines (1056 loc) · 28.8 KB
/
JavascriptGeneration.ns
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
Newspeak3
'Root'
class JavascriptGeneration usingPlatform: platform = (
(* Building blocks of Javascript syntax trees and a tree writer. The nodes are intended to represent Javascript code to be generated rather than the result of parsing an arbitrary Javascript program, so there is no provision for some ungood things such as the with statement.
Copyright 2012 SAP AG.
Copyright 2013 Ryan Macnak
Licensed under the Apache License, Version 2.0 (the ''License''); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*)
|
private List = platform collections List.
private Map = platform collections Map.
syntax = Syntax new.
public factory = Factory new.
|) (
class Factory = (
(* Provides a set of methods to build Javascript ASTs by writing expressions more readable than those that instantiate AST classes directly. An instance is held onto by the containing module as the slot named 'factory'. It's convenient to bind that instance in a user module to a slot with a short name such as 'js', to create Javascript ASTs with expressions like
js block: {js return: (js ident: 'self')} *)
) (
public array: elements <List[Node]> ^<ArrayExpression> = (
elements do: [:each | assert: [each isKindOfJsNode] message: 'JS node expected'].
^syntax ArrayExpression elements: elements
)
public assign: lhs <Node> toBe: rhs <Node> ^<Node> = (
assert: [lhs isKindOfJsNode] message: 'JS node expected on left'.
assert: [rhs isKindOfJsNode] message: 'JS node expected on right'.
^syntax AssignmentExpression leftHandSide: lhs rightHandSide: rhs
)
public block: statements <List[Node]> ^<Node> = (
statements do: [:each | assert: [each isKindOfJsNode] message: 'Malformed statement tree'].
^syntax Block statements: statements
)
public call: expression <Node> with: arguments <List[Node]> = (
^syntax CallExpression function: expression arguments: arguments
)
public for: varName <String> in: expr <Node> do: bodyStmt <Node> = (
| canonicalVarName |
canonicalVarName:: varName isNil
ifTrue: [varName]
ifFalse: [varName isKindOfString ifTrue: [ident: varName] ifFalse: [varName]].
^syntax ForInStatement
varName: canonicalVarName
expression: expr
body: bodyStmt
)
public for: initExpr while: testExpr step: incExpr do: bodyStmt = (
^syntax ForStatement
initExpression: initExpr
test: testExpr
increment: incExpr
body: bodyStmt
)
public function: name <String> of: params body: body <Block> = (
| canonicalName canonicalParams |
canonicalName:: name isNil
ifTrue: [name]
ifFalse: [name isKindOfString ifTrue: [ident: name] ifFalse: [name]].
canonicalParams:: params collect: [:each | each isKindOfString ifTrue: [ident: each] ifFalse: [each]].
^syntax FunctionStatement name: canonicalName parameters: canonicalParams body: body
)
public functionOf: params body: body <Block> = (
| paramIds |
paramIds:: params collect: [:each | each isKindOfString ifTrue: [ident: each] ifFalse: [each]].
^syntax FunctionExpression name: nil parameters: paramIds body: body
)
public ident: name <String> ^<Node> = (
assert: [name isKindOfString] message: 'name must be a string'.
^syntax IdentifierExpression name: name
)
public if: expression <Node> then: then <Node> ^<Node> = (
^syntax IfStatement expression: expression then: then else: nil
)
public if: expression <Node> then: then <Node> else: else <Node> ^<Node> = (
^syntax IfStatement expression: expression then: then else: else
)
public literal: value <Boolean | Integer | String> ^<Node> = (
value isKindOfString ifTrue: [^syntax StringLiteral value: value].
value isKindOfNumber ifTrue: [^syntax NumberLiteral value: value].
(true = value or: [false = value]) ifTrue: [^syntax BooleanLiteral value: value].
error: 'this value cannot be a Javascript literal'
)
public new: expression <Node> with: args <List[Node]> ^<Node> = (
^syntax NewExpression expression: expression arguments: args
)
public objectLiteral = (
^syntax ObjectLiteral new
)
public objectLiteral: slots <List> = (
slots keysAndValuesDo:
[:index : element |
assert: (index odd
ifTrue: [[element isKindOfString]]
ifFalse: [[element isKindOfJsNode]]) message: 'JS node or string expected'].
^syntax ObjectLiteral slots: slots
)
public objectLiteralSlotNames: names <List[String]> values: values <List[Node]> = (
^syntax ObjectLiteral slotNames: names values: values
)
public operator: name <String> with: left <Node> and: right <Node> = (
| canonicalName |
canonicalName:: name isNil
ifTrue: [name]
ifFalse: [name isKindOfString ifTrue: [ident: name] ifFalse: [name]].
assert: [left isKindOfJsNode] message: 'JS node expected on left'.
assert: [right isKindOfJsNode] message: 'JS node expected on right'.
^syntax OperatorExpression operator: canonicalName with: left and: right
)
public postfixOperator: name <String> on: operand <Node> = (
| canonicalName |
canonicalName:: name isNil
ifTrue: [name]
ifFalse: [name isKindOfString ifTrue: [ident: name] ifFalse: [name]].
^syntax UnaryOperatorExpression operator: canonicalName postfix: true on: operand
)
public prefixOperator: name <String> on: operand <Node> = (
| canonicalName |
canonicalName:: name isNil
ifTrue: [name]
ifFalse: [name isKindOfString ifTrue: [ident: name] ifFalse: [name]].
^syntax UnaryOperatorExpression operator: canonicalName postfix: false on: operand
)
public propertyOf: expression <Node> at: indexExpr <Node> = (
assert: [expression isKindOfJsNode] message: 'JS node expected'.
assert: [indexExpr isKindOfJsNode] message: 'JS node expected for index expression'.
^syntax MemberExpression primary: expression property: indexExpr
)
public propertyOf: expression <Node> atPath: path <List[Node]> = (
assert: [expression isKindOfJsNode] message: 'JS node expected'.
^path isEmpty
ifTrue: [expression]
ifFalse:
[propertyOf: (propertyOf: expression at: path first)
atPath: (path copyFrom: 2 to: path size)]
)
public return = (
^syntax ReturnStatement expression: nil
)
public return: expression <Node | nil> = (
^syntax ReturnStatement expression: expression
)
public script: statements <List[Node]> = (
^syntax Script statements: statements
)
public ternaryIf: condition then: then else: else = (
^syntax TernaryOperatorExpression
if: condition
then: then
else: else
)
public throw: expression <Node> ^<Node> = (
^syntax ThrowStatement expression: expression
)
public try: block <Block> catch: varName <String> with: catchBlock <Block> = (
| canonicalVarName |
canonicalVarName:: varName isNil
ifTrue: [varName]
ifFalse: [varName isKindOfString ifTrue: [ident: varName] ifFalse: [varName]].
^syntax TryStatement block: block catch: canonicalVarName with: catchBlock finally: nil
)
public try: block <Block> catch: varName <String> with: catchBlock <Block> finally: finallyBlock <Block> = (
| canonicalVarName |
canonicalVarName:: varName isNil
ifTrue: [varName]
ifFalse: [varName isKindOfString ifTrue: [ident: varName] ifFalse: [varName]].
^syntax TryStatement block: block catch: canonicalVarName with: catchBlock finally: finallyBlock
)
public try: block <Block> finally: finallyBlock <Block> = (
^syntax TryStatement block: block catch: nil with: nil finally: finallyBlock
)
public var: name <String> ^<VariableStatement> = (
| canonicalName |
canonicalName:: name isNil
ifTrue: [name]
ifFalse: [name isKindOfString ifTrue: [ident: name] ifFalse: [name]].
^syntax VariableStatement name: canonicalName initializer: nil
)
public var: name <String> value: expr <Node> ^<VariableStatement> = (
| canonicalName |
canonicalName:: name isNil
ifTrue: [name]
ifFalse: [name isKindOfString ifTrue: [ident: name] ifFalse: [name]].
^syntax VariableStatement name: canonicalName initializer: expr
)
public verbatim: text <String> = (
assert: [text isKindOfString] message: 'String expected'.
^syntax VerbatimNode text: text
)
) : (
)
class Syntax = (
(* The AST nodes. The preferred way to construct instances is to use the factory object (an instance of Factory, a sibling of this class) held onto by the module. *)
) (
public class ArrayExpression elements: elements = Node (
(* An array constructor expression. *)
|
protected elements_ = elements.
|) (
public = anotherNode = (
^super = anotherNode and: [elements = anotherNode elements]
)
public elements = (
^elements_
)
public visitBy: visitor = (
^visitor visitArrayExpression: self
)
) : (
)
public class AssignmentExpression leftHandSide: lhs rightHandSide: rhs = Node (|
public leftHandSide <Expression> = lhs.
public rightHandSide <Expression> = rhs.
|) (
public = anotherNode = (
^super = anotherNode
and: [leftHandSide = anotherNode leftHandSide
and: [rightHandSide = anotherNode rightHandSide]]
)
public isKindOfOperatorExpression = (
^true
)
public printOn: stream = (
super printOn: stream.
stream nextPutAll: ' ('.
leftHandSide printOn: stream.
stream nextPutAll: ', '.
rightHandSide printOn: stream.
stream nextPutAll: ')'
)
public visitBy: visitor = (
^visitor visitAssignmentExpression: self
)
) : (
)
public class Block statements: s = Node (
(* Block; ECMA-262 p. 220. *)
|
statementsS <List[Statement | Expression]> = s.
|) (
public = anotherNode = (
^super = anotherNode and: [statements = anotherNode statements]
)
public asBlock = (
^self
)
public statements = (
^statementsS
)
public visitBy: visitor = (
^visitor visitBlock: self
)
) : (
public empty = (
^Block statements: {}
)
)
public class BooleanLiteral value: b <Boolean> = Node (|
valueS = b.
|) (
public = anotherNode = (
^super = anotherNode and: [value = anotherNode value]
)
public printOn: stream = (
super printOn: stream.
stream nextPutAll: ' ('.
value printOn: stream.
stream nextPutAll: ')'
)
public value = (
^valueS
)
public visitBy: visitor = (
^visitor visitBooleanLiteral: self
)
) : (
)
public class CallExpression function: f arguments: args = Node (
(* expression ( arguments ) *)
|
public function <Expression> = f.
public arguments <List[Expression]> = args.
|) (
public = anotherNode = (
^super = anotherNode and:
[function = anotherNode function and: [arguments = anotherNode arguments]]
)
public visitBy: visitor = (
^visitor visitCallExpression: self
)
) : (
)
public class DeleteExpression memberExpression: m = Node (
(* AST node for the delete operator. *)
|
public memberExpression <MemberExpression> = m.
|) (
public = anotherNode = (
^super = anotherNode and:
[memberExpression = anotherNode memberExpression]
)
public visitBy: visitor = (
^visitor visitDeleteExpression: self
)
) : (
)
public class ForInStatement varName: name <IdentifierExpression> expression: expr body: body = Node (|
public varName = name.
public expression = expr.
public body = body.
|) (
public = anotherNode = (
^super = anotherNode and:
[varName = anotherNode varName and:
[expression = anotherNode expression and:
[body = anotherNode body]]]
)
public visitBy: visitor = (
^visitor visitForInStatement: self
)
) : (
)
public class ForStatement initExpression: initExpr test: testExpr increment: incExpr body: body = Node (|
public initExpression = initExpr.
public testExpression = testExpr.
public incrementExpression = incExpr.
public body = body.
|) (
public = anotherNode = (
^super = anotherNode and:
[initExpression = anotherNode initExpression and:
[testExpression = anotherNode testExpression and:
[incrementExpression = anotherNode incrementExpression and:
[body = anotherNode body]]]]
)
public visitBy: visitor = (
^visitor visitForStatement: self
)
) : (
)
public class FunctionExpression name: s <IdentifierExpression | nil> parameters: names <List[IdentifierExpression]> body: b = Node (
(* FunctionExpression. ECMA-262, p. 262. *)
|
public name <IdentifierExpression | nil> = s.
public parameters <List[IdentifierExpression]> = names.
public body <Block> = b asBlock.
|) (
public = anotherNode = (
^super = anotherNode and:
[name = anotherNode name and:
[parameters = anotherNode parameters and:
[body = anotherNode body]]]
)
public isKindOfFunctionExpression = (
^true
)
public visitBy: visitor = (
^visitor visitFunctionExpression: self
)
) : (
public parameters: names <List[IdentifierExpression]> body: b <Node> = (
^FunctionExpression name: nil parameters: names body: b
)
)
public class FunctionStatement name: s parameters: names body: b = FunctionExpression name: s parameters: names body: b (
(* A function statement. Essentially the same thing as a function expression, but needs to be written out differently. Note that unlike in a function expression, the name argument is not optional. TODO: should we move name up to this class and make function expressions anonymous? What does the standard say? *)
) (
public visitBy: visitor = (
^visitor visitFunctionStatement: self
)
) : (
)
public class IdentifierExpression name: s <String> = Node (
(* a case of PrimaryExpression *)
|
nameS = s.
|) (
public = anotherNode = (
^super = anotherNode and: [name = anotherNode name]
)
public name = (
^nameS
)
public printOn: stream = (
super printOn: stream.
stream nextPutAll: ' ('.
stream nextPutAll: name.
stream nextPutAll: ')'
)
public visitBy: visitor = (
^visitor visitIdentifierExpression: self
)
) : (
)
public class IfStatement expression: expr then: then else: else = Node (
(* if ( expression ) thenStatement else elseStatement *)
|
public expression = expr.
public thenStatement = then.
public elseStatement = else.
|) (
public = anotherNode = (
^super = anotherNode and:
[expression = anotherNode expression and:
[thenStatement = anotherNode thenStatement and:
[elseStatement = anotherNode elseStatement]]]
)
public visitBy: visitor = (
^visitor visitIfStatement: self
)
) : (
public expression: expr then: then = (
^IfStatement expression: expr then: then else: nil
)
)
public class MemberExpression primary: expr <Expression> property: p <Node> = Node (
(* primary[property] *)
|
public primary = expr.
public property = p.
|) (
public = anotherNode = (
^super = anotherNode and:
[primary = anotherNode primary and:
[property = anotherNode property]]
)
public visitBy: visitor = (
^visitor visitMemberExpression: self
)
) : (
)
public class NewExpression expression: expr arguments: args = Node (
(* new expr ( arg, .... ) *)
|
public expression = expr.
public arguments = args.
|) (
public = anotherNode = (
^super = anotherNode and:
[expression = anotherNode expression and:
[arguments = anotherNode arguments]]
)
public visitBy: visitor = (
^visitor visitNewExpression: self
)
) : (
)
class Node = (
(* The superclass of all other nodes, both statements and expressions. *)
|
public comment <String>
|) (
public = anotherNode = (
^class = anotherNode class
)
public asBlock = (
^Block statements: {self}
)
public isKindOfFunctionExpression = (
^false
)
public isKindOfJsNode = (
^true
)
public isKindOfNumberLiteral ^<Boolean> = (
^false
)
public isKindOfOperatorExpression = (
^false
)
public isKindOfStringLiteral = (
^false
)
) : (
)
public class NumberLiteral value: v <Number> = Node (|
valueS = v.
|) (
public = anotherNode = (
^super = anotherNode and:
[value = anotherNode value]
)
public isKindOfNumberLiteral ^<Boolean> = (
^true
)
public printOn: stream = (
super printOn: stream.
stream nextPutAll: ' ('.
value printOn: stream.
stream nextPutAll: ')'
)
public value = (
^valueS
)
public visitBy: visitor = (
^visitor visitNumberLiteral: self
)
) : (
)
public class ObjectLiteral = Node (
(* Object literal. ECMA-262, p. 216. *)
|
public slotNames = List new.
public slotValues = List new.
|) (
public = anotherNode = (
^super = anotherNode and:
[slotNames = anotherNode slotNames and:
[slotValues = anotherNode slotValues]]
)
public addSlot: name <String> value: object = (
slotNames addLast: name.
slotValues addLast: object
)
public slotCount = (
^slotNames size
)
public slotsDo: aBlock = (
(* Invoke a two-argument aBlock with the name and the value of each slot in this literal. *)
^slotNames with: slotValues do: aBlock
)
public slotsDo: aBlock betweenDo: betweenBlock = (
(* Invoke a two-argument aBlock with the name and the value of each slot in this literal. Between the invocations (but not after the last one), invoke the betweenBlock. *)
| lastIndex |
lastIndex:: slotNames size.
1 to: lastIndex do:
[:index |
aBlock value: (slotNames at: index) value: (slotValues at: index).
index = lastIndex ifFalse: [betweenBlock value]]
)
public visitBy: visitor = (
^visitor visitObjectLiteral: self
)
) : (
public slotNames: names values: values = (
| instance |
instance:: ObjectLiteral new.
1 to: names size do:
[:index | instance addSlot: (names at: index) value: (values at: index)].
^instance
)
public slots: namesAndValues = (
| instance |
namesAndValues size odd ifTrue: [error: 'invalid slot initialization data'].
instance:: ObjectLiteral new.
1 to: namesAndValues size by: 2 do:
[:i |
instance addSlot: (namesAndValues at: i) value: (namesAndValues at: i + 1)].
^instance
)
)
public class OperatorExpression operator: name <String> with: left <Node> and: right <Node> = Node (|
public operatorName = name.
public left = left.
public right = right.
|) (
public = anotherNode = (
^super = anotherNode and:
[operatorName = anotherNode operatorName and:
[left = anotherNode left and:
[right = anotherNode right]]]
)
public isKindOfOperatorExpression = (
^true
)
public visitBy: visitor = (
^visitor visitOperatorExpression: self
)
) : (
)
public class ReturnStatement expression: expr = Node (
(* return expression/opt. *)
|
expressionS = expr.
|) (
public = anotherNode = (
^super = anotherNode and: [expression = anotherNode expression]
)
public expression = (
^expressionS
)
public printOn: stream = (
super printOn: stream.
stream nextPutAll: ' ('.
expression printOn: stream.
stream nextPutAll: ')'
)
public visitBy: visitor = (
^visitor visitReturnStatement: self
)
) : (
)
public class Script statements: statements <List[Node]> = Node (
(* A series of statements, a top-level node to hold them together. *)
|
statements_ = statements.
|) (
public = anotherNode = (
^super = anotherNode and: [statements = anotherNode statements]
)
public statements ^<List[Node]> = (
^statements_
)
public visitBy: visitor = (
^visitor visitScript: self
)
) : (
)
public class StringLiteral value: string <String> = Node (|
valueS = string.
|) (
public = anotherNode = (
^super = anotherNode and:
[value = anotherNode value]
)
public isKindOfStringLiteral ^<Boolean> = (
^true
)
public printOn: stream = (
super printOn: stream.
stream nextPutAll: ' ('.
value printOn: stream.
stream nextPutAll: ')'
)
public value = (
^valueS
)
public visitBy: visitor = (
^visitor visitStringLiteral: self
)
) : (
)
public class TernaryOperatorExpression if: condExpr then: thenExpr else: elseExpr = Node (|
public condition = condExpr.
public then = thenExpr.
public else = elseExpr.
|) (
public = anotherNode = (
^super = anotherNode and:
[condition = anotherNode condition and:
[then = anotherNode then and:
[else = anotherNode else]]]
)
public visitBy: visitor = (
^visitor visitTernaryOperatorExpression: self
)
) : (
)
public class ThrowStatement expression: expr = Node (|
expressionS = expr.
|) (
public = anotherNode = (
^super = anotherNode and: [expression = anotherNode expression]
)
public expression = (
^expressionS
)
public visitBy: visitor = (
^visitor visitThrowStatement: self
)
) : (
)
public class TryStatement block: pb <Block> catch: cvar <IdentifierExpression | nil> with: cb <Block | nil> finally: fb <Block | nil> = Node (
(* ECMA-262 p. 222 *)
|
public protectedBlock = pb.
public catchVar = cvar.
public catchBlock = cb.
public finallyBlock = fb.
|) (
public = anotherNode = (
^super = anotherNode and:
[protectedBlock = anotherNode protectedBlock and:
[catchVar = anotherNode catchVar and:
[catchBlock = anotherNode catchBlock and:
[finallyBlock = anotherNode finallyBlock]]]]
)
public visitBy: visitor = (
^visitor visitTryStatement: self
)
) : (
)
public class UnaryOperatorExpression operator: name <String> postfix: postfix <Boolean> on: node <Node> = Node (
(* A unary operator application, either prefix or postfix. *)
|
public operatorName <String> = name.
public isPostfix <Boolean> = postfix.
public operand <Node> = node.
|) (
public = anotherNode = (
^super = anotherNode and:
[operatorName = anotherNode operatorName and:
[isPostfix = anotherNode isPostfix and:
[operand = anotherNode operand]]]
)
public visitBy: visitor = (
^visitor visitUnaryOperatorExpression: self
)
) : (
)
public class VariableStatement name: n <IdentifierExpression> initializer: expr <Expression> = Node (
(* var name [= expression] *)
|
public name = n.
public initializer = expr.
|) (
public = anotherNode = (
^super = anotherNode and:
[name = anotherNode name and:
[initializer = anotherNode initializer]]
)
public printOn: stream = (
super printOn: stream.
stream nextPutAll: ' ('.
name printOn: stream.
stream nextPutAll: ', '.
initializer printOn: stream.
stream nextPutAll: ')'
)
public visitBy: visitor = (
^visitor visitVariableStatement: self
)
) : (
public name: s = (
^VariableStatement name: s initializer: nil
)
)
public class VerbatimNode text: s <String> = Node (
(* A node holding onto arbitrary text, to be emitted verbatim into the generated Javascript source. Nothing but the text is emitted, however the context may insert additional tokens before or after the text. For example if a verbatim node appears as a statement in a block, it will have a terminating semicolon. *)
|
public text = s.
|) (
public = anotherNode = (
^super = anotherNode and: [text = anotherNode text]
)
public visitBy: visitor = (
^visitor visitVerbatimNode: self
)
) : (
)
) : (
)
public class Writer = (
(* Writes out a tree of Javascript syntax elements. *)
|
private output <StringBuilder>
private tabLevel <Integer>
private stringTranslation = Map new.
|stringTranslation at: "\" put: '\\'.
stringTranslation at: String cr put: '\r'.
stringTranslation at: String lf put: '\n'.
stringTranslation at: """ put: '\"') (
beginNewWriteCycleUsing: sb <StringBuilder> = (
output:: sb.
tabLevel:: 0.
)
contents ^<String> = (
(* Answer the Javascript source generated last. *)
^output asString
)
cr = (
(* Write a newline and the next line's indentation. *)
output add: String lf.
tabLevel timesRepeat: [output add: ' ']
)
public generateSourceFor: node <Node> on: sb <StringBuilder> = (
(* Generate Javascript source to represent the node, writing it out to the stream. *)
beginNewWriteCycleUsing: sb.
node visitBy: self.
)
inIndentedBlock: closure = (
write: '{'.
indentCr.
^closure ensure:
[unindentCr.
write: '}']
)
indentCr = (
tabLevel: tabLevel + 1.
cr
)
unindentCr = (
tabLevel:: 0 max: tabLevel - 1.
cr
)
public visitArrayExpression: node <ArrayExpression> = (
| elements |
elements: node elements.
elements isEmpty ifTrue:
[^write: '[]'].
elements size = 1 ifTrue:
[write: '['.
elements first visitBy: self.
^write: ']'].
write: '['.
indentCr.
elements
do:
[:each | each visitBy: self]
separatedBy:
[write: ','.
cr].
unindentCr.
write: ']'
)
public visitAssignmentExpression: node <AssignmentExpression> = (
node leftHandSide visitBy: self.
write: ' = '.
node rightHandSide visitBy: self.
)
public visitBlock: node <Block> = (
| statements |
statements: node statements.
statements isEmpty ifTrue:
[^write: '{}'].
write: '{'.
indentCr.
statements
do:
[:each |
each visitBy: self.
write: ';']
separatedBy:
[cr].
unindentCr.
write: '}'
)
public visitBooleanLiteral: node <BooleanLiteral> = (
write:: node value ifTrue: ['true'] ifFalse: ['false']
)
public visitCallExpression: node <CallExpression> = (
| parenthesize |
parenthesize:: node function isKindOfFunctionExpression.
parenthesize ifTrue: [write: '('].
node function visitBy: self.
parenthesize ifTrue: [write: ')'].
write: '('.
node arguments
do:
[:arg |
arg visitBy: self]
separatedBy:
[write: ', '].
write: ')'
)
public visitDeleteExpression: node <DeleteExpression> = (
write: 'delete '.
node memberExpression visitBy: self.
)
public visitForInStatement: node = (
write: 'for ('.
node varName visitBy: self.
write: ' in '.
node expression visitBy: self.
write: ') '.
node body asBlock visitBy: self.
)
public visitForStatement: node = (
write: 'for ('.
node initExpression visitBy: self.
write: '; '.
node testExpression visitBy: self.
write: '; '.
node incrementExpression visitBy: self.
write: ') '.
node body asBlock visitBy: self.
)
public visitFunctionExpression: node <FunctionExpression> = (
visitFunctionStatement: node.
)
public visitFunctionStatement: node <FunctionStatement> = (
write: 'function '.
node name ifNotNil:
[:name |
name visitBy: self.
write: ' '].
write: '('.
node parameters
do: [:each | each visitBy: self]
separatedBy: [write: ', '].
write: ') '.
writeBlock: node body.
)
public visitIdentifierExpression: node <IdentifierExpression> = (
write: node name
)
public visitIfStatement: node <IfStatement> = (
write: 'if ('.
node expression visitBy: self.
write: ') '.
writeBlock: node thenStatement asBlock.
node elseStatement ifNotNil:
[:else |
write: ' else '.
writeBlock: node elseStatement asBlock]
)
public visitMemberExpression: node <MemberExpression> = (
| parenthesize |
parenthesize:: (node primary isKindOfNumberLiteral
or: [node primary isKindOfOperatorExpression])
or: [node primary isKindOfFunctionExpression].
parenthesize ifTrue: [write: '('].
node primary visitBy: self.
parenthesize ifTrue: [write: ')'].
(node property isKindOfStringLiteral and: [isLegalIdentifier: node property value])
ifTrue: [write: '.'.
write: node property value]
ifFalse: [write: '['.
node property visitBy: self.
write: ']']
)
public visitNewExpression: node <NewExpression> = (
write: 'new '.
node expression visitBy: self.
write: '('.
node arguments
do:
[:arg |
arg visitBy: self]
separatedBy:
[write: ', '].
write: ')'
)
public visitNumberLiteral: node <NumberLiteral> = (
write: node value printString
)
public visitObjectLiteral: node <ObjectLiteral> = (
node slotCount = 0 ifTrue:
[write: '{}'.
^self].
write: '{'.
indentCr.
node
slotsDo:
[:name :value |
name visitBy: self.
write: ': '.
value visitBy: self]
betweenDo:
[write: ','.
cr].
unindentCr.
write: '}'
)
public visitOperatorExpression: node <OperatorExpression> = (
| parenthesizeLeft parenthesizeRight |
parenthesizeLeft:: node left isKindOfOperatorExpression.
parenthesizeRight:: node right isKindOfOperatorExpression.
parenthesizeLeft ifTrue: [write: '('].
node left visitBy: self.
parenthesizeLeft ifTrue: [write: ') '] ifFalse: [write: ' '].
node operatorName visitBy: self.
parenthesizeRight ifTrue: [write: ' ('] ifFalse: [write: ' '].
node right visitBy: self.
parenthesizeRight ifTrue: [write: ')']
)
public visitReturnStatement: node <ReturnStatement> = (
write: 'return'.
node expression ifNotNil:
[:expr |
write: ' '.
expr visitBy: self]
)
public visitScript: node <Script> = (
node statements
do:
[:each |
each visitBy: self.
write: ';']
separatedBy:
[cr.
cr].
)
public visitStringLiteral: node <StringLiteral> = (
write: '"'.
node value do:
[:each | | s |
s:: stringTranslation
at: each
ifAbsent: [{each}].
write: s].
write: '"'
)
public visitTernaryOperatorExpression: node = (
write: '('.
node condition visitBy: self.
write: ' ? '.
node then visitBy: self.
write: ' : '.
node else visitBy: self.
write: ')'
)
public visitThrowStatement: node <ThrowStatement> = (
write: 'throw '.
node expression visitBy: self
)
public visitTryStatement: node <TryStatement> = (
write: 'try '.