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
|
|
20 import sema.Visitor;
|
|
21
|
|
22 import dang.OptParse;
|
|
23
|
|
24 void checkFiles(char[][] *files)
|
|
25 {
|
|
26 bool error = false;
|
|
27
|
|
28 char[][] validFiles;
|
|
29
|
|
30 foreach(file ; *files)
|
|
31 {
|
|
32 auto path = new FilePath(file);
|
|
33
|
|
34 if(!path.exists)
|
|
35 {
|
|
36 Stdout("File '"~file~"' does not exists").newline;
|
|
37 error = true;
|
|
38
|
|
39 continue;
|
|
40 }
|
|
41
|
|
42 bool fileInStack = false;
|
|
43 foreach(vFile ; validFiles)
|
|
44 if(vFile == file)
|
|
45 fileInStack = true;
|
|
46
|
|
47 if(fileInStack)
|
|
48 continue;
|
|
49
|
|
50 validFiles ~= file;
|
|
51 }
|
|
52
|
|
53 *files = validFiles;
|
|
54
|
|
55 if(error)
|
|
56 throw new Exception("Some file(s) did not exist");
|
|
57 }
|
|
58 void main(char[][] args)
|
|
59 {
|
|
60 char[][] filesToHandle;
|
|
61
|
|
62 Signal!(char[][]*) preStart;
|
|
63
|
|
64 Signal!(char[]) preLex;
|
|
65 Signal!(Lexer) postLex;
|
|
66
|
|
67 Signal!(Lexer) preParse;
|
|
68 Signal!(Decl[], DataSource) postParse;
|
|
69
|
|
70 preStart.attach(&checkFiles);
|
|
71
|
|
72 auto argParse = new OptionParser;
|
|
73
|
|
74 argParse.addOption(
|
|
75 ["-h", "--help"],{
|
|
76 argParse.helpText();
|
|
77 return;
|
|
78 }
|
|
79 ).help("Show this help message");
|
|
80
|
|
81 argParse.addOption(
|
|
82 ["--ast-dump-dot"], {
|
|
83 postParse.attach(
|
|
84 (Decl[] decls, DataSource src) {
|
|
85 auto print = new DotPrinter();
|
|
86 print.print(decls);
|
|
87 });
|
|
88 }
|
|
89 ).help("Output the AST as dot-graphicz");
|
|
90
|
|
91 argParse.addOption(
|
|
92 ["--ast-dump-code"], {
|
|
93 postParse.attach(
|
|
94 (Decl[] decls, DataSource src) {
|
|
95 auto print = new AstPrinter(src);
|
|
96 print.print(decls);
|
|
97 });
|
|
98 }
|
|
99 ).help("Output the AST as dot-graphicz");
|
|
100
|
|
101 argParse.addOption(
|
|
102 ["--gen-lua"], {
|
|
103 postParse.attach(
|
|
104 (Decl[] decls, DataSource src) {
|
|
105 auto luaGen = new LuaGen();
|
|
106 luaGen.gen(decls);
|
|
107 });
|
|
108 }
|
|
109 ).help("Compile to Lua code");
|
|
110
|
|
111 argParse.addOption(
|
|
112 ["--gen-llvm"], {
|
|
113 postParse.attach(
|
|
114 (Decl[] decls, DataSource src) {
|
|
115 auto llvmGen = new LLVMGen();
|
|
116 llvmGen.gen(decls);
|
|
117 });
|
|
118 }
|
|
119 ).help("Compile to LLVM code");
|
|
120
|
|
121 auto options = argParse.parse(args);
|
|
122
|
|
123 filesToHandle ~= options.args;
|
|
124
|
|
125 try
|
|
126 {
|
|
127 preStart(&filesToHandle);
|
|
128 }
|
|
129 catch(Exception e)
|
|
130 {
|
|
131 return;
|
|
132 }
|
|
133
|
|
134 foreach(file ; filesToHandle)
|
|
135 {
|
|
136 preLex(file);
|
|
137
|
|
138 auto src = DataSource(file);
|
|
139 auto lexer = new Lexer(src);
|
|
140
|
|
141 postLex(lexer);
|
|
142
|
|
143 preParse(lexer);
|
|
144
|
|
145 auto parser = new Parser;
|
|
146 auto decls = parser.parse(lexer);
|
|
147
|
|
148 postParse(decls, src);
|
|
149 }
|
|
150
|
|
151 /* if (args.length > 1 && args[1] == "lex")
|
|
152 {
|
|
153 Token t;
|
|
154
|
|
155 t = lexer.next();
|
|
156 while(t.type != Tok.EOF)
|
|
157 {
|
|
158 Stdout(src.get(t.position, t.length)).newline;
|
|
159 t = lexer.next();
|
|
160 }
|
|
161 }
|
|
162 else
|
|
163 {
|
|
164 auto decl = parser.parse(lexer);
|
|
165 if(args.length > 1 && args[1] == "dump-ast")
|
|
166 {
|
|
167 auto buffer = new AstBuffer(src.data);
|
|
168 decl.print(buffer);
|
|
169 }
|
|
170 }*/
|
|
171 }
|