package org.nfunk.jep.function;

import java.util.Stack;
import org.nfunk.jep.ASTVarNode;
import org.nfunk.jep.Node;
import org.nfunk.jep.ParseException;
import org.nfunk.jep.ParserVisitor;
import org.nfunk.jep.Variable;

public class Assign extends PostfixMathCommand implements SpecialEvaluationI {
  public Object evaluate(Node paramNode, Object paramObject, ParserVisitor paramParserVisitor, Stack paramStack) throws ParseException {
    if (paramNode.jjtGetNumChildren() != 2)
      throw new ParseException("Assignment opperator must have 2 operators."); 
    paramNode.jjtGetChild(1).jjtAccept(paramParserVisitor, paramObject);
    checkStack(paramStack);
    Object object = paramStack.peek();
    Node node = paramNode.jjtGetChild(0);
    if (node instanceof ASTVarNode) {
      ASTVarNode aSTVarNode = (ASTVarNode)node;
      Variable variable = aSTVarNode.getVar();
      variable.setValue(object);
      return object;
    } 
    throw new ParseException("Assignment should have a variable for the lhs.");
  }
}
