Mercurial > projects > dang
annotate tools/AstPrinter.d @ 94:48bb2287c035 new_gen
Added Modules. Right now it's very simple - will grow with time and need.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Tue, 06 May 2008 16:24:14 +0200 |
parents | eb5b2c719a39 |
children | fea8d61a2451 |
rev | line source |
---|---|
1 | 1 module tools.AstPrinter; |
2 | |
3 import tango.io.Stdout; | |
4 | |
94
48bb2287c035
Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents:
88
diff
changeset
|
5 import ast.Module, |
48bb2287c035
Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents:
88
diff
changeset
|
6 ast.Decl, |
1 | 7 ast.Stmt, |
8 ast.Exp; | |
9 | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
79
diff
changeset
|
10 import basic.SourceManager; |
1 | 11 |
12 class AstPrinter | |
13 { | |
14 const char[] tabType = " "; // 4 spaces | |
15 | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
79
diff
changeset
|
16 this(SourceManager sm) |
1 | 17 { |
18 | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
79
diff
changeset
|
19 this.sm = sm; |
1 | 20 } |
21 | |
94
48bb2287c035
Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents:
88
diff
changeset
|
22 void print(Module m) |
1 | 23 { |
94
48bb2287c035
Added Modules. Right now it's very simple - will grow with time and need.
Anders Johnsen <skabet@gmail.com>
parents:
88
diff
changeset
|
24 foreach(decl ; m.decls) |
1 | 25 { |
26 printDecl(decl); | |
27 } | |
28 } | |
29 | |
30 void printDecl(Decl decl) | |
31 { | |
32 switch(decl.declType) | |
33 { | |
34 case DeclType.FuncDecl: | |
35 auto funcDecl = cast(FuncDecl)decl; | |
36 printBeginLine(); | |
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:
60
diff
changeset
|
37 printIdentifier(funcDecl.returnType); |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
38 space; |
1 | 39 printIdentifier(funcDecl.identifier); |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
40 printFuncArgs(funcDecl); |
1 | 41 printOpenBrace(); |
42 foreach(stmt ; funcDecl.statements) | |
43 printStatement(stmt); | |
44 printCloseBrace(); | |
45 break; | |
46 | |
47 case DeclType.VarDecl: | |
48 auto varDecl = cast(VarDecl)decl; | |
49 printBeginLine(); | |
77
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
68
diff
changeset
|
50 printExp(varDecl.varType); |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
51 space; |
77
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
68
diff
changeset
|
52 printExp(varDecl.identifier); |
1 | 53 if(varDecl.init) |
54 { | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
55 print(" = "); |
1 | 56 printExp(varDecl.init); |
57 } | |
58 printEndLine(";"); | |
59 break; | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
60 |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
61 case DeclType.StructDecl: |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
62 auto structDecl = cast(StructDecl)decl; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
63 printBeginLine("struct "); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
64 printIdentifier(structDecl.identifier); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
65 printEndLine; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
66 printOpenBrace; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
67 foreach( var ; structDecl.vars) |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
68 printDecl(var); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
69 printCloseBrace; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
70 break; |
1 | 71 } |
72 } | |
73 | |
74 void printStatement(Stmt stmt) | |
75 { | |
76 switch(stmt.stmtType) | |
77 { | |
78 case StmtType.Return: | |
79 auto ret = cast(ReturnStmt)stmt; | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
80 printBeginLine("return"); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
81 if(ret.exp) |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
82 { |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
83 space; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
84 printExp(ret.exp); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
85 } |
1 | 86 printEndLine(";"); |
87 break; | |
88 case StmtType.Decl: | |
89 auto declStmt = cast(DeclStmt)stmt; | |
90 printDecl(declStmt.decl); | |
91 break; | |
92 case StmtType.Exp: | |
93 auto expStmt = cast(ExpStmt)stmt; | |
94 printBeginLine(); | |
95 printExp(expStmt.exp); | |
96 printEndLine(";"); | |
97 break; | |
98 | |
99 } | |
100 } | |
101 | |
102 void printExp(Exp exp) | |
103 { | |
104 switch(exp.expType) | |
105 { | |
106 case ExpType.Binary: | |
107 auto binaryExp = cast(BinaryExp)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
|
108 print("("); |
1 | 109 printExp(binaryExp.left); |
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
|
110 print(" " ~ binaryExp.getOp[binaryExp.op] ~ " "); |
1 | 111 printExp(binaryExp.right); |
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
|
112 print(")"); |
1 | 113 break; |
114 case ExpType.IntegerLit: | |
115 auto integetLit = cast(IntegerLit)exp; | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
79
diff
changeset
|
116 print(integetLit.get); |
1 | 117 break; |
118 case ExpType.Negate: | |
119 auto negateExp = cast(NegateExp)exp; | |
120 print("-"); | |
121 printExp(negateExp.exp); | |
122 break; | |
78
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
123 case ExpType.Deref: |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
124 auto derefExp = cast(DerefExp)exp; |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
125 print("*"); |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
126 printExp(derefExp.exp); |
ad956143dcdc
Parse and gen for dereferences
Anders Halager <halager@gmail.com>
parents:
68
diff
changeset
|
127 break; |
1 | 128 case ExpType.AssignExp: |
129 auto assignExp = cast(AssignExp)exp; | |
28
69464d465284
Now supporting structs - both read and write. Still a few errors though, so watch out.
Anders Johnsen <skabet@gmail.com>
parents:
1
diff
changeset
|
130 printExp(assignExp.identifier); |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
131 print(" = "); |
1 | 132 printExp(assignExp.exp); |
133 break; | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
134 case ExpType.MemberReference: |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
135 auto mrExp = cast(MemberReference)exp; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
136 printExp(mrExp.target); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
137 print("."); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
138 printIdentifier(mrExp.child); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
139 break; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
140 case ExpType.Identifier: |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
141 auto iden = cast(Identifier)exp; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
142 printIdentifier(iden); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
143 break; |
77
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
68
diff
changeset
|
144 case ExpType.PointerIdentifier: |
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
68
diff
changeset
|
145 auto iden = cast(PointerIdentifier)exp; |
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
68
diff
changeset
|
146 printExp(iden.pointerOf); |
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
68
diff
changeset
|
147 print("*"); |
13eea2c4e60d
Now able of --ast-dump-code with Pointer types and also codeGen int* x;
Anders Johnsen <skabet@gmail.com>
parents:
68
diff
changeset
|
148 break; |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
149 case ExpType.CallExp: |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
150 auto callExp = cast(CallExp)exp; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
151 printExp(callExp.exp); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
152 print("("); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
153 foreach(i, e; callExp.args) |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
154 { |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
155 printExp(e); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
156 if(i+1 < callExp.args.length) |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
157 print(", "); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
158 } |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
159 print(")"); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
160 break; |
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
|
161 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
|
162 auto castExp = cast(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
|
163 print("cast"); |
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
|
164 print("("); |
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
|
165 printExp(castExp.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
|
166 print(")"); |
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
|
167 printExp(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
|
168 break; |
1 | 169 } |
170 | |
171 } | |
172 | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
173 void printFuncArgs(FuncDecl decl) |
1 | 174 { |
175 print("("); | |
176 | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
177 foreach(i, d; decl.funcArgs) |
1 | 178 { |
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:
60
diff
changeset
|
179 printIdentifier(d.varType); |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
180 if(i == 0 && decl.sret) |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
181 print("*"); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
182 space; |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
183 printIdentifier(d.identifier); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
184 if(i+1 < decl.funcArgs.length) |
1 | 185 print(","); |
186 } | |
187 | |
188 printEndLine(")"); | |
189 } | |
190 | |
191 void printIdentifier(Identifier identifier) | |
192 { | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
193 print(identifier.get); |
1 | 194 } |
195 | |
196 void printOpenBrace() | |
197 { | |
198 printEndLine(tabIndex~"{"); | |
199 tabIndex ~= tabType; | |
200 } | |
201 | |
202 void printCloseBrace() | |
203 { | |
204 tabIndex = tabIndex[0 .. $-tabType.length]; | |
205 printEndLine(tabIndex~"}"); | |
206 } | |
207 | |
208 void printBeginLine(char[] line = "") | |
209 { | |
210 Stdout(tabIndex~line); | |
211 } | |
212 | |
213 void printEndLine(char[] line = "") | |
214 { | |
215 Stdout(line).newline; | |
216 } | |
217 | |
218 void print(char[] line) | |
219 { | |
220 Stdout(line); | |
221 } | |
60
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
222 |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
223 void space() |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
224 { |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
225 print(" "); |
2451f0904bf6
Dumping Ast with AstPrinter is now possible again! :)
Anders Johnsen <skabet@gmail.com>
parents:
28
diff
changeset
|
226 } |
1 | 227 private: |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
79
diff
changeset
|
228 SourceManager sm; |
1 | 229 char[] tabIndex; |
230 } |