view ast/Decl.d @ 53:da551f90e03f new_gen

Added struct decl and forward ref. A note on structs: they need to make a new scope when declared. Otherwise you could access struct members as globals
author Anders Johnsen <skabet@gmail.com>
date Sat, 26 Apr 2008 18:52:27 +0200
parents 495188f9078e
children 4ae365eff712
line wrap: on
line source

module ast.Decl;

import ast.Exp,
       ast.Stmt;

import lexer.Token;

import sema.SymbolTable;

enum DeclType
{
    VarDecl,
    FuncDecl,
    StructDecl,
}

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

    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;
    }

    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;
    }

    Identifier type, identifier;
    VarDecl[] funcArgs;
    Stmt[] statements;
}

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

    Identifier identifier;
    VarDecl[] vars;
}