package gve.calc.formula;
import java.awt.*; 
import java.util.Vector;

public class OperatorComma extends InfixBinaryOp {
	public String getName() { return ","; }
	public int getPri() { return 500; }
	public int getLeftPri() { return 510; }
	public int getRightPri() { return 490; }

	public OperatorComma(Part l,Part r) {
		super(l,r);
	}

	public Part evaluate(Evaluator ev) {
		return new OperatorComma(left.evaluate(ev),right.evaluate(ev));
	}

	
Converts a tree containing "a,b,c,..." to an array with elements a, b, c, ... Warning: the subtrees are not copied. You just get a bunch of pointers into the existing formula back.
public static Part[] chopCommas(Part p) { if (Part.isEmpty(p)) return new Part[0]; // special case (added 29-apr-2001) Vector v = new Vector(); while (p instanceof OperatorComma) { OperatorComma comma = (OperatorComma)p; v.addElement(comma.left); p = comma.right; } Part [] result = new Part[v.size()+1]; v.copyInto(result); // last piece is in p result[result.length-1] = p; return result; }
If this is the leftmost comma of a list, create a copy of it, except one part. If you also want to handle lists with 0 or 1 elements (which have no commas), use the static copyListExcept(Part,Part) variant.
public final Part copyListExcept(Part toRemove) { Part result; if (left == toRemove) return (Part)right.clone(); // found it; copy rest of list literally else if (right == toRemove) return (Part)left.clone(); // voorlaatste elt van de lijst clonen else { if (right instanceof OperatorComma) return new OperatorComma((Part)left.clone(),((OperatorComma)right).copyListExcept(toRemove)); else return (Part)right.clone(); } }
Copy a list, excluding one element. The list may be a single identifier or a chain of Comma operators. Empty lists are represented by an empty identifier.
public static Part copyListExcept(Part list,Part toRemove) { if (Part.isEmpty(list)) { // List has 0 elements return new Identifier(""); } else if (list instanceof OperatorComma) { return ((OperatorComma)list).copyListExcept(toRemove); } else { // List has 1 element if (list == toRemove) return new Identifier(""); return (Part)list.clone(); } }
Add 'toAdd' in front of the list. The list should not be inside brackets. The list may also be a single identifier.
public final static void addToFrontOfList(Formula formula,Part list,Part toAdd) { if (Part.isEmpty(list)) { formula.replace(list,toAdd); } else { Identifier dummy = new Identifier(""); formula.replace(list,new OperatorComma(toAdd,dummy)); formula.replace(dummy,list); } } public final static boolean listContains(Part list,Part contains) { while (list instanceof OperatorComma) { OperatorComma comma = (OperatorComma)list; if (comma.left.same(contains)) return true; list = comma.right; } if (list.same(contains)) return true; return false; } public static Part read(java.io.BufferedReader r) throws java.io.IOException, ClassNotFoundException,NoSuchMethodException, java.lang.reflect.InvocationTargetException,IllegalAccessException{ Part left = Part.read(r); Part right = Part.read(r); return new OperatorComma(left,right); } }