view ast/Exp.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 2f493057cf17
children 495188f9078e
line wrap: on
line source

module ast.Exp;

import tango.text.Util : jhash;

import lexer.Token;

import sema.SymbolTable;

enum ExpType
{
    Binary,
    Negate,
    IntegerLit,
    MemberLookup,
    ArrayLookup,
    Identifier,
    AssignExp,
    CallExp,
}

class Exp
{
    this(ExpType expType) 
    {
        this.expType = expType;
    }

    ExpType expType;
    Scope env;
}

class CallExp : Exp
{
    this(Exp exp, Exp[] args)
    {
        super(ExpType.CallExp);
        this.exp = exp;
        this.args = args;
    }

    Exp exp;
    Exp[] args;
}

class AssignExp : Exp
{
    this(Exp identifier, Exp exp)
    {
        super(ExpType.AssignExp);
        this.identifier = identifier;
        this.exp = exp;
    }

    Exp identifier;
    Exp exp;
}

class BinaryExp : Exp
{
    public enum Operator
    {
        Eq, Ne,
        Lt, Le,
        Gt, Ge,
        Mul, Div,
        Add, Sub,
    }

    this(Operator op, Exp left, Exp right)
    {
        super(ExpType.Binary);
        this.op = op;
        this.left = left;
        this.right = right;
    }

    char[] resultType()
    {
        if (op >= Operator.Eq && op <= Operator.Ge)
            return "bool";
        return null;
    }

    Operator op;
    Exp left, right;
}

class NegateExp : Exp
{
    this(Exp exp)
    {
        super(ExpType.Negate);
        this.exp = exp;
    }

    public Exp exp;
}

class IntegerLit : Exp
{
    this(Token t)
    {
        super(ExpType.IntegerLit);
        this.token = t;
    }

    Token token;
}

class MemberLookup : Exp
{
    this(Exp target, Identifier child)
    {
        super(ExpType.MemberLookup);
        this.target = target;
        this.child = child;
    }

    Identifier child;
    Exp target;
}

class ArrayLookup : Exp
{
    this(Exp target, IntegerLit pos)
    {
        super(ExpType.ArrayLookup);
        this.target = target;
        this.pos = pos;
    }

    Exp target;
    IntegerLit pos;
}

class Identifier : Exp
{
    this(Token t)
    {
        super(ExpType.Identifier);
        this.token = t;
        name = t.get;
    }

    char[] get()
    {
        return name;
    }

    hash_t toHash()
    {
        return jhash(name);
    }

    int opCmp(Object o)
    {
        if (auto id = cast(Identifier)o)
            return typeid(char[]).compare(&name, &id.name);
        return 0;
    }

    int opEquals(Object o)
    {
        if (auto id = cast(Identifier)o)
            return typeid(char[]).equals(&name, &id.name);
        return 0;
    }

    Token token;
    char[] name;
}