Mercurial > projects > dang
annotate sema/Visitor.d @ 68:381975d76baf new_gen
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Thu, 01 May 2008 19:25:49 +0200 |
parents | 9f8131676242 |
children | 13eea2c4e60d ad956143dcdc |
rev | line source |
---|---|
1 | 1 module sema.Visitor; |
2 | |
3 import tango.io.Stdout; | |
4 | |
5 public | |
6 import ast.Decl, | |
7 ast.Stmt, | |
8 ast.Exp; | |
9 | |
10 import lexer.Token; | |
11 | |
12 class Visitor(FinalT = int, DeclT = FinalT, StmtT = DeclT, ExpT = StmtT) | |
13 { | |
14 public: | |
15 FinalT visit(Decl[] decls) | |
16 { | |
17 foreach (decl; decls) | |
18 visitDecl(decl); | |
19 static if (is(FinalT == void)) | |
20 return; | |
21 else | |
22 return FinalT.init; | |
23 } | |
24 | |
25 DeclT visitDecl(Decl decl) | |
26 { | |
27 switch(decl.declType) | |
28 { | |
29 case DeclType.FuncDecl: | |
30 return visitFuncDecl(cast(FuncDecl)decl); | |
31 case DeclType.VarDecl: | |
32 return visitVarDecl(cast(VarDecl)decl); | |
22 | 33 case DeclType.StructDecl: |
34 return visitStructDecl(cast(StructDecl)decl); | |
1 | 35 default: |
36 throw new Exception("Unknown declaration type"); | |
37 } | |
38 } | |
39 | |
40 StmtT visitStmt(Stmt stmt) | |
41 { | |
42 switch(stmt.stmtType) | |
43 { | |
44 case StmtType.Return: | |
45 return visitReturnStmt(cast(ReturnStmt)stmt); | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
46 case StmtType.Compound: |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
47 return visitCompoundStmt(cast(CompoundStatement)stmt); |
1 | 48 case StmtType.Decl: |
49 return visitDeclStmt(cast(DeclStmt)stmt); | |
50 case StmtType.Exp: | |
51 return visitExpStmt(cast(ExpStmt)stmt); | |
5
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
52 case StmtType.If: |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
53 return visitIfStmt(cast(IfStmt)stmt); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
54 case StmtType.While: |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
55 return visitWhileStmt(cast(WhileStmt)stmt); |
36
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
56 case StmtType.Switch: |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
57 return visitSwitchStmt(cast(SwitchStmt)stmt); |
1 | 58 default: |
59 throw new Exception("Unknown statement type"); | |
60 } | |
61 } | |
62 | |
63 ExpT visitExp(Exp exp) | |
64 { | |
65 switch(exp.expType) | |
66 { | |
67 case ExpType.Binary: | |
68 return visitBinaryExp(cast(BinaryExp)exp); | |
69 case ExpType.IntegerLit: | |
70 return visitIntegerLit(cast(IntegerLit)exp); | |
71 case ExpType.Negate: | |
72 return visitNegateExp(cast(NegateExp)exp); | |
73 case ExpType.AssignExp: | |
74 return visitAssignExp(cast(AssignExp)exp); | |
75 case ExpType.CallExp: | |
76 return visitCallExp(cast(CallExp)exp); | |
68
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
77 case ExpType.CastExp: |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
78 return visitCastExp(cast(CastExp)exp); |
1 | 79 case ExpType.Identifier: |
80 return visitIdentifier(cast(Identifier)exp); | |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
46
diff
changeset
|
81 case ExpType.MemberReference: |
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
46
diff
changeset
|
82 return visitMemberReference(cast(MemberReference)exp); |
1 | 83 default: |
84 throw new Exception("Unknown expression type"); | |
85 } | |
86 } | |
87 | |
88 // Declarations: | |
89 DeclT visitVarDecl(VarDecl d) | |
90 { | |
63
9f8131676242
Now Decl's have a DType type(), and should use varType and returnType to get the old type id
Anders Halager <halager@gmail.com>
parents:
55
diff
changeset
|
91 visitExp(d.varType); |
1 | 92 visitExp(d.identifier); |
93 if (d.init) | |
94 visitExp(d.init); | |
95 | |
96 static if (is(DeclT == void)) | |
97 return; | |
98 else | |
99 return DeclT.init; | |
100 } | |
101 | |
102 DeclT visitFuncDecl(FuncDecl f) | |
103 { | |
63
9f8131676242
Now Decl's have a DType type(), and should use varType and returnType to get the old type id
Anders Halager <halager@gmail.com>
parents:
55
diff
changeset
|
104 visitExp(f.returnType); |
1 | 105 visitExp(f.identifier); |
106 foreach (arg; f.funcArgs) | |
107 visitDecl(arg); | |
108 foreach (stmt; f.statements) | |
109 visitStmt(stmt); | |
110 | |
111 static if (is(DeclT == void)) | |
112 return; | |
113 else | |
114 return DeclT.init; | |
115 } | |
116 | |
22 | 117 DeclT visitStructDecl(StructDecl s) |
118 { | |
119 visitExp(s.identifier); | |
120 | |
121 foreach (arg; s.vars) | |
122 visitDecl(arg); | |
123 | |
124 static if (is(DeclT == void)) | |
125 return; | |
126 else | |
127 return DeclT.init; | |
128 } | |
129 | |
1 | 130 // Statements: |
131 StmtT visitReturnStmt(ReturnStmt s) | |
132 { | |
37 | 133 if (s.exp) |
134 visitExp(s.exp); | |
1 | 135 static if (is(StmtT == void)) |
136 return; | |
137 else | |
138 return StmtT.init; | |
139 } | |
140 | |
141 StmtT visitDeclStmt(DeclStmt d) | |
142 { | |
143 visitDecl(d.decl); | |
144 static if (is(StmtT == void)) | |
145 return; | |
146 else | |
147 return StmtT.init; | |
148 } | |
149 | |
44
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
150 StmtT visitCompoundStmt(CompoundStatement c) |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
151 { |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
152 foreach (stmt; c.statements) |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
153 visitStmt(stmt); |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
154 static if (is(StmtT == void)) |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
155 return; |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
156 else |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
157 return StmtT.init; |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
158 } |
495188f9078e
Big update - Moving towards a better, more seperated parser
Anders Halager <halager@gmail.com>
parents:
37
diff
changeset
|
159 |
5
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
160 StmtT visitIfStmt(IfStmt s) |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
161 { |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
162 visitExp(s.cond); |
45 | 163 visitStmt(s.then_body); |
164 if (s.else_body !is null) | |
165 visitStmt(s.else_body); | |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
166 static if (is(StmtT == void)) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
167 return; |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
168 else |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
169 return StmtT.init; |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
170 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
171 |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
172 StmtT visitWhileStmt(WhileStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
173 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
5
diff
changeset
|
174 visitExp(s.cond); |
46 | 175 visitStmt(s.whileBody); |
5
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
176 static if (is(StmtT == void)) |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
177 return; |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
178 else |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
179 return StmtT.init; |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
180 } |
2c5a8f4c254a
Added very simple if support.
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
181 |
36
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
182 StmtT visitSwitchStmt(SwitchStmt s) |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
183 { |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
184 visitExp(s.cond); |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
185 foreach(stmt; s.defaultBlock) |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
186 visitStmt(stmt); |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
187 foreach (c; s.cases) |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
188 { |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
189 foreach(lit; c.values) |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
190 visitIntegerLit(lit); |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
191 foreach(stmt; c.stmts) |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
192 visitStmt(stmt); |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
193 } |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
194 static if (is(StmtT == void)) |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
195 return; |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
196 else |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
197 return StmtT.init; |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
198 } |
ce17bea8e9bd
Switch statements support
Anders Halager <halager@gmail.com>
parents:
28
diff
changeset
|
199 |
1 | 200 StmtT visitExpStmt(ExpStmt s) |
201 { | |
202 visitExp(s.exp); | |
203 static if (is(StmtT == void)) | |
204 return; | |
205 else | |
206 return StmtT.init; | |
207 } | |
208 | |
209 // Expressions: | |
210 ExpT visitAssignExp(AssignExp exp) | |
211 { | |
212 visitExp(exp.identifier); | |
213 visitExp(exp.exp); | |
214 static if (is(ExpT == void)) | |
215 return; | |
216 else | |
217 return ExpT.init; | |
218 } | |
219 | |
220 ExpT visitBinaryExp(BinaryExp exp) | |
221 { | |
222 visitExp(exp.left); | |
223 visitExp(exp.right); | |
224 static if (is(ExpT == void)) | |
225 return; | |
226 else | |
227 return ExpT.init; | |
228 } | |
229 | |
230 ExpT visitCallExp(CallExp exp) | |
231 { | |
232 visitExp(exp.exp); | |
233 foreach (arg; exp.args) | |
234 visitExp(arg); | |
235 static if (is(ExpT == void)) | |
236 return; | |
237 else | |
238 return ExpT.init; | |
239 } | |
240 | |
68
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
241 ExpT visitCastExp(CastExp exp) |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
242 { |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
243 visitExp(exp.castType); |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
244 visitExp(exp.exp); |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
245 static if (is(ExpT == void)) |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
246 return; |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
247 else |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
248 return ExpT.init; |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
249 } |
381975d76baf
A LOT of bug fixing - also implemented implicit casts. If you do a --ast-dump-code on a target with some algebra of differant types, you should now see the type casts being made. Also, Tests are again back with only switches failing...
Anders Johnsen <skabet@gmail.com>
parents:
63
diff
changeset
|
250 |
1 | 251 ExpT visitNegateExp(NegateExp exp) |
252 { | |
253 visitExp(exp.exp); | |
254 static if (is(ExpT == void)) | |
255 return; | |
256 else | |
257 return ExpT.init; | |
258 } | |
259 | |
260 ExpT visitIntegerLit(IntegerLit exp) | |
261 { | |
262 static if (is(ExpT == void)) | |
263 return; | |
264 else | |
265 return ExpT.init; | |
266 } | |
267 | |
268 ExpT visitIdentifier(Identifier exp) | |
269 { | |
270 static if (is(ExpT == void)) | |
271 return; | |
272 else | |
273 return ExpT.init; | |
274 } | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
275 |
55
79cb0afafabe
Now structs are somewhat useable to use.
Anders Johnsen <skabet@gmail.com>
parents:
46
diff
changeset
|
276 ExpT visitMemberReference(MemberReference mem) |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
277 { |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
278 visitExp(mem.target); |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
279 visitExp(mem.child); |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
280 |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
281 static if (is(ExpT == void)) |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
282 return; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
283 else |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
284 return ExpT.init; |
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
22
diff
changeset
|
285 } |
1 | 286 } |
287 |