package net.minecraft.src.weasel.lang;

import net.minecraft.src.NBTTagCompound;
import net.minecraft.src.weasel.InstructionList;
import net.minecraft.src.weasel.WeaselEngine;
import net.minecraft.src.weasel.exception.WeaselRuntimeException;
import net.minecraft.src.weasel.obj.WeaselObject;

/**
 * Push instruction
 * 
 * @author MightyPork
 *
 */
public class InstructionPop extends Instruction {
	
	private String poppedVariableName;

	@Override
	public NBTTagCompound writeToNBT(NBTTagCompound tag) {
		tag.setString("Name", poppedVariableName);
		return tag;
	}

	@Override
	public InstructionPop readFromNBT(NBTTagCompound tag) {
		poppedVariableName = tag.getString("Name");
		return this;
	}
	
	/**
	 * @return name of this label
	 */
	public String getLabelName(){
		return poppedVariableName;
	}
	
	/**
	 * Set label name
	 * @param labelName label name to set
	 * @return this
	 */
	public InstructionPop setLabelName(String labelName){
		this.poppedVariableName = labelName;
		return this;
	}

	@Override
	public void execute(WeaselEngine engine, InstructionList instructionList) throws WeaselRuntimeException {
		WeaselObject obj = engine.variables.get(poppedVariableName);
		if(obj == null) throw new WeaselRuntimeException("Variable "+poppedVariableName+" does not exist in this scope.");
		engine.dataStack.push(obj);
	}

}
