annotate src/dil/semantic/Scope.d @ 839:4063da6f3edd default tip

Refactored the config file and how it is loaded.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 21 Aug 2008 17:51:04 +0200
parents 1ecf05e680ba
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
1 /++
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
2 Author: Aziz Köksal
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
3 License: GPL3
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
4 +/
592
b8dd677e0ace Moved dil.Scope to dil.semantic.Scope.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 590
diff changeset
5 module dil.semantic.Scope;
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
6
589
de365ddcfbd4 Moved dil.Symbol to dil.semantic.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 574
diff changeset
7 import dil.semantic.Symbol;
590
641041912670 Moved dil.Symbols to dil.semantic.Symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 589
diff changeset
8 import dil.semantic.Symbols;
611
6d449e777f5d Added semantic code to insert symbols into the scope.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 596
diff changeset
9 import dil.lexer.Identifier;
407
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
10 import common;
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
11
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 692
diff changeset
12 /// Builds a hierarchy of environments.
407
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
13 class Scope
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
14 {
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
15 Scope parent; /// The surrounding scope, or null if this is the root scope.
407
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
16
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
17 ScopeSymbol symbol; /// The current symbol with the symbol table.
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
18
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
19 this(Scope parent, ScopeSymbol symbol)
430
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
20 {
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
21 this.parent = parent;
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
22 this.symbol = symbol;
430
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
23 }
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
24
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 692
diff changeset
25 /// Find a symbol in this scope.
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 692
diff changeset
26 /// Params:
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 692
diff changeset
27 /// name = the name of the symbol.
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
28 Symbol lookup(Identifier* name)
430
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
29 {
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
30 return symbol.lookup(name);
562
b0533550d64c Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 560
diff changeset
31 }
b0533550d64c Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 560
diff changeset
32
838
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
33 /// Searches for a symbol in this scope and all enclosing scopes.
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
34 /// Params:
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
35 /// name = the name of the symbol.
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
36 Symbol search(Identifier* name)
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
37 {
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
38 Symbol symbol;
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
39 for (auto sc = this; sc; sc = sc.parent)
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
40 {
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
41 symbol = sc.lookup(name);
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
42 if (symbol !is null)
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
43 break;
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
44 }
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
45 return symbol;
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
46 }
1ecf05e680ba Changed build configuration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
47
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 692
diff changeset
48 /// Create a new inner scope and return that.
620
f15b054bb27e Renamed Scope.push/pop to Scope.enter/exit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 611
diff changeset
49 Scope enter(ScopeSymbol symbol)
430
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
50 {
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
51 return new Scope(this, symbol);
430
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
52 }
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
53
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 692
diff changeset
54 /// Destroy this scope and return the outer scope.
620
f15b054bb27e Renamed Scope.push/pop to Scope.enter/exit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 611
diff changeset
55 Scope exit()
430
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
56 {
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
57 auto sc = parent;
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
58 // delete this;
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
59 return sc;
e6c759e151cd Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 407
diff changeset
60 }
490
47be6bfe39cd Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 486
diff changeset
61
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
62 /// Search for the enclosing Class scope.
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
63 Scope classScope()
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
64 {
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
65 auto scop = this;
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
66 do
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
67 {
567
ab9f5020cd02 Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 565
diff changeset
68 if (scop.symbol.isClass)
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
69 return scop;
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
70 scop = scop.parent;
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
71 } while (scop)
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
72 return null;
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
73 }
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
74
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
75 /// Search for the enclosing Module scope.
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
76 Scope moduleScope()
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
77 {
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
78 auto scop = this;
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
79 do
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
80 {
567
ab9f5020cd02 Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 565
diff changeset
81 if (scop.symbol.isModule)
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
82 return scop;
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
83 scop = scop.parent;
692
d33895f679eb Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 683
diff changeset
84 } while (scop)
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
85 return null;
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
86 }
407
5431c0faf3b5 Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
87 }