Mercurial > projects > dil
comparison trunk/src/dil/translator/German.d @ 669:1e3c5967ef12
Added module dil.translator.German.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Thu, 17 Jan 2008 19:45:28 +0100 |
parents | |
children | d8c32113afde |
comparison
equal
deleted
inserted
replaced
668:a1f8d8f2db38 | 669:1e3c5967ef12 |
---|---|
1 /++ | |
2 Author: Aziz Köksal | |
3 License: GPL3 | |
4 +/ | |
5 module dil.translator.German; | |
6 | |
7 import dil.ast.DefaultVisitor; | |
8 import dil.ast.Node; | |
9 import dil.ast.Declarations, | |
10 dil.ast.Statements, | |
11 dil.ast.Types, | |
12 dil.ast.Parameters; | |
13 import tango.io.Print; | |
14 | |
15 private alias Declaration D; | |
16 | |
17 /++ | |
18 Traverses a D syntax tree and explains in German. | |
19 +/ | |
20 class GermanTranslator : DefaultVisitor | |
21 { | |
22 Print!(char) put; | |
23 | |
24 char[] indent; | |
25 char[] indentStep; | |
26 | |
27 Declaration inAggregate; | |
28 Declaration inFunc; | |
29 | |
30 /++ | |
31 Construct a GermanTranslator. | |
32 Params: | |
33 put = buffer to print to. | |
34 indentStep = added at every indendation step. | |
35 +/ | |
36 this(Print!(char) put, char[] indentStep) | |
37 { | |
38 this.put = put; | |
39 this.indentStep = indentStep; | |
40 } | |
41 | |
42 /// Start translation. | |
43 void translate(Node root) | |
44 { | |
45 visitN(root); | |
46 } | |
47 | |
48 scope class Indent | |
49 { | |
50 char[] old_indent; | |
51 this() | |
52 { | |
53 old_indent = this.outer.indent; | |
54 this.outer.indent ~= this.outer.indentStep; | |
55 } | |
56 | |
57 ~this() | |
58 { this.outer.indent = old_indent; } | |
59 | |
60 char[] toString() | |
61 { return this.outer.indent; } | |
62 } | |
63 | |
64 scope class Enter(T) | |
65 { | |
66 T t_save; | |
67 this(T t) | |
68 { | |
69 auto t_save = t; | |
70 static if (is(T == ClassDeclaration) || | |
71 is(T == InterfaceDeclaration) || | |
72 is(T == StructDeclaration) || | |
73 is(T == UnionDeclaration)) | |
74 this.outer.inAggregate = t; | |
75 static if (is(T == FunctionDeclaration) || | |
76 is(T == ConstructorDeclaration)) | |
77 this.outer.inFunc = t; | |
78 } | |
79 | |
80 ~this() | |
81 { | |
82 static if (is(T == ClassDeclaration) || | |
83 is(T == InterfaceDeclaration) || | |
84 is(T == StructDeclaration) || | |
85 is(T == UnionDeclaration)) | |
86 this.outer.inAggregate = t_save; | |
87 static if (is(T == FunctionDeclaration) || | |
88 is(T == ConstructorDeclaration)) | |
89 this.outer.inFunc = t_save; | |
90 } | |
91 } | |
92 | |
93 alias Enter!(ClassDeclaration) EnteredClass; | |
94 alias Enter!(FunctionDeclaration) EnteredFunction; | |
95 alias Enter!(ConstructorDeclaration) EnteredConstructor; | |
96 | |
97 | |
98 override: | |
99 D visit(ModuleDeclaration n) | |
100 { | |
101 put.format("Das Modul '{}'", n.moduleName.str); | |
102 if (n.packages.length) | |
103 put.format(" im Paket '{}'", n.getPackageName('.')); | |
104 put(".").newline; | |
105 return n; | |
106 } | |
107 | |
108 D visit(ClassDeclaration n) | |
109 { | |
110 scope E = new EnteredClass(n); | |
111 put(indent).formatln("'{}' is eine Klasse mit den Eigenschaften:", n.name.str); | |
112 scope I = new Indent(); | |
113 n.decls && visitD(n.decls); | |
114 return n; | |
115 } | |
116 | |
117 D visit(VariableDeclaration n) | |
118 { | |
119 char[] was; | |
120 if (inAggregate) | |
121 was = "MemberVariable"; | |
122 else if (inFunc) | |
123 was = "lokale Variable"; | |
124 else | |
125 was = "globale Variable"; | |
126 foreach (name; n.idents) | |
127 { | |
128 put(indent).format("'{}' ist eine {} des Typs: ", name.str, was); | |
129 if (n.typeNode) | |
130 visitT(n.typeNode); | |
131 else | |
132 put("auto"); | |
133 put.newline; | |
134 } | |
135 return n; | |
136 } | |
137 | |
138 D visit(FunctionDeclaration n) | |
139 { | |
140 scope E = new EnteredFunction(n); | |
141 char[] was = inAggregate ? "Methode" : "Funktion"; | |
142 put(indent).format("'{}' ist eine {} ", n.name.str, was); | |
143 if (n.params.length) | |
144 put("mit den Argumenten"), visitN(n.params); | |
145 else | |
146 put("ohne Argumente"); | |
147 put.newline; | |
148 scope I = new Indent(); | |
149 return n; | |
150 } | |
151 | |
152 D visit(ConstructorDeclaration n) | |
153 { | |
154 scope E = new EnteredConstructor(n); | |
155 put(indent)("Ein Konstruktor "); | |
156 if (n.params.length == 1) | |
157 put("mit dem Argument "),put((visitN(n.params), ".")); | |
158 else if (n.params.length > 1) | |
159 put("mit den Argumenten "),put((visitN(n.params), ".")); | |
160 else | |
161 put("ohne Argumente."); | |
162 put.newline; | |
163 return n; | |
164 } | |
165 | |
166 D visit(StaticConstructorDeclaration n) | |
167 { | |
168 put(indent)("Statischer Konstruktor.").newline; | |
169 return n; | |
170 } | |
171 | |
172 D visit(DestructorDeclaration n) | |
173 { | |
174 put(indent)("Destruktor.").newline; | |
175 return n; | |
176 } | |
177 | |
178 D visit(StaticDestructorDeclaration n) | |
179 { | |
180 put(indent)("Statischer Destruktor.").newline; | |
181 return n; | |
182 } | |
183 | |
184 D visit(InvariantDeclaration n) | |
185 { | |
186 put(indent)("Eine Unveränderliche.").newline; | |
187 return n; | |
188 } | |
189 | |
190 D visit(UnittestDeclaration n) | |
191 { | |
192 put(".").newline; | |
193 return n; | |
194 } | |
195 | |
196 Node visit(Parameter n) | |
197 { | |
198 put.format("'{}' des Typs \"", n.name ? n.name.str : "unbenannt"); | |
199 visitN(n.type); | |
200 put(\"); | |
201 return n; | |
202 } | |
203 | |
204 TypeNode visit(ArrayType n) | |
205 { | |
206 if (n.assocType) | |
207 visitT(n.assocType); | |
208 else if (n.e) | |
209 visitE(n.e), n.e2 && visitE(n.e2); | |
210 else | |
211 put("dynamisches Array von "), visitT(n.next); | |
212 return n; | |
213 } | |
214 | |
215 TypeNode visit(PointerType n) | |
216 { | |
217 put("Zeiger auf "), visitT(n.next); | |
218 return n; | |
219 } | |
220 | |
221 TypeNode visit(IntegralType n) | |
222 { | |
223 put(n.begin.srcText); | |
224 return n; | |
225 } | |
226 } |