Mercurial > projects > dang
annotate sema/SymbolTableBuilder.d @ 2:ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Lots of changes.
Now supports various integer-types in signed mode (byte, short, int, long)
Will cast back and forth when necessary
Doesnt check types and return type of functions is still hardcoded
Function return-types.
* Returned expressions are converted to the correct type for the function
* Callers get the correct type back
* Void functions work, and get an automatic "ret void" in the end
author | Anders Halager <halager@gmail.com> |
---|---|
date | Fri, 18 Apr 2008 02:31:07 +0200 |
parents | 2168f4cb73f1 |
children | 642c6a998fd9 |
rev | line source |
---|---|
1 | 1 module sema.SymbolTableBuilder; |
2 | |
3 import tango.io.Stdout; | |
4 | |
5 public import sema.SymbolTable; | |
6 | |
7 import sema.Visitor; | |
8 | |
9 class SymbolTableBuilder : Visitor!(void) | |
10 { | |
11 this() | |
12 { | |
13 table ~= new Scope; | |
14 } | |
15 | |
16 override void visit(Decl[] decls) | |
17 { | |
18 foreach (decl; decls) | |
19 visitDecl(decl); | |
20 } | |
21 | |
22 override void visitDecl(Decl d) | |
23 { | |
24 d.env = current(); | |
25 super.visitDecl(d); | |
26 } | |
27 | |
28 override void visitStmt(Stmt s) | |
29 { | |
30 s.env = current(); | |
31 super.visitStmt(s); | |
32 } | |
33 | |
34 override void visitExp(Exp e) | |
35 { | |
36 e.env = current(); | |
37 super.visitExp(e); | |
38 } | |
39 | |
40 override void visitFuncDecl(FuncDecl d) | |
41 { | |
42 auto sym = current().add(d.identifier); | |
43 sym.type = d.type; | |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
44 visitExp(d.type); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
45 visitExp(d.identifier); |
1 | 46 d.env = current(); |
2
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
47 push(); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
48 current().parentFunction = sym; |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
49 foreach (arg; d.funcArgs) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
50 visitDecl(arg); |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
51 foreach (stmt; d.statements) |
ae5bbe4e7fd6
Lots of stuff, here are the git comments:
Anders Halager <halager@gmail.com>
parents:
1
diff
changeset
|
52 visitStmt(stmt); |
1 | 53 pop(); |
54 } | |
55 | |
56 override void visitVarDecl(VarDecl d) | |
57 { | |
58 auto sc = current(); | |
59 auto sym = sc.add(d.identifier); | |
60 sym.type = d.type; | |
61 super.visitVarDecl(d); | |
62 } | |
63 | |
64 private: | |
65 Scope[] table; | |
66 | |
67 void push() | |
68 { | |
69 table ~= new Scope(current()); | |
70 } | |
71 | |
72 Scope pop() | |
73 { | |
74 auto res = table[$ - 1]; | |
75 table.length = table.length - 1; | |
76 return res; | |
77 } | |
78 | |
79 Scope current() | |
80 { | |
81 return table[$ - 1]; | |
82 } | |
83 } | |
84 |