Mercurial > projects > dang
annotate sema/SymbolTableBuilder.d @ 56:4ae365eff712 new_gen
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Mon, 28 Apr 2008 21:40:00 +0200 |
parents | 79cb0afafabe |
children | 1d6f4ad38a91 |
rev | line source |
---|---|
1 | 1 module sema.SymbolTableBuilder; |
2 | |
14 | 3 import tango.io.Stdout, |
4 tango.core.Array : find; | |
1 | 5 |
26 | 6 public |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
7 import sema.SymbolTable; |
1 | 8 |
9 import sema.Visitor; | |
10 | |
11 class SymbolTableBuilder : Visitor!(void) | |
12 { | |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
13 override void visit(Decl[] decls) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
14 { |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
15 auto sb = new ScopeBuilder(); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
16 sb.visit(decls); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
17 foreach (decl; decls) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
18 visitDecl(decl); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
19 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
20 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
21 override void visitFuncDecl(FuncDecl d) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
22 { |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
23 d.env.find(d.identifier).type = typeOf(d.type, d.env); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
24 visitExp(d.type); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
25 visitExp(d.identifier); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
26 foreach (arg; d.funcArgs) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
27 visitDecl(arg); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
28 foreach (stmt; d.statements) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
29 visitStmt(stmt); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
30 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
31 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
32 override void visitVarDecl(VarDecl d) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
33 { |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
34 if (d.init) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
35 visitExp(d.init); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
36 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
37 d.env.find(d.identifier).type = typeOf(d.type, d.env); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
38 visitExp(d.type); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
39 visitExp(d.identifier); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
40 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
41 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
42 override void visitStructDecl(StructDecl s) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
43 { |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
44 DType[char[]] types; |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
45 |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
46 auto st = (cast(DStruct)s.env.types[s.identifier.get]); |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
47 foreach(varDecl ; s.vars) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
48 { |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
49 st.addMember(typeOf(varDecl.type, varDecl.env), varDecl.identifier.get); |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
50 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
51 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
52 super.visitStructDecl(s); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
53 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
54 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
55 DType typeOf(Identifier id, Scope sc) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
56 { |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
57 return sc.findType(id); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
58 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
59 /* |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
60 if (auto type = id.get in types) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
61 return *type; |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
62 DType res = new DType(id); |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
63 types[id.get] = res; |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
64 return res;*/ |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
65 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
66 } |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
67 |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
68 class ScopeBuilder : Visitor!(void) |
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
69 { |
1 | 70 this() |
71 { | |
72 table ~= new Scope; | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
73 table[0].types["void"] = DType.Void; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
74 table[0].types["bool"] = DType.Bool; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
75 table[0].types["byte"] = DType.Byte; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
76 table[0].types["ubyte"] = DType.UByte; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
77 table[0].types["short"] = DType.Short; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
78 table[0].types["ushort"] = DType.UShort; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
79 table[0].types["int"] = DType.Int; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
80 table[0].types["uint"] = DType.UInt; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
81 table[0].types["long"] = DType.Long; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
82 table[0].types["ulong"] = DType.ULong; |
1 | 83 } |
84 | |
85 override void visit(Decl[] decls) | |
86 { | |
87 foreach (decl; decls) | |
88 visitDecl(decl); | |
89 } | |
90 | |
91 override void visitDecl(Decl d) | |
92 { | |
93 d.env = current(); | |
94 super.visitDecl(d); | |
95 } | |
96 | |
97 override void visitStmt(Stmt s) | |
98 { | |
99 s.env = current(); | |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
100 s.stmtIndex = s.env.stmtIndex; |
1 | 101 super.visitStmt(s); |
102 } | |
103 | |
104 override void visitExp(Exp e) | |
105 { | |
106 e.env = current(); | |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
107 e.stmtIndex = e.env.stmtIndex; |
1 | 108 super.visitExp(e); |
109 } | |
110 | |
111 override void visitFuncDecl(FuncDecl d) | |
112 { | |
113 auto sym = current().add(d.identifier); | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
114 auto sc = push(); |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
115 |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
116 visitExp(d.type); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
117 visitExp(d.identifier); |
1 | 118 d.env = current(); |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
119 sc.parentFunction = d; |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
120 foreach (arg; d.funcArgs) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
121 visitDecl(arg); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
122 foreach (stmt; d.statements) |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
123 { |
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
124 sc.currentStmtIndex++; |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
125 visitStmt(stmt); |
56
4ae365eff712
Now return types works for structs... Also, simplyfing in AST have been startet - but still messy. This update is a little messy...
Anders Johnsen <skabet@gmail.com>
parents:
55
diff
changeset
|
126 } |
14 | 127 pop(sc); |
1 | 128 } |
129 | |
130 override void visitVarDecl(VarDecl d) | |
131 { | |
24 | 132 if (d.init) |
133 visitExp(d.init); | |
134 | |
53
da551f90e03f
Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
Anders Johnsen <skabet@gmail.com>
parents:
45
diff
changeset
|
135 if (need_push > 0 && current().parentFunction !is null) { |
24 | 136 push(); |
137 --need_push; | |
138 } | |
139 | |
1 | 140 auto sc = current(); |
141 auto sym = sc.add(d.identifier); | |
24 | 142 d.env = sc; |
143 visitExp(d.type); | |
144 visitExp(d.identifier); | |
1 | 145 } |
146 | |
22 | 147 override void visitStructDecl(StructDecl s) |
148 { | |
149 auto sc = current(); | |
150 auto sym = sc.add(s.identifier); | |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
151 s.env = sc; |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
152 auto type = new DStruct(s.identifier); |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
153 |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
154 |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
27
diff
changeset
|
155 sc.types[s.identifier.get] = type; |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
156 |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
157 sc = push(); |
22 | 158 super.visitStructDecl(s); |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
53
diff
changeset
|
159 pop(sc); |
22 | 160 } |
161 | |
14 | 162 override void visitDeclStmt(DeclStmt d) |
163 { | |
24 | 164 ++need_push; |
14 | 165 super.visitDeclStmt(d); |
166 } | |
24 | 167 private uint need_push = 0; |
14 | 168 |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
169 override void visitIfStmt(IfStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
170 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
171 s.env = current(); |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
172 visitExp(s.cond); |
14 | 173 auto sc = push(); |
45 | 174 visitStmt(s.then_body); |
14 | 175 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
176 |
45 | 177 if (s.else_body !is null) |
178 { | |
179 sc = push(); | |
180 visitStmt(s.else_body); | |
181 pop(sc); | |
182 } | |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
183 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
184 |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
185 override void visitWhileStmt(WhileStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
186 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
187 s.env = current(); |
14 | 188 auto sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
189 super.visitWhileStmt(s); |
14 | 190 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
191 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
192 |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
193 override void visitCompoundStmt(CompoundStatement s) |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
194 { |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
195 s.env = current(); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
196 auto sc = push(); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
197 super.visitCompoundStmt(s); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
198 pop(sc); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
199 } |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
200 |
1 | 201 private: |
202 Scope[] table; | |
203 | |
14 | 204 Scope push() |
1 | 205 { |
14 | 206 auto sc = new Scope(current()); |
207 table ~= sc; | |
208 return sc; | |
1 | 209 } |
210 | |
14 | 211 Scope pop(Scope sc = null) |
1 | 212 { |
14 | 213 if (sc !is null) |
214 { | |
215 table.length = table.find(sc); | |
216 return sc; | |
217 } | |
218 | |
1 | 219 auto res = table[$ - 1]; |
220 table.length = table.length - 1; | |
221 return res; | |
222 } | |
223 | |
224 Scope current() | |
225 { | |
226 return table[$ - 1]; | |
227 } | |
228 } | |
229 |