Mercurial > projects > dang
comparison dang/compiler.d @ 211:9e9f3e7e342b default tip
Added dang folder and Module in ast.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Tue, 12 Aug 2008 20:07:35 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
210:f4149d4f6896 | 211:9e9f3e7e342b |
---|---|
1 module dang.compiler; | |
2 | |
3 import tango.io.Stdout, | |
4 tango.core.Signal, | |
5 tango.core.Memory, | |
6 tango.sys.Process, | |
7 tango.time.StopWatch, | |
8 tango.text.Util, | |
9 tango.io.FileConduit, | |
10 tango.io.FilePath; | |
11 | |
12 import lexer.Lexer, | |
13 parser.Action, | |
14 parser.Parser; | |
15 | |
16 import basic.SourceManager; | |
17 | |
18 import basic.Message; | |
19 | |
20 import ast.Module; | |
21 | |
22 import tango.stdc.posix.unistd; | |
23 import tango.stdc.stdlib; | |
24 | |
25 import Opt = dang.OptParse; | |
26 | |
27 class NullAction : Action | |
28 { | |
29 } | |
30 | |
31 void checkFiles(char[][] *files) | |
32 { | |
33 // GC.disable(); | |
34 bool non_existant_files = false; | |
35 bool duplicate_files = false; | |
36 | |
37 char[][] validFiles; | |
38 | |
39 foreach (file; *files) | |
40 { | |
41 scope path = new FilePath(file); | |
42 | |
43 if (!path.exists) | |
44 { | |
45 Stderr.formatln("'{}' does not exist", file).newline; | |
46 non_existant_files = true; | |
47 | |
48 continue; | |
49 } | |
50 | |
51 bool fileInStack = false; | |
52 foreach (vFile; validFiles) | |
53 if (vFile == file) | |
54 { | |
55 fileInStack = true; | |
56 duplicate_files = true; | |
57 } | |
58 | |
59 if (fileInStack) | |
60 continue; | |
61 | |
62 validFiles ~= path.toString(); | |
63 } | |
64 | |
65 *files = validFiles; | |
66 | |
67 if (non_existant_files) | |
68 throw new Exception("All files given must exist"); | |
69 if (duplicate_files) | |
70 Stderr("warning: duplicate files ignored").newline; | |
71 } | |
72 | |
73 void main(char[][] args) | |
74 { | |
75 char[][] filesToHandle; | |
76 | |
77 Signal!(char[][]*) preStart; | |
78 | |
79 Signal!(char[]) preLex; | |
80 Signal!(Lexer) postLex; | |
81 | |
82 Signal!(Lexer) preParse; | |
83 Signal!(Module[], SourceManager) postParse; | |
84 Signal!(Module[], SourceManager) postSema; | |
85 | |
86 preStart.attach(&checkFiles); | |
87 | |
88 auto argParse = new Opt.OptionParser(`Dang "D" compiler v0.1`); | |
89 | |
90 bool optimize = false; | |
91 bool inline = false; | |
92 | |
93 | |
94 SourceManager src_mgr = new SourceManager; | |
95 MessageHandler messages = new MessageHandler(src_mgr); | |
96 | |
97 argParse.addOption(["-h", "--help"], Opt.Action.Help) | |
98 .help("Show this help message"); | |
99 | |
100 auto options = argParse.parse(args); | |
101 | |
102 filesToHandle ~= options.args; | |
103 | |
104 // Will throw exception if some files don't exist | |
105 preStart(&filesToHandle); | |
106 | |
107 struct Measurement { char[] label; double time; } | |
108 Measurement[] timings; | |
109 StopWatch total; | |
110 total.start; | |
111 | |
112 Module[] modules; | |
113 | |
114 StopWatch watch; | |
115 watch.start; | |
116 foreach (file; filesToHandle) | |
117 { | |
118 preLex(file); | |
119 | |
120 auto start = src_mgr.addFile(file); | |
121 auto lexer = new Lexer(start, src_mgr, messages); | |
122 postLex(lexer); | |
123 | |
124 preParse(lexer); | |
125 | |
126 auto parser = new Parser(messages); | |
127 auto action = new NullAction; | |
128 modules ~= cast(Module)parser.parse(src_mgr, lexer, action); | |
129 timings ~= Measurement("Lex + Parse of '"~file~"'", watch.stop); | |
130 messages.checkErrors(ExitLevel.Parser); | |
131 /* | |
132 StopWatch watch2; | |
133 watch.start; | |
134 watch2.start; | |
135 Module[] mods = (new LoadModule).visit(m, src_mgr, messages); | |
136 (new ScopeBuilder).visit(m); | |
137 auto scope_builder = watch2.stop; | |
138 watch2.start; | |
139 (new ScopeCheck).visit(m); | |
140 auto scope_check = watch2.stop; | |
141 watch2.start; | |
142 (new TypeCheck).visit(m); | |
143 auto type_check = watch2.stop; | |
144 watch2.start; | |
145 | |
146 foreach (decl; m.decls) | |
147 decl.simplify(); | |
148 auto simplify = watch2.stop; | |
149 auto extra_stuff = watch.stop; | |
150 timings ~= Measurement("Extra stuff", watch.stop); | |
151 timings ~= Measurement(" - Building scopes", scope_builder); | |
152 timings ~= Measurement(" - Checking scopes", scope_check); | |
153 timings ~= Measurement(" - Checking types", type_check); | |
154 | |
155 postParse(m, src_mgr);*/ | |
156 } | |
157 /* | |
158 (new LiteralInterpreter(messages)).visit(modules); | |
159 messages.checkErrors; | |
160 postParse(modules, src_mgr); | |
161 | |
162 class ModuleLoader : Visitor!(void) | |
163 { | |
164 Module[] visit(Module[] modules, MessageHandler messages, SourceManager src_mgr) | |
165 { | |
166 this.modules = modules; | |
167 this.messages = messages; | |
168 this.src_mgr = src_mgr; | |
169 super.visit(modules); | |
170 return this.modules; | |
171 } | |
172 | |
173 override void visitImportDecl(ImportDecl decl) | |
174 { | |
175 char[] path = replace!(char)(decl.get,'.','/')~".d"; | |
176 | |
177 auto start = src_mgr.addFile(path); | |
178 auto lexer = new Lexer(start, src_mgr, messages); | |
179 | |
180 auto parser = new Parser(messages); | |
181 auto action = new AstAction(src_mgr); | |
182 | |
183 Module m = cast(Module)parser.parse(src_mgr, lexer, action); | |
184 modules ~= m; | |
185 m.outputModule = false; | |
186 // decl.env.mHandle.add(m); | |
187 messages.checkErrors(ExitLevel.Parser); | |
188 } | |
189 | |
190 Module[] modules; | |
191 SourceManager src_mgr; | |
192 MessageHandler messages; | |
193 } | |
194 | |
195 modules = (new ModuleLoader()).visit(modules, messages, src_mgr); | |
196 messages.checkErrors; | |
197 | |
198 (new BuildScopes).visit(modules); | |
199 (new BuildSymbols).visit(modules); | |
200 StopWatch watch2; | |
201 watch.start; | |
202 watch2.start; | |
203 | |
204 (new BuildTypes(messages)).visit(modules); | |
205 | |
206 (new CheckScopes(messages)).visit(modules); | |
207 messages.checkErrors; | |
208 auto scope_check = watch2.stop; | |
209 | |
210 watch2.start; | |
211 (new CheckTypes(messages)).visit(modules); | |
212 messages.checkErrors; | |
213 auto type_check = watch2.stop; | |
214 | |
215 watch2.start; | |
216 (new ObjectOriented(messages)).visit(modules); | |
217 messages.checkErrors; | |
218 auto object_check = watch2.stop; | |
219 | |
220 watch2.start; | |
221 auto vc = new VC; | |
222 vc.msg = messages; | |
223 foreach (m; modules) | |
224 m.verify(vc); | |
225 messages.checkErrors; | |
226 auto ast_verify = watch2.stop; | |
227 | |
228 foreach (m; modules) | |
229 foreach (decl; m.decls) | |
230 decl.simplify(); | |
231 | |
232 timings ~= Measurement("Total", total.stop); | |
233 postSema(modules, src_mgr); | |
234 | |
235 if (options.flag("time")) | |
236 foreach (m; timings) | |
237 Stderr.formatln("{,-45} {}ms", m.label, m.time*1e3); | |
238 */ | |
239 } | |
240 |