rendered paste bodyimport a4.Parser;
import a4.SymbolTable;
import a4.MalformedExpressionException;
import a4.NoSuchVariableException;
public abstract class ExpressionTree {
ExpressionTree parent;
ExpressionTree leftChild;
ExpressionTree rightChild;
String data;
// Static CreateTree Method
public static ExpressionTree createTree(Parser tokens) {
return(createNode(tokens,null));
}
// ********************************************************************************************
// ********************************************************************************************
// ********************************************************************************************
// ********************************************************************************************
public static ExpressionTree createNode(Parser tokens, ExpressionTree parent) {
// Check if we have a next token or else return null
String curItem;
if (tokens.hasNext()) {
curItem = tokens.next();
if (curItem.equals("(")) {
createNode(tokens,null);
System.out.println("Returning: "+parent);
return(parent);
}
System.out.println("New curItem: "+curItem);
} else {
curItem = "";
}
// Creating a root node, should only happen once
if (parent == null && !curItem.equals("")) {
System.out.println("*** case zero ***");
ExpressionTree firstNode = new IntTerm(null,tokens,curItem);
createNode(tokens,firstNode);
return(firstNode); // return the root node to createTree after everything is done
} else {
// Check for types
if (curItem.equals("@b+")) {
parent = new AddExp(parent.parent,tokens,curItem,parent);
// Swap parent
createNode(tokens,parent);
createNode(tokens,parent.rightChild);
return(parent);
} else if (curItem.equals("(")) {
System.out.println("( reached, P:"+parent);
createNode(tokens,parent); // keep going to next, we don't care about (
} else if (curItem.equals(")")) {
if (parent.parent != null) {
System.out.println("*** MIDDLEGROUND");
ExpressionTree middleGround = null;
System.out.println(parent.parent.leftChild+" (parent.parent.leftChild) ---> MiddleGround");
parent.parent.leftChild = middleGround;
System.out.println(parent.parent+" (parent.parent) ---> MiddleGround");
parent.parent = middleGround;
middleGround = createNode(tokens,parent.parent);
System.out.println("MiddleGround = "+middleGround);
createNode(tokens,parent);
} else {
parent.parent = new AddExp(null,tokens,curItem,parent.parent);
}
} else {
if (!curItem.equals("")) {
if (parent instanceof BinaryExp) {
parent.rightChild = new IntTerm(parent,tokens,curItem);
return(parent);
} else {
parent.leftChild = new IntTerm(parent,tokens,curItem);
return(parent);
}
} else {
System.out.println("Returning itself to P:"+parent);
return(parent);
}
}
System.out.println("Returning parent to P:"+parent);
return(parent);
}
}
// ********************************************************************************************
// ********************************************************************************************
// ********************************************************************************************
// ********************************************************************************************
// PRINTOUT METHOD FOR TESTING ONLY
public void printOut() {
if (leftChild != null && rightChild != null) {
System.out.println("["+data+"] ("+this+") L:"+leftChild.data+" R:"+rightChild.data + " P:"+parent);
leftChild.printOut();
rightChild.printOut();
} else if (leftChild != null && rightChild == null) {
System.out.println("["+data+"] ("+this+") L:"+leftChild.data+" R:null" + " P:"+parent);
leftChild.printOut();
} else if (leftChild == null && rightChild != null) {
System.out.println("["+data+"] ("+this+") L:null R:"+rightChild.data + " P:"+parent);
rightChild.printOut();
} else {
System.out.println("["+data+"] ("+this+") L:null R:null" + " P:"+parent);
}
}
//public abstract String toString();
//public abstract boolean isWellFormed();
//public abstract int eval(SymbolTable sym);
//public abstract ExpressionTree copy();
//public abstract ExpressionTree simplify();
}