view tools/AstPrinter.d @ 28:69464d465284 new_gen

Now supporting structs - both read and write. Still a few errors though, so watch out.
author Anders Johnsen <skabet@gmail.com>
date Sun, 20 Apr 2008 11:20:28 +0200
parents 2168f4cb73f1
children 2451f0904bf6
line wrap: on
line source

module tools.AstPrinter;

import tango.io.Stdout;

import ast.Decl,
       ast.Stmt,
       ast.Exp;

import misc.DataSource;

class AstPrinter
{
    const char[] tabType = "    "; // 4 spaces

    this(DataSource ds)
    {

        this.ds = ds;
    }

    void print(Decl[] decls)
    {
        foreach(decl ; decls)
        {
            printDecl(decl);
        }
    }

    void printDecl(Decl decl)
    {
        switch(decl.declType)
        {
            case DeclType.FuncDecl:
                auto funcDecl = cast(FuncDecl)decl;
                printBeginLine();
                printIdentifier(funcDecl.type);
                printIdentifier(funcDecl.identifier);
                printFuncArgs(funcDecl.funcArgs);
                printOpenBrace();
                foreach(stmt ; funcDecl.statements)
                    printStatement(stmt);
                printCloseBrace();
                break;

            case DeclType.VarDecl:
                auto varDecl = cast(VarDecl)decl;
                printBeginLine();
                printIdentifier(varDecl.type);
                printIdentifier(varDecl.identifier);
                if(varDecl.init)
                {
                    print("= ");
                    printExp(varDecl.init);
                }
                printEndLine(";");
                break;
        }
    }

    void printStatement(Stmt stmt)
    {
        switch(stmt.stmtType)
        {
            case StmtType.Return:
                auto ret = cast(ReturnStmt)stmt;
                printBeginLine("return ");
                printExp(ret.exp);
                printEndLine(";");
                break;
            case StmtType.Decl:
                auto declStmt = cast(DeclStmt)stmt;
                printDecl(declStmt.decl);
                break;
            case StmtType.Exp:
                auto expStmt = cast(ExpStmt)stmt;
                printBeginLine();
                printExp(expStmt.exp);
                printEndLine(";");
                break;

        }
    }

    void printExp(Exp exp)
    {
        switch(exp.expType)
        {
            case ExpType.Binary:
                auto binaryExp = cast(BinaryExp)exp;
                printExp(binaryExp.left);
                print([binaryExp.op] ~ " ");
                printExp(binaryExp.right);
                break;
            case ExpType.IntegerLit:
                auto integetLit = cast(IntegerLit)exp;
                auto t = integetLit.token;
                print(t.get ~ " ");
                break;
            case ExpType.Negate:
                auto negateExp = cast(NegateExp)exp;
                print("-");
                printExp(negateExp.exp);
                break;
            case ExpType.AssignExp:
                auto assignExp = cast(AssignExp)exp;
                printExp(assignExp.identifier);
                print("= ");
                printExp(assignExp.exp);
                break;
        }
        
    }

    void printFuncArgs(VarDecl[] decls)
    {
        print("(");
     
        foreach(i, decl; decls)
        {
            printIdentifier(decl.type);
            printIdentifier(decl.identifier);
            if(i+1 < decls.length)
                print(",");
        }

        printEndLine(")");
    }

    void printIdentifier(Identifier identifier)
    {
        auto t = identifier.token;
        print(t.get ~ " ");
    }

    void printOpenBrace()
    {
        printEndLine(tabIndex~"{");
        tabIndex ~= tabType;
    }

    void printCloseBrace()
    {
        tabIndex = tabIndex[0 .. $-tabType.length];
        printEndLine(tabIndex~"}");
    }

    void printBeginLine(char[] line = "")
    {
        Stdout(tabIndex~line);
    }

    void printEndLine(char[] line = "")
    {
        Stdout(line).newline;
    }

    void print(char[] line)
    {
        Stdout(line);
    }
private:
    DataSource ds;
    char[] tabIndex;
}