Mercurial > projects > dang
annotate sema/SymbolTableBuilder.d @ 27:9031487e97d7 new_gen
Various changes related to DType
* Drop the typeToLLVM table from LLVMGen
* Removed circular dependency
* Added basic types to DType
author | Anders Halager <halager@gmail.com> |
---|---|
date | Sun, 20 Apr 2008 01:08:50 +0200 |
parents | b4dc2b2c0e38 |
children | 69464d465284 |
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 { | |
13 this() | |
14 { | |
15 table ~= new Scope; | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
16 types["void"] = DType.Void; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
17 types["bool"] = DType.Bool; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
18 types["byte"] = DType.Byte; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
19 types["ubyte"] = DType.UByte; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
20 types["short"] = DType.Short; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
21 types["ushort"] = DType.UShort; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
22 types["int"] = DType.Int; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
23 types["uint"] = DType.UInt; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
24 types["long"] = DType.Long; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
25 types["ulong"] = DType.ULong; |
1 | 26 } |
27 | |
28 override void visit(Decl[] decls) | |
29 { | |
30 foreach (decl; decls) | |
31 visitDecl(decl); | |
32 } | |
33 | |
34 override void visitDecl(Decl d) | |
35 { | |
36 d.env = current(); | |
37 super.visitDecl(d); | |
38 } | |
39 | |
40 override void visitStmt(Stmt s) | |
41 { | |
42 s.env = current(); | |
43 super.visitStmt(s); | |
44 } | |
45 | |
46 override void visitExp(Exp e) | |
47 { | |
48 e.env = current(); | |
49 super.visitExp(e); | |
50 } | |
51 | |
52 override void visitFuncDecl(FuncDecl d) | |
53 { | |
54 auto sym = current().add(d.identifier); | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
55 sym.type = typeOf(d.type); |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
56 visitExp(d.type); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
57 visitExp(d.identifier); |
1 | 58 d.env = current(); |
14 | 59 auto sc = push(); |
60 sc.parentFunction = sym; | |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
61 foreach (arg; d.funcArgs) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
62 visitDecl(arg); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
63 foreach (stmt; d.statements) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
64 visitStmt(stmt); |
14 | 65 pop(sc); |
1 | 66 } |
67 | |
68 override void visitVarDecl(VarDecl d) | |
69 { | |
24 | 70 if (d.init) |
71 visitExp(d.init); | |
72 | |
73 if (need_push > 0) { | |
74 push(); | |
75 --need_push; | |
76 } | |
77 | |
1 | 78 auto sc = current(); |
79 auto sym = sc.add(d.identifier); | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
80 sym.type = typeOf(d.type); |
24 | 81 d.env = sc; |
82 visitExp(d.type); | |
83 visitExp(d.identifier); | |
1 | 84 } |
85 | |
22 | 86 override void visitStructDecl(StructDecl s) |
87 { | |
88 auto sc = current(); | |
89 auto sym = sc.add(s.identifier); | |
90 // sym.type = Tok.Struct; | |
91 super.visitStructDecl(s); | |
92 } | |
93 | |
14 | 94 override void visitDeclStmt(DeclStmt d) |
95 { | |
24 | 96 ++need_push; |
14 | 97 super.visitDeclStmt(d); |
98 } | |
24 | 99 private uint need_push = 0; |
14 | 100 |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
101 override void visitIfStmt(IfStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
102 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
103 s.env = current(); |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
104 visitExp(s.cond); |
14 | 105 auto sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
106 foreach (stmt; s.then_body) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
107 visitStmt(stmt); |
14 | 108 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
109 |
14 | 110 sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
111 foreach (stmt; s.else_body) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
112 visitStmt(stmt); |
14 | 113 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
114 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
115 |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
116 override void visitWhileStmt(WhileStmt s) |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
117 { |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
118 s.env = current(); |
14 | 119 auto sc = push(); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
120 super.visitWhileStmt(s); |
14 | 121 pop(sc); |
11
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
122 } |
642c6a998fd9
Support for while statements and fixed scope for if
Anders Halager <halager@gmail.com>
parents:
2
diff
changeset
|
123 |
1 | 124 private: |
125 Scope[] table; | |
126 | |
14 | 127 Scope push() |
1 | 128 { |
14 | 129 auto sc = new Scope(current()); |
130 table ~= sc; | |
131 return sc; | |
1 | 132 } |
133 | |
14 | 134 Scope pop(Scope sc = null) |
1 | 135 { |
14 | 136 if (sc !is null) |
137 { | |
138 table.length = table.find(sc); | |
139 return sc; | |
140 } | |
141 | |
1 | 142 auto res = table[$ - 1]; |
143 table.length = table.length - 1; | |
144 return res; | |
145 } | |
146 | |
147 Scope current() | |
148 { | |
149 return table[$ - 1]; | |
150 } | |
27
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
151 |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
152 DType typeOf(Identifier id) |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
153 { |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
154 if (auto type = id.get in types) |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
155 return *type; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
156 DType res = new DType(id); |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
157 types[id.get] = res; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
158 return res; |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
159 } |
9031487e97d7
Various changes related to DType
Anders Halager <halager@gmail.com>
parents:
26
diff
changeset
|
160 DType[char[]] types; |
1 | 161 } |
162 |