mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
26 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b3648d4f3 | |||
| 2ba6e22fcb | |||
| 5228773b5e | |||
| 42393ca6a6 | |||
| c9fad36d16 | |||
| 2cc4bd14ce | |||
| f119f19c04 | |||
| 65772c8d57 | |||
| bbbb2e855e | |||
| 8a29019852 | |||
| 0d7a416446 | |||
| 167e13cfe1 | |||
| b0ae3f90fc | |||
| a7c2084254 | |||
|
|
bf6f48bf82 | ||
| f7da896fc0 | |||
| 6813643b15 | |||
| e6cb755ec9 | |||
|
|
088a45cf4c | ||
|
|
557bc66e53 | ||
|
|
9666ef9019 | ||
|
|
ba30227b28 | ||
|
|
ea5a7a9558 | ||
|
|
3e52a9d645 | ||
|
|
7a0fa31cad | ||
|
|
aec37b6720 |
@@ -3,12 +3,16 @@ package org.nwapw.abacus.function;
|
|||||||
/**
|
/**
|
||||||
* A class that represents a single infix operator.
|
* A class that represents a single infix operator.
|
||||||
*/
|
*/
|
||||||
public abstract class Operator {
|
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.
|
||||||
*/
|
*/
|
||||||
@@ -24,8 +28,9 @@ public abstract 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, int precedence, Function function){
|
public Operator(OperatorAssociativity associativity, OperatorType operatorType, 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;
|
||||||
}
|
}
|
||||||
@@ -38,6 +43,14 @@ public abstract 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.
|
||||||
|
|||||||
8
src/org/nwapw/abacus/function/OperatorType.java
Normal file
8
src/org/nwapw/abacus/function/OperatorType.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
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 HashMap<PatternEntry<T>, Pattern<T>> patterns;
|
private Map<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 ArrayList<Match<T>> lexAll(String from, int startAt, Comparator<T> compare){
|
public List<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,6 +2,7 @@ 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;
|
||||||
|
|
||||||
@@ -32,7 +33,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 HashMap<Character, Function<PatternChain<T>, PatternChain<T>>> operations =
|
private Map<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);
|
||||||
@@ -235,4 +236,22 @@ public class Pattern<T> {
|
|||||||
public PatternNode<T> getHead() {
|
public PatternNode<T> getHead() {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes all characters that are considered "special" from
|
||||||
|
* the given string.
|
||||||
|
* @param from the string to sanitize.
|
||||||
|
* @return the resulting string.
|
||||||
|
*/
|
||||||
|
public static String sanitize(String from){
|
||||||
|
Pattern<Integer> pattern = new Pattern<>("", 0);
|
||||||
|
from = from.replace(".", "\\.");
|
||||||
|
from = from.replace("|", "\\|");
|
||||||
|
from = from.replace("(", "\\(");
|
||||||
|
from = from.replace(")", "\\)");
|
||||||
|
for(Character key : pattern.operations.keySet()){
|
||||||
|
from = from.replace("" + key, "\\" + key);
|
||||||
|
}
|
||||||
|
return from;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ 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
|
||||||
@@ -16,7 +17,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 HashSet<PatternNode<T>> outputStates;
|
protected Set<PatternNode<T>> outputStates;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new pattern node.
|
* Creates a new pattern node.
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ public class NaiveNumber implements NumberInterface {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int precision() {
|
public int precision() {
|
||||||
return 15;
|
return 18;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
65
src/org/nwapw/abacus/plugin/ClassFinder.java
Normal file
65
src/org/nwapw/abacus/plugin/ClassFinder.java
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
|
import java.io.BufferedInputStream;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.jar.JarEntry;
|
||||||
|
import java.util.jar.JarFile;
|
||||||
|
import java.util.zip.ZipInputStream;
|
||||||
|
|
||||||
|
public class ClassFinder extends ClassLoader{
|
||||||
|
|
||||||
|
ArrayList<Class> classes;
|
||||||
|
|
||||||
|
public ClassFinder(){
|
||||||
|
super(ClassFinder.class.getClassLoader());
|
||||||
|
classes=new ArrayList();
|
||||||
|
}
|
||||||
|
public Class loadClass(String className) throws ClassNotFoundException{
|
||||||
|
return findClass(className);
|
||||||
|
}
|
||||||
|
public ArrayList<String> loadClass(File jarLocation) throws ClassNotFoundException, IOException{
|
||||||
|
return addJar(jarLocation);
|
||||||
|
}
|
||||||
|
public ArrayList<String> addJar(File jarLocation) throws IOException {
|
||||||
|
JarFile jarFolder = new JarFile(jarLocation);
|
||||||
|
Enumeration jarList = jarFolder.entries();
|
||||||
|
HashMap classSize = new HashMap();
|
||||||
|
HashMap classContent = new HashMap();
|
||||||
|
ArrayList<String> classNames = new ArrayList();
|
||||||
|
JarEntry tempJar;
|
||||||
|
ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(new FileInputStream(jarLocation)));
|
||||||
|
while(jarList.hasMoreElements()){
|
||||||
|
tempJar = (JarEntry)jarList.nextElement();
|
||||||
|
zipStream.getNextEntry();
|
||||||
|
if(!tempJar.isDirectory()) {
|
||||||
|
if (tempJar.getName().substring(tempJar.getName().indexOf('.')).equals(".class") && (tempJar.getName().length() < 9 || !tempJar.getName().substring(0, 9).equals("META-INF/"))) {
|
||||||
|
int size = (int)tempJar.getSize();
|
||||||
|
classSize.put(tempJar.getName(),new Integer((int)tempJar.getSize()));
|
||||||
|
byte[] bytes = new byte[size];
|
||||||
|
zipStream.read(bytes,0,size);
|
||||||
|
classContent.put(tempJar.getName(),bytes);
|
||||||
|
classNames.add(tempJar.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
jarFolder.close();
|
||||||
|
for(String name:classNames) {
|
||||||
|
classes.add(super.defineClass(name, (byte[]) classContent.get(name), 0, (int) classSize.get(name)));
|
||||||
|
}
|
||||||
|
return classNames;
|
||||||
|
}
|
||||||
|
public ArrayList<Class> getClasses(){
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
public Class getClass(int number){
|
||||||
|
return classes.get(number);
|
||||||
|
}
|
||||||
|
public void delClasses(){
|
||||||
|
classes=new ArrayList();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,11 +19,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 HashMap<String, Function> functions;
|
private Map<String, Function> functions;
|
||||||
/**
|
/**
|
||||||
* A hash map of operators mapped to their string names.
|
* A hash map of operators mapped to their string names.
|
||||||
*/
|
*/
|
||||||
private HashMap<String, Operator> operators;
|
private Map<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,
|
||||||
|
|||||||
@@ -15,29 +15,29 @@ public class PluginManager {
|
|||||||
/**
|
/**
|
||||||
* A list of loaded plugins.
|
* A list of loaded plugins.
|
||||||
*/
|
*/
|
||||||
private ArrayList<Plugin> plugins;
|
private List<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 HashMap<String, Function> cachedFunctions;
|
private Map<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 HashMap<String, Operator> cachedOperators;
|
private Map<String, Operator> cachedOperators;
|
||||||
/**
|
/**
|
||||||
* List of all functions loaded by the plugins.
|
* List of all functions loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private HashSet<String> allFunctions;
|
private Set<String> allFunctions;
|
||||||
/**
|
/**
|
||||||
* List of all operators loaded by the plugins.
|
* List of all operators loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private HashSet<String> allOperators;
|
private Set<String> allOperators;
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
private HashSet<PluginListener> listeners;
|
private Set<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 HashSet<String> getAllFunctions() {
|
public Set<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 HashSet<String> getAllOperators() {
|
public Set<String> getAllOperators() {
|
||||||
return allOperators;
|
return allOperators;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
package org.nwapw.abacus.plugin;
|
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.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;
|
||||||
|
|
||||||
@@ -18,7 +21,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
registerFunction("+", new Function() {
|
registerOperator("+", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length >= 1;
|
||||||
@@ -32,9 +35,9 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
return sum;
|
return sum;
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
|
|
||||||
registerFunction("-", new Function() {
|
registerOperator("-", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -44,9 +47,9 @@ public class StandardPlugin extends Plugin {
|
|||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return params[0].subtract(params[1]);
|
return params[0].subtract(params[1]);
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
|
|
||||||
registerFunction("*", new Function() {
|
registerOperator("*", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length >= 1;
|
return params.length >= 1;
|
||||||
@@ -60,9 +63,9 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
|
|
||||||
registerFunction("/", new Function() {
|
registerOperator("/", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX,1, new Function() {
|
||||||
@Override
|
@Override
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
return params.length == 2;
|
return params.length == 2;
|
||||||
@@ -72,9 +75,22 @@ public class StandardPlugin extends Plugin {
|
|||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return params[0].divide(params[1]);
|
return params[0].divide(params[1]);
|
||||||
}
|
}
|
||||||
});
|
}));
|
||||||
|
|
||||||
registerFunction("!", new Function() {
|
registerOperator("^", new Operator(OperatorAssociativity.RIGHT, OperatorType.BINARY_INFIX, 2, new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return StandardPlugin.this.getFunction("exp").apply(StandardPlugin.this.getFunction("ln").apply(params[0]).multiply(params[1]));
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
|
||||||
|
registerOperator("!", new Operator(OperatorAssociativity.RIGHT, OperatorType.UNARY_POSTFIX, 0, new Function() {
|
||||||
|
//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) {
|
||||||
return params.length == 1;
|
return params.length == 1;
|
||||||
@@ -92,6 +108,23 @@ public class StandardPlugin extends Plugin {
|
|||||||
factorial = factorial.multiply(multiplier);
|
factorial = factorial.multiply(multiplier);
|
||||||
}
|
}
|
||||||
return factorial;
|
return factorial;
|
||||||
|
/*if(!storedList.containsKey(params[0].getClass())){
|
||||||
|
storedList.put(params[0].getClass(), new ArrayList<NumberInterface>());
|
||||||
|
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() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return params[0].multiply((new NaiveNumber(params[0].signum())).promoteTo(params[0].getClass()));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -103,7 +136,100 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
return sumSeries(params[0], StandardPlugin.this::getExpSeriesTerm, getNTermsExp(getMaxError(params[0]), params[0]));
|
boolean takeReciprocal = params[0].signum() == -1;
|
||||||
|
params[0] = StandardPlugin.this.getFunction("abs").apply(params[0]);
|
||||||
|
NumberInterface sum = sumSeries(params[0], StandardPlugin.this::getExpSeriesTerm, getNTermsExp(getMaxError(params[0]), params[0]));
|
||||||
|
if(takeReciprocal){
|
||||||
|
sum = NaiveNumber.ONE.promoteTo(sum.getClass()).divide(sum);
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
registerFunction("ln", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
NumberInterface param = params[0];
|
||||||
|
int powersOf2 = 0;
|
||||||
|
while(StandardPlugin.this.getFunction("abs").apply(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass()))).compareTo((new NaiveNumber(0.1)).promoteTo(param.getClass())) >= 0){
|
||||||
|
if(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() == 1) {
|
||||||
|
param = param.divide(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||||
|
powersOf2++;
|
||||||
|
if(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||||
|
break;
|
||||||
|
//No infinite loop for you.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
param = param.multiply(new NaiveNumber(2).promoteTo(param.getClass()));
|
||||||
|
powersOf2--;
|
||||||
|
if(param.subtract(NaiveNumber.ONE.promoteTo(param.getClass())).signum() != 1) {
|
||||||
|
break;
|
||||||
|
//No infinite loop for you.
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return getLog2(param).multiply((new NaiveNumber(powersOf2)).promoteTo(param.getClass())).add(getLogPartialSum(param));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the partial sum of the Taylor series for logx (around x=1).
|
||||||
|
* Automatically determines the number of terms needed based on the precision of x.
|
||||||
|
* @param x value at which the series is evaluated. 0 < x < 2. (x=2 is convergent but impractical.)
|
||||||
|
* @return the partial sum.
|
||||||
|
*/
|
||||||
|
private NumberInterface getLogPartialSum(NumberInterface x){
|
||||||
|
NumberInterface maxError = StandardPlugin.this.getMaxError(x);
|
||||||
|
x = x.subtract(NaiveNumber.ONE.promoteTo(x.getClass())); //Terms used are for log(x+1).
|
||||||
|
NumberInterface currentTerm = x, sum = x;
|
||||||
|
int n = 1;
|
||||||
|
while(StandardPlugin.this.getFunction("abs").apply(currentTerm).compareTo(maxError) > 0){
|
||||||
|
n++;
|
||||||
|
currentTerm = currentTerm.multiply(x).multiply((new NaiveNumber(n-1)).promoteTo(x.getClass())).divide((new NaiveNumber(n)).promoteTo(x.getClass())).negate();
|
||||||
|
sum = sum.add(currentTerm);
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns natural log of 2 to the required precision of the class of number.
|
||||||
|
* @param number a number of the same type as the return type. (Used for precision.)
|
||||||
|
* @return the value of log(2) with the appropriate precision.
|
||||||
|
*/
|
||||||
|
private NumberInterface getLog2(NumberInterface number){
|
||||||
|
NumberInterface maxError = StandardPlugin.this.getMaxError(number);
|
||||||
|
//NumberInterface errorBound = (new NaiveNumber(1)).promoteTo(number.getClass());
|
||||||
|
//We'll use the series \sigma_{n >= 1) ((1/3^n + 1/4^n) * 1/n)
|
||||||
|
//In the following, a=1/3^n, b=1/4^n, c = 1/n.
|
||||||
|
//a is also an error bound.
|
||||||
|
NumberInterface a = (new NaiveNumber(1)).promoteTo(number.getClass()), b = a, c = a;
|
||||||
|
NumberInterface sum = NaiveNumber.ZERO.promoteTo(number.getClass());
|
||||||
|
int n = 0;
|
||||||
|
while(a.compareTo(maxError) >= 1){
|
||||||
|
n++;
|
||||||
|
a = a.divide((new NaiveNumber(3)).promoteTo(number.getClass()));
|
||||||
|
b = b.divide((new NaiveNumber(4)).promoteTo(number.getClass()));
|
||||||
|
c = NaiveNumber.ONE.promoteTo(number.getClass()).divide((new NaiveNumber(n)).promoteTo(number.getClass()));
|
||||||
|
sum = sum.add(a.add(b).multiply(c));
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
registerFunction("sqrt", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return StandardPlugin.this.getOperator("^").getFunction().apply(params[0], (new NaiveNumber(0.5)));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -120,7 +246,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.getFunction("!").apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
return x.intPow(n).divide(this.getOperator("!").getFunction().apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -130,15 +256,16 @@ public class StandardPlugin extends Plugin {
|
|||||||
* @param x where the function is evaluated.
|
* @param x where the function is evaluated.
|
||||||
* @return the number of terms needed to evaluated the exponential function.
|
* @return the number of terms needed to evaluated the exponential function.
|
||||||
*/
|
*/
|
||||||
private int getNTermsExp(NumberInterface maxError, NumberInterface x){
|
private int getNTermsExp(NumberInterface maxError, NumberInterface x) {
|
||||||
//We need n such that x^(n+2) <= (n+1)! * maxError
|
//We need n such that |x^(n+1)| <= (n+1)! * maxError
|
||||||
//The variables LHS and RHS refer to the above inequality.
|
//The variables LHS and RHS refer to the above inequality.
|
||||||
int n = 0;
|
int n = 0;
|
||||||
NumberInterface LHS = x.intPow(2), RHS = maxError;
|
x = this.getFunction("abs").apply(x);
|
||||||
while(LHS.compareTo(RHS) > 0){
|
NumberInterface LHS = x, RHS = maxError;
|
||||||
|
while (LHS.compareTo(RHS) > 0) {
|
||||||
n++;
|
n++;
|
||||||
LHS = LHS.multiply(x);
|
LHS = LHS.multiply(x);
|
||||||
RHS = RHS.multiply(new NaiveNumber(n).promoteTo(RHS.getClass()));
|
RHS = RHS.multiply(new NaiveNumber(n + 1).promoteTo(RHS.getClass()));
|
||||||
}
|
}
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 OpNode extends TreeNode {
|
public class BinaryInfixNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation being applied.
|
* The operation being applied.
|
||||||
@@ -18,14 +18,14 @@ public class OpNode extends TreeNode {
|
|||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
private OpNode() {}
|
private BinaryInfixNode() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 OpNode(String operation){
|
public BinaryInfixNode(String operation){
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -36,7 +36,7 @@ public class OpNode 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 OpNode(String operation, TreeNode left, TreeNode right){
|
public BinaryInfixNode(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,6 +1,7 @@
|
|||||||
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.
|
||||||
@@ -14,7 +15,7 @@ public class FunctionNode extends TreeNode {
|
|||||||
/**
|
/**
|
||||||
* The list of arguments to the function.
|
* The list of arguments to the function.
|
||||||
*/
|
*/
|
||||||
private ArrayList<TreeNode> children;
|
private List<TreeNode> children;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a function node with no function.
|
* Creates a function node with no function.
|
||||||
@@ -39,13 +40,21 @@ public class FunctionNode extends TreeNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a child to this node.
|
* Adds a child to the end of this node's child list.
|
||||||
* @param node the child to add.
|
* @param node the child to add.
|
||||||
*/
|
*/
|
||||||
public void addChild(TreeNode node){
|
public void appendChild(TreeNode node){
|
||||||
children.add(node);
|
children.add(node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a new child to the beginning of this node's child list.
|
||||||
|
* @param node the node to add.
|
||||||
|
*/
|
||||||
|
public void prependChild(TreeNode node) {
|
||||||
|
children.add(0, node);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
Object[] reducedChildren = new Object[children.size()];
|
Object[] reducedChildren = new Object[children.size()];
|
||||||
|
|||||||
@@ -27,12 +27,17 @@ 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 OpNode){
|
} else if(node instanceof BinaryInfixNode){
|
||||||
NumberInterface left = (NumberInterface) children[0];
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
NumberInterface right = (NumberInterface) children[1];
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
Function function = manager.functionFor(((OpNode) node).getOperation());
|
Function function = manager.operatorFor(((BinaryInfixNode) 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++){
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ package org.nwapw.abacus.tree;
|
|||||||
public enum TokenType {
|
public enum TokenType {
|
||||||
|
|
||||||
INTERNAL_FUNCTION_END(-1),
|
INTERNAL_FUNCTION_END(-1),
|
||||||
ANY(0), COMMA(1), OP(2), NUM(3), FUNCTION(4), OPEN_PARENTH(5), CLOSE_PARENTH(6);
|
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.
|
* The priority by which this token gets sorted.
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
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 java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
@@ -18,11 +20,15 @@ public class TreeBuilder {
|
|||||||
/**
|
/**
|
||||||
* The map of operator precedences.
|
* The map of operator precedences.
|
||||||
*/
|
*/
|
||||||
private HashMap<String, Integer> precedenceMap;
|
private Map<String, Integer> precedenceMap;
|
||||||
/**
|
/**
|
||||||
* The map of operator associativity.
|
* The map of operator associativity.
|
||||||
*/
|
*/
|
||||||
private HashMap<String, OperatorAssociativity> associativityMap;
|
private Map<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.
|
||||||
@@ -34,6 +40,7 @@ public class TreeBuilder {
|
|||||||
*/
|
*/
|
||||||
public TreeBuilder(){
|
public TreeBuilder(){
|
||||||
lexer = new Lexer<TokenType>(){{
|
lexer = new Lexer<TokenType>(){{
|
||||||
|
register(" ", TokenType.WHITESPACE);
|
||||||
register(",", TokenType.COMMA);
|
register(",", TokenType.COMMA);
|
||||||
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
|
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
|
||||||
register("\\(", TokenType.OPEN_PARENTH);
|
register("\\(", TokenType.OPEN_PARENTH);
|
||||||
@@ -41,6 +48,7 @@ public class TreeBuilder {
|
|||||||
}};
|
}};
|
||||||
precedenceMap = new HashMap<>();
|
precedenceMap = new HashMap<>();
|
||||||
associativityMap = new HashMap<>();
|
associativityMap = new HashMap<>();
|
||||||
|
typeMap = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,7 +56,7 @@ public class TreeBuilder {
|
|||||||
* @param function the function to register.
|
* @param function the function to register.
|
||||||
*/
|
*/
|
||||||
public void registerFunction(String function){
|
public void registerFunction(String function){
|
||||||
lexer.register(function, TokenType.FUNCTION);
|
lexer.register(Pattern.sanitize(function), TokenType.FUNCTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -57,10 +65,12 @@ 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, int precedence, OperatorAssociativity associativity){
|
public void registerOperator(String operator, OperatorAssociativity associativity,
|
||||||
lexer.register(operator, TokenType.OP);
|
OperatorType operatorType, int precedence){
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,7 +78,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 ArrayList<Match<TokenType>> tokenize(String string){
|
public List<Match<TokenType>> tokenize(String string){
|
||||||
return lexer.lexAll(string, 0, tokenSorter);
|
return lexer.lexAll(string, 0, tokenSorter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +88,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 ArrayList<Match<TokenType>> intoPostfix(String source, ArrayList<Match<TokenType>> from){
|
public List<Match<TokenType>> intoPostfix(String source, List<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()){
|
||||||
@@ -91,18 +101,26 @@ 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();
|
||||||
if(otherMatchType != TokenType.OP) break;
|
if(!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
||||||
|
|
||||||
int otherPrecdence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo()));
|
if(otherMatchType == TokenType.OP){
|
||||||
if(otherPrecdence < precedence ||
|
int otherPrecedence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo()));
|
||||||
(associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) {
|
if(otherPrecedence < precedence ||
|
||||||
break;
|
(associativity == OperatorAssociativity.RIGHT && otherPrecedence == precedence)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
output.add(tokenStack.pop());
|
output.add(tokenStack.pop());
|
||||||
}
|
}
|
||||||
@@ -134,15 +152,23 @@ 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, ArrayList<Match<TokenType>> matches){
|
public TreeNode fromStringRecursive(String source, List<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){
|
||||||
TreeNode right = fromStringRecursive(source, matches);
|
String operator = source.substring(match.getFrom(), match.getTo());
|
||||||
TreeNode left = fromStringRecursive(source, matches);
|
OperatorType type = typeMap.get(operator);
|
||||||
if(left == null || right == null) return null;
|
if(type == OperatorType.BINARY_INFIX){
|
||||||
else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right);
|
TreeNode right = fromStringRecursive(source, matches);
|
||||||
|
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){
|
||||||
@@ -151,7 +177,7 @@ public class TreeBuilder {
|
|||||||
while(!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END){
|
while(!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END){
|
||||||
TreeNode argument = fromStringRecursive(source, matches);
|
TreeNode argument = fromStringRecursive(source, matches);
|
||||||
if(argument == null) return null;
|
if(argument == null) return null;
|
||||||
node.addChild(argument);
|
node.prependChild(argument);
|
||||||
}
|
}
|
||||||
if(matches.isEmpty()) return null;
|
if(matches.isEmpty()) return null;
|
||||||
matches.remove(0);
|
matches.remove(0);
|
||||||
@@ -166,8 +192,9 @@ public class TreeBuilder {
|
|||||||
* @return the resulting tree.
|
* @return the resulting tree.
|
||||||
*/
|
*/
|
||||||
public TreeNode fromString(String string){
|
public TreeNode fromString(String string){
|
||||||
ArrayList<Match<TokenType>> matches = tokenize(string);
|
List<Match<TokenType>> matches = tokenize(string);
|
||||||
if(matches == null) return null;
|
if(matches == null) return null;
|
||||||
|
matches.removeIf(m -> m.getType() == TokenType.WHITESPACE);
|
||||||
matches = intoPostfix(string, matches);
|
matches = intoPostfix(string, matches);
|
||||||
if(matches == null) return null;
|
if(matches == null) return null;
|
||||||
|
|
||||||
|
|||||||
54
src/org/nwapw/abacus/tree/UnaryPrefixNode.java
Normal file
54
src/org/nwapw/abacus/tree/UnaryPrefixNode.java
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
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,6 +6,7 @@ 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
|
||||||
@@ -57,7 +58,7 @@ public class HistoryTableModel extends AbstractTableModel {
|
|||||||
/**
|
/**
|
||||||
* The list of entries.
|
* The list of entries.
|
||||||
*/
|
*/
|
||||||
ArrayList<HistoryEntry> entries;
|
List<HistoryEntry> entries;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new empty history table model
|
* Creates a new empty history table model
|
||||||
|
|||||||
@@ -269,7 +269,10 @@ 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, operatorObject.getPrecedence(), operatorObject.getAssociativity());
|
builder.registerOperator(operator,
|
||||||
|
operatorObject.getAssociativity(),
|
||||||
|
operatorObject.getType(),
|
||||||
|
operatorObject.getPrecedence());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user