forked from gracelang/minigrace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.grace
3500 lines (3382 loc) · 119 KB
/
ast.grace
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
#pragma noTypeChecks
import "util" as util
import "identifierKinds" as k
// This module contains classes and pseudo-classes for all the AST nodes used
// in the parser. Because of the limitations of the class syntax, classes that
// need more than one method are written as object literals containing methods.
// Each node has a different signature according to its function, but the
// common interface is given by type ASTNode
//
// Most nodes also contain a "value" field, with varied type, holding the `main value`
// in the node. This field is confusing and should be appropriately re-named in
// each case. Some nodes contain other fields for their specific use: while has
// both a value (the condition) and a body, for example.
type Position = interface {
line -> Number
column -> Number
> (other) -> Boolean
≥ (other) -> Boolean
== (other) -> Boolean
< (other) -> Boolean
≤ (other) -> Boolean
}
type Range = interface {
start -> Position
end -> Position
}
class line (l:Number) column (c:Number) -> Position {
use equality
def line is public = l
def column is public = c
method > (other:Position) -> Boolean {
if (line > other.line) then { return true }
if (line < other.line) then { return false }
(column > other.column)
}
method ≥ (other:Position) -> Boolean {
if (line > other.line) then { return true }
if (line < other.line) then { return false }
(column ≥ other.column)
}
method == (other:Position) -> Boolean {
(line == other.line) && (column == other.column)
}
method hash -> Number {
hashCombine(line.hash, column.hash)
}
method ≤ (other:Position) -> Boolean {
(other > self).not
}
method < (other:Position) -> Boolean {
(other ≥ self).not
}
method asString { "{line}:{column}" }
}
class start (s:Position) end (e:Position) -> Range {
use equality
def start is public = s
def end is public = e
method asString {
if (start.line == end.line) then {
"{start}-{end.column}"
} elseif { end.line == noPosition } then {
start.asString
} else {
"{start}-{end}"
}
}
method == (other) {
(start == other.start) && (end == other.end)
}
method hash -> Number {
hashCombine(start.hash, end.hash)
}
}
def noPosition is public = line 0 column 0
def emptyRange is public = start (noPosition) end (noPosition)
method positionOfNext (needle:String) after (pos:Position) -> Position {
// returns the Position of the end of needle in the source
if (needle == "⟦") then {
return positionOfNext "[[" or "⟦" after (pos)
}
if (needle == "⟧") then {
return positionOfNext "]]" or "⟧" after (pos)
}
def sourceLines = util.lines
var lineNr := pos.line
if (lineNr == 0) then { return noPosition }
var found := sourceLines.at(lineNr).indexOf (needle) startingAt (pos.column + 1)
while { found == 0 } do {
lineNr := lineNr + 1
if (lineNr > sourceLines.size) then { return noPosition }
found := sourceLines.at(lineNr).indexOf (needle)
}
line (lineNr) column (found + needle.size - 1)
}
method positionOfNext (needle1:String) or (needle2:String)
after (pos:Position) -> Position {
def sourceLines = util.lines
def startLine = pos.line
if (startLine == 0) then { return noPosition }
var found := sourceLines.at(startLine).indexOf (needle1) startingAt (pos.column + 1)
if (found ≠ 0) then {
return line (startLine) column (found + needle1.size - 1)
}
found := sourceLines.at(startLine).indexOf (needle2) startingAt (pos.column + 1)
if (found ≠ 0) then {
return line (startLine) column (found + needle2.size - 1)
}
for (startLine..sourceLines.size) do { ln ->
if (ln > sourceLines.size) then { return noPosition }
found := sourceLines.at(ln).indexOf (needle1)
if (found ≠ 0) then {
return line (ln) column (found + needle1.size - 1)
}
found := sourceLines.at(ln).indexOf (needle2)
if (found ≠ 0) then {
return line (ln) column (found + needle2.size - 1)
}
}
}
def lineLength is public = 80
def uninitialized = Singleton.named "uninitialized"
method listMap(l, b) ancestors(ac) is confidential {
def newList = list [ ]
l.do { nd -> newList.addLast(nd.map(b) ancestors(ac)) }
newList
}
method maybeMap(n, b) ancestors(ac) is confidential {
if (false != n) then {
n.map(b) ancestors(ac)
} else {
n
}
}
method maybeListMap(n, b) ancestors(ac) is confidential {
if (false != n) then {
listMap(n, b) ancestors(ac)
} else {
n
}
}
def ancestorChain is public = object {
class empty {
method isEmpty { true }
method asString { "ancestorChain ▫" }
method extend(n) { cons(n) onto(self) }
}
method with(n) { empty.extend(n) }
class cons(p) onto(ac) is confidential {
method forebears { ac }
method isEmpty { false }
method parent { p }
method grandparent { forebears.parent }
method asString {
var a := self
var s := "ancestorChain "
while { a.isEmpty.not } do {
s := s ++ a.parent ++ "➤"
a := a.forebears
}
s ++ "▫"
}
method suchThat(cond) ifAbsent (action) {
var a := self
while { a.isEmpty.not } do {
if (cond.apply(a.parent)) then { return a.parent }
a := a.forebears
}
action.apply
}
method extend(n) { cons(n) onto(self) }
}
}
def emptySeq = emptySequence
type AstNode = interface {
kind -> String
// Used for pseudo-instanceof tests, and for printing
register -> String
// Used in the code generator to name the resulting object
line -> Number
// The source line the node came from; the first line is 1
line:=(ln:Number)
column -> Number
linePos -> Number
// linePos and column are aliases; the first column is 1
linePos:=(lp:Number)
scope -> SymbolTable
// The symbolTable for names defined in this node and its sub-nodes
pretty(n:Number) -> String
// Pretty-print-string of node at depth n
comments -> AstNode
// Comments associated with this node
range -> Range
// The source range represented by this node
start -> Position
// The start of the source range represented by this node
end -> Position
// The end of the source range represented by this node
}
type SymbolTable = Unknown
class baseNode {
// the superclass of all AST nodes
use identityEquality
var register is public := ""
var line is public := util.linenum
var linePos is public := util.linepos
var symbolTable := fakeSymbolTable
symbolTable.node := self
var comments is public := false
method setLine (l) col (c) {
line := l
linePos := c
self
}
method setPositionFrom (tokenOrNode) {
line := tokenOrNode.line
linePos := tokenOrNode.linePos
self
}
method setStart(p: Position) {
line := p.line
linePos := p.column
self
}
method column { linePos } // so that AstNode conforms to Position
method start { line (line) column (linePos) }
method end -> Position { line (line) column (linePos + self.value.size - 1) }
method range { start (start) end (end) }
method kind { abstract }
method isNull { false }
method isAppliedOccurrence { isBindingOccurrence.not }
method isBindingOccurrence { true }
method isMatchingBlock { false }
method isFieldDec { false }
method isInherits { false }
method isLegalInTrait { false }
method isMember { false }
method isMethod { false }
method isExecutable { true }
method isCall { false }
method isComment { false }
method isClass { false } // is a method that returns a fresh object
method inClass { false } // object in a syntactic class definiton
method isTrait { false } // is a method that returns a trait object
method inTrait { false } // object in a syntactic trait definition
method isBind { false }
method isReturn { false }
method isSelf { false }
method isSuper { false }
method isPrelude { false }
method isOuter { false }
method isSelfOrOuter { false }
method isBlock { false }
method isObject { false }
method isIdentifier { false }
method isDialect { false }
method isImport { false }
method isTypeDec { false }
method isExternal { false }
method isFresh { false }
method isConstant { false }
method isSequenceConstructor { false }
method canInherit { false }
method returnsObject { false }
method isImplicit { false }
method usesAsType(aNode) { false }
method hash { line.hash * linePos.hash }
method asString { "{kind} {nameString}" }
method nameString { "?" }
method isWritable { true }
method isReadable { true }
method isPublic { true }
method isConfidential { isPublic.not }
method decType {
if (false == self.dtype) then {
return unknownType
}
return self.dtype
}
method isSimple { true } // needs no parens when used as receiver
method isDelimited { false } // needs no parens when used as argument
method description { kind }
method accept(visitor) {
self.accept(visitor) from (ancestorChain.empty)
}
method hasScope { fakeSymbolTable ≠ symbolTable }
method scope {
if (hasScope) then {
symbolTable
} else {
ProgrammingError.raise "accessing unset symbol table"
}
}
method scope:=(st) {
// override this method in subobjects that open a new scope. In such
// subobjects, and only in such subobjects, there should be a 2-way
// conection between the node and the symbol table that defines its scope.
symbolTable := st
}
method setScope(st) {
// sets the symboltable, and answers self, for chaining.
scope := st
self
}
method shallowCopyFieldsFrom(other) {
register := other.register
line := other.line
linePos := other.linePos
scope := other.scope
postCopy(other)
self
}
method postCopy(other) {
// hook method, to be overridden by sub-objects if desired
}
method prettyPrefix(depth) {
def spc = " " * (depth+1)
if ((scope.node == self) && {util.target == "symbols"}) then {
"{range} {description}\n{spc}Symbols({scope.variety}): {scope}{scope.elementScopesAsString}"
} elseif {scope.variety == "fake"} then {
"{range} {description}"
} else {
"{range} {description} {scope.asDebugString}"
}
}
method basePretty(depth) { prettyPrefix(depth) }
method pretty(depth) { basePretty(depth) }
method deepCopy {
self.map { each -> each } ancestors(ancestorChain.empty)
}
method enclosingObject {
def obj = scope.enclosingObjectScope.node
obj
}
method addComment(cmtNode) {
if (false == comments) then {
comments := cmtNode
} else {
comments.extendCommentUsing(cmtNode)
}
}
method addComments(cmtNodeList) {
cmtNodeList.do { each -> addComment(each) }
}
method statementName { kind }
}
def implicit is public = object {
inherit baseNode
line := 0
linePos := 0
def kind is public = "implicit"
def nameString is public = "implicit"
method range { emptyRange }
method isImplicit { true }
method toGrace(depth) { "implicit" }
method asString { "the implicit receiver" }
method map(blk) ancestors(ac) { self }
method accept(visitor) from (ac) {
visitor.visitImplicit(self) up (ac)
}
method pretty(depth) { "implicit" }
}
def nullNode is public = object {
inherit baseNode
def kind is public = "null"
method toGrace(depth) {
"// null"
}
method range { emptyRange }
method asString { "the nullNode" }
method isNull { true }
}
class fakeSymbolTable is public {
use identityEquality
var node is public // will be initialized when this node
// is placed in an AstNode using scope:=(_).
// Can't make it nullNode now, because nullNode
// inherits from baseNode, which uses fakeSymbolTable
method asString { "the fakeSymbolTable" }
method addNode (n) ac (kind) {
ProgrammingError.raise "fakeSymbolTable(on node {node}).addNode({n}) ac \"{kind}\""
}
method thatDefines (name) ifNone (action) {
action.apply
}
method thatDefines (name) {
ProgrammingError.raise "fakeSymbolTable(on node {node}).thatDefines({name})."
}
method enclosingObjectScope {
ProgrammingError.raise "fakeSymbolTable(on node {node}).enclosingObjectScope"
}
method variety { "fake" }
method elementScopesAsString { "[fake]" }
}
def ifNode is public = object {
class new(cond, thenblock', elseblock') {
inherit baseNode
def kind is public = "if"
var value is public := cond
var thenblock is public := thenblock'
var elseblock is public := elseblock'
var handledIdentifiers is public := false
method isSimple { false } // needs parens when used as receiver
method accept(visitor : AstVisitor) from(ac) {
if (visitor.visitIf(self) up(ac)) then {
def newChain = ac.extend(self)
value.accept(visitor) from(newChain)
thenblock.accept(visitor) from(newChain)
elseblock.accept(visitor) from(newChain)
}
}
method end -> Position { elseblock.end }
method map(blk) ancestors(ac) {
var n := shallowCopy
def newChain = ac.extend(n)
n.value := value.map(blk) ancestors(newChain)
n.thenblock := thenblock.map(blk) ancestors(newChain)
n.elseblock := elseblock.map(blk) ancestors(newChain)
blk.apply(n, ac)
}
method pretty(depth) {
def spc = " " * (depth+1)
var s := basePretty(depth) ++ "\n"
s := s ++ spc ++ self.value.pretty(depth+1)
s := s ++ "\n"
if (util.target == "symbols") then {
s := s ++ spc ++ "Then: {thenblock.pretty(depth+2)}\n"
s := s ++ spc ++ "Else: {elseblock.pretty(depth+2)}"
} else {
s := s ++ spc ++ "Then:"
for (self.thenblock.body) do { ix ->
s := s ++ "\n "++ spc ++ ix.pretty(depth+2)
}
s := s ++ "\n"
s := s ++ spc ++ "Else:"
for (self.elseblock.body) do { ix ->
s := s ++ "\n "++ spc ++ ix.pretty(depth+2)
}
}
s
}
method toGrace(depth : Number) -> String {
def spc = " " * depth
var s := "if ({self.value.toGrace(0)}) then \{"
for (self.thenblock.body) do { ix ->
s := s ++ "\n" ++ spc ++ " " ++ ix.toGrace(depth + 1)
}
if (self.elseblock.isntEmpty) then {
s := s ++ "\n" ++ spc ++ "\} else \{"
for (self.elseblock.body) do { ix ->
s := s ++ "\n" ++ spc ++ " " ++ ix.toGrace(depth + 1)
}
}
s := s ++ "\n" ++ spc ++ "\}"
s
}
method shallowCopy {
ifNode.new(nullNode, nullNode, nullNode).shallowCopyFieldsFrom(self)
}
method postCopy(other) {
handledIdentifiers := other.handledIdentifiers
self
}
}
}
def blockNode is public = object {
class new(params', body') {
inherit baseNode
def kind is public = "block"
def value is public = "block"
var params is public := params'
var body is public := body'
def selfclosure is public = true
var matchingPattern is public := false
var extraRuntimeData is public := false
for (params') do {p->
p.accept(patternMarkVisitor) from(ancestorChain.with(self))
}
method isBlock { true }
method isDelimited { true }
method isEmpty { body.size == 0 }
method isntEmpty { body.size > 0 }
method scope:=(st) {
// sets up the 2-way conection between this node
// and the synmol table that defines the scope that I open.
symbolTable := st
st.node := self
}
method declarationKindWithAncestors(ac) { k.parameter }
method isMatchingBlock { params.size == 1 }
method returnsObject {
(body.size > 0) && { body.last.returnsObject }
}
method returnedObjectScope {
// precondition: returnsObject
body.last.returnedObjectScope
}
method hasParams { params.isEmpty.not }
method numParams { params.size }
method parametersDo(b) {
params.do(b)
}
method parameterCounts { [ params.size ] }
method parameterNames {
list.withAll(params.map { each -> each.nameString })
}
method typeParameterNames { list.empty }
method hasTypeParams { false }
method aParametersHasATypeAnnotation {
params.do { p -> if (false ≠ p.dtype) then { return true } }
return false
}
method end -> Position {
if (body.size > 0) then { return body.last.end }
if (params.isEmpty) then {
positionOfNext "}" after (start)
} else {
positionOfNext "}" after (params.last.end)
}
}
method accept(visitor : AstVisitor) from(ac) {
if (visitor.visitBlock(self) up(ac)) then {
def newChain = ac.extend(self)
for (self.params) do { mx ->
mx.accept(visitor) from(newChain)
}
for (self.body) do { mx ->
mx.accept(visitor) from(newChain)
}
if (false != self.matchingPattern) then {
self.matchingPattern.accept(visitor) from(newChain)
}
}
}
method map(blk) ancestors(ac) {
var n := shallowCopy
def newChain = ac.extend(n)
n.params := listMap(params, blk) ancestors(newChain)
n.body := listMap(body, blk) ancestors(newChain)
n.matchingPattern := maybeMap(matchingPattern, blk) ancestors(newChain)
blk.apply(n, ac)
}
method pretty(depth) {
def spc = " " * (depth+1)
var s := basePretty(depth) ++ "\n"
s := s ++ spc ++ "Parameters:"
for (self.params) do { mx ->
s := s ++ "\n "++ spc ++ mx.pretty(depth+1)
}
s := s ++ "\n"
s := s ++ spc ++ "Body:"
for (self.body) do { mx ->
s := s ++ "\n "++ spc ++ mx.pretty(depth+1)
}
if (false != self.matchingPattern) then {
s := s ++ "\n"
s := s ++ spc ++ "Pattern:"
s := s ++ "\n "++ spc ++ self.matchingPattern.pretty(depth+1)
}
s
}
method toGrace(depth : Number) -> String {
def spc = " " * depth
var s := "\{"
if (self.params.size > 0) then {
s := s ++ " "
for (self.params.indices) do { i ->
var p := self.params.at(i)
if (false != self.matchingPattern) then {
s := s ++ "(" ++ p.toGrace(0) ++ ")"
} else {
s := s ++ p.toGrace(0)
}
if (i < self.params.size) then {
s := s ++ ", "
} else {
s := s ++ " →"
}
}
}
for (self.body) do { mx ->
s := s ++ "\n" ++ spc ++ mx.toGrace(depth + 1)
}
s := s ++ "\n"
repeat (depth - 1) times { s := s ++ " " }
s ++ "\}"
}
method shallowCopy {
blockNode.new(params, body).shallowCopyFieldsFrom(self)
}
method postCopy(other) {
matchingPattern := other.matchingPattern
extraRuntimeData := other.extraRuntimeData
self
}
}
}
def tryCatchNode is public = object {
class new(block, cases', finally') {
inherit baseNode
def kind is public = "trycatch"
var value is public := block
var cases is public := cases'
var finally is public := finally'
method isSimple { false } // needs parens when used as receiver
method end -> Position {
if (false ≠ finally) then { return finally.end }
if (cases.isEmpty.not) then { return cases.last.end }
return value.end
}
method accept(visitor : AstVisitor) from(ac) {
if (visitor.visitTryCatch(self) up(ac)) then {
def newChain = ac.extend(self)
self.value.accept(visitor) from(newChain)
for (self.cases) do { mx ->
mx.accept(visitor) from(newChain)
}
if (false != self.finally) then {
self.finally.accept(visitor) from(newChain)
}
}
}
method map(blk) ancestors(ac) {
var n := shallowCopy
def newChain = ac.extend(n)
n.value := value.map(blk) ancestors(newChain)
n.cases := listMap(cases, blk) ancestors(newChain)
n.finally := maybeMap(finally, blk) ancestors(newChain)
blk.apply(n, ac)
}
method pretty(depth) {
def spc = " " * (depth+1)
var s := "{basePretty(depth)}\n"
s := s ++ spc ++ value.pretty(depth + 2)
for (self.cases) do { mx ->
s := s ++ "\n{spc}Case:\n{spc} {mx.pretty(depth+2)}"
}
if (false != self.finally) then {
s := s ++ "\n{spc}Finally:\n{spc} {self.finally.pretty(depth+2)}"
}
s
}
method toGrace(depth : Number) -> String {
def spc = " " * depth
var s := "try " ++ self.value.toGrace(depth + 1) ++ " "
for (self.cases) do { case ->
s := s ++ "\n" ++ spc ++ " " ++ "catch " ++ case.toGrace(depth + 1)
}
if (false != self.finally) then {
s := s ++ "\n" ++ spc ++ " " ++ "finally " ++ self.finally.toGrace(depth + 1)
}
s
}
method shallowCopy {
tryCatchNode.new(nullNode, emptySeq, false).shallowCopyFieldsFrom(self)
}
}
}
def matchCaseNode is public = object {
class new(matchee', cases', elsecase') {
inherit baseNode
def kind is public = "matchcase"
var value is public := matchee'
var cases is public := cases'
var elsecase is public := elsecase'
method isSimple { false } // needs parens when used as receiver
method end -> Position {
if (false ≠ elsecase) then { return elsecase.end }
if (cases.isEmpty.not) then { return cases.last.end }
return value.end
}
method matchee { value }
method accept(visitor : AstVisitor) from(ac) {
if (visitor.visitMatchCase(self) up(ac)) then {
def newChain = ac.extend(self)
self.value.accept(visitor) from(newChain)
for (self.cases) do { mx ->
mx.accept(visitor) from(newChain)
}
if (false != self.elsecase) then {
self.elsecase.accept(visitor) from(newChain)
}
}
}
method map(blk) ancestors(ac) {
var n := shallowCopy
def newChain = ac.extend(n)
n.value := value.map(blk) ancestors(newChain)
n.cases := listMap(cases, blk) ancestors(newChain)
n.elsecase := maybeMap(elsecase, blk) ancestors(newChain)
blk.apply(n, ac)
}
method pretty(depth) {
def spc = " " * (depth+1)
var s := basePretty(depth) ++ "\n"
s := s ++ spc ++ matchee.pretty(depth + 2)
for (self.cases) do { mx ->
s := s ++ "\n{spc}Case:\n{spc} {mx.pretty(depth+2)}"
}
if (false != self.elsecase) then {
s := s ++ "\n{spc}Else:\n{spc} {self.elsecase.pretty(depth+2)}"
}
s
}
method toGrace(depth : Number) -> String {
def spc = " " * depth
var s := "match(" ++ self.value.toGrace(0) ++ ")"
for (self.cases) do { case ->
s := s ++ "\n" ++ spc ++ " " ++ "case " ++ case.toGrace(depth + 2)
}
if (false != self.elsecase) then {
s := s ++ "\n" ++ spc ++ " " ++ "else " ++ self.elsecase.toGrace(depth + 2)
}
s
}
method shallowCopy {
matchCaseNode.new(nullNode, emptySeq, false).shallowCopyFieldsFrom(self)
}
}
}
class methodSignatureNode(parts', rtype') {
// Represents a method signature in a type literal, or in an inheritance modifier.
// parts' is a collection of signaturePart objects, which
// contain the parts of this method's name and the parameter lists;
// rtype' is the return type of this method, or false if not specified.
inherit baseNode
def kind is public = "methodtype"
var signature is public := parts'
var rtype is public := rtype'
var cachedIdentifier := uninitialized
var isBindingOccurrence is readable := true
// the only exceptions are the oldMethodName in an alias clause,
// and an excluded name
method postCopy(other) {
isBindingOccurrence := other.isBindingOccurrence
}
method appliedOccurrence {
isBindingOccurrence := false
if (uninitialized ≠ cachedIdentifier) then {
cachedIdentifier.isBindingOccurrence := false
}
self
}
method hasParams { signature.first.params.isEmpty.not }
method numParams {
signature.fold { acc, p -> acc + p.numParams } startingWith 0
}
method parametersDo(b) {
signature.do { part ->
part.params.do { each -> b.apply(each) }
}
}
method parameterCounts {
def result = list [ ]
signature.do { part ->
result.push(part.params.size)
}
result
}
method parameterNames {
def result = list [ ]
signature.do { part ->
part.params.do { param ->
result.push(param.nameString)
}
}
result
}
method typeParameterNames {
if (hasTypeParams.not) then { return list [ ] }
def result = list [ ]
signature.first.typeParams.do { each ->
result.push(each.nameString)
}
result
}
method numTypeParams { signature.first.numTypeParams }
method hasTypeParams { false ≠ signature.first.typeParams }
method typeParams { signature.first.typeParams }
method withTypeParams(tp) {
signature.first.typeParams := tp
self
}
method end -> Position {
if ((false ≠ rtype) && {rtype.line ≠ 0}) then { return rtype.end }
signature.last.end
}
method nameString {
// the name of the method being defined, in numeric form
signature.fold { acc, each -> acc ++ each.nameString }
startingWith ""
}
method canonicalName {
// the name of the method being defined, in underscore form
signature.fold { acc, each -> acc ++ each.canonicalName }
startingWith ""
}
method asIdentifier {
if (uninitialized == cachedIdentifier) then {
cachedIdentifier := identifierNode.new(nameString, false)
cachedIdentifier.line := signature.first.line
cachedIdentifier.linePos := signature.first.linePos
cachedIdentifier.end := signature.last.end
cachedIdentifier.canonicalName := canonicalName
cachedIdentifier.isBindingOccurrence := isBindingOccurrence
}
cachedIdentifier
}
method isExecutable { false }
method scope:=(st) {
// sets up the 2-way conection between this node
// and the symbol table that defines the scope that I open.
symbolTable := st
st.node := self
}
method declarationKindWithAncestors(ac) {
ac.parent.declarationKindWithAncestors(ac)
}
method accept(visitor : AstVisitor) from(ac) {
if (visitor.visitMethodType(self) up(ac)) then {
def newChain = ac.extend(self)
for (signature) do { part ->
part.accept(visitor) from(newChain)
}
if (false != rtype) then {
rtype.accept(visitor) from(newChain)
}
}
}
method map(blk) ancestors(ac) {
var n := shallowCopy
def newChain = ac.extend(n)
n.rtype := maybeMap(rtype, blk) ancestors(newChain)
n.signature := listMap(signature, blk) ancestors(newChain)
blk.apply(n, ac)
}
method pretty(depth) {
def spc = " " * (depth+1)
var s := basePretty(depth) ++ "\n"
s := "{s}{spc}Name: {asIdentifier}\n"
if (false != rtype) then {
s := "{s}{spc}Returns:\n {spc}{rtype.pretty(depth + 2)}"
}
s := "{s}\n{spc}Signature Parts:"
for (signature) do { part ->
s := "{s}\n {spc}{part.pretty(depth + 2)}"
}
s
}
method toGrace(depth : Number) -> String {
var s := ""
signature.do { part -> s:= s ++ part.toGrace(depth + 2) }
if (false != rtype) then {
s := "{s} → {rtype.toGrace(depth + 2)}"
}
s
}
method shallowCopy {
methodSignatureNode(signature, rtype).shallowCopyFieldsFrom(self)
}
}
def typeLiteralNode is public = object {
class new(methods', types') {
inherit baseNode
def kind is public = "typeliteral"
var methods is public := methods'
var types is public := types'
var nominal is public := false
var anonymous is public := true
var value is public := "‹anon›"
method name { value }
method name:=(n) {
value := n
anonymous := false
}
method asString {
"typeliteral: methods = {methods}, types = {types}"
}
method declarationKindWithAncestors(ac) { k.typedec }
method isExecutable { false }
method end -> Position {
def tEnd = if (types.isEmpty) then {noPosition} else {types.last.end}
def mEnd = if (methods.isEmpty) then {noPosition} else {methods.last.end}
positionOfNext "}" after (max(max(tEnd, mEnd), start))
}
method accept(visitor : AstVisitor) from(ac) {
if (visitor.visitTypeLiteral(self) up(ac)) then {
def newChain = ac.extend(self)
for (self.methods) do { each ->
each.accept(visitor) from(newChain)
}
for (self.types) do { each ->
each.accept(visitor) from(newChain)
}
}
}
method map(blk) ancestors(ac) {
var n := shallowCopy
def newChain = ac.extend(n)
n.methods := listMap(methods, blk) ancestors (ac)
n.types := listMap(types, blk) ancestors (ac)
blk.apply(n, ac)
}
method pretty(depth) {
def spc = " " * (depth+1)
var s := basePretty(depth) ++ "\n"
s := s ++ spc ++ "Types:"
for (types) do { each ->
s := s ++ "\n "++ spc ++ each.pretty(depth+2)
}
s := s ++ "\n" ++ spc ++ "Methods:"
for (methods) do { each ->
s := s ++ "\n "++ spc ++ each.pretty(depth+2)
}
s := s ++ "\n"
s
}
method toGrace(depth : Number) -> String {
def spc = " " * depth
var s := "interface \{"
for (self.methods) do { each ->
s := s ++ "\n" ++ spc ++ " " ++ each.toGrace(depth + 1)
}
for (self.types) do { each ->
s := s ++ "\n" ++ spc ++ " " ++ each.toGrace(depth + 1)
}
s ++ "\}"
}
method shallowCopy {
typeLiteralNode.new(emptySeq, emptySeq).shallowCopyFieldsFrom(self)
}
method postCopy(other) {
nominal := other.nominal
anonymous := other.anonymous
value := other.value
self
}
}
}
def typeDecNode is public = object {
class new(name', typeValue) {
inherit baseNode
def kind is public = "typedec"
var name is public := name'
var value is public := typeValue
var parentKind is public := "unset"
var annotations is public := list [ ]
var typeParams is public := false
method nameString → String { name.value }
method end -> Position { value.end }
method isLegalInTrait { true }
method isTypeDec { true }
method scope:=(st) {
// sets up the 2-way conection between this node
// and the synmol table that defines the scope that I open.
symbolTable := st
st.node := self
}
method isExecutable { false }
method declarationKindWithAncestors(ac) { k.typeparam }
method isConfidential { findAnnotation(self, "confidential") }
method isPublic { isConfidential.not }
method isWritable { false }
method isReadable { isPublic }
method numTypeParams {