view ast/Decl.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 9f8131676242
line wrap: on
line source

module ast.Decl;

import ast.Exp,
       ast.Stmt;

import lexer.Token;

import tango.io.Stdout;

import sema.SymbolTable;

enum DeclType
{
    VarDecl,
    FuncDecl,
    StructDecl,
}

class Decl
{
    this(DeclType declType)
    {
        this.declType = declType;
    }

    void simplify()
    {
    }

    DeclType declType;
    Scope env;
}

class VarDecl : Decl
{
    this(Identifier type, Identifier identifier,
            Exp e = null)
    {
        super(DeclType.VarDecl);
        this.type = type;
        this.identifier = identifier;
        this.init = e;
    }

    void simplify()
    {
    }

    Identifier type, identifier;
    Exp init;
}

class FuncDecl : Decl
{
    this(Identifier type, Identifier identifier)
    {
        super(DeclType.FuncDecl);
        this.type = type;
        this.identifier = identifier;
    }

    void addParam(Identifier type, Identifier name)
    {
        funcArgs ~= new VarDecl(type, name, null);
    }

    void setBody(CompoundStatement stmts)
    {
        statements = stmts.statements;
    }

    void simplify()
    {
        if(auto t = cast(DFunction)env.find(identifier).type)
        {
            if(auto s = cast(DStruct)t.return_type)
            {
                VarDecl[] funcArgs;
                auto i = new Identifier("ret.val");
                i.env = env;
                i.env.add(i);
                i.env.find(i).type = s;
                auto var = new VarDecl(type, i);
                var.env = env;
                funcArgs ~= var;
                funcArgs ~= this.funcArgs;
                this.funcArgs = funcArgs;
                t.return_type = DType.Void;
                this.type = new Identifier("void");
                env.find(identifier).type = t;
                sret = true;
            }
        }

        foreach ( funcArg ; funcArgs )
            funcArg.simplify();
        foreach ( stmt ; statements )
            stmt.simplify();
    }

    Identifier type, identifier;
    VarDecl[] funcArgs;
    Stmt[] statements;
    bool sret = false;
}

class StructDecl : Decl
{
    this(Identifier identifier)
    {
        super(DeclType.StructDecl);
        this.identifier = identifier;
        this.vars = vars;
    }

    void addMember(Identifier type, Identifier name, Exp exp)
    {
        vars ~= new VarDecl(type, name, exp);
    }

    void simplify()
    {
    }

    Identifier identifier;
    VarDecl[] vars;
}