view ast/Expr.d @ 204:227d6a8fb574

Added some random stuff...
author Anders Johnsen <skabet@gmail.com>
date Mon, 11 Aug 2008 21:27:44 +0200
parents 4f94b5adbc8a
children 8387cbaa85ab
line wrap: on
line source

module ast.Expr;

/**
  The base class for all Expressions.
  */
class Expr
{
    /// Get the type of the Expr
    Object getType() 
    {
        assert(0, "Unimplemented");
        return null;
    }

    /// Set the type of the Expr
    void setType(Object type) 
    {
        assert(0, "Unimplemented");
    }

    /// Returns true if the Expr can be compile-time evaluated.
    bool isConstantExpr() { return false; }

    NumberLiteral asNumberLiteral() { return null; }
    bool isNumberLiteral() { return false; }

    StringLiteral asStringLiteral() { return null; }
    bool isStringLiteral() { return false; }

    ArrayLiteral asArrayLiteral() { return null; }
    bool isArrayLiteral() { return false; }

    Assign asAssign() { return null; }
    bool isAssign() { return false; }

    Binary asBinary() { return null; }
    bool isBinary() { return false; }

    Negate asNegate() { return null; }
    bool isNegate() { return false; }

    Deref asDeref() { return null; }
    bool isDeref() { return false; }

    AddressOf asAddressOf() { return null; }
    bool isAddressOf() { return false; }

    Index asIndex() { return null; }
    bool isIndex() { return false; }

    Identifier asIdentifier() { return null; }
    bool isIdentifier() { return false; }

    Member asMember() { return null; }
    bool isMember() { return false; }

private:
/// FIXME: Add DType? here
}

/**
  NumberLiteral
  */
/// FIXME: Should there be an IntegerLiteral and FloatLiteral instead?
class NumberLiteral : Expr
{
    override bool isConstantExpr() { return true; }

    override NumberLiteral asNumberLiteral() { return this; }
    override bool isNumberLiteral() { return true; }
}

/**
  StringLiteral
  */
class StringLiteral : Expr
{
    override bool isConstantExpr() { return true; }

    override StringLiteral asStringLiteral() { return this; }
    override bool isStringLiteral() { return true; }
}

/**
  ArrayLiteral
  */
class ArrayLiteral : Expr
{
    /// Return the arguments for the ArrayLiteral
    Expr[] getArguments() { return arguments; }

    /// Return a given argument for the ArrayLiteral
    Expr getArgument(int index) { return arguments[index]; }

    /// Get the count of arguments for the ArrayLiteral
    int getArgumentCount() { return arguments.length; }

    override bool isConstantExpr() 
    {
        /**
          If all the arguments to the ArrayLiteral is an constant Expr, then
          this ArrayLiteral can be considered an constant Expr aswell.
          */
        // FIXME: consider if it should save the result
        foreach (arg; arguments)
            if (!arg.isConstantExpr())
                return false;
        return true; 
    }

    override ArrayLiteral asArrayLiteral() { return this; }
    override bool isArrayLiteral() { return true; }

private:
    Expr[] arguments;
}

/**
  Assign
  */
class Assign : Expr
{
    override Assign asAssign() { return this; }
    override bool isAssign() { return true; }

private:
    Expr left, right;
}

/**
  Binary
  */
class Binary : Expr
{
    override Binary asBinary() { return this; }
    override bool isBinary() { return true; }

private:
    Expr left, right;
}

/**
  Negate
  */
class Negate : Expr
{
    override Negate asNegate() { return this; }
    override bool isNegate() { return true; }

private:
    Expr expr;
}

/**
  Deref
  */
class Deref : Expr
{
    override Deref asDeref() { return this; }
    override bool isDeref() { return true; }
}

/**
  AddressOf
  */
class AddressOf : Expr
{
    override AddressOf asAddressOf() { return this; }
    override bool isAddressOf() { return true; }
}

/**
  Index
  */
class Index : Expr
{
    override Index asIndex() { return this; }
    override bool isIndex() { return true; }
}

/**
  Identifier
  */
class Identifier : Expr
{
    override Identifier asIdentifier() { return this; }
    override bool isIdentifier() { return true; }
}

/**
  Member
  */
class Member : Expr
{
    override Member asMember() { return this; }
    override bool isMember() { return true; }
}