view dang/compiler.d @ 60:2451f0904bf6 new_gen

Dumping Ast with AstPrinter is now possible again! :)
author Anders Johnsen <skabet@gmail.com>
date Tue, 29 Apr 2008 15:00:11 +0200
parents 4ae365eff712
children 381975d76baf
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.CodeGen;

import sema.Visitor,
       sema.AstAction,
       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);
                        exit(0);
                    });
            }
    ).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);
                        exit(0);
                    });
            }
    ).help("Output the AST as dot-graphicz");

    argParse.addOption(
            ["--gen-llvm"], {
/*                postParse.attach(
                    (Decl[] decls, DataSource src) {
                        auto llvmGen = new CodeGen();
                        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 CodeGen();
                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 = cast(Decl[])parser.parse(lexer, new AstAction);

        (new SymbolTableBuilder).visit(decls);
        (new Declarations).visit(decls);

        foreach(decl ; decls)
            decl.simplify();

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