mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 00:25:20 +00:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1190ece7dd |
@@ -9,10 +9,6 @@ public class Operator {
|
|||||||
* The associativity of the operator.
|
* The associativity of the operator.
|
||||||
*/
|
*/
|
||||||
private OperatorAssociativity associativity;
|
private OperatorAssociativity associativity;
|
||||||
/**
|
|
||||||
* The type of this operator.
|
|
||||||
*/
|
|
||||||
private OperatorType type;
|
|
||||||
/**
|
/**
|
||||||
* The precedence of the operator.
|
* The precedence of the operator.
|
||||||
*/
|
*/
|
||||||
@@ -28,9 +24,8 @@ public class Operator {
|
|||||||
* @param precedence the precedence of the operator.
|
* @param precedence the precedence of the operator.
|
||||||
* @param function the function that the operator calls.
|
* @param function the function that the operator calls.
|
||||||
*/
|
*/
|
||||||
public Operator(OperatorAssociativity associativity, OperatorType operatorType, int precedence, Function function){
|
public Operator(OperatorAssociativity associativity, int precedence, Function function){
|
||||||
this.associativity = associativity;
|
this.associativity = associativity;
|
||||||
this.type = operatorType;
|
|
||||||
this.precedence = precedence;
|
this.precedence = precedence;
|
||||||
this.function = function;
|
this.function = function;
|
||||||
}
|
}
|
||||||
@@ -43,14 +38,6 @@ public class Operator {
|
|||||||
return associativity;
|
return associativity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operator's type.
|
|
||||||
* @return the type.
|
|
||||||
*/
|
|
||||||
public OperatorType getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets the operator's precedence.
|
* Gets the operator's precedence.
|
||||||
* @return the precedence.
|
* @return the precedence.
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
package org.nwapw.abacus.function;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The type of an operator, describing how it should behave.
|
|
||||||
*/
|
|
||||||
public enum OperatorType {
|
|
||||||
BINARY_INFIX, UNARY_POSTFIX
|
|
||||||
}
|
|
||||||
@@ -54,7 +54,7 @@ public class Lexer<T> {
|
|||||||
/**
|
/**
|
||||||
* The registered patterns.
|
* The registered patterns.
|
||||||
*/
|
*/
|
||||||
private Map<PatternEntry<T>, Pattern<T>> patterns;
|
private HashMap<PatternEntry<T>, Pattern<T>> patterns;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new lexer with no registered patterns.
|
* Creates a new lexer with no registered patterns.
|
||||||
@@ -127,7 +127,7 @@ public class Lexer<T> {
|
|||||||
* @param compare the comparator used to sort matches by their IDs.
|
* @param compare the comparator used to sort matches by their IDs.
|
||||||
* @return the resulting list of matches, in order, or null on error.
|
* @return the resulting list of matches, in order, or null on error.
|
||||||
*/
|
*/
|
||||||
public List<Match<T>> lexAll(String from, int startAt, Comparator<T> compare){
|
public ArrayList<Match<T>> lexAll(String from, int startAt, Comparator<T> compare){
|
||||||
int index = startAt;
|
int index = startAt;
|
||||||
ArrayList<Match<T>> matches = new ArrayList<>();
|
ArrayList<Match<T>> matches = new ArrayList<>();
|
||||||
Match<T> lastMatch = null;
|
Match<T> lastMatch = null;
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package org.nwapw.abacus.lexing.pattern;
|
|||||||
|
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
@@ -33,7 +32,7 @@ public class Pattern<T> {
|
|||||||
* A map of regex operator to functions that modify a PatternChain
|
* A map of regex operator to functions that modify a PatternChain
|
||||||
* with the appropriate operation.
|
* with the appropriate operation.
|
||||||
*/
|
*/
|
||||||
private Map<Character, Function<PatternChain<T>, PatternChain<T>>> operations =
|
private HashMap<Character, Function<PatternChain<T>, PatternChain<T>>> operations =
|
||||||
new HashMap<Character, Function<PatternChain<T>, PatternChain<T>>>() {{
|
new HashMap<Character, Function<PatternChain<T>, PatternChain<T>>>() {{
|
||||||
put('+', Pattern.this::transformPlus);
|
put('+', Pattern.this::transformPlus);
|
||||||
put('*', Pattern.this::transformStar);
|
put('*', Pattern.this::transformStar);
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.nwapw.abacus.lexing.pattern;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A base class for a pattern node. Provides all functions
|
* A base class for a pattern node. Provides all functions
|
||||||
@@ -17,7 +16,7 @@ public class PatternNode<T> {
|
|||||||
* The set of states to which the lexer should continue
|
* The set of states to which the lexer should continue
|
||||||
* should this node be correctly matched.
|
* should this node be correctly matched.
|
||||||
*/
|
*/
|
||||||
protected Set<PatternNode<T>> outputStates;
|
protected HashSet<PatternNode<T>> outputStates;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new pattern node.
|
* Creates a new pattern node.
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import org.nwapw.abacus.function.Function;
|
|||||||
import org.nwapw.abacus.function.Operator;
|
import org.nwapw.abacus.function.Operator;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -19,11 +18,11 @@ public abstract class Plugin {
|
|||||||
/**
|
/**
|
||||||
* A hash map of functions mapped to their string names.
|
* A hash map of functions mapped to their string names.
|
||||||
*/
|
*/
|
||||||
private Map<String, Function> functions;
|
private HashMap<String, Function> functions;
|
||||||
/**
|
/**
|
||||||
* A hash map of operators mapped to their string names.
|
* A hash map of operators mapped to their string names.
|
||||||
*/
|
*/
|
||||||
private Map<String, Operator> operators;
|
private HashMap<String, Operator> operators;
|
||||||
/**
|
/**
|
||||||
* The plugin manager in which to search for functions
|
* The plugin manager in which to search for functions
|
||||||
* not inside this package,
|
* not inside this package,
|
||||||
|
|||||||
106
src/org/nwapw/abacus/plugin/PluginLoader.java
Normal file
106
src/org/nwapw/abacus/plugin/PluginLoader.java
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Properties;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A plugin loader, used to scan a directory for
|
||||||
|
* plugins and load them into classes that can then be
|
||||||
|
* used by the plugin manager.
|
||||||
|
*/
|
||||||
|
public class PluginLoader {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An internal class that represents a Jar file that
|
||||||
|
* has been founded, but not loaded.
|
||||||
|
*/
|
||||||
|
private static final class PluginEntry {
|
||||||
|
String mainClass;
|
||||||
|
File jarPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The path which to search for plugins.
|
||||||
|
*/
|
||||||
|
private File path;
|
||||||
|
/**
|
||||||
|
* The array of loaded plugin main classes.
|
||||||
|
*/
|
||||||
|
private ArrayList<Class<?>> foundMainClasses;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new plugin loader at the given path.
|
||||||
|
* @param path the path which to search for plugins.
|
||||||
|
*/
|
||||||
|
public PluginLoader(File path) {
|
||||||
|
this.path = path;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads all the plugin classes that have been found.
|
||||||
|
* @return the list of loaded classes.
|
||||||
|
* @throws IOException thrown when loading classes from URL fails.
|
||||||
|
* @throws ClassNotFoundException thrown when the class specified in plugin.properties is missing.
|
||||||
|
*/
|
||||||
|
private ArrayList<Class<?>> loadPluginClasses() throws IOException, ClassNotFoundException {
|
||||||
|
ArrayList<Class<?>> foundMainClasses = new ArrayList<>();
|
||||||
|
for(PluginEntry entry : findPlugins()){
|
||||||
|
if(entry.mainClass == null) continue;
|
||||||
|
ClassLoader loader = URLClassLoader.newInstance(
|
||||||
|
new URL[]{ entry.jarPath.toURI().toURL() },
|
||||||
|
getClass().getClassLoader());
|
||||||
|
Class<?> loadedClass = loader.loadClass(entry.mainClass);
|
||||||
|
if(!Plugin.class.isAssignableFrom(loadedClass)) continue;
|
||||||
|
foundMainClasses.add(loadedClass);
|
||||||
|
}
|
||||||
|
return foundMainClasses;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find all jars that have a plugin.properties file in the plugin folder.
|
||||||
|
* @return the list of all plugin entries, with their main class names and the jars files.
|
||||||
|
* @throws IOException thrown if reading the jar file fails
|
||||||
|
*/
|
||||||
|
private ArrayList<PluginEntry> findPlugins() throws IOException {
|
||||||
|
ArrayList<PluginEntry> pluginEntries = new ArrayList<>();
|
||||||
|
File[] childFiles = path.listFiles();
|
||||||
|
|
||||||
|
if(childFiles == null) return pluginEntries;
|
||||||
|
for(File file : childFiles){
|
||||||
|
if(!file.isFile() || !file.getName().endsWith(".jar")) continue;
|
||||||
|
JarFile jarFile = new JarFile(file);
|
||||||
|
if(jarFile.getEntry("plugin.properties") == null) continue;
|
||||||
|
Properties properties = new Properties();
|
||||||
|
properties.load(jarFile.getInputStream(jarFile.getEntry("plugin.properties")));
|
||||||
|
|
||||||
|
PluginEntry entry = new PluginEntry();
|
||||||
|
entry.mainClass = properties.getProperty("mainClass");
|
||||||
|
entry.jarPath = file;
|
||||||
|
pluginEntries.add(entry);
|
||||||
|
}
|
||||||
|
return pluginEntries;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads all valid plugins and keeps track of them.
|
||||||
|
* @throws IOException thrown if loading from jar files fails.
|
||||||
|
* @throws ClassNotFoundException thrown if class specified in plugin.properties doesn't exist.
|
||||||
|
*/
|
||||||
|
public void loadValidPlugins() throws IOException, ClassNotFoundException {
|
||||||
|
foundMainClasses = loadPluginClasses();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of all the plugins that have last been loaded.
|
||||||
|
* @return the list of loaded class files.
|
||||||
|
*/
|
||||||
|
public ArrayList<Class<?>> getFoundMainClasses() {
|
||||||
|
return foundMainClasses;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -15,29 +15,29 @@ public class PluginManager {
|
|||||||
/**
|
/**
|
||||||
* A list of loaded plugins.
|
* A list of loaded plugins.
|
||||||
*/
|
*/
|
||||||
private List<Plugin> plugins;
|
private ArrayList<Plugin> plugins;
|
||||||
/**
|
/**
|
||||||
* List of functions that have been cached,
|
* List of functions that have been cached,
|
||||||
* that is, found in a plugin and returned.
|
* that is, found in a plugin and returned.
|
||||||
*/
|
*/
|
||||||
private Map<String, Function> cachedFunctions;
|
private HashMap<String, Function> cachedFunctions;
|
||||||
/**
|
/**
|
||||||
* List of operators tha have been cached,
|
* List of operators tha have been cached,
|
||||||
* that is, found in a plugin and returned.
|
* that is, found in a plugin and returned.
|
||||||
*/
|
*/
|
||||||
private Map<String, Operator> cachedOperators;
|
private HashMap<String, Operator> cachedOperators;
|
||||||
/**
|
/**
|
||||||
* List of all functions loaded by the plugins.
|
* List of all functions loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private Set<String> allFunctions;
|
private HashSet<String> allFunctions;
|
||||||
/**
|
/**
|
||||||
* List of all operators loaded by the plugins.
|
* List of all operators loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private Set<String> allOperators;
|
private HashSet<String> allOperators;
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
private Set<PluginListener> listeners;
|
private HashSet<PluginListener> listeners;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new plugin manager.
|
* Creates a new plugin manager.
|
||||||
@@ -155,7 +155,7 @@ public class PluginManager {
|
|||||||
* Gets all the functions loaded by the Plugin Manager.
|
* Gets all the functions loaded by the Plugin Manager.
|
||||||
* @return the set of all functions that were loaded.
|
* @return the set of all functions that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllFunctions() {
|
public HashSet<String> getAllFunctions() {
|
||||||
return allFunctions;
|
return allFunctions;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,7 +163,7 @@ public class PluginManager {
|
|||||||
* Gets all the operators loaded by the Plugin Manager.
|
* Gets all the operators loaded by the Plugin Manager.
|
||||||
* @return the set of all operators that were loaded.
|
* @return the set of all operators that were loaded.
|
||||||
*/
|
*/
|
||||||
public Set<String> getAllOperators() {
|
public HashSet<String> getAllOperators() {
|
||||||
return allOperators;
|
return allOperators;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,12 @@ package org.nwapw.abacus.plugin;
|
|||||||
import org.nwapw.abacus.function.Function;
|
import org.nwapw.abacus.function.Function;
|
||||||
import org.nwapw.abacus.function.Operator;
|
import org.nwapw.abacus.function.Operator;
|
||||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
import org.nwapw.abacus.function.OperatorType;
|
|
||||||
import org.nwapw.abacus.number.NaiveNumber;
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
|
||||||
|
import javax.print.attribute.standard.MediaSize;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -21,7 +23,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerOperator("+", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
registerOperator("+", new Operator(OperatorAssociativity.LEFT, 0, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length >= 1;
|
||||||
@@ -37,7 +39,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("-", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
registerOperator("-", new Operator(OperatorAssociativity.LEFT, 0, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -49,7 +51,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("*", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
registerOperator("*", new Operator(OperatorAssociativity.LEFT, 1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length >= 1;
|
||||||
@@ -65,7 +67,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("/", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
registerOperator("/", new Operator(OperatorAssociativity.LEFT, 1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -77,7 +79,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("^", new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
registerOperator("^", new Operator(OperatorAssociativity.RIGHT, 2, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -89,7 +91,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
|
|
||||||
registerOperator("!", new Operator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0, new Function() {
|
registerFunction("!", new Function() {
|
||||||
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
//private HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> storedList = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
@@ -114,7 +116,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass()));
|
storedList.get(params[0].getClass()).add(NaiveNumber.ONE.promoteTo(params[0].getClass()));
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
}));
|
});
|
||||||
|
|
||||||
registerFunction("abs", new Function() {
|
registerFunction("abs", new Function() {
|
||||||
@Override
|
@Override
|
||||||
@@ -229,7 +231,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return StandardPlugin.this.getOperator("^").getFunction().apply(params[0], (new NaiveNumber(0.5)));
|
return StandardPlugin.this.getFunction("pow").apply(params[0], (new NaiveNumber(0.5)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -246,7 +248,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @return the nth term of the series.
|
* @return the nth term of the series.
|
||||||
*/
|
*/
|
||||||
private NumberInterface getExpSeriesTerm(int n, NumberInterface x){
|
private NumberInterface getExpSeriesTerm(int n, NumberInterface x){
|
||||||
return x.intPow(n).divide(this.getOperator("!").getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
return x.intPow(n).divide(this.getFunction("!").apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A node that represents a function call.
|
* A node that represents a function call.
|
||||||
@@ -15,7 +14,7 @@ public class FunctionNode extends TreeNode {
|
|||||||
/**
|
/**
|
||||||
* The list of arguments to the function.
|
* The list of arguments to the function.
|
||||||
*/
|
*/
|
||||||
private List<TreeNode> children;
|
private ArrayList<TreeNode> children;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function node with no function.
|
* Creates a function node with no function.
|
||||||
|
|||||||
@@ -27,17 +27,12 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
|||||||
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
||||||
if(node instanceof NumberNode) {
|
if(node instanceof NumberNode) {
|
||||||
return ((NumberNode) node).getNumber();
|
return ((NumberNode) node).getNumber();
|
||||||
} else if(node instanceof BinaryInfixNode){
|
} else if(node instanceof OpNode){
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
Function function = manager.operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
Function function = manager.operatorFor(((OpNode) node).getOperation()).getFunction();
|
||||||
if(function == null) return null;
|
if(function == null) return null;
|
||||||
return function.apply(left, right);
|
return function.apply(left, right);
|
||||||
} else if(node instanceof UnaryPrefixNode) {
|
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
|
||||||
Function functionn = manager.operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
|
||||||
if(functionn == null) return null;
|
|
||||||
return functionn.apply(child);
|
|
||||||
} else if(node instanceof FunctionNode){
|
} else if(node instanceof FunctionNode){
|
||||||
NumberInterface[] convertedChildren = new NumberInterface[children.length];
|
NumberInterface[] convertedChildren = new NumberInterface[children.length];
|
||||||
for(int i = 0; i < convertedChildren.length; i++){
|
for(int i = 0; i < convertedChildren.length; i++){
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package org.nwapw.abacus.tree;
|
|||||||
/**
|
/**
|
||||||
* A tree node that represents an operation being applied to two operands.
|
* A tree node that represents an operation being applied to two operands.
|
||||||
*/
|
*/
|
||||||
public class BinaryInfixNode extends TreeNode {
|
public class OpNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation being applied.
|
* The operation being applied.
|
||||||
@@ -18,14 +18,14 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
private BinaryInfixNode() {}
|
private OpNode() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new operation node with the given operation
|
* Creates a new operation node with the given operation
|
||||||
* and no child nodes.
|
* and no child nodes.
|
||||||
* @param operation the operation.
|
* @param operation the operation.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation){
|
public OpNode(String operation){
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ public class BinaryInfixNode extends TreeNode {
|
|||||||
* @param left the left node of the expression.
|
* @param left the left node of the expression.
|
||||||
* @param right the right node of the expression.
|
* @param right the right node of the expression.
|
||||||
*/
|
*/
|
||||||
public BinaryInfixNode(String operation, TreeNode left, TreeNode right){
|
public OpNode(String operation, TreeNode left, TreeNode right){
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
import org.nwapw.abacus.function.OperatorType;
|
|
||||||
import org.nwapw.abacus.lexing.Lexer;
|
import org.nwapw.abacus.lexing.Lexer;
|
||||||
import org.nwapw.abacus.lexing.pattern.Match;
|
import org.nwapw.abacus.lexing.pattern.Match;
|
||||||
import org.nwapw.abacus.lexing.pattern.Pattern;
|
import org.nwapw.abacus.lexing.pattern.Pattern;
|
||||||
@@ -20,15 +19,11 @@ public class TreeBuilder {
|
|||||||
/**
|
/**
|
||||||
* The map of operator precedences.
|
* The map of operator precedences.
|
||||||
*/
|
*/
|
||||||
private Map<String, Integer> precedenceMap;
|
private HashMap<String, Integer> precedenceMap;
|
||||||
/**
|
/**
|
||||||
* The map of operator associativity.
|
* The map of operator associativity.
|
||||||
*/
|
*/
|
||||||
private Map<String, OperatorAssociativity> associativityMap;
|
private HashMap<String, OperatorAssociativity> associativityMap;
|
||||||
/**
|
|
||||||
* The map of operator types.
|
|
||||||
*/
|
|
||||||
private Map<String, OperatorType> typeMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comparator used to sort token types.
|
* Comparator used to sort token types.
|
||||||
@@ -48,7 +43,6 @@ public class TreeBuilder {
|
|||||||
}};
|
}};
|
||||||
precedenceMap = new HashMap<>();
|
precedenceMap = new HashMap<>();
|
||||||
associativityMap = new HashMap<>();
|
associativityMap = new HashMap<>();
|
||||||
typeMap = new HashMap<>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,12 +59,10 @@ public class TreeBuilder {
|
|||||||
* @param precedence the precedence of the operator.
|
* @param precedence the precedence of the operator.
|
||||||
* @param associativity the associativity of the operator.
|
* @param associativity the associativity of the operator.
|
||||||
*/
|
*/
|
||||||
public void registerOperator(String operator, OperatorAssociativity associativity,
|
public void registerOperator(String operator, int precedence, OperatorAssociativity associativity){
|
||||||
OperatorType operatorType, int precedence){
|
|
||||||
lexer.register(Pattern.sanitize(operator), TokenType.OP);
|
lexer.register(Pattern.sanitize(operator), TokenType.OP);
|
||||||
precedenceMap.put(operator, precedence);
|
precedenceMap.put(operator, precedence);
|
||||||
associativityMap.put(operator, associativity);
|
associativityMap.put(operator, associativity);
|
||||||
typeMap.put(operator, operatorType);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -78,7 +70,7 @@ public class TreeBuilder {
|
|||||||
* @param string the string to tokenize.
|
* @param string the string to tokenize.
|
||||||
* @return the list of tokens produced.
|
* @return the list of tokens produced.
|
||||||
*/
|
*/
|
||||||
public List<Match<TokenType>> tokenize(String string){
|
public ArrayList<Match<TokenType>> tokenize(String string){
|
||||||
return lexer.lexAll(string, 0, tokenSorter);
|
return lexer.lexAll(string, 0, tokenSorter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +80,7 @@ public class TreeBuilder {
|
|||||||
* @param from the tokens to be rearranged.
|
* @param from the tokens to be rearranged.
|
||||||
* @return the resulting list of rearranged tokens.
|
* @return the resulting list of rearranged tokens.
|
||||||
*/
|
*/
|
||||||
public List<Match<TokenType>> intoPostfix(String source, List<Match<TokenType>> from){
|
public ArrayList<Match<TokenType>> intoPostfix(String source, ArrayList<Match<TokenType>> from){
|
||||||
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
||||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||||
while(!from.isEmpty()){
|
while(!from.isEmpty()){
|
||||||
@@ -101,15 +93,9 @@ public class TreeBuilder {
|
|||||||
tokenStack.push(match);
|
tokenStack.push(match);
|
||||||
} else if(matchType == TokenType.OP){
|
} else if(matchType == TokenType.OP){
|
||||||
String tokenString = source.substring(match.getFrom(), match.getTo());
|
String tokenString = source.substring(match.getFrom(), match.getTo());
|
||||||
OperatorType type = typeMap.get(tokenString);
|
|
||||||
int precedence = precedenceMap.get(tokenString);
|
int precedence = precedenceMap.get(tokenString);
|
||||||
OperatorAssociativity associativity = associativityMap.get(tokenString);
|
OperatorAssociativity associativity = associativityMap.get(tokenString);
|
||||||
|
|
||||||
if(type == OperatorType.UNARY_POSTFIX){
|
|
||||||
output.add(match);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
while(!tokenStack.empty()) {
|
while(!tokenStack.empty()) {
|
||||||
Match<TokenType> otherMatch = tokenStack.peek();
|
Match<TokenType> otherMatch = tokenStack.peek();
|
||||||
TokenType otherMatchType = otherMatch.getType();
|
TokenType otherMatchType = otherMatch.getType();
|
||||||
@@ -152,23 +138,15 @@ public class TreeBuilder {
|
|||||||
* @param matches the list of tokens from the source string.
|
* @param matches the list of tokens from the source string.
|
||||||
* @return the construct tree expression.
|
* @return the construct tree expression.
|
||||||
*/
|
*/
|
||||||
public TreeNode fromStringRecursive(String source, List<Match<TokenType>> matches){
|
public TreeNode fromStringRecursive(String source, ArrayList<Match<TokenType>> matches){
|
||||||
if(matches.size() == 0) return null;
|
if(matches.size() == 0) return null;
|
||||||
Match<TokenType> match = matches.remove(0);
|
Match<TokenType> match = matches.remove(0);
|
||||||
TokenType matchType = match.getType();
|
TokenType matchType = match.getType();
|
||||||
if(matchType == TokenType.OP){
|
if(matchType == TokenType.OP){
|
||||||
String operator = source.substring(match.getFrom(), match.getTo());
|
TreeNode right = fromStringRecursive(source, matches);
|
||||||
OperatorType type = typeMap.get(operator);
|
TreeNode left = fromStringRecursive(source, matches);
|
||||||
if(type == OperatorType.BINARY_INFIX){
|
if(left == null || right == null) return null;
|
||||||
TreeNode right = fromStringRecursive(source, matches);
|
else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right);
|
||||||
TreeNode left = fromStringRecursive(source, matches);
|
|
||||||
if(left == null || right == null) return null;
|
|
||||||
else return new BinaryInfixNode(operator, left, right);
|
|
||||||
} else {
|
|
||||||
TreeNode applyTo = fromStringRecursive(source, matches);
|
|
||||||
if(applyTo == null) return null;
|
|
||||||
else return new UnaryPrefixNode(operator, applyTo);
|
|
||||||
}
|
|
||||||
} else if(matchType == TokenType.NUM){
|
} else if(matchType == TokenType.NUM){
|
||||||
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
|
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
|
||||||
} else if(matchType == TokenType.FUNCTION){
|
} else if(matchType == TokenType.FUNCTION){
|
||||||
@@ -192,7 +170,7 @@ public class TreeBuilder {
|
|||||||
* @return the resulting tree.
|
* @return the resulting tree.
|
||||||
*/
|
*/
|
||||||
public TreeNode fromString(String string){
|
public TreeNode fromString(String string){
|
||||||
List<Match<TokenType>> matches = tokenize(string);
|
ArrayList<Match<TokenType>> matches = tokenize(string);
|
||||||
if(matches == null) return null;
|
if(matches == null) return null;
|
||||||
matches.removeIf(m -> m.getType() == TokenType.WHITESPACE);
|
matches.removeIf(m -> m.getType() == TokenType.WHITESPACE);
|
||||||
matches = intoPostfix(string, matches);
|
matches = intoPostfix(string, matches);
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
public class UnaryPrefixNode extends TreeNode {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The operation this node will apply.
|
|
||||||
*/
|
|
||||||
private String operation;
|
|
||||||
/**
|
|
||||||
* The tree node to apply the operation to.
|
|
||||||
*/
|
|
||||||
private TreeNode applyTo;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new node with the given operation and no child.
|
|
||||||
* @param operation the operation for this node.
|
|
||||||
*/
|
|
||||||
public UnaryPrefixNode(String operation){
|
|
||||||
this(operation, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new node with the given operation and child.
|
|
||||||
* @param operation the operation for this node.
|
|
||||||
* @param applyTo the node to apply the function to.
|
|
||||||
*/
|
|
||||||
public UnaryPrefixNode(String operation, TreeNode applyTo){
|
|
||||||
this.operation = operation;
|
|
||||||
this.applyTo = applyTo;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
|
||||||
Object reducedChild = applyTo.reduce(reducer);
|
|
||||||
if(reducedChild == null) return null;
|
|
||||||
return reducer.reduceNode(this, reducedChild);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the operation of this node.
|
|
||||||
* @return the operation this node performs.
|
|
||||||
*/
|
|
||||||
public String getOperation() {
|
|
||||||
return operation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the node to which this node's operation applies.
|
|
||||||
* @return the tree node to which the operation will be applied.
|
|
||||||
*/
|
|
||||||
public TreeNode getApplyTo() {
|
|
||||||
return applyTo;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -6,7 +6,6 @@ import javax.swing.event.TableModelListener;
|
|||||||
import javax.swing.table.AbstractTableModel;
|
import javax.swing.table.AbstractTableModel;
|
||||||
import javax.swing.table.TableModel;
|
import javax.swing.table.TableModel;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A table model to store data about the history of inputs
|
* A table model to store data about the history of inputs
|
||||||
@@ -58,7 +57,7 @@ public class HistoryTableModel extends AbstractTableModel {
|
|||||||
/**
|
/**
|
||||||
* The list of entries.
|
* The list of entries.
|
||||||
*/
|
*/
|
||||||
List<HistoryEntry> entries;
|
ArrayList<HistoryEntry> entries;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new empty history table model
|
* Creates a new empty history table model
|
||||||
|
|||||||
@@ -269,10 +269,7 @@ public class Window extends JFrame implements PluginListener {
|
|||||||
}
|
}
|
||||||
for(String operator : manager.getAllOperators()){
|
for(String operator : manager.getAllOperators()){
|
||||||
Operator operatorObject = manager.operatorFor(operator);
|
Operator operatorObject = manager.operatorFor(operator);
|
||||||
builder.registerOperator(operator,
|
builder.registerOperator(operator, operatorObject.getPrecedence(), operatorObject.getAssociativity());
|
||||||
operatorObject.getAssociativity(),
|
|
||||||
operatorObject.getType(),
|
|
||||||
operatorObject.getPrecedence());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user