forked from jdehaan2014/GearLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uresolver.pas
848 lines (762 loc) · 23.8 KB
/
uresolver.pas
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
unit uResolver;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, uVisitor, Generics.Collections, uAST, uToken, uError,
Variants;
type
TStatus = (Declared, Enabled);
TFuncKind = (fkNone, fkFunc, fkInit, fkMethod);
TClassKind = (ckNone, ckClass, ckSubClass, ckExtension, ckTrait, ckArray,
ckDictionary, ckEnum);
TSymbol = class
Name: String;
Status: TStatus;
Mutable: Boolean;
IsNull: Boolean;
constructor Create(const AName: String; const AStatus: TStatus;
const AMutable: Boolean=False);
end;
TScope = class(specialize TDictionary<String, TSymbol>)
private
FEnclosing: TScope;
public
property Enclosing: TScope read FEnclosing;
constructor Create(AEnclosing: TScope=Nil);
procedure Enter(ASymbol: TSymbol);
function Lookup(AName: String): TSymbol;
end;
TScopes = class(specialize TObjectList<TScope>)
procedure Push(Item: TScope);
procedure Pop;
function Top: TScope;
end;
TResolver = class(TVisitor)
public
constructor Create;
destructor Destroy; override;
procedure Resolve(Tree: TProduct);
published
procedure VisitNode(Node: TNode);
procedure VisitIdent(Ident: TIdent);
// Expr
procedure VisitBinaryExpr(BinaryExpr: TBinaryExpr);
procedure VisitConstExpr(ConstExpr: TConstExpr);
procedure VisitUnaryExpr(UnaryExpr: TUnaryExpr);
procedure VisitCallExpr(CallExpr: TCallExpr);
procedure VisitVariable(Variable: TVariable);
procedure VisitIfExpr(IfExpr: TIfExpr);
procedure VisitMatchExpr(MatchExpr: TMatchExpr);
procedure VisitTupleExpr(TupleExpr: TTupleExpr);
procedure VisitGetExpr(GetExpr: TGetExpr);
procedure VisitSelfExpr(SelfExpr: TSelfExpr);
procedure VisitInheritedExpr(InheritedExpr: TInheritedExpr);
procedure VisitArrayDeclExpr(ArrayDeclExpr: TArrayDeclExpr);
procedure VisitDictDeclExpr(DictDeclExpr: TDictDeclExpr);
procedure VisitIndexedExpr(IndexedExpr: TIndexedExpr);
procedure VisitInterpolatedExpr(InterpolatedExpr: TInterpolatedExpr);
// Stmt
procedure VisitPrintStmt(PrintStmt: TPrintStmt);
procedure VisitAssignStmt(AssignStmt: TAssignStmt);
procedure VisitCallExprStmt(CallExprStmt: TCallExprStmt);
procedure VisitSetStmt(SetStmt: TSetStmt);
procedure VisitIndexedExprStmt(IndexedExprStmt: TIndexedExprStmt);
procedure VisitIfStmt(IfStmt: TIfStmt);
procedure VisitWhileStmt(WhileStmt: TWhileStmt);
procedure VisitRepeatStmt(RepeatStmt: TRepeatStmt);
procedure VisitEnsureStmt(EnsureStmt: TEnsureStmt);
procedure VisitSwitchStmt(SwitchStmt: TSwitchStmt);
procedure VisitBreakStmt(BreakStmt: TBreakStmt);
procedure VisitContinueStmt(ContinueStmt: TContinueStmt);
procedure VisitReturnStmt(ReturnStmt: TReturnStmt);
procedure VisitUseStmt(UseStmt: TUseStmt);
// Decl
procedure VisitVarDecl(VarDecl: TVarDecl);
procedure VisitVarDecls(VarDecls: TVarDecls);
procedure VisitFuncDecl(FuncDecl: TFuncDecl);
procedure VisitFuncDeclExpr(FuncDeclExpr: TFuncDeclExpr);
procedure VisitClassDecl(ClassDecl: TClassDecl);
procedure VisitValDecl(ValDecl: TValDecl);
procedure VisitExtensionDecl(ExtensionDecl: TExtensionDecl);
procedure VisitTraitDecl(TraitDecl: TTraitDecl);
procedure VisitArrayDecl(ArrayDecl: TArrayDecl);
procedure VisitDictDecl(DictDecl: TDictDecl);
procedure VisitEnumDecl(EnumDecl: TEnumDecl);
// Blocks
procedure VisitBlock(Block: TBlock);
procedure VisitProduct(Product: TProduct);
private
GlobalScope: TScope;
CurrentScope: TScope;
Scopes: TScopes;
CurrentFuncKind: TFuncKind;
CurrentClassKind: TClassKind;
procedure BeginScope;
procedure EndScope;
procedure Declare(Ident: TIdent; const isMutable: Boolean=False);
procedure Enable(Ident: TIdent);
function Retrieve(Ident: TIdent): TSymbol;
procedure ResolveLocal(Variable: TVariable);
procedure ResolveFunction(Func: TFuncDecl; FuncKind: TFuncKind);
procedure EnableStandardFunctions;
procedure ResolveClass(ClassDecl: TClassDecl);
end;
implementation
uses uStandard;
const
ErrCannotReadLocalVar = 'Cannot read local variable in its own declaration.';
ErrCannotAssignToConstant = 'Cannot assign value to constant "%s".';
ErrDuplicateIdInScope = 'Duplicate identifier "%s" in this scope.';
ErrUndeclaredVar = 'Undeclared variable or function signature "%s".';
ErrReturnFromFunc = 'Return can only be used from a function.';
ErrSelfOutsideClass = 'Cannot use "self" outside a class.';
ErrParentNoClass = 'Parent "%s" is not defined as class.';
ErrInheritedInClass = 'Can only use "inherited" inside a class.';
ErrInheritedNotInSubClass = 'Cannot use "inherited" in a class without parent class.';
{ TSymbol }
constructor TSymbol.Create(const AName: String; const AStatus: TStatus;
const AMutable: Boolean);
begin
Name := AName;
Status := AStatus;
Mutable := AMutable;
isNull := False;
end;
{ TScope }
constructor TScope.Create(AEnclosing: TScope);
begin
inherited Create;
FEnclosing := AEnclosing;
end;
procedure TScope.Enter(ASymbol: TSymbol);
begin
Add(ASymbol.Name, ASymbol);
end;
function TScope.Lookup(AName: String): TSymbol;
begin
if ContainsKey(AName) then
Result := Items[AName]
else if Assigned(FEnclosing) then
Result := FEnclosing.Lookup(AName)
else
Result := Nil;
end;
{ TScopes }
procedure TScopes.Push(Item: TScope);
begin
Add(Item); // This item becomes the top of the stack
end;
procedure TScopes.Pop;
begin
Remove(Last); // Throw away the top of the stack
end;
function TScopes.Top: TScope;
begin
Result := Last; // Last is the lastly added item and thus the top of the stack
end;
{ TResolver }
constructor TResolver.Create;
begin
GlobalScope := TScope.Create;
CurrentScope := GlobalScope;
Scopes := TScopes.Create();
CurrentFuncKind := fkNone;
EnableStandardFunctions;
CurrentClassKind := ckNone;
end;
destructor TResolver.Destroy;
begin
GlobalScope.Free;
Scopes.Free;
inherited Destroy;
end;
procedure TResolver.Resolve(Tree: TProduct);
begin
VisitProc(Tree);
end;
procedure TResolver.VisitNode(Node: TNode);
begin
// do nothing
end;
procedure TResolver.VisitIdent(Ident: TIdent);
begin
// do nothing
end;
procedure TResolver.VisitBinaryExpr(BinaryExpr: TBinaryExpr);
begin
VisitProc(BinaryExpr.Left);
VisitProc(BinaryExpr.Right);
end;
procedure TResolver.VisitConstExpr(ConstExpr: TConstExpr);
begin
// do nothing
end;
procedure TResolver.VisitUnaryExpr(UnaryExpr: TUnaryExpr);
begin
VisitProc(UnaryExpr.Expr);
end;
procedure TResolver.VisitCallExpr(CallExpr: TCallExpr);
var
i: Integer;
begin
if (CallExpr.Callee is TVariable) and
ClassNameList.Contains(TVariable(CallExpr.Callee).Ident.Text) then
begin
CallExpr.FromClass := True; // set for reuse in interpreter
VisitProc(CallExpr.Callee); // just the class ident
end
else
VisitProc(CallExpr.Signature); // func ident + argument names
// visit argument expressions
for i := 0 to CallExpr.Args.Count-1 do
VisitProc(CallExpr.Args[i].Expr);
end;
procedure TResolver.VisitVariable(Variable: TVariable);
var
Symbol: TSymbol;
begin
Symbol := Retrieve(Variable.Ident);
if Symbol <> Nil then begin
if Symbol.Status = Declared then
with Variable.Token do
Errors.Append(Variable.Token, ErrCannotReadLocalVar);
ResolveLocal(Variable);
end;
end;
procedure TResolver.VisitIfExpr(IfExpr: TIfExpr);
begin
VisitProc(IfExpr.Condition);
VisitProc(IfExpr.TrueExpr);
VisitProc(IfExpr.FalseExpr);
end;
procedure TResolver.VisitMatchExpr(MatchExpr: TMatchExpr);
var
Key: TExpr;
begin
VisitProc(MatchExpr.Expr);
for Key in MatchExpr.IfLimbs.Keys do begin
VisitProc(Key);
VisitProc(MatchExpr.IfLimbs[Key]);
end;
VisitProc(MatchExpr.ElseLimb);
end;
procedure TResolver.VisitTupleExpr(TupleExpr: TTupleExpr);
var
Expr: TExpr;
begin
for Expr in TupleExpr.ExprList do
VisitProc(Expr);
end;
procedure TResolver.VisitGetExpr(GetExpr: TGetExpr);
begin
VisitProc(GetExpr.Instance);
end;
procedure TResolver.VisitSelfExpr(SelfExpr: TSelfExpr);
begin
if CurrentClassKind = ckNone then
Errors.Append(SelfExpr.Token, ErrSelfOutsideClass);
ResolveLocal(SelfExpr.Variable);
end;
procedure TResolver.VisitInheritedExpr(InheritedExpr: TInheritedExpr);
begin
if CurrentClassKind in [ckNone, ckTrait, ckArray, ckDictionary, ckEnum] then
Errors.Append(InheritedExpr.Token, ErrInheritedInClass)
else if CurrentClassKind <> ckSubClass then
Errors.Append(InheritedExpr.Token, ErrInheritedNotInSubClass);
ResolveLocal(InheritedExpr.Variable);
end;
procedure TResolver.VisitArrayDeclExpr(ArrayDeclExpr: TArrayDeclExpr);
begin
VisitProc(ArrayDeclExpr.ArrayDecl);
end;
procedure TResolver.VisitDictDeclExpr(DictDeclExpr: TDictDeclExpr);
begin
VisitProc(DictDeclExpr.DictDecl);
end;
procedure TResolver.VisitIndexedExpr(IndexedExpr: TIndexedExpr);
begin
VisitProc(IndexedExpr.Variable);
VisitProc(IndexedExpr.Index);
end;
procedure TResolver.VisitInterpolatedExpr(InterpolatedExpr: TInterpolatedExpr);
var
Expr: TExpr;
begin
for Expr in InterpolatedExpr.ExprList do
VisitProc(Expr);
end;
procedure TResolver.VisitPrintStmt(PrintStmt: TPrintStmt);
var
Expr: TExpr;
begin
for Expr in PrintStmt.ExprList do
VisitProc(Expr);
end;
procedure TResolver.VisitAssignStmt(AssignStmt: TAssignStmt);
var
Symbol: TSymbol;
Ident: TIdent;
begin
VisitProc(AssignStmt.Expr);
Ident := AssignStmt.Variable.Ident;
Symbol := Retrieve(Ident);
if Symbol <> Nil then begin
if (not Symbol.Mutable) and (not Symbol.isNull) then
Errors.Append(Ident.Token,
Format(ErrCannotAssignToConstant, [Ident.Text]))
else if Symbol.isNull then
Symbol.isNull := False;
ResolveLocal(AssignStmt.Variable);
end;
end;
procedure TResolver.VisitCallExprStmt(CallExprStmt: TCallExprStmt);
begin
VisitProc(CallExprStmt.CallExpr);
end;
procedure TResolver.VisitSetStmt(SetStmt: TSetStmt);
begin
VisitProc(SetStmt.GetExpr);
VisitProc(SetStmt.Expr);
end;
procedure TResolver.VisitIndexedExprStmt(IndexedExprStmt: TIndexedExprStmt);
begin
VisitProc(IndexedExprStmt.IndexedExpr);
VisitProc(IndexedExprStmt.Expr);
end;
procedure TResolver.VisitIfStmt(IfStmt: TIfStmt);
var
i: Integer;
begin
if Assigned(IfStmt.VarDecl) then begin
BeginScope;
VisitProc(IfStmt.VarDecl);
end;
VisitProc(IfStmt.Condition);
VisitProc(IfStmt.ThenPart);
if Assigned(IfStmt.ElseIfs) then begin
for i := 0 to IfStmt.ElseIfs.Count-1 do begin;
VisitProc(IfStmt.ElseIfs[i]);
VisitProc(IfStmt.ElseIfParts[i]);
end;
end;
if IfStmt.ElsePart <> Nil then
VisitProc(IfStmt.ElsePart);
if Assigned(IfStmt.VarDecl) then
EndScope;
end;
procedure TResolver.VisitWhileStmt(WhileStmt: TWhileStmt);
begin
if Assigned(WhileStmt.VarDecl) then begin
BeginScope;
VisitProc(WhileStmt.VarDecl);
end;
VisitProc(WhileStmt.Condition);
VisitProc(WhileStmt.Block);
if Assigned(WhileStmt.VarDecl) then
EndScope;
end;
procedure TResolver.VisitRepeatStmt(RepeatStmt: TRepeatStmt);
begin
VisitProc(RepeatStmt.Condition);
VisitProc(RepeatStmt.Block);
end;
procedure TResolver.VisitEnsureStmt(EnsureStmt: TEnsureStmt);
begin
if Assigned(EnsureStmt.VarDecl) then
VisitProc(EnsureStmt.VarDecl);
VisitProc(EnsureStmt.Condition);
VisitProc(EnsureStmt.ElsePart);
end;
procedure TResolver.VisitSwitchStmt(SwitchStmt: TSwitchStmt);
var
Key: TCaseItem;
begin
VisitProc(SwitchStmt.Expr);
for Key in SwitchStmt.CaseLimbs.Keys do begin
VisitProc(Key.Expr);
VisitProc(SwitchStmt.CaseLimbs[Key]);
end;
VisitProc(SwitchStmt.ElseLimb);
end;
procedure TResolver.VisitBreakStmt(BreakStmt: TBreakStmt);
begin
if Assigned(BreakStmt.Condition) then
VisitProc(BreakStmt.Condition);
end;
procedure TResolver.VisitContinueStmt(ContinueStmt: TContinueStmt);
begin
// nothing to resolve
end;
procedure TResolver.VisitReturnStmt(ReturnStmt: TReturnStmt);
begin
if CurrentFuncKind in [fkNone, fkInit] then
Errors.Append(ReturnStmt.Token, ErrReturnFromFunc);
VisitProc(ReturnStmt.Expr);
end;
procedure TResolver.VisitUseStmt(UseStmt: TUseStmt);
begin
// do nothing
end;
procedure TResolver.VisitVarDecl(VarDecl: TVarDecl);
var
Symbol: TSymbol;
begin
Declare(VarDecl.Ident, VarDecl.Mutable);
VisitProc(VarDecl.Expr);
Enable(VarDecl.Ident);
if (VarDecl.Expr is TConstExpr) and
((VarDecl.Expr as TConstExpr).Token.Typ = ttNull) then
begin
Symbol := Retrieve(VarDecl.Ident);
Symbol.isNull := True;
end;
end;
procedure TResolver.VisitVarDecls(VarDecls: TVarDecls);
var
Decl: TDecl;
begin
for Decl in VarDecls.List do
VisitProc(Decl);
end;
procedure TResolver.VisitFuncDecl(FuncDecl: TFuncDecl);
begin
if FuncDecl.Ident <> Nil then begin
Declare(FuncDecl.Ident);
Enable(FuncDecl.Ident);
end;
ResolveFunction(FuncDecl, fkFunc);
end;
procedure TResolver.VisitFuncDeclExpr(FuncDeclExpr: TFuncDeclExpr);
begin
VisitProc(FuncDeclExpr.FuncDecl);
end;
procedure TResolver.VisitClassDecl(ClassDecl: TClassDecl);
var
EnclosingClassKind: TClassKind;
begin
Declare(ClassDecl.Ident);
Enable(ClassDecl.Ident);
EnclosingClassKind := CurrentClassKind;
CurrentClassKind := ckClass;
if Assigned(ClassDecl.Parent) then begin
VisitProc(ClassDecl.Parent);
CurrentClassKind := ckSubClass;
if (not ClassNameList.Contains(ClassDecl.Parent.Ident.Text)) or
(ClassDecl.Ident.Text = ClassDecl.Parent.Ident.Text) then
with ClassDecl.Parent.Ident do
Errors.Append(Token, Format(ErrParentNoClass, [Text]));
BeginScope;
Scopes.Top.Enter(TSymbol.Create('inherited', Enabled));
end;
ResolveClass(ClassDecl);
if Assigned(ClassDecl.Parent) then EndScope;
CurrentClassKind := EnclosingClassKind;
end;
procedure TResolver.VisitValDecl(ValDecl: TValDecl);
begin
VisitProc(ValDecl.FuncDecl);
end;
procedure TResolver.VisitExtensionDecl(ExtensionDecl: TExtensionDecl);
var
Decl: TDecl;
EnclosingClassKind: TClassKind;
begin
EnclosingClassKind := CurrentClassKind;
CurrentClassKind := ckExtension;
BeginScope;
Scopes.Top.Enter(TSymbol.Create('self', Enabled));
for Decl in ExtensionDecl.DeclList do begin
case Decl.Kind of
dkFunc: ResolveFunction(Decl as TFuncDecl, fkMethod);
dkVal: ResolveFunction((Decl as TValDecl).FuncDecl, fkMethod);
end;
end;
EndScope;
CurrentClassKind := EnclosingClassKind;
end;
procedure TResolver.VisitTraitDecl(TraitDecl: TTraitDecl);
var
Decl: TDecl;
EnclosingClassKind: TClassKind;
Trait: TExpr;
begin
Declare(TraitDecl.Ident);
Enable(TraitDecl.Ident);
EnclosingClassKind := CurrentClassKind;
CurrentClassKind := ckTrait;
for Trait in TraitDecl.Traits do
VisitProc(Trait);
BeginScope;
Scopes.Top.Enter(TSymbol.Create('self', Enabled));
for Decl in TraitDecl.DeclList do begin
if Decl.Kind = dkFunc then
ResolveFunction(Decl as TFuncDecl, fkMethod);
end;
EndScope;
CurrentClassKind := EnclosingClassKind;
end;
procedure TResolver.VisitArrayDecl(ArrayDecl: TArrayDecl);
var
Expr: TExpr;
Decl: TDecl;
EnclosingClassKind: TClassKind;
begin
for Expr in ArrayDecl.Elements do
VisitProc(Expr);
if Assigned(ArrayDecl.Ident) then begin
Declare(ArrayDecl.Ident);
Enable(ArrayDecl.Ident);
EnclosingClassKind := CurrentClassKind;
CurrentClassKind := ckArray;
BeginScope;
Scopes.Top.Enter(TSymbol.Create('self', Enabled));
for Decl in ArrayDecl.DeclList do begin
case Decl.Kind of
dkFunc: ResolveFunction(Decl as TFuncDecl, fkMethod);
dkVal: ResolveFunction((Decl as TValDecl).FuncDecl, fkMethod);
end;
end;
EndScope;
CurrentClassKind := EnclosingClassKind;
end;
end;
procedure TResolver.VisitDictDecl(DictDecl: TDictDecl);
var
Key: TExpr;
Decl: TDecl;
EnclosingClassKind: TClassKind;
begin
for Key in DictDecl.KeyValueList.Keys do begin
VisitProc(Key);
VisitProc(DictDecl.KeyValueList[Key]);
end;
if Assigned(DictDecl.Ident) then begin
Declare(DictDecl.Ident);
Enable(DictDecl.Ident);
EnclosingClassKind := CurrentClassKind;
CurrentClassKind := ckDictionary;
BeginScope;
Scopes.Top.Enter(TSymbol.Create('self', Enabled));
for Decl in DictDecl.DeclList do begin
case Decl.Kind of
dkFunc: ResolveFunction(Decl as TFuncDecl, fkMethod);
dkVal: ResolveFunction((Decl as TValDecl).FuncDecl, fkMethod);
end;
end;
EndScope;
CurrentClassKind := EnclosingClassKind;
end;
end;
procedure TResolver.VisitEnumDecl(EnumDecl: TEnumDecl);
var
Decl: TDecl;
EnclosingClassKind: TClassKind;
begin
Declare(EnumDecl.Ident);
Enable(EnumDecl.Ident);
EnclosingClassKind := CurrentClassKind;
CurrentClassKind := ckEnum;
BeginScope;
Scopes.Top.Enter(TSymbol.Create('self', Enabled));
for Decl in EnumDecl.DeclList do begin
case Decl.Kind of
dkFunc: ResolveFunction(Decl as TFuncDecl, fkMethod);
dkVal: ResolveFunction((Decl as TValDecl).FuncDecl, fkMethod);
end;
end;
EndScope;
CurrentClassKind := EnclosingClassKind;
end;
procedure TResolver.VisitBlock(Block: TBlock);
var
Node: TNode;
begin
BeginScope;
for Node in Block.Nodes do
VisitProc(Node);
EndScope;
end;
procedure TResolver.VisitProduct(Product: TProduct);
var
Node: TNode;
begin
for Node in Product.Nodes do
VisitProc(Node);
end;
procedure TResolver.BeginScope;
begin
Scopes.Push(TScope.Create(CurrentScope));
CurrentScope := Scopes.Top;
end;
procedure TResolver.EndScope;
begin
CurrentScope := Scopes.Top.Enclosing;
Scopes.Pop;
end;
procedure TResolver.Declare(Ident: TIdent; const isMutable: Boolean);
begin
if CurrentScope.ContainsKey(Ident.Text) then
Errors.Append(Ident.Token, Format(
ErrDuplicateIdInScope, [Ident.Text]))
else
CurrentScope.Enter(TSymbol.Create(Ident.Text, Declared, isMutable));
end;
procedure TResolver.Enable(Ident: TIdent);
var
Symbol: TSymbol;
begin
Symbol := Retrieve(Ident);
if Symbol <> Nil then
Symbol.Status := Enabled;
end;
function TResolver.Retrieve(Ident: TIdent): TSymbol;
begin
Result := CurrentScope.Lookup(Ident.Text);
if Result = Nil then
Errors.Append(Ident.Token, Format(
ErrUndeclaredVar, [Ident.Text]));
end;
procedure TResolver.ResolveLocal(Variable: TVariable);
var
i: Integer;
begin
for i := Scopes.Count-1 downto 0 do
if Scopes[i].ContainsKey(Variable.Ident.Text) then begin
Variable.Distance := Scopes.Count-1-i;
Exit;
end;
end;
procedure TResolver.ResolveFunction(Func: TFuncDecl; FuncKind: TFuncKind);
var
EnclosingFuncKind: TFuncKind;
i: Integer;
begin
EnclosingFuncKind := CurrentFuncKind;
CurrentFuncKind := FuncKind;
BeginScope;
for i := 0 to Func.Params.Count-1 do begin
Declare(Func.Params[i].Ident);
Enable(Func.Params[i].Ident);
end;
VisitProc(Func.Body);
EndScope;
CurrentFuncKind := EnclosingFuncKind;
end;
procedure TResolver.ResolveClass(ClassDecl: TClassDecl);
var
Decl: TDecl;
FuncKind: TFuncKind;
Trait: TExpr;
begin
for Trait in ClassDecl.Traits do
VisitProc(Trait);
BeginScope;
Scopes.Top.Enter(TSymbol.Create('self', Enabled));
for Decl in ClassDecl.DeclList do begin
case Decl.Kind of
dkFunc: begin
FuncKind := fkMethod;
if Decl.Ident.Text = 'init' then
FuncKind := fkInit;
ResolveFunction(Decl as TFuncDecl, FuncKind);
end;
dkVal: ResolveFunction((Decl as TValDecl).FuncDecl, fkMethod);
dkVar: VisitProc(Decl as TVarDecl);
end;
end;
// resolve static declarations
for Decl in ClassDecl.StaticList do begin
BeginScope;
Scopes.Top.Enter(TSymbol.Create('self', Enabled));
case Decl.Kind of
//dkVar: Visit(Decl as TVarDecl);
dkFunc: ResolveFunction(Decl as TFuncDecl, fkMethod);
end;
EndScope;
end;
EndScope;
end;
procedure TResolver.EnableStandardFunctions;
begin
with GlobalScope do begin
Enter(TSymbol.Create('abs', Enabled, False));
Enter(TSymbol.Create('arctan', Enabled, False));
Enter(TSymbol.Create('chr', Enabled, False));
Enter(TSymbol.Create('cos', Enabled, False));
Enter(TSymbol.Create('date', Enabled, False));
Enter(TSymbol.Create('exp', Enabled, False));
Enter(TSymbol.Create('frac', Enabled, False));
Enter(TSymbol.Create('length', Enabled, False));
Enter(TSymbol.Create('ln', Enabled, False));
Enter(TSymbol.Create('milliseconds', Enabled, False));
Enter(TSymbol.Create('now', Enabled, False));
Enter(TSymbol.Create('ord', Enabled, False));
Enter(TSymbol.Create('pi', Enabled, False));
Enter(TSymbol.Create('random', Enabled, False));
Enter(TSymbol.Create('randomLimit', Enabled, False));
Enter(TSymbol.Create('randomize', Enabled, False));
Enter(TSymbol.Create('round', Enabled, False));
Enter(TSymbol.Create('sin', Enabled, False));
Enter(TSymbol.Create('sqr', Enabled, False));
Enter(TSymbol.Create('sqrt', Enabled, False));
Enter(TSymbol.Create('time', Enabled, False));
Enter(TSymbol.Create('today', Enabled, False));
Enter(TSymbol.Create('trunc', Enabled, False));
Enter(TSymbol.Create('assigned', Enabled, False));
Enter(TSymbol.Create('readln', Enabled, False));
Enter(TSymbol.Create('floor', Enabled, False));
Enter(TSymbol.Create('ceil', Enabled, False));
Enter(TSymbol.Create('toNum', Enabled, False));
Enter(TSymbol.Create('toStr', Enabled, False));
Enter(TSymbol.Create('pred', Enabled, False));
Enter(TSymbol.Create('succ', Enabled, False));
Enter(TSymbol.Create('method', Enabled, False));
Enter(TSymbol.Create('Array', Enabled, False));
Enter(TSymbol.Create('Dictionary', Enabled, False));
// list functions
Enter(TSymbol.Create('listAdd', Enabled, False));
Enter(TSymbol.Create('listAddList', Enabled, False));
Enter(TSymbol.Create('listClear', Enabled, False));
Enter(TSymbol.Create('listContains', Enabled, False));
Enter(TSymbol.Create('listDelete', Enabled, False));
Enter(TSymbol.Create('listExtract', Enabled, False));
Enter(TSymbol.Create('listFirst', Enabled, False));
Enter(TSymbol.Create('listIndexOf', Enabled, False));
Enter(TSymbol.Create('listInit', Enabled, False));
Enter(TSymbol.Create('listInsert', Enabled, False));
Enter(TSymbol.Create('listLast', Enabled, False));
Enter(TSymbol.Create('listRemove', Enabled, False));
Enter(TSymbol.Create('listRetrieve', Enabled, False));
// dictionary functions
Enter(TSymbol.Create('dictAdd', Enabled, False));
Enter(TSymbol.Create('dictAddList', Enabled, False));
Enter(TSymbol.Create('dictClear', Enabled, False));
Enter(TSymbol.Create('dictContainsKey', Enabled, False));
Enter(TSymbol.Create('dictContainsValue', Enabled, False));
Enter(TSymbol.Create('dictDelete', Enabled, False));
Enter(TSymbol.Create('dictKeyOf', Enabled, False));
Enter(TSymbol.Create('dictSetSorted', Enabled, False));
Enter(TSymbol.Create('dictSort', Enabled, False));
Enter(TSymbol.Create('dictValueOf', Enabled, False));
Enter(TSymbol.Create('dictKeys', Enabled, False));
Enter(TSymbol.Create('dictValues', Enabled, False));
// CRT functions
Enter(TSymbol.Create('window', Enabled, False));
Enter(TSymbol.Create('gotoXY', Enabled, False));
Enter(TSymbol.Create('clrScr', Enabled, False));
// files
Enter(TSymbol.Create('forReading', Enabled, False));
Enter(TSymbol.Create('forWriting', Enabled, False));
Enter(TSymbol.Create('forReadingAndWriting', Enabled, False));
Enter(TSymbol.Create('fromBeginning', Enabled, False));
Enter(TSymbol.Create('fromCurrent', Enabled, False));
Enter(TSymbol.Create('fromEnd', Enabled, False));
Enter(TSymbol.Create('fileOpen', Enabled, False));
Enter(TSymbol.Create('fileRead', Enabled, False));
Enter(TSymbol.Create('fileClose', Enabled, False));
Enter(TSymbol.Create('fileWrite', Enabled, False));
Enter(TSymbol.Create('fileSeek', Enabled, False));
Enter(TSymbol.Create('fileCreate', Enabled, False));
end;
end;
end.