Mercurial > projects > dang
annotate tools/DotPrinter.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 |
rev | line source |
---|---|
1 | 1 module tools.DotPrinter; |
2 | |
3 import tango.io.Stdout, | |
4 Int = tango.text.convert.Integer; | |
5 | |
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
|
6 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
|
7 ast.Decl, |
1 | 8 ast.Stmt, |
9 ast.Exp; | |
10 | |
11 class DotPrinter | |
12 { | |
13 this() | |
14 { | |
15 } | |
16 | |
17 private char[][void*] identifiers; | |
18 private int current_id = 0; | |
19 | |
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
|
20 void print(Module m) |
1 | 21 { |
22 Stdout("digraph {").newline; | |
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
|
23 foreach(decl ; m.decls) |
1 | 24 { |
25 printDecl(decl); | |
26 } | |
27 Stdout("}").newline; | |
28 } | |
29 | |
30 void printDecl(Decl decl) | |
31 { | |
32 switch(decl.declType) | |
33 { | |
34 case DeclType.FuncDecl: | |
35 FuncDecl funcDecl = cast(FuncDecl)decl; | |
36 | |
37 //printIdentifier(funcDecl.identifier); | |
38 //printFuncArgs(funcDecl.funcArgs); | |
39 Stdout(dotId(decl))(` [label="function`); | |
40 Stdout(`\n name: `)(text(funcDecl.identifier)); | |
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:
28
diff
changeset
|
41 Stdout(`\n return type: `)(text(funcDecl.returnType)); |
1 | 42 Stdout(`", shape=box, fillcolor=lightblue, style=filled]`); |
43 Stdout.newline; | |
44 //Stdout(`"`); | |
45 foreach(stmt ; funcDecl.statements) | |
46 printStatement(dotId(decl), stmt); | |
47 break; | |
48 | |
49 case DeclType.VarDecl: | |
50 VarDecl varDecl = cast(VarDecl)decl; | |
51 | |
52 //printIdentifier(funcDecl.identifier); | |
53 //printFuncArgs(funcDecl.funcArgs); | |
54 Stdout(dotId(decl))(` [label="var`); | |
55 Stdout(`\n name: `)(text(varDecl.identifier)); | |
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:
28
diff
changeset
|
56 Stdout(`\n type: `)(text(varDecl.varType)); |
1 | 57 Stdout(`"]`).newline; |
58 | |
59 if (varDecl.init !is null) | |
60 printExpression(dotId(decl), varDecl.init); | |
61 break; | |
62 } | |
63 } | |
64 | |
65 void printStatement(char[] parent, Stmt stmt) | |
66 { | |
67 auto id = dotId(stmt); | |
68 switch (stmt.stmtType) | |
69 { | |
70 case StmtType.Stmt: | |
71 Stdout(id)(` [label="Statement"]`).newline; | |
72 Stdout(parent)(` -> `)(id).newline; | |
73 break; | |
74 | |
75 case StmtType.Decl: | |
76 Stdout(id)(` [label="Decl"]`).newline; | |
77 Stdout(parent)(` -> `)(id).newline; | |
78 auto decl = (cast(DeclStmt)stmt).decl; | |
79 printDecl(decl); | |
80 Stdout(id)(` -> `)(dotId(decl)).newline; | |
81 break; | |
82 | |
83 case StmtType.Return: | |
84 Stdout(id)(` [label="Return"]`).newline; | |
85 Stdout(parent)(` -> `)(id).newline; | |
86 printExpression(id, (cast(ReturnStmt)stmt).exp); | |
87 break; | |
88 | |
89 case StmtType.Exp: | |
90 Stdout(parent)(` -> `)(id).newline; | |
91 printExpression(id, (cast(ExpStmt)stmt).exp); | |
92 break; | |
93 } | |
94 } | |
95 | |
96 void printExpression(char[] parent, Exp exp) | |
97 { | |
98 auto id = dotId(exp); | |
99 | |
100 switch(exp.expType) | |
101 { | |
102 case ExpType.Binary: | |
103 auto bin = cast(BinaryExp)exp; | |
104 Stdout(id)(` [label="`)(bin.op)(`"]`).newline; | |
105 printExpression(id, bin.left); | |
106 printExpression(id, bin.right); | |
107 break; | |
108 | |
109 case ExpType.Negate: | |
110 auto neg = cast(NegateExp)exp; | |
111 Stdout(id)(` [label="Negate"]`).newline; | |
112 printExpression(id, neg.exp); | |
113 break; | |
114 | |
115 case ExpType.IntegerLit: | |
116 auto e = cast(IntegerLit)exp; | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
63
diff
changeset
|
117 Stdout(id)(` [label="`)(text(e.get))(`"]`).newline; |
1 | 118 break; |
119 | |
120 case ExpType.Identifier: | |
121 auto e = cast(Identifier)exp; | |
122 Stdout(id)(` [label="`)(text(e))(`"]`).newline; | |
123 break; | |
124 | |
125 case ExpType.AssignExp: | |
126 auto ass = cast(AssignExp)exp; | |
127 Stdout(parent)(` [label="Assign"]`).newline; | |
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
|
128 // Stdout(id)(` [label="`)(text(ass.identifier))(`"]`).newline; |
1 | 129 printExpression(parent, ass.exp); |
130 break; | |
131 | |
132 case ExpType.CallExp: | |
133 break; | |
134 } | |
135 Stdout(parent)(` -> `)(id).newline; | |
136 } | |
137 | |
138 char[] dotId(Decl d) { return dotId(cast(void*)d); } | |
139 char[] dotId(Stmt s) { return dotId(cast(void*)s); } | |
140 char[] dotId(Exp e) { return dotId(cast(void*)e); } | |
141 | |
142 char[] dotId(void* o) | |
143 { | |
144 auto id = o in identifiers; | |
145 if (id is null) | |
146 { | |
147 ++current_id; | |
148 identifiers[o] = Int.toString(current_id); | |
149 id = o in identifiers; | |
150 } | |
151 return *id; | |
152 } | |
153 | |
154 char[] text(Identifier identifier) | |
155 { | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
63
diff
changeset
|
156 return identifier.get; |
1 | 157 } |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
63
diff
changeset
|
158 char[] text(char[] t) |
1 | 159 { |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
63
diff
changeset
|
160 return t; |
1 | 161 } |
162 } | |
88
eb5b2c719a39
Major change to locations, tokens and expressions.
Anders Halager <halager@gmail.com>
parents:
63
diff
changeset
|
163 |