Mercurial > projects > dang
annotate sema/SymbolTableBuilder.d @ 22:e331e4e816e4
now handling structs to some extend
author | johnsen@johnsen-laptop |
---|---|
date | Fri, 18 Apr 2008 23:45:45 +0200 |
parents | a51bdf15a33d |
children | 2d28b21faad6 |
rev | line source |
---|---|
1 | 1 module sema.SymbolTableBuilder; |
2 | |
14 | 3 import tango.io.Stdout, |
4 tango.core.Array : find; | |
1 | 5 |
6 public import sema.SymbolTable; | |
7 | |
8 import sema.Visitor; | |
9 | |
10 class SymbolTableBuilder : Visitor!(void) | |
11 { | |
12 this() | |
13 { | |
14 table ~= new Scope; | |
15 } | |
16 | |
17 override void visit(Decl[] decls) | |
18 { | |
19 foreach (decl; decls) | |
20 visitDecl(decl); | |
21 } | |
22 | |
23 override void visitDecl(Decl d) | |
24 { | |
25 d.env = current(); | |
26 super.visitDecl(d); | |
27 } | |
28 | |
29 override void visitStmt(Stmt s) | |
30 { | |
31 s.env = current(); | |
32 super.visitStmt(s); | |
33 } | |
34 | |
35 override void visitExp(Exp e) | |
36 { | |
37 e.env = current(); | |
38 super.visitExp(e); | |
39 } | |
40 | |
41 override void visitFuncDecl(FuncDecl d) | |
42 { | |
43 auto sym = current().add(d.identifier); | |
44 sym.type = d.type; | |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
45 visitExp(d.type); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
46 visitExp(d.identifier); |
1 | 47 d.env = current(); |
14 | 48 auto sc = push(); |
49 sc.parentFunction = sym; | |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
50 foreach (arg; d.funcArgs) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
51 visitDecl(arg); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
52 foreach (stmt; d.statements) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
53 visitStmt(stmt); |
14 | 54 pop(sc); |
1 | 55 } |
56 | |
57 override void visitVarDecl(VarDecl d) | |
58 { | |
59 auto sc = current(); | |
60 auto sym = sc.add(d.identifier); | |
61 sym.type = d.type; | |
62 super.visitVarDecl(d); | |
63 } | |
64 | |
22 | 65 override void visitStructDecl(StructDecl s) |
66 { | |
67 auto sc = current(); | |
68 auto sym = sc.add(s.identifier); | |
69 // sym.type = Tok.Struct; | |
70 super.visitStructDecl(s); | |
71 } | |
72 | |
14 | 73 override void visitDeclStmt(DeclStmt d) |
74 { | |
75 super.visitDeclStmt(d); | |
76 push(); | |
77 } | |
78 | |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
79 override void visitIfStmt(IfStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
80 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
81 s.env = current(); |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
82 visitExp(s.cond); |
14 | 83 auto sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
84 foreach (stmt; s.then_body) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
85 visitStmt(stmt); |
14 | 86 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
87 |
14 | 88 sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
89 foreach (stmt; s.else_body) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
90 visitStmt(stmt); |
14 | 91 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
92 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
93 |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
94 override void visitWhileStmt(WhileStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
95 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
96 s.env = current(); |
14 | 97 auto sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
98 super.visitWhileStmt(s); |
14 | 99 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
100 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
101 |
1 | 102 private: |
103 Scope[] table; | |
104 | |
14 | 105 Scope push() |
1 | 106 { |
14 | 107 auto sc = new Scope(current()); |
108 table ~= sc; | |
109 return sc; | |
1 | 110 } |
111 | |
14 | 112 Scope pop(Scope sc = null) |
1 | 113 { |
14 | 114 if (sc !is null) |
115 { | |
116 table.length = table.find(sc); | |
117 return sc; | |
118 } | |
119 | |
1 | 120 auto res = table[$ - 1]; |
121 table.length = table.length - 1; | |
122 return res; | |
123 } | |
124 | |
125 Scope current() | |
126 { | |
127 return table[$ - 1]; | |
128 } | |
129 } | |
130 |