/**
 * Represent an SFAL expression.
 * 
 * @author Antonio Cruz adc2104@columbia.edu
 * @version $Id: Expr.java,v 1.11 2007/12/16 07:30:45 admin Exp $
 */

import java.util.LinkedList;
import java.util.Iterator;


public class Expr {
	/** 
	 * String representation of expression node,
	 * e.g. "+", "FUNC_CALL", "ID".
	 */
	String type;
	
	/**
	 * Value of expression node for "ID" and "FUNC_CALL",
	 * e.g. "x", "makecircle".
	 */
	String value;
	
	/**
	 * List of expression node's children, e.g.
	 * for "+": Expr operand1, Expr operand2.
	 */
	LinkedList values = new LinkedList();

	
	public Expr(String t) {
		type = t;
	}
	
	public String toString() {
		StringBuffer sb = new StringBuffer();
		
		if (value != null) {
			sb.append(value).append(',');
		}
		
		/* For operations w/ 2 operands */
		if (child1 != null) {
			if (type.equals("U-")) sb.append('-');
			sb.append(child1.value);
		}
		if (child1 != null && child2 != null) sb.append(type);
		if (child2 != null) {
			sb.append(child2.value);
		}


		/* For expression lists */
		for (Iterator itr = values.iterator(); itr.hasNext(); ) {
			Expr e = (Expr) itr.next();
			sb.append(e.toString());
		}
		
		return sb.toString();
	}
	
	/* For operations w/ 2 operands */
	Expr child1;
	Expr child2;
	String error;
}