// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.expression;

import bdc.scheme.Environment;
import bdc.scheme.GlobalEnvironment;
import bdc.scheme.Scheme;
import bdc.scheme.SchemeException;
import bdc.scheme.Stack;
import bdc.scheme.Symbol;
import bdc.scheme.compiler.CompileTimeEnvironment;
import bdc.scheme.exception.UndefinedVarException;

public class GlobalVariable extends Expression
{
    public Symbol name;
    public Object object;
    public int    type;

    public GlobalVariable (Symbol name, Object object, int type)
    {
        this.name   = name;
        this.object = object;
        this.type   = type;
    }

    public Object eval (Environment environment, Stack stack)
      throws SchemeException
    {
        try {
            if (object == Scheme.Undefined) {
                throw new UndefinedVarException(name);
            }
            if (type != GlobalEnvironment.Location) {
                throw new UndefinedVarException(name);
            }
            return object;
        }
        catch (SchemeException se) {
            backTrace(se);
            return Scheme.NotReached;
        }
    }

    /**
        GlobalVariables don't need to fixup anything
    */
    public Expression fixupVariables (CompileTimeEnvironment environment)
    {
        return this;
    }

    public String toString ()
    {
        return name.toString();
    }
}
