view dang/compiler.d @ 1:2168f4cb73f1

First push
author johnsen@johnsen-desktop
date Fri, 18 Apr 2008 02:01:38 +0200
parents
children e5caf9971207
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;

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;

    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");

    auto options = argParse.parse(args);

    filesToHandle ~= options.args;
    
    try
    {
        preStart(&filesToHandle);
    }
    catch(Exception e)
    {
        return;
    }

    foreach(file ; filesToHandle)
    {
        preLex(file);

        auto src = DataSource(file);
        auto lexer = new Lexer(src);

        postLex(lexer);

        preParse(lexer);

        auto parser = new Parser;
        auto decls = parser.parse(lexer);

        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);
        }
    }*/
}