mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 08:05:19 +00:00
Compare commits
1 Commits
big-decima
...
loader
| Author | SHA1 | Date | |
|---|---|---|---|
| 1190ece7dd |
@@ -54,7 +54,7 @@ public class Lexer<T> {
|
||||
/**
|
||||
* 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.
|
||||
@@ -127,7 +127,7 @@ public class Lexer<T> {
|
||||
* @param compare the comparator used to sort matches by their IDs.
|
||||
* @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;
|
||||
ArrayList<Match<T>> matches = new ArrayList<>();
|
||||
Match<T> lastMatch = null;
|
||||
|
||||
@@ -2,7 +2,6 @@ package org.nwapw.abacus.lexing.pattern;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Stack;
|
||||
import java.util.function.Function;
|
||||
|
||||
@@ -33,7 +32,7 @@ public class Pattern<T> {
|
||||
* A map of regex operator to functions that modify a PatternChain
|
||||
* 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>>>() {{
|
||||
put('+', Pattern.this::transformPlus);
|
||||
put('*', Pattern.this::transformStar);
|
||||
|
||||
@@ -3,7 +3,6 @@ package org.nwapw.abacus.lexing.pattern;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 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
|
||||
* should this node be correctly matched.
|
||||
*/
|
||||
protected Set<PatternNode<T>> outputStates;
|
||||
protected HashSet<PatternNode<T>> outputStates;
|
||||
|
||||
/**
|
||||
* Creates a new pattern node.
|
||||
|
||||
@@ -88,9 +88,6 @@ public class NaiveNumber implements NumberInterface {
|
||||
@Override
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if(toClass == this.getClass()) return this;
|
||||
else if(toClass == PreciseNumber.class){
|
||||
return new PreciseNumber(Double.toString(value));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
package org.nwapw.abacus.number;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
import java.math.MathContext;
|
||||
import java.math.RoundingMode;
|
||||
|
||||
public class PreciseNumber implements NumberInterface{
|
||||
|
||||
/**
|
||||
* The number one.
|
||||
*/
|
||||
static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
|
||||
/**
|
||||
* The number zero.
|
||||
*/
|
||||
static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
|
||||
/**
|
||||
* The number ten.
|
||||
*/
|
||||
static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
|
||||
|
||||
BigDecimal value = new BigDecimal("0");
|
||||
|
||||
/**
|
||||
* Constructs a precise number from the given string.
|
||||
* @param string a string representation of the number meeting the same conditions
|
||||
* as the BidDecimal(String) constructor.
|
||||
*/
|
||||
public PreciseNumber(String string){
|
||||
value = new BigDecimal(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a precise number from the given BigDecimal.
|
||||
* @param value a BigDecimal object representing the value of the number.
|
||||
*/
|
||||
public PreciseNumber(BigDecimal value){
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int precision() {
|
||||
return 54;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface multiply(NumberInterface multiplier) {
|
||||
return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface divide(NumberInterface divisor) {
|
||||
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.precision(), RoundingMode.HALF_UP));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface add(NumberInterface summand) {
|
||||
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface intPow(int exponent) {
|
||||
if(exponent == 0){
|
||||
return PreciseNumber.ONE;
|
||||
}
|
||||
boolean takeReciprocal = exponent < 0;
|
||||
exponent = Math.abs(exponent);
|
||||
NumberInterface power = this;
|
||||
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
|
||||
power = power.multiply(this);
|
||||
}
|
||||
if(takeReciprocal){
|
||||
power = PreciseNumber.ONE.divide(power);
|
||||
}
|
||||
return power;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(NumberInterface number) {
|
||||
return value.compareTo(((PreciseNumber) number).value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int signum() {
|
||||
return value.signum();
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface negate(){
|
||||
return new PreciseNumber(value.negate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||
if(toClass == this.getClass()){
|
||||
return this;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
BigDecimal rounded = value.setScale(precision() - 4, RoundingMode.HALF_UP);
|
||||
return rounded.stripTrailingZeros().toPlainString();
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,6 @@ import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -19,11 +18,11 @@ public abstract class Plugin {
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private Map<String, Operator> operators;
|
||||
private HashMap<String, Operator> operators;
|
||||
/**
|
||||
* The plugin manager in which to search for functions
|
||||
* 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.
|
||||
*/
|
||||
private List<Plugin> plugins;
|
||||
private ArrayList<Plugin> plugins;
|
||||
/**
|
||||
* List of functions that have been cached,
|
||||
* 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,
|
||||
* 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.
|
||||
*/
|
||||
private Set<String> allFunctions;
|
||||
private HashSet<String> allFunctions;
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
private Set<PluginListener> listeners;
|
||||
private HashSet<PluginListener> listeners;
|
||||
|
||||
/**
|
||||
* Creates a new plugin manager.
|
||||
@@ -155,7 +155,7 @@ public class PluginManager {
|
||||
* Gets all the functions loaded by the Plugin Manager.
|
||||
* @return the set of all functions that were loaded.
|
||||
*/
|
||||
public Set<String> getAllFunctions() {
|
||||
public HashSet<String> getAllFunctions() {
|
||||
return allFunctions;
|
||||
}
|
||||
|
||||
@@ -163,7 +163,7 @@ public class PluginManager {
|
||||
* Gets all the operators loaded by the Plugin Manager.
|
||||
* @return the set of all operators that were loaded.
|
||||
*/
|
||||
public Set<String> getAllOperators() {
|
||||
public HashSet<String> getAllOperators() {
|
||||
return allOperators;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,9 @@ import org.nwapw.abacus.function.OperatorAssociativity;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -228,7 +231,7 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
@Override
|
||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||
return StandardPlugin.this.getOperator("^").getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
|
||||
return StandardPlugin.this.getFunction("pow").apply(params[0], (new NaiveNumber(0.5)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A node that represents a function call.
|
||||
@@ -15,7 +14,7 @@ public class FunctionNode extends TreeNode {
|
||||
/**
|
||||
* The list of arguments to the function.
|
||||
*/
|
||||
private List<TreeNode> children;
|
||||
private ArrayList<TreeNode> children;
|
||||
|
||||
/**
|
||||
* Creates a function node with no function.
|
||||
|
||||
@@ -19,11 +19,11 @@ public class TreeBuilder {
|
||||
/**
|
||||
* The map of operator precedences.
|
||||
*/
|
||||
private Map<String, Integer> precedenceMap;
|
||||
private HashMap<String, Integer> precedenceMap;
|
||||
/**
|
||||
* The map of operator associativity.
|
||||
*/
|
||||
private Map<String, OperatorAssociativity> associativityMap;
|
||||
private HashMap<String, OperatorAssociativity> associativityMap;
|
||||
|
||||
/**
|
||||
* Comparator used to sort token types.
|
||||
@@ -70,7 +70,7 @@ public class TreeBuilder {
|
||||
* @param string the string to tokenize.
|
||||
* @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);
|
||||
}
|
||||
|
||||
@@ -80,7 +80,7 @@ public class TreeBuilder {
|
||||
* @param from the tokens to be rearranged.
|
||||
* @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<>();
|
||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||
while(!from.isEmpty()){
|
||||
@@ -138,7 +138,7 @@ public class TreeBuilder {
|
||||
* @param matches the list of tokens from the source string.
|
||||
* @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;
|
||||
Match<TokenType> match = matches.remove(0);
|
||||
TokenType matchType = match.getType();
|
||||
@@ -170,7 +170,7 @@ public class TreeBuilder {
|
||||
* @return the resulting tree.
|
||||
*/
|
||||
public TreeNode fromString(String string){
|
||||
List<Match<TokenType>> matches = tokenize(string);
|
||||
ArrayList<Match<TokenType>> matches = tokenize(string);
|
||||
if(matches == null) return null;
|
||||
matches.removeIf(m -> m.getType() == TokenType.WHITESPACE);
|
||||
matches = intoPostfix(string, matches);
|
||||
|
||||
@@ -6,7 +6,6 @@ import javax.swing.event.TableModelListener;
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
import javax.swing.table.TableModel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A table model to store data about the history of inputs
|
||||
@@ -58,7 +57,7 @@ public class HistoryTableModel extends AbstractTableModel {
|
||||
/**
|
||||
* The list of entries.
|
||||
*/
|
||||
List<HistoryEntry> entries;
|
||||
ArrayList<HistoryEntry> entries;
|
||||
|
||||
/**
|
||||
* Creates a new empty history table model
|
||||
|
||||
Reference in New Issue
Block a user