//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\
// PLEASE DO NOT EDIT WITHOUT THE EXPLICIT CONSENT OF THE AUTHOR! \\
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\

package hlt.language.design.instructions;

/**
 * @version     Last modified on Wed Jun 20 14:29:51 2012 by hak
 * @author      <a href="mailto:hak@acm.org">Hassan A&iuml;t-Kaci</a>
 * @copyright   &copy; <a href="http://www.hassan-ait-kaci.net/">by the author</a>
 */

import hlt.language.design.types.Type;
import hlt.language.design.types.ClassType;
import hlt.language.design.types. ObjectInitializationException;
import hlt.language.design.backend.Runtime;
import hlt.language.design.backend.ObjectInstance;

public class PushNewObject extends Instruction implements PushValue, PushObject
{
  private ClassType _type;

  public PushNewObject (ClassType type)
    {
      setName("PUSH_NEW_OBJECT");
      _type = type;
    }

  final ClassType type ()
    {
      return _type;
    }

  public final void execute (Runtime r) throws ObjectInitializationException
    {
      ObjectInstance object = new ObjectInstance(_type);
      _type.initialize(object,r);
    }

  public final boolean equals (Object object)
    {
      if (this == object)
        return true;

      if (!(object instanceof PushNewObject))
        return false;

      return _type.name() == ((PushNewObject)object).type().name();
    }

  public final int hashCode ()
    {
      return name().hashCode() + _type.name().hashCode();
    }

  public final String toString ()
    {
      Type.resetNames();
      return name()+"("+_type+")";
    }
}

