Mercurial > projects > dil
annotate trunk/src/dil/semantic/Scope.d @ 692:d33895f679eb
Tidied up the class Scope a bit.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Tue, 22 Jan 2008 18:32:39 +0100 |
parents | 1ae72234db26 |
children | c24be8d4f6ab |
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 |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
12 class Scope |
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
13 { |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
14 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
|
15 |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
16 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
|
17 |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
18 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
|
19 { |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
20 this.parent = parent; |
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
21 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
|
22 } |
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 /++ |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
25 Find a symbol in this scope. |
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
26 Params: |
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
27 name = the name of the symbol. |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
28 +/ |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
29 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
|
30 { |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
31 return symbol.lookup(name); |
562
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
32 } |
b0533550d64c
Added semantic() to VariableDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
560
diff
changeset
|
33 |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
34 /++ |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
35 Create a new inner scope and return that. |
430
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
36 +/ |
620
f15b054bb27e
Renamed Scope.push/pop to Scope.enter/exit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
611
diff
changeset
|
37 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
|
38 { |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
39 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
|
40 } |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
41 |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
42 /++ |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
43 Destroy this scope and return the outer scope. |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
44 +/ |
620
f15b054bb27e
Renamed Scope.push/pop to Scope.enter/exit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
611
diff
changeset
|
45 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
|
46 { |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
47 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
|
48 // delete this; |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
49 return sc; |
e6c759e151cd
Fixed a few things regarding encoding/decoding UTF-8 sequences.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
407
diff
changeset
|
50 } |
490
47be6bfe39cd
Refactored code and added new modules.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
486
diff
changeset
|
51 |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
52 /// Search for the enclosing Class scope. |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
53 Scope classScope() |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
54 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
55 auto scop = this; |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
56 do |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
57 { |
567
ab9f5020cd02
Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
565
diff
changeset
|
58 if (scop.symbol.isClass) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
59 return scop; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
60 scop = scop.parent; |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
61 } while (scop) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
62 return null; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
63 } |
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 /// Search for the enclosing Module scope. |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
66 Scope moduleScope() |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
67 { |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
68 auto scop = this; |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
69 do |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
70 { |
567
ab9f5020cd02
Added 'is-methods' to class Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
565
diff
changeset
|
71 if (scop.symbol.isModule) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
72 return scop; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
73 scop = scop.parent; |
692
d33895f679eb
Tidied up the class Scope a bit.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
683
diff
changeset
|
74 } while (scop) |
560
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
75 return null; |
709e223a8eb9
Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
532
diff
changeset
|
76 } |
407
5431c0faf3b5
Added modules dil.Scope and dil.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
77 } |