Mercurial > projects > dang
annotate dang/compiler.d @ 13:e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
author | johnsen@johnsen-desktop |
---|---|
date | Fri, 18 Apr 2008 14:47:23 +0200 |
parents | 2168f4cb73f1 |
children | e331e4e816e4 |
rev | line source |
---|---|
1 | 1 module dang.compiler; |
2 | |
3 import tango.io.Stdout, | |
4 tango.core.Signal, | |
5 tango.io.FilePath; | |
6 | |
7 import lexer.Lexer, | |
8 parser.Parser; | |
9 | |
10 import misc.DataSource; | |
11 | |
12 import ast.Decl; | |
13 | |
14 import tools.AstPrinter, | |
15 tools.DotPrinter; | |
16 | |
17 import gen.LuaGen, | |
18 gen.LLVMGen; | |
19 | |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
20 import sema.Visitor, |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
21 sema.SymbolTableBuilder, |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
22 sema.Declarations; |
1 | 23 |
24 import dang.OptParse; | |
25 | |
26 void checkFiles(char[][] *files) | |
27 { | |
28 bool error = false; | |
29 | |
30 char[][] validFiles; | |
31 | |
32 foreach(file ; *files) | |
33 { | |
34 auto path = new FilePath(file); | |
35 | |
36 if(!path.exists) | |
37 { | |
38 Stdout("File '"~file~"' does not exists").newline; | |
39 error = true; | |
40 | |
41 continue; | |
42 } | |
43 | |
44 bool fileInStack = false; | |
45 foreach(vFile ; validFiles) | |
46 if(vFile == file) | |
47 fileInStack = true; | |
48 | |
49 if(fileInStack) | |
50 continue; | |
51 | |
52 validFiles ~= file; | |
53 } | |
54 | |
55 *files = validFiles; | |
56 | |
57 if(error) | |
58 throw new Exception("Some file(s) did not exist"); | |
59 } | |
60 void main(char[][] args) | |
61 { | |
62 char[][] filesToHandle; | |
63 | |
64 Signal!(char[][]*) preStart; | |
65 | |
66 Signal!(char[]) preLex; | |
67 Signal!(Lexer) postLex; | |
68 | |
69 Signal!(Lexer) preParse; | |
70 Signal!(Decl[], DataSource) postParse; | |
71 | |
72 preStart.attach(&checkFiles); | |
73 | |
74 auto argParse = new OptionParser; | |
75 | |
76 argParse.addOption( | |
77 ["-h", "--help"],{ | |
78 argParse.helpText(); | |
79 return; | |
80 } | |
81 ).help("Show this help message"); | |
82 | |
83 argParse.addOption( | |
84 ["--ast-dump-dot"], { | |
85 postParse.attach( | |
86 (Decl[] decls, DataSource src) { | |
87 auto print = new DotPrinter(); | |
88 print.print(decls); | |
89 }); | |
90 } | |
91 ).help("Output the AST as dot-graphicz"); | |
92 | |
93 argParse.addOption( | |
94 ["--ast-dump-code"], { | |
95 postParse.attach( | |
96 (Decl[] decls, DataSource src) { | |
97 auto print = new AstPrinter(src); | |
98 print.print(decls); | |
99 }); | |
100 } | |
101 ).help("Output the AST as dot-graphicz"); | |
102 | |
103 argParse.addOption( | |
104 ["--gen-lua"], { | |
105 postParse.attach( | |
106 (Decl[] decls, DataSource src) { | |
107 auto luaGen = new LuaGen(); | |
108 luaGen.gen(decls); | |
109 }); | |
110 } | |
111 ).help("Compile to Lua code"); | |
112 | |
113 argParse.addOption( | |
114 ["--gen-llvm"], { | |
115 postParse.attach( | |
116 (Decl[] decls, DataSource src) { | |
117 auto llvmGen = new LLVMGen(); | |
118 llvmGen.gen(decls); | |
119 }); | |
120 } | |
121 ).help("Compile to LLVM code"); | |
122 | |
123 auto options = argParse.parse(args); | |
124 | |
125 filesToHandle ~= options.args; | |
126 | |
127 try | |
128 { | |
129 preStart(&filesToHandle); | |
130 } | |
131 catch(Exception e) | |
132 { | |
133 return; | |
134 } | |
135 | |
136 foreach(file ; filesToHandle) | |
137 { | |
138 preLex(file); | |
139 | |
140 auto src = DataSource(file); | |
141 auto lexer = new Lexer(src); | |
142 | |
143 postLex(lexer); | |
144 | |
145 preParse(lexer); | |
146 | |
147 auto parser = new Parser; | |
148 auto decls = parser.parse(lexer); | |
149 | |
13
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
150 (new SymbolTableBuilder).visit(decls); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
151 (new Declarations).visit(decls); |
e5caf9971207
Checking for types and identifiers. TODO: Make each varDecl create a new scope
johnsen@johnsen-desktop
parents:
1
diff
changeset
|
152 |
1 | 153 postParse(decls, src); |
154 } | |
155 | |
156 /* if (args.length > 1 && args[1] == "lex") | |
157 { | |
158 Token t; | |
159 | |
160 t = lexer.next(); | |
161 while(t.type != Tok.EOF) | |
162 { | |
163 Stdout(src.get(t.position, t.length)).newline; | |
164 t = lexer.next(); | |
165 } | |
166 } | |
167 else | |
168 { | |
169 auto decl = parser.parse(lexer); | |
170 if(args.length > 1 && args[1] == "dump-ast") | |
171 { | |
172 auto buffer = new AstBuffer(src.data); | |
173 decl.print(buffer); | |
174 } | |
175 }*/ | |
176 } |