package nl.bluering; import java.util.Vector; /** * Expressions are simple trees. It can be used for anything. * An expression consists of an object o and zero or more * subexpressions (children). It is common that the object is a string, but depending * on your needs it can be something different. (it better be printable if * you intend to print it. I can also disrecommend null pointers, * but it is not forbidden:-) the problems are yours though). */ public class Expression { Expression[] e; Object function = ""; int childs = 0; /** * create an Expression with the object s and * room for the given number of children. * @param s the object in this expression. Often a string * @param ch the estimated number of children. You can give any value, * regardless what number of children you will really add, but memory use * is more efficient if it is correct. Note that -1 is allowed: it indicates * that it came in in a notation that does not allow arguments: for instance * "123" and "a" do not allow children, but "f()" does. */ public Expression(Object s,int ch) { if(ch==-1) childs = -1; else { ch=0; e = new Expression [ch]; } function=s; } Expression(String fname,Vector v) {this(fname,v.size()); for(int i=0;i=0)) { return e[child]; } else { return null; } } /** * Converts the complete expression to a one-line string, including it's childs. */ public String toString() { String r=function.toString(); if (childs==0) { return r+"()"; } if(childs>0){ r+="("+e[0]; for(int i=1;i