mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
1 Commits
tree-funct
...
dokka-setu
| Author | SHA1 | Date | |
|---|---|---|---|
| ece9f1ae04 |
16
build.gradle
16
build.gradle
@@ -1,6 +1,22 @@
|
||||
buildscript {
|
||||
ext.kotlin_version = '1.1.3'
|
||||
ext.dokka_version = '0.9.15'
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
|
||||
}
|
||||
}
|
||||
|
||||
subprojects {
|
||||
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'org.jetbrains.dokka'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
plugins {
|
||||
id 'org.jetbrains.kotlin.jvm' version '1.1.3'
|
||||
}
|
||||
|
||||
dependencies {
|
||||
compile 'com.moandjiezana.toml:toml4j:0.7.1'
|
||||
testCompile 'junit:junit:4.12'
|
||||
|
||||
@@ -53,7 +53,7 @@ public class Abacus {
|
||||
numberReducer = new NumberReducer(this);
|
||||
this.configuration = new Configuration(configuration);
|
||||
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||
ShuntingYardParser shuntingYardParser = new ShuntingYardParser();
|
||||
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
||||
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
||||
|
||||
pluginManager.addListener(shuntingYardParser);
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package org.nwapw.abacus.function;
|
||||
|
||||
/**
|
||||
* A class that represents something that can be applied to one or more
|
||||
* arguments of the same type, and returns a single value from that application.
|
||||
* @param <T> the type of the parameters passed to this applicable.
|
||||
* @param <O> the return type of the applicable.
|
||||
*/
|
||||
public abstract class Applicable<T, O> {
|
||||
|
||||
/**
|
||||
* Checks if the given applicable can be used with the given parameters.
|
||||
* @param params the parameter array to verify for compatibility.
|
||||
* @return whether the array can be used with applyInternal.
|
||||
*/
|
||||
protected abstract boolean matchesParams(T[] params);
|
||||
|
||||
/**
|
||||
* Applies the applicable object to the given parameters,
|
||||
* without checking for compatibility.
|
||||
* @param params the parameters to apply to.
|
||||
* @return the result of the application.
|
||||
*/
|
||||
protected abstract O applyInternal(T[] params);
|
||||
|
||||
/**
|
||||
* If the parameters can be used with this applicable, returns
|
||||
* the result of the application of the applicable to the parameters.
|
||||
* Otherwise, returns null.
|
||||
* @param params the parameters to apply to.
|
||||
* @return the result of the operation, or null if parameters do not match.
|
||||
*/
|
||||
public O apply(T... params){
|
||||
if(!matchesParams(params)) return null;
|
||||
return applyInternal(params);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,6 @@ package org.nwapw.abacus.function;
|
||||
*/
|
||||
public enum DocumentationType {
|
||||
|
||||
FUNCTION, TREE_VALUE_FUNCTION
|
||||
FUNCTION
|
||||
|
||||
}
|
||||
|
||||
@@ -6,6 +6,34 @@ import org.nwapw.abacus.number.NumberInterface;
|
||||
* A function that operates on one or more
|
||||
* inputs and returns a single number.
|
||||
*/
|
||||
public abstract class Function extends Applicable<NumberInterface, NumberInterface> {
|
||||
public abstract class Function {
|
||||
|
||||
/**
|
||||
* Checks whether the given params will work for the given function.
|
||||
*
|
||||
* @param params the given params
|
||||
* @return true if the params can be used with this function.
|
||||
*/
|
||||
protected abstract boolean matchesParams(NumberInterface[] params);
|
||||
|
||||
/**
|
||||
* Internal apply implementation, which already receives appropriately promoted
|
||||
* parameters that have bee run through matchesParams
|
||||
*
|
||||
* @param params the promoted parameters.
|
||||
* @return the return value of the function.
|
||||
*/
|
||||
protected abstract NumberInterface applyInternal(NumberInterface[] params);
|
||||
|
||||
/**
|
||||
* Function to check, promote arguments and run the function.
|
||||
*
|
||||
* @param params the raw input parameters.
|
||||
* @return the return value of the function, or null if an error occurred.
|
||||
*/
|
||||
public NumberInterface apply(NumberInterface... params) {
|
||||
if (!matchesParams(params)) return null;
|
||||
return applyInternal(params);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
package org.nwapw.abacus.function;
|
||||
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.tree.Reducer;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
||||
/**
|
||||
* A function that operates on parse tree nodes instead of on already simplified numbers.
|
||||
* Despite this, it returns a number, not a tree.
|
||||
*/
|
||||
public abstract class TreeValueFunction extends Applicable<TreeNode, NumberInterface> {
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(TreeNode[] params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface apply(TreeNode... params) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the tree value functions to the given tree nodes,
|
||||
* using the given reducer.
|
||||
* @param reducer the reducer to use.
|
||||
* @param params the parameters to apply to.
|
||||
* @return the result of the application.
|
||||
*/
|
||||
public abstract NumberInterface applyWithReducerInternal(Reducer<NumberInterface> reducer, TreeNode[] params);
|
||||
|
||||
/**
|
||||
* Checks if the given parameters and reducer can be used
|
||||
* with this function, and if so, calls the function on them.
|
||||
* @param reducer the reducer to use.
|
||||
* @param params the parameters to apply to.
|
||||
* @return the result of the application, or null if the parameters are incompatible.
|
||||
*/
|
||||
public NumberInterface applyWithReducer(Reducer<NumberInterface> reducer, TreeNode... params) {
|
||||
if(!matchesParams(params) || reducer == null) return null;
|
||||
return applyWithReducerInternal(reducer, params);
|
||||
}
|
||||
}
|
||||
@@ -34,7 +34,6 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
|
||||
register(" ", TokenType.WHITESPACE);
|
||||
register(",", TokenType.COMMA);
|
||||
register("[0-9]*(\\.[0-9]+)?", TokenType.NUM);
|
||||
register("[a-zA-Z]+", TokenType.VARIABLE);
|
||||
register("\\(", TokenType.OPEN_PARENTH);
|
||||
register("\\)", TokenType.CLOSE_PARENTH);
|
||||
}};
|
||||
@@ -53,9 +52,6 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
|
||||
for (String function : manager.getAllFunctions()) {
|
||||
lexer.register(Pattern.sanitize(function), TokenType.FUNCTION);
|
||||
}
|
||||
for (String function : manager.getAllTreeValueFunctions()){
|
||||
lexer.register(Pattern.sanitize(function), TokenType.TREE_VALUE_FUNCTION);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -66,9 +62,6 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
|
||||
for (String function : manager.getAllFunctions()) {
|
||||
lexer.unregister(Pattern.sanitize(function), TokenType.FUNCTION);
|
||||
}
|
||||
for (String function : manager.getAllTreeValueFunctions()){
|
||||
lexer.unregister(Pattern.sanitize(function), TokenType.TREE_VALUE_FUNCTION);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package org.nwapw.abacus.parsing;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||
import org.nwapw.abacus.function.OperatorType;
|
||||
@@ -16,6 +17,10 @@ import java.util.*;
|
||||
*/
|
||||
public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListener {
|
||||
|
||||
/**
|
||||
* The Abacus instance used to create number instances.
|
||||
*/
|
||||
private Abacus abacus;
|
||||
/**
|
||||
* Map of operator precedences, loaded from the plugin operators.
|
||||
*/
|
||||
@@ -30,9 +35,12 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
private Map<String, OperatorType> typeMap;
|
||||
|
||||
/**
|
||||
* Creates a new Shunting Yard parser.
|
||||
* Creates a new Shunting Yard parser with the given Abacus instance.
|
||||
*
|
||||
* @param abacus the abacus instance.
|
||||
*/
|
||||
public ShuntingYardParser() {
|
||||
public ShuntingYardParser(Abacus abacus) {
|
||||
this.abacus = abacus;
|
||||
precedenceMap = new HashMap<>();
|
||||
associativityMap = new HashMap<>();
|
||||
typeMap = new HashMap<>();
|
||||
@@ -53,9 +61,9 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
Match<TokenType> match = from.remove(0);
|
||||
previousType = matchType;
|
||||
matchType = match.getType();
|
||||
if (matchType == TokenType.NUM || matchType == TokenType.VARIABLE) {
|
||||
if (matchType == TokenType.NUM) {
|
||||
output.add(match);
|
||||
} else if (matchType == TokenType.FUNCTION || matchType == TokenType.TREE_VALUE_FUNCTION) {
|
||||
} else if (matchType == TokenType.FUNCTION) {
|
||||
output.add(new Match<>("", TokenType.INTERNAL_FUNCTION_END));
|
||||
tokenStack.push(match);
|
||||
} else if (matchType == TokenType.OP) {
|
||||
@@ -78,8 +86,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
while (!tokenStack.empty() && type == OperatorType.BINARY_INFIX) {
|
||||
Match<TokenType> otherMatch = tokenStack.peek();
|
||||
TokenType otherMatchType = otherMatch.getType();
|
||||
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION ||
|
||||
otherMatchType == TokenType.TREE_VALUE_FUNCTION)) break;
|
||||
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
||||
|
||||
if (otherMatchType == TokenType.OP) {
|
||||
int otherPrecedence = precedenceMap.get(otherMatch.getContent());
|
||||
@@ -106,8 +113,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
while (!tokenStack.empty()) {
|
||||
Match<TokenType> match = tokenStack.peek();
|
||||
TokenType newMatchType = match.getType();
|
||||
if (!(newMatchType == TokenType.OP || newMatchType == TokenType.FUNCTION ||
|
||||
newMatchType == TokenType.TREE_VALUE_FUNCTION)) return null;
|
||||
if (!(newMatchType == TokenType.OP || newMatchType == TokenType.FUNCTION)) return null;
|
||||
output.add(tokenStack.pop());
|
||||
}
|
||||
return output;
|
||||
@@ -137,21 +143,14 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
else return new UnaryNode(operator, applyTo);
|
||||
}
|
||||
} else if (matchType == TokenType.NUM) {
|
||||
return new NumberNode(match.getContent());
|
||||
} else if (matchType == TokenType.VARIABLE) {
|
||||
return new VariableNode(match.getContent());
|
||||
} else if (matchType == TokenType.FUNCTION || matchType == TokenType.TREE_VALUE_FUNCTION) {
|
||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||
} else if (matchType == TokenType.FUNCTION) {
|
||||
String functionName = match.getContent();
|
||||
CallNode node;
|
||||
if(matchType == TokenType.FUNCTION){
|
||||
node = new FunctionNode(functionName);
|
||||
} else {
|
||||
node = new TreeValueFunctionNode(functionName);
|
||||
}
|
||||
FunctionNode node = new FunctionNode(functionName);
|
||||
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);
|
||||
node.prependChild(argument);
|
||||
}
|
||||
if (matches.isEmpty()) return null;
|
||||
matches.remove(0);
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.function.*;
|
||||
import org.nwapw.abacus.function.Documentation;
|
||||
import org.nwapw.abacus.function.DocumentationType;
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
|
||||
/**
|
||||
@@ -66,17 +69,6 @@ public abstract class Plugin {
|
||||
manager.registerFunction(name, toRegister);
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in load(). Registers a tree value function abstract class
|
||||
* with the plugin internally, which makes it accessible to the plugin manager.
|
||||
*
|
||||
* @param name the name to register by.
|
||||
* @param toRegister the tree value function implementation.
|
||||
*/
|
||||
protected final void registerTreeValueFunction(String name, TreeValueFunction toRegister) {
|
||||
manager.registerTreeValueFunction(name, toRegister);
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in load(). Registers an operator abstract class
|
||||
* with the plugin internally, which makes it accessible to
|
||||
@@ -122,18 +114,6 @@ public abstract class Plugin {
|
||||
return manager.functionFor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the PluginManager for the given function name.
|
||||
* This can be used by the plugins internally in order to call functions
|
||||
* they do not provide.
|
||||
*
|
||||
* @param name the name for which to search.
|
||||
* @return the resulting tree value function, or null if none was found for that name.
|
||||
*/
|
||||
protected final TreeValueFunction treeValueFunctionFor(String name){
|
||||
return manager.treeValueFunctionFor(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the PluginManager for the given operator name.
|
||||
* This can be used by the plugins internally in order to call
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.function.*;
|
||||
import org.nwapw.abacus.function.Documentation;
|
||||
import org.nwapw.abacus.function.DocumentationType;
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
@@ -28,10 +31,6 @@ public class PluginManager {
|
||||
* The map of functions registered by the plugins.
|
||||
*/
|
||||
private Map<String, Function> registeredFunctions;
|
||||
/**
|
||||
* The map of tree value functions regstered by the plugins.
|
||||
*/
|
||||
private Map<String, TreeValueFunction> registeredTreeValueFunctions;
|
||||
/**
|
||||
* The map of operators registered by the plugins
|
||||
*/
|
||||
@@ -73,7 +72,6 @@ public class PluginManager {
|
||||
loadedPluginClasses = new HashSet<>();
|
||||
plugins = new HashSet<>();
|
||||
registeredFunctions = new HashMap<>();
|
||||
registeredTreeValueFunctions = new HashMap<>();
|
||||
registeredOperators = new HashMap<>();
|
||||
registeredNumberImplementations = new HashMap<>();
|
||||
registeredDocumentation = new HashSet<>();
|
||||
@@ -92,16 +90,6 @@ public class PluginManager {
|
||||
registeredFunctions.put(name, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a tree value function under the given name.
|
||||
*
|
||||
* @param name the name of the function.
|
||||
* @param function the function to register.
|
||||
*/
|
||||
public void registerTreeValueFunction(String name, TreeValueFunction function) {
|
||||
registeredTreeValueFunctions.put(name, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers an operator under the given name.
|
||||
*
|
||||
@@ -142,16 +130,6 @@ public class PluginManager {
|
||||
return registeredFunctions.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tree value function registered under the given name.
|
||||
*
|
||||
* @param name the name of the function.
|
||||
* @return the function, or null if it was not found.
|
||||
*/
|
||||
public TreeValueFunction treeValueFunctionFor(String name){
|
||||
return registeredTreeValueFunctions.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operator registered under the given name.
|
||||
*
|
||||
@@ -324,15 +302,6 @@ public class PluginManager {
|
||||
return registeredFunctions.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the tree vlaue functions loaded by the PluginManager.
|
||||
*
|
||||
* @return the set of all the tree value functions that were loaded.
|
||||
*/
|
||||
public Set<String> getAllTreeValueFunctions() {
|
||||
return registeredTreeValueFunctions.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the operators loaded by the Plugin Manager.
|
||||
*
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.nwapw.abacus.tree;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.TreeValueFunction;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
|
||||
/**
|
||||
@@ -28,9 +27,7 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||
@Override
|
||||
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
||||
if (node instanceof NumberNode) {
|
||||
return abacus.numberFromString(((NumberNode) node).getNumber());
|
||||
} else if(node instanceof VariableNode) {
|
||||
return abacus.numberFromString("0");
|
||||
return ((NumberNode) node).getNumber();
|
||||
} else if (node instanceof BinaryNode) {
|
||||
NumberInterface left = (NumberInterface) children[0];
|
||||
NumberInterface right = (NumberInterface) children[1];
|
||||
@@ -47,19 +44,9 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||
for (int i = 0; i < convertedChildren.length; i++) {
|
||||
convertedChildren[i] = (NumberInterface) children[i];
|
||||
}
|
||||
Function function = abacus.getPluginManager().functionFor(((FunctionNode) node).getCallTo());
|
||||
Function function = abacus.getPluginManager().functionFor(((FunctionNode) node).getFunction());
|
||||
if (function == null) return null;
|
||||
return function.apply(convertedChildren);
|
||||
} else if (node instanceof TreeValueFunctionNode){
|
||||
CallNode callNode = (CallNode) node;
|
||||
TreeNode[] realChildren = new TreeNode[callNode.getChildren().size()];
|
||||
for(int i = 0; i < realChildren.length; i++){
|
||||
realChildren[i] = callNode.getChildren().get(i);
|
||||
}
|
||||
TreeValueFunction function =
|
||||
abacus.getPluginManager().treeValueFunctionFor(callNode.getCallTo());
|
||||
if(function == null) return null;
|
||||
return function.applyWithReducer(this, realChildren);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ package org.nwapw.abacus.tree;
|
||||
public enum TokenType {
|
||||
|
||||
INTERNAL_FUNCTION_END(-1),
|
||||
ANY(0), WHITESPACE(1), COMMA(2), OP(3), NUM(4), VARIABLE(5), FUNCTION(6), TREE_VALUE_FUNCTION(6), OPEN_PARENTH(7), CLOSE_PARENTH(8);
|
||||
ANY(0), WHITESPACE(1), COMMA(2), OP(3), NUM(4), FUNCTION(5), OPEN_PARENTH(6), CLOSE_PARENTH(7);
|
||||
|
||||
/**
|
||||
* The priority by which this token gets sorted.
|
||||
|
||||
@@ -11,7 +11,7 @@ package org.nwapw.abacus.tree
|
||||
* @param left the left node.
|
||||
* @param right the right node.
|
||||
*/
|
||||
class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() {
|
||||
data class BinaryNode(val operation: String, val left: TreeNode? = null, val right: TreeNode?) : TreeNode() {
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
val leftReduce = left?.reduce(reducer) ?: return null
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
package org.nwapw.abacus.tree
|
||||
|
||||
/**
|
||||
* Represents a more generic function call.
|
||||
*
|
||||
* This class does not specify how it should be reduced, allowing other classes
|
||||
* to extend this functionality.
|
||||
*
|
||||
* @param callTo the name of the things being called.
|
||||
*/
|
||||
abstract class CallNode(val callTo: String) : TreeNode() {
|
||||
|
||||
/**
|
||||
* The list of children this node has.
|
||||
*/
|
||||
val children: MutableList<TreeNode> = mutableListOf()
|
||||
|
||||
override fun toString(): String {
|
||||
val buffer = StringBuffer()
|
||||
buffer.append(callTo)
|
||||
buffer.append("(")
|
||||
for(i in 0 until children.size){
|
||||
buffer.append(children[i].toString())
|
||||
buffer.append(if(i != children.size - 1) ", " else ")")
|
||||
}
|
||||
return buffer.toString()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,11 +8,45 @@ package org.nwapw.abacus.tree
|
||||
*
|
||||
* @param function the function string.
|
||||
*/
|
||||
class FunctionNode(function: String) : CallNode(function) {
|
||||
data class FunctionNode(val function: String) : TreeNode() {
|
||||
|
||||
/**
|
||||
* List of function parameters added to this node.
|
||||
*/
|
||||
val children: MutableList<TreeNode> = mutableListOf()
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
val children = Array<Any>(children.size, { children[it].reduce(reducer) ?: return null; })
|
||||
return reducer.reduceNode(this, *children)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
val buffer = StringBuffer()
|
||||
buffer.append(function)
|
||||
buffer.append('(')
|
||||
for (i in 0 until children.size) {
|
||||
buffer.append(children[i].toString())
|
||||
buffer.append(if (i == children.size - 1) ")" else ",")
|
||||
}
|
||||
return buffer.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a child to this node's list of children.
|
||||
*
|
||||
* @node the node to append.
|
||||
*/
|
||||
fun appendChild(node: TreeNode) {
|
||||
children.add(node)
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepends a child to this node's list of children.
|
||||
*
|
||||
* @node the node to prepend.
|
||||
*/
|
||||
fun prependChild(node: TreeNode) {
|
||||
children.add(0, node)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,14 +10,14 @@ import org.nwapw.abacus.number.NumberInterface
|
||||
*
|
||||
* @number the number value of this node.
|
||||
*/
|
||||
class NumberNode(val number: String) : TreeNode() {
|
||||
data class NumberNode(val number: NumberInterface) : TreeNode() {
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
return reducer.reduceNode(this)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return number
|
||||
return number.toString()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
package org.nwapw.abacus.tree
|
||||
|
||||
/**
|
||||
* A tree node that represents a tree value function call.
|
||||
*
|
||||
* This is in many ways similar to a simple FunctionNode, and the distinction
|
||||
* 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) {
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
return reducer.reduceNode(this)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -9,7 +9,7 @@ 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.
|
||||
*/
|
||||
class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() {
|
||||
data class UnaryNode(val operation: String, val applyTo: TreeNode? = null) : TreeNode() {
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
val reducedChild = applyTo?.reduce(reducer) ?: return null
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
package org.nwapw.abacus.tree
|
||||
|
||||
/**
|
||||
* A tree node that holds a placeholder variable.
|
||||
*
|
||||
* This node holds a variable string, and acts similarly to a number,
|
||||
* with the key difference of not actually holding a value at runtime.
|
||||
*
|
||||
* @param variable the actual variable name that this node represents.
|
||||
*/
|
||||
class VariableNode(val variable: String) : TreeNode() {
|
||||
|
||||
override fun <T : Any> reduce(reducer: Reducer<T>): T? {
|
||||
return reducer.reduceNode(this)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return variable
|
||||
}
|
||||
|
||||
}
|
||||
@@ -101,7 +101,7 @@ public class CalculationTests {
|
||||
testOutput("2^50", "(2^50)", "112589990684262");
|
||||
testOutput("7^(-sqrt2*17)", "(7^((sqrt(2)*17))`)", "4.81354609155297814551845300063563");
|
||||
testEvalError("0^0", "(0^0)");
|
||||
testEvalError("(-13)^.9999", "((13)`^.9999)");
|
||||
testEvalError("(-13)^.9999", "((13)`^0.9999)");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,3 @@
|
||||
plugins {
|
||||
id 'org.jetbrains.kotlin.jvm' version '1.1.3'
|
||||
}
|
||||
apply plugin: 'application'
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -360,8 +360,6 @@ public class AbacusController implements PluginListener {
|
||||
PluginManager pluginManager = abacus.getPluginManager();
|
||||
functionList.addAll(manager.getAllFunctions().stream().map(name -> pluginManager.documentationFor(name, DocumentationType.FUNCTION))
|
||||
.collect(Collectors.toCollection(ArrayList::new)));
|
||||
functionList.addAll(manager.getAllTreeValueFunctions().stream().map(name -> pluginManager.documentationFor(name, DocumentationType.TREE_VALUE_FUNCTION))
|
||||
.collect(Collectors.toCollection(ArrayList::new)));
|
||||
functionList.sort(Comparator.comparing(Documentation::getCodeName));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user