/* Represents an expression made up of two expressions (terminal or nonterminal) separated by a single operator: +, -, *, / */
public class CompositeExpression extends Expression {

	private Expression exp1;
	private Expression exp2;
	private char operator;
	private SymbolTable table;
	
	public CompositeExpression(Expression exp1, Expression exp2, char operator, SymbolTable table) { 
		this.exp1 = exp1;
		this.exp2 = exp2;
		this.operator = operator;
		this.table = table;
	}
	
	public Expression getExp1() {
		return exp1;
	}
	
	public Expression getExp2() {
		return exp2;
	}
	
	public char getOperator() {
		return operator;
	}
	
	@Override
	int interpret() {
		/*Case 1: Assignment*/
		if (operator == '=') { 
			//verify that exp1 is a variable, and not a constant.
			table.storeVal(exp1.toString(), exp2.toString());
			return exp2.interpret();
		/*Case 2: Arithmetic*/
		} else { 
			int exp1Val = exp1.interpret();
			int exp2Val = exp2.interpret();
	        switch (operator) {
	        	case '+': return exp1Val + exp2Val;
	        	case '-': return exp1Val - exp2Val;
	        	case '*': return exp1Val * exp2Val;
	        	case '/': return exp1Val / exp2Val;
	        }
		}
		throw new java.lang.Error("Uh oh. Invalid operator or something bad...");
	}
	
	public String toString() {
		return exp1.toString() + " " + operator + " " + exp2.toString();
	}

}
