-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathidentifierresolution.grace
955 lines (940 loc) · 33.2 KB
/
identifierresolution.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
#pragma DefaultVisibility=public
import "io" as io
import "sys" as sys
import "ast" as ast
import "util" as util
import "xmodule" as xmodule
import "mgcollections" as collections
import "mirrors" as mirrors
import "errormessages" as errormessages
class Scope.new(parent') {
def elements = collections.map.new
def elementScopes = collections.map.new
def elementDeclarations = collections.map.new
def elementLines = collections.map.new
def elementTokens = collections.map.new
def parent = parent'
var hasParent := true
var variety := "block"
var name := ""
method add(n) {
elements.put(n, "method")
}
method add(n)as(k) {
elements.put(n, k)
elementLines.put(n, util.linenum)
}
method contains(n) {
elements.contains(n)
}
method do(b) {
b.apply(self)
if (hasParent) then {
parent.do(b)
}
}
method getKind(n) {
elements.get(n)
}
method bindAs(n) {
parent.elementScopes.put(n, self)
}
method getScope(n) {
if (elementScopes.contains(n)) then {
return elementScopes.get(n)
}
return Scope.new(self)
}
method new {
Scope.new(self)
}
}
def builtinObj = Scope.new(object { })
builtinObj.variety := "top"
builtinObj.hasParent := false
def preludeObj = Scope.new(builtinObj)
preludeObj.variety := "dialect"
def moduleObj = Scope.new(preludeObj)
moduleObj.variety := "module"
var scope := moduleObj
method pushScope {
scope := scope.new
}
method popScope {
scope := scope.parent
}
method haveBinding(nm) {
var cur := scope
while {cur.hasParent} do {
if (cur.contains(nm)) then {
return true
}
cur := cur.parent
}
if (cur.contains(nm)) then {
return true
}
return false
}
method getNameKind(nm) {
scope.do {s->
if (s.contains(nm)) then {
return s.getKind(nm)
}
}
return "undefined"
}
method getNameScope(nm) {
scope.do {s->
if (s.contains(nm)) then {
return s
}
}
return "undefined"
}
method findDeepMethod(name) {
var mem := ast.identifierNode.new("self", false)
scope.do { s->
if (s.contains(name)) then {
if (s.variety == "dialect") then {
return ast.memberNode.new(name,
ast.identifierNode.new("prelude", false))
}
return ast.memberNode.new(name, mem)
}
match(s.variety)
case { "object" -> mem := ast.memberNode.new("outer", mem) }
case { "class" ->
mem := ast.memberNode.new("outer", mem)
mem := ast.memberNode.new("outer", mem)
}
case { _ -> }
}
// Not found - leave it alone
return ast.identifierNode.new(name, false)
}
method findDeepScope'(node, scope') {
if (node.kind == "identifier") then {
if (node.value == "self") then {
var s := scope'
while {s.hasParent} do {
if (s.variety == "object") then {
return s
}
if (s.variety == "class") then {
return s
}
if (s.variety == "module") then {
return s
}
s := s.parent
}
}
if (node.value == "prelude") then {
return preludeObj
}
scope'.do {s->
if (s.contains(node.value)) then {
return s.getScope(node.value)
}
}
return scope'.getScope(node.value)
}
if (node.kind == "member") then {
def tmp = findDeepScope'(node.in, scope')
if (node.value == "outer") then {
return tmp.parent
}
return tmp.getScope(node.value)
}
if (node.kind == "call") then {
return findDeepScope'(node.value, scope')
}
return Scope.new(scope')
}
method findDeepScope(node) {
findDeepScope'(node, scope)
}
method rewritematchblockterm(arg) {
if (arg.kind == "num") then {
return [arg, []]
}
if (arg.kind == "string") then {
return [arg, []]
}
if (arg.kind == "boolean") then {
return [arg, []]
}
if ((arg.kind == "call").andAlso {arg.value.value.substringFrom(1)to(6)
== "prefix"}) then {
return [arg, []]
}
if (arg.kind == "member") then {
return [arg, []]
}
if (arg.kind == "call") then {
def bindings = []
def subpats = []
for (arg.with) do { part ->
for (part.args) do { a ->
def tmp = rewritematchblockterm(a)
subpats.push(tmp[1])
for (tmp[2]) do {b->
bindings.push(b)
}
}
}
def callpat = ast.callNode.new(
ast.memberNode.new(
"new",
ast.memberNode.new("MatchAndDestructuringPattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [arg.value, ast.arrayNode.new(subpats)])]
)
return [callpat, bindings]
}
if (arg.kind == "identifier") then {
def varpat = ast.callNode.new(
ast.memberNode.new(
"new",
ast.memberNode.new("VariablePattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [ast.stringNode.new(arg.value)])]
)
if (arg.dtype != false) then {
if (arg.dtype.kind == "identifier") then {
return [ast.callNode.new(
ast.memberNode.new(
"new",
ast.memberNode.new("AndPattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [varpat, arg.dtype])]
), [arg]]
}
def tmp = rewritematchblockterm(arg.dtype)
def bindings = [arg]
for (tmp[2]) do {b->
bindings.push(b)
}
def bindingpat = ast.callNode.new(
ast.memberNode.new(
"new",
ast.memberNode.new("AndPattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [varpat, tmp[1]])]
)
return [bindingpat, bindings]
}
return [varpat, [arg]]
}
if (arg.kind == "type") then {
return [arg, []]
}
Error.raise("Compiler logic error: fell through when rewriting match block"
++ ": unexpected kind '{arg.kind}'.")
}
method rewritematchblock(blk) {
def arg = blk.params[1]
var pattern := false
var newparams := collections.list.new
for (blk.params) do { p ->
newparams.push(p)
}
if ((arg.kind == "num") || (arg.kind == "string") ||
(arg.kind == "boolean")) then {
def tmp = rewritematchblockterm(arg)
pattern := tmp[1]
newparams := tmp[2]
}
if (arg.kind == "identifier") then {
def varpat = ast.callNode.new(
ast.memberNode.new(
"new",
ast.memberNode.new("VariablePattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [ast.stringNode.new(arg.value)])]
)
if (arg.dtype != false) then {
match (arg.dtype.kind)
case { "identifier" ->
pattern := ast.callNode.new(
ast.memberNode.new("new",
ast.memberNode.new("AndPattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [varpat, arg.dtype])])
} case { "op" ->
pattern := ast.callNode.new(
ast.memberNode.new("new",
ast.memberNode.new("AndPattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [varpat, arg.dtype])])
} case { _ ->
def tmp = rewritematchblockterm(arg.dtype)
def bindingpat = ast.callNode.new(
ast.memberNode.new("new",
ast.memberNode.new("AndPattern",
ast.identifierNode.new("prelude", false)
)
),
[ast.callWithPart.new("new", [varpat, tmp[1]])]
)
pattern := bindingpat
for (tmp[2]) do {p->
newparams.push(p)
}
}
pattern := resolveIdentifiers(pattern)
} else {
if (false != blk.matchingPattern) then {
if (blk.matchingPattern.value == arg.value) then {
pattern := resolveIdentifiers(arg)
newparams := []
}
}
}
} else {
if (false != blk.matchingPattern) then {
if (blk.matchingPattern.value == arg.value) then {
pattern := resolveIdentifiers(arg)
newparams := []
}
}
}
def newblk = ast.blockNode.new(newparams, blk.body)
newblk.matchingPattern := pattern
newblk.line := blk.line
return newblk
}
method resolveIdentifier(node) {
if (node.kind != "identifier") then {
return node
}
var nm := node.value
util.setPosition(node.line, node.linePos)
if (node.inBind) then {
if (!haveBinding(nm)) then {
if (haveBinding(nm ++ ":=")) then {
if (getNameKind(nm ++ ":=") == "method") then {
// Bare method call with no arguments
def meth = findDeepMethod(nm ++ ":=")
def meth2 = ast.memberNode.new(nm, meth.in)
return ast.callNode.new(meth2, [ast.callWithPart.new(meth2.value)])
}
}
}
}
if (haveBinding(nm).not) then {
if (node.wildcard) then {
errormessages.syntaxError("'_' can only be used as a parameter name")atRange(node.line, node.linePos, node.linePos)
} else {
def suggestions = []
var suggestion
for(scope.elements) do { v ->
var thresh := 1
if (nm.size > 5) then {
thresh := ((nm.size - 2) / 4 + 1).truncate
}
if(errormessages.dameraulevenshtein(v, nm) <= thresh) then {
suggestion := errormessages.suggestion.new
suggestion.replaceRange(node.linePos, node.linePos + node.value.size - 1)with(v)onLine(node.line)
suggestions.push(suggestion)
}
}
for(scope.elementScopes) do { s ->
if(scope.elementScopes.get(s).contains(nm)) then {
suggestion := errormessages.suggestion.new
suggestion.insert("{s}.")atPosition(node.linePos)onLine(node.line)
suggestions.push(suggestion)
}
}
var offerString := !node.inBind && !node.inRequest
var highlightLength := node.value.size
if (node.value.replace "()" with "XX" != node.value) then {
offerString := false
var i := 0
var found := false
for (node.value) do {c->
if ((c == "(") && (!found)) then {
highlightLength := i
found := true
}
i := i + 1
}
}
if (offerString) then {
suggestion := errormessages.suggestion.new
suggestion.insert("\"")atPosition(node.linePos + node.value.size)onLine(node.line)
suggestion.insert("\"")atPosition(node.linePos)onLine(node.line)
suggestions.push(suggestion)
}
if (node.inRequest) then {
var extra := ""
if (node.value == "while") then {
suggestion := errormessages.suggestion.new
suggestion.append " do \{ \}" onLine(node.line)
suggestions.push(suggestion)
}
if (node.value == "for") then {
suggestion := errormessages.suggestion.new
suggestion.append " do \{ aVarName -> \}" onLine(node.line)
suggestions.push(suggestion)
}
errormessages.syntaxError "Unknown method name '{nm}'. This may be due to a spelling mistake or trying to access a method within another scope.{extra}"
atRange(node.line, node.linePos, node.linePos +
highlightLength - 1)
withSuggestions(suggestions)
}
errormessages.syntaxError("Unknown variable or method name '{nm}'. This may be due to a spelling mistake or trying to access a variable within another scope.")atRange(
node.line, node.linePos, node.linePos + highlightLength - 1)withSuggestions(suggestions)
}
}
if (nm == "outer") then {
return ast.memberNode.new("outer", ast.identifierNode.new("self", false))
}
if (nm == "self") then {
return node
}
if (getNameKind(nm) == "method") then {
// Bare method call with no arguments
def meth = findDeepMethod(nm)
return ast.callNode.new(meth, [ast.callWithPart.new(meth.value)])
}
node
}
method resolveIdentifiersActual(node) {
util.setline(node.line)
if (node == false) then {
return node
}
if (node.kind == "identifier") then {
return resolveIdentifier(node)
}
if (node.kind == "import") then {
scope.add(node.value) as "def"
}
if (node.kind == "call") then {
if (node.value.kind == "call") then {
def tmp = ast.callNode.new(node.value.value, node.with)
tmp.line := node.line
tmp.generics := node.generics
return tmp
}
if (node.value.kind == "identifier") then {
def ck = getNameKind(node.value.value)
if (!node.isPattern) then {
if ((ck == "def") || (ck == "var")) then {
util.semantic_error ""
}
}
}
}
if (node.kind == "block") then {
if (node.params.size == 1) then {
return rewritematchblock(node)
}
}
if (node.kind == "bind") then {
if (node.dest.kind == "call") then {
def tmp = ast.bindNode.new(node.dest.value, node.value)
tmp.line := node.line
return tmp
}
if (node.dest.kind == "identifier") then {
if (getNameKind(node.dest.value) == "def") then {
def name = node.dest.value
def scp = getNameScope(name)
var more := ""
def suggestions = []
if (scp.elementLines.contains(name)) then {
more := " on line {scp.elementLines.get(name)}"
}
if (scp.elementTokens.contains(name)) then {
def tok = scp.elementTokens.get(name)
def sugg = errormessages.suggestion.new
var eq := tok
while {(eq.kind != "op") || (eq.value != "=")} do {
eq := eq.next
}
sugg.replaceToken(eq)with(":=")
sugg.replaceToken(tok)with("var")
suggestions.push(sugg)
}
errormessages.syntaxError("The value of '{node.dest.value}' cannot be changed because it is a constant. To make it a variable use 'var' instead of 'def' in the declaration{more}.")
atLine(node.line)
withSuggestions(suggestions)
}
}
}
if (node.kind == "method") then {
if (node.body.size > 0) then {
def lastStatement = node.body.last
if (lastStatement.kind == "object") then {
node.properties.put("fresh", true)
}
}
}
if (node.kind == "inherits") then {
if (node.value.kind == "call") then{
node.value.with.push(ast.callWithPart.new("object",
[ast.identifierNode.new("self", false)]))
def newmem = ast.memberNode.new(node.value.value.value
++ "()object",
node.value.value.in
)
def newcall = ast.callNode.new(newmem, node.value.with)
return ast.inheritsNode.new(newcall)
} else {
if (node.value.kind == "member") then {
def newmem = ast.memberNode.new(node.value.value
++ "()object",
node.value.in
)
def newcall = ast.callNode.new(newmem, collections.list.new(
ast.callWithPart.new(node.value.value, []),
ast.callWithPart.new("object",
[ast.identifierNode.new("self", false)])
)
)
if (node.value.in.value == "StandardPrelude") then {
return node
}
return ast.inheritsNode.new(newcall)
}
}
}
node
}
method checkRedefinition(ident) {
def nk = getNameKind(ident.value)
if ((nk == "def").orElse {nk == "var"}) then {
if (getNameScope(ident.value)
.elementDeclarations.contains(ident.value)
) then {
def scp = getNameScope(ident.value)
var more := ""
if (scp.elementLines.contains(ident.value)) then {
more := " as a {scp.elements.get(ident.value)}"
++ " on line {scp.elementLines.get(ident.value)}"
}
if(nk == "def") then {
errormessages.syntaxError("'{ident.value}' cannot be "
++ "redeclared because it is already declared in "
++ "scope{more}.")
atRange(ident.line, ident.linePos,
ident.linePos + ident.value.size - 1)
} else {
def suggs = collections.list.new
def sugg = errormessages.suggestion.new
if (sugg.replaceUntil("=")with("{ident.value} :=")
onLine(ident.line)
) then {
suggs.push(sugg)
}
errormessages.syntaxError("'{ident.value}' cannot be "
++ "redeclared because it is already declared in "
++ "scope{more}. To assign to the existing variable, "
++ "remove 'var' or 'def'.")
atRange(ident.line, ident.linePos,
ident.linePos + ident.value.size - 1)
withSuggestions(suggs)
}
}
}
util.setline(ident.line)
scope.elementDeclarations.put(ident.value, true)
}
method resolveIdentifiers(topNode) {
// Recursively replace bare identifiers with their fully-qualified
// equivalents.
if (topNode == false) then {
return topNode
}
topNode.map { n -> resolveIdentifiersActual(n) } before { node ->
util.setline(node.line)
if (node.kind == "class") then {
checkRedefinition(node.name)
scope.add(node.name.value) as "def"
def classScope = Scope.new(scope)
classScope.add(node.constructorName.value)
classScope.bindAs(node.name.value)
pushScope
def paramScope = scope
if (false != node.generics) then {
for (node.generics) do {g->
scope.add(g.value) as "def"
}
}
for (node.signature) do {s->
for (s.params) do {p->
scope.add(p.value)as "def"
}
if (false != s.vararg) then {
scope.add(s.vararg.value) as "def"
}
}
pushScope
scope.variety := "class"
classScope.elementScopes.put(node.constructorName.value, scope)
for (node.value) do {n->
if (n.kind == "method") then {
scope.add(n.value.value)
}
if (n.kind == "vardec") then {
scope.add(n.name.value)
scope.add(n.name.value ++ ":=")
}
if (n.kind == "defdec") then {
scope.add(n.name.value)
}
if (n.kind == "inherits") then {
def parent = resolveIdentifiers(n.value)
def parentScope = findDeepScope(parent)
for (parentScope.elements) do {e->
scope.add(e)
}
for (node.signature) do {s->
for (s.params) do {p->
if (parentScope.elements.contains(p.value)) then {
def suggestion = errormessages.suggestion.new
suggestion.insert("'")atPosition(p.linePos + p.value.size)onLine(p.line)
var primes := "'"
while { scope.elements.contains(p.value ++ primes) || parentScope.elements.contains(p.value ++ primes) } do {
suggestion.insert("'")atPosition(p.linePos + p.value.size)onLine(p.line)
primes := primes ++ "'"
}
errormessages.syntaxError("The parameter name '{p.value}' cannot be used because this class inherits a method named '{p.value}'.")atRange(
p.line, p.linePos, p.linePos + p.value.size - 1)withSuggestion(suggestion)
}
}
}
}
}
for (node.signature) do {s->
for (s.params) do {p->
if (scope.elements.contains(p.value)) then {
def suggestion = errormessages.suggestion.new
suggestion.insert("'")atPosition(p.linePos + p.value.size)onLine(p.line)
var primes := "'"
while { scope.elements.contains(p.value ++ primes) || paramScope.elements.contains(p.value ++ primes) } do {
suggestion.insert("'")atPosition(p.linePos + p.value.size)onLine(p.line)
primes := primes ++ "'"
}
errormessages.syntaxError("The parameter name '{p.value}' cannot be used because this class declares something named '{p.value}'.")atRange(
p.line, p.linePos, p.linePos + p.value.size - 1)withSuggestion(suggestion)
}
}
}
}
if (node.kind == "object") then {
pushScope
scope.variety := "object"
for (node.value) do {n->
if (n.kind == "method") then {
scope.add(n.value.value)
}
if (n.kind == "vardec") then {
scope.add(n.name.value)
scope.add(n.name.value ++ ":=")
}
if (n.kind == "defdec") then {
scope.add(n.name.value)
}
if (n.kind == "inherits") then {
def parent = resolveIdentifiers(n.value)
def parentScope = findDeepScope(parent)
for (parentScope.elements) do {e->
scope.add(e)
}
}
}
}
if (node.kind == "if") then {
pushScope
}
if (node.kind == "block") then {
pushScope
var tmp := node
if (node.params.size == 1) then {
tmp := rewritematchblock(tmp)
}
for (tmp.params) do {p->
checkRedefinition(p)
scope.add(p.value)as "def"
}
}
if (node.kind == "type") then {
scope.add(node.value) as "def"
pushScope
scope.variety := "type"
for (node.generics) do {n->
scope.add(n.value) as "def"
}
}
if (node.kind == "methodtype") then {
scope.add(node.value)
pushScope
for (node.generics) do {g->
scope.add(g.value) as "def"
}
for (node.signature) do {s->
for (s.params) do {p->
scope.add(p.value)as "def"
}
if (false != s.vararg) then {
scope.add(s.vararg.value) as "def"
}
}
}
if (node.kind == "method") then {
scope.add(node.value.value)
pushScope
scope.variety := "method"
scope.name := node.value.value
for (node.generics) do {g->
checkRedefinition(g)
scope.add(g.value) as "def"
}
for (node.signature) do {s->
for (s.params) do {p->
checkRedefinition(p)
scope.add(p.value)as "def"
}
if (false != s.vararg) then {
scope.add(s.vararg.value) as "def"
}
}
}
if (node.kind == "vardec") then {
if ((scope.variety != "object") && (scope.variety != "class")) then {
checkRedefinition(node.name)
scope.add(node.name.value)as "var"
} else {
scope.add(node.name.value)
}
}
if (node.kind == "defdec") then {
if ((scope.variety != "object") && (scope.variety != "class")) then {
checkRedefinition(node.name)
scope.add(node.name.value)as "def"
if (false != node.startToken) then {
scope.elementTokens.put(node.name.value, node.startToken)
}
} else {
scope.add(node.name.value)
}
}
if (node.kind == "import") then {
checkRedefinition(node)
scope.add(node.value) as "def"
}
} after { node ->
if (node.kind == "class") then {
node.data := scope
popScope
popScope
}
if (node.kind == "object") then {
if (scope.parent.variety == "method") then {
scope.parent.parent.elementScopes.put(scope.parent.name,
scope)
}
node.data := scope
popScope
}
if (node.kind == "if") then {
popScope
}
if (node.kind == "block") then {
popScope
}
if (node.kind == "method") then {
popScope
}
if (node.kind == "methodtype") then {
popScope
}
if (node.kind == "type") then {
popScope
}
if (node.kind == "defdec") then {
if (node.value.kind == "object") then {
scope.elementScopes.put(node.name.value, node.value.data)
node.data := scope
} else {
def sc = findDeepScope(node.value)
scope.elementScopes.put(node.name.value, sc)
}
if (ast.findAnnotation(node, "parent")) then {
def sc = findDeepScope(node.value)
for (sc.elements) do {m->
scope.add(m)
}
}
}
}
}
method processGCT(gct, otherModule) {
def classes = collections.map.new
if (gct.contains("classes")) then {
for (gct.get("classes")) do {c->
def cmeths = []
def constrs = gct.get("constructors-of:{c}")
def classScope = Scope.new(otherModule)
for (constrs) do {constr->
def ns = Scope.new(otherModule)
classScope.add(constr)
classScope.elementScopes.put(constr, ns)
for (gct.get("methods-of:{c}.{constr}")) do {mn->
ns.add(mn)
}
}
otherModule.add(c)
otherModule.elementScopes.put(c, classScope)
}
}
def freshmeths = collections.map.new
if (gct.contains("fresh-methods")) then {
for (gct.get("fresh-methods")) do {c->
def mScope = Scope.new(otherModule)
for (gct.get("fresh:{c}")) do {mn->
mScope.add(mn)
}
otherModule.add(c)
otherModule.elementScopes.put(c, mScope)
}
}
}
method handleImport(e) {
def gct = xmodule.parseGCT(e.path, "/nosuchpath")
def otherModule = Scope.new(builtinObj)
processGCT(gct, otherModule)
scope.add(e.value) as "def"
scope.elementScopes.put(e.value, otherModule)
}
method resolve(values) {
util.log_verbose "resolving identifiers."
preludeObj.add "for()do"
preludeObj.add "while()do"
preludeObj.add "print"
builtinObj.add "Object" as "def"
builtinObj.add "Unknown" as "def"
builtinObj.add "Dynamic" as "def"
builtinObj.add "String" as "def"
builtinObj.add "Number" as "def"
builtinObj.add "Boolean" as "def"
builtinObj.add "List" as "def"
builtinObj.add "Block" as "def"
builtinObj.add "Done" as "def"
builtinObj.add "done" as "def"
builtinObj.add "true" as "def"
builtinObj.add "false" as "def"
builtinObj.add "self" as "def"
builtinObj.add "super" as "def"
builtinObj.add "outer" as "def"
builtinObj.add "readable" as "def"
builtinObj.add "writable" as "def"
builtinObj.add "public" as "def"
builtinObj.add "confidential" as "def"
builtinObj.add "override" as "def"
builtinObj.add "parent" as "method"
builtinObj.add "prelude" as "def"
builtinObj.add "_prelude" as "def"
builtinObj.add "..." as "def"
// Historical - should be removed eventually
if (!util.extensions.contains("NativePrelude")) then {
var hadDialect := false
for (values) do {val->
if (val.kind == "dialect") then {
hadDialect := true
def data = xmodule.parseGCT(val.value, "/nosuchfile")
if (data.contains("public")) then {
for (data.get("public")) do {mn->
preludeObj.add(mn)
}
}
if (data.contains("confidential")) then {
for (data.get("confidential")) do {mn->
preludeObj.add(mn)
}
}
processGCT(data, preludeObj)
}
}
if (!hadDialect) then {
for (prelude._methods) do {mn->
preludeObj.add(mn)
}
}
}
def vals = collections.list.new
for (values) do { n ->
if (n.kind == "method") then {
scope.add(n.value.value)
}
if ((n.kind == "class") || (n.kind == "defdec")) then {
scope.add(n.name.value) as "def"
}
if (n.kind == "vardec") then {
scope.add(n.name.value) as "var"
}
if (n.kind == "type") then {
scope.add(n.value)
}
if (n.kind == "import") then {
handleImport(n)
}
}
def vis = object {
inherits ast.baseVisitor
method visitBind(o) {
def d = o.dest
if (d.kind == "identifier") then {
d.inBind := true
}
return true
}
method visitCall(o) {
def d = o.value
if (d.kind == "identifier") then {
d.inRequest := true
}
return true
}
}
for (values) do { v ->
v.accept(vis)
def v' = resolveIdentifiers(v)
vals.push(v')
if (v'.kind == "method") then {
if (moduleObj.elementScopes.contains(v'.value.value)) then {
v'.properties.put("fresh", moduleObj.getScope(v'.value.value))
}
if (v'.body.size > 0) then {
def lastStatement = v'.body.last
if (lastStatement.kind == "call") then {
if (lastStatement.value.kind == "member") then {
def mem = lastStatement.value
if (mem.value == "clone") then {
v'.properties.put("fresh", moduleObj)
}
}
}
}
}
}
vals
}