2017-07-26 18:44:17 -07:00
|
|
|
package org.nwapw.abacus.tree;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2017-07-27 18:19:12 -07:00
|
|
|
import java.util.List;
|
2017-07-26 18:44:17 -07:00
|
|
|
|
2017-07-26 19:16:10 -07:00
|
|
|
/**
|
|
|
|
* A node that represents a function call.
|
|
|
|
*/
|
2017-07-26 18:44:17 -07:00
|
|
|
public class FunctionNode extends TreeNode {
|
|
|
|
|
2017-07-26 19:16:10 -07:00
|
|
|
/**
|
|
|
|
* The name of the function being called
|
|
|
|
*/
|
2017-07-26 18:44:17 -07:00
|
|
|
private String function;
|
2017-07-26 19:16:10 -07:00
|
|
|
/**
|
|
|
|
* The list of arguments to the function.
|
|
|
|
*/
|
2017-07-27 18:19:12 -07:00
|
|
|
private List<TreeNode> children;
|
2017-07-26 18:44:17 -07:00
|
|
|
|
2017-07-26 19:16:10 -07:00
|
|
|
/**
|
|
|
|
* Creates a function node with no function.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
private FunctionNode() {
|
|
|
|
}
|
2017-07-26 18:44:17 -07:00
|
|
|
|
2017-07-26 19:16:10 -07:00
|
|
|
/**
|
|
|
|
* Creates a new function node with the given function name.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-26 19:16:10 -07:00
|
|
|
* @param function the function name.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public FunctionNode(String function) {
|
2017-07-26 18:44:17 -07:00
|
|
|
this.function = function;
|
|
|
|
children = new ArrayList<>();
|
|
|
|
}
|
|
|
|
|
2017-07-26 19:16:10 -07:00
|
|
|
/**
|
|
|
|
* Gets the function name for this node.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-26 19:16:10 -07:00
|
|
|
* @return the function name.
|
|
|
|
*/
|
2017-07-26 18:44:17 -07:00
|
|
|
public String getFunction() {
|
|
|
|
return function;
|
|
|
|
}
|
|
|
|
|
2017-07-26 19:16:10 -07:00
|
|
|
/**
|
2017-07-27 16:52:16 -07:00
|
|
|
* Adds a child to the end of this node's child list.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-26 19:16:10 -07:00
|
|
|
* @param node the child to add.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public void appendChild(TreeNode node) {
|
2017-07-26 18:44:17 -07:00
|
|
|
children.add(node);
|
|
|
|
}
|
|
|
|
|
2017-07-27 16:52:16 -07:00
|
|
|
/**
|
|
|
|
* Adds a new child to the beginning of this node's child list.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-27 16:52:16 -07:00
|
|
|
* @param node the node to add.
|
|
|
|
*/
|
|
|
|
public void prependChild(TreeNode node) {
|
|
|
|
children.add(0, node);
|
|
|
|
}
|
|
|
|
|
2017-07-26 18:44:17 -07:00
|
|
|
@Override
|
|
|
|
public <T> T reduce(Reducer<T> reducer) {
|
2017-08-03 14:04:09 -07:00
|
|
|
if(Thread.currentThread().isInterrupted())
|
|
|
|
return null;
|
2017-07-26 18:44:17 -07:00
|
|
|
Object[] reducedChildren = new Object[children.size()];
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int i = 0; i < reducedChildren.length; i++) {
|
2017-07-26 18:44:17 -07:00
|
|
|
reducedChildren[i] = children.get(i).reduce(reducer);
|
2017-08-03 15:16:26 -07:00
|
|
|
if (Thread.currentThread().isInterrupted()||reducedChildren[i] == null) return null;
|
2017-07-26 18:44:17 -07:00
|
|
|
}
|
2017-08-03 14:04:09 -07:00
|
|
|
T a = reducer.reduceNode(this, reducedChildren);
|
|
|
|
if(Thread.currentThread().isInterrupted())
|
|
|
|
return null;
|
|
|
|
return a;
|
2017-07-26 18:44:17 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
StringBuilder buffer = new StringBuilder();
|
|
|
|
buffer.append(function);
|
|
|
|
buffer.append("(");
|
2017-07-30 21:11:32 -07:00
|
|
|
for (int i = 0; i < children.size(); i++) {
|
2017-07-26 18:44:17 -07:00
|
|
|
buffer.append(children.get(i));
|
|
|
|
buffer.append(i == children.size() - 1 ? "" : ", ");
|
|
|
|
}
|
|
|
|
buffer.append(")");
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
}
|