Remove nullability from tree nodes.

This commit is contained in:
Danila Fedorin 2017-09-07 12:31:40 -07:00
parent 00f8475044
commit 1575d3e574
10 changed files with 23 additions and 26 deletions

View File

@ -154,19 +154,20 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
return new VariableNode(match.getContent());
} else if (matchType == TokenType.FUNCTION || matchType == TokenType.TREE_VALUE_FUNCTION) {
String functionName = match.getContent();
CallNode node;
if (matchType == TokenType.FUNCTION) {
node = new FunctionNode(functionName);
} else {
node = new TreeValueFunctionNode(functionName);
}
List<TreeNode> children = new ArrayList<>();
while (!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END) {
TreeNode argument = constructRecursive(matches);
if (argument == null) return null;
node.getChildren().add(0, argument);
children.add(0, argument);
}
if (matches.isEmpty()) return null;
matches.remove(0);
CallNode node;
if (matchType == TokenType.FUNCTION) {
node = new FunctionNode(functionName, children);
} else {
node = new TreeValueFunctionNode(functionName, children);
}
return node;
}
return null;

View File

@ -11,10 +11,10 @@ package org.nwapw.abacus.tree
* @param left the left node.
* @param right the right node.
*/
abstract class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() {
abstract class BinaryNode(val operation: String, val left: TreeNode, val right: TreeNode) : TreeNode() {
override fun toString(): String {
return "(" + (left?.toString() ?: "null") + operation + (right?.toString() ?: "null") + ")"
return "(" + left.toString() + operation + right.toString() + ")"
}
}

View File

@ -7,13 +7,9 @@ package org.nwapw.abacus.tree
* to extend this functionality.
*
* @param callTo the name of the things being called.
* @param children the children of this node.
*/
abstract class CallNode(val callTo: String) : TreeNode() {
/**
* The list of children this node has.
*/
val children: MutableList<TreeNode> = mutableListOf()
abstract class CallNode(val callTo: String, val children: List<TreeNode>) : TreeNode() {
override fun toString(): String {
val buffer = StringBuffer()

View File

@ -8,7 +8,7 @@ package org.nwapw.abacus.tree
*
* @param function the function string.
*/
class FunctionNode(function: String) : CallNode(function) {
class FunctionNode(function: String, children: List<TreeNode>) : CallNode(function, children) {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
val children = Array<Any>(children.size, { children[it].reduce(reducer) ?: return null; })

View File

@ -10,12 +10,12 @@ package org.nwapw.abacus.tree
* @param left the left child of this node.
* @param right the right child of this node.
*/
class NumberBinaryNode(operation: String, left: TreeNode?, right: TreeNode?)
class NumberBinaryNode(operation: String, left: TreeNode, right: TreeNode)
: BinaryNode(operation, left, right) {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
val left = left?.reduce(reducer) ?: return null
val right = right?.reduce(reducer) ?: return null
val left = left.reduce(reducer) ?: return null
val right = right.reduce(reducer) ?: return null
return reducer.reduceNode(this, left, right)
}

View File

@ -8,11 +8,11 @@ package org.nwapw.abacus.tree
* @param operation the operation this node performs.
* @param child the child this node should be applied to.
*/
class NumberUnaryNode(operation: String, child: TreeNode?)
class NumberUnaryNode(operation: String, child: TreeNode)
: UnaryNode(operation, child) {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
val child = applyTo?.reduce(reducer) ?: return null
val child = applyTo.reduce(reducer)
return reducer.reduceNode(this, child)
}

View File

@ -11,7 +11,7 @@ package org.nwapw.abacus.tree
* @param left the left child of this node.
* @param right the right child of this node.
*/
class TreeValueBinaryNode(operation: String, left: TreeNode?, right: TreeNode?)
class TreeValueBinaryNode(operation: String, left: TreeNode, right: TreeNode)
: BinaryNode(operation, left, right) {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {

View File

@ -7,7 +7,7 @@ package org.nwapw.abacus.tree
* is mostly to help the reducer. Besides that, this class also does not
* even attempt to reduce its children.
*/
class TreeValueFunctionNode(name: String) : CallNode(name) {
class TreeValueFunctionNode(name: String, children: List<TreeNode>) : CallNode(name, children) {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
return reducer.reduceNode(this)

View File

@ -9,7 +9,7 @@ package org.nwapw.abacus.tree
* @param operation the operation this node performs.
* @param child the node the operation should be applied to.
*/
class TreeValueUnaryNode(operation: String, child: TreeNode?)
class TreeValueUnaryNode(operation: String, child: TreeNode)
: UnaryNode(operation, child) {
override fun <T : Any> reduce(reducer: Reducer<T>): T? {

View File

@ -9,10 +9,10 @@ package org.nwapw.abacus.tree
* @param operation the operation applied to the given node.
* @param applyTo the node to which the operation will be applied.
*/
abstract class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() {
abstract class UnaryNode(val operation: String, val applyTo: TreeNode) : TreeNode() {
override fun toString(): String {
return "(" + (applyTo?.toString() ?: "null") + ")" + operation
return "(" + applyTo.toString() + ")" + operation
}
}