Mercurial > projects > dang
view dang/compiler.d @ 40:9fb190ad81a4 new_gen
Added -O and --inline args to Dang.
author | Anders Johnsen <skabet@gmail.com> |
---|---|
date | Tue, 22 Apr 2008 00:31:57 +0200 |
parents | e331e4e816e4 |
children | 495188f9078e |
line wrap: on
line source
module dang.compiler; import tango.io.Stdout, tango.core.Signal, tango.io.FilePath; import lexer.Lexer, parser.Parser; import misc.DataSource; import ast.Decl; import tools.AstPrinter, tools.DotPrinter; import gen.LuaGen, gen.LLVMGen; import sema.Visitor, sema.SymbolTableBuilder, sema.Declarations; import dang.OptParse; void checkFiles(char[][] *files) { bool error = false; char[][] validFiles; foreach(file ; *files) { auto path = new FilePath(file); if(!path.exists) { Stdout("File '"~file~"' does not exists").newline; error = true; continue; } bool fileInStack = false; foreach(vFile ; validFiles) if(vFile == file) fileInStack = true; if(fileInStack) continue; validFiles ~= file; } *files = validFiles; if(error) throw new Exception("Some file(s) did not exist"); } void main(char[][] args) { char[][] filesToHandle; Signal!(char[][]*) preStart; Signal!(char[]) preLex; Signal!(Lexer) postLex; Signal!(Lexer) preParse; Signal!(Decl[], DataSource) postParse; preStart.attach(&checkFiles); auto argParse = new OptionParser; bool optimize = false; bool inline = false; argParse.addOption( ["-h", "--help"],{ argParse.helpText(); return; } ).help("Show this help message"); argParse.addOption( ["--ast-dump-dot"], { postParse.attach( (Decl[] decls, DataSource src) { auto print = new DotPrinter(); print.print(decls); }); } ).help("Output the AST as dot-graphicz"); argParse.addOption( ["--ast-dump-code"], { postParse.attach( (Decl[] decls, DataSource src) { auto print = new AstPrinter(src); print.print(decls); }); } ).help("Output the AST as dot-graphicz"); argParse.addOption( ["--gen-lua"], { postParse.attach( (Decl[] decls, DataSource src) { auto luaGen = new LuaGen(); luaGen.gen(decls); }); } ).help("Compile to Lua code"); argParse.addOption( ["--gen-llvm"], { /* postParse.attach( (Decl[] decls, DataSource src) { auto llvmGen = new LLVMGen(); llvmGen.gen(decls); }); */ } ).help("Compile to LLVM code (default)"); argParse.addOption( ["-O","--optimize"], { optimize = true; } ).help("Optimize code when compiling to LLVM"); argParse.addOption( ["--inline"], { inline = true; } ).help("Inline functions when compiling to LLVM"); auto options = argParse.parse(args); filesToHandle ~= options.args; try { preStart(&filesToHandle); } catch(Exception e) { return; } postParse.attach( (Decl[] decls, DataSource src) { auto llvmGen = new LLVMGen(); llvmGen.gen(decls, optimize, inline); }); foreach(file ; filesToHandle) { preLex(file); auto src = DataSource(file); auto lexer = new Lexer(src); /* auto t = lexer.next; while(t.getType != "EOF") { Stdout(t.getType)(" : ")(t.get).newline; t = lexer.next; } lexer = new Lexer(src); */ postLex(lexer); preParse(lexer); auto parser = new Parser; auto decls = parser.parse(lexer); (new SymbolTableBuilder).visit(decls); (new Declarations).visit(decls); postParse(decls, src); } /* if (args.length > 1 && args[1] == "lex") { Token t; t = lexer.next(); while(t.type != Tok.EOF) { Stdout(src.get(t.position, t.length)).newline; t = lexer.next(); } } else { auto decl = parser.parse(lexer); if(args.length > 1 && args[1] == "dump-ast") { auto buffer = new AstBuffer(src.data); decl.print(buffer); } }*/ }