mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 16:15:19 +00:00
Compare commits
6 Commits
configurat
...
architectu
| Author | SHA1 | Date | |
|---|---|---|---|
| 9454620489 | |||
| 1160768ee5 | |||
| 1ce9fc6b1c | |||
| acf3d85584 | |||
| 6c80d8fe93 | |||
| a949a27da4 |
@@ -1,46 +1,188 @@
|
||||
package org.nwapw.abacus;
|
||||
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
//import org.nwapw.abacus.plugin.StandardPlugin;
|
||||
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||
import org.nwapw.abacus.window.Window;
|
||||
import org.nwapw.abacus.config.ConfigurationObject;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.plugin.ClassFinder;
|
||||
import org.nwapw.abacus.plugin.PluginListener;
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||
import org.nwapw.abacus.tree.NumberReducer;
|
||||
import org.nwapw.abacus.tree.TreeBuilder;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
import org.nwapw.abacus.window.Window;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
|
||||
public class Abacus {
|
||||
/**
|
||||
* The main calculator class. This is responsible
|
||||
* for piecing together all of the components, allowing
|
||||
* their interaction with each other.
|
||||
*/
|
||||
public class Abacus implements PluginListener {
|
||||
|
||||
/**
|
||||
* The default implementation to use for the number representation.
|
||||
*/
|
||||
public static final Class<? extends NumberInterface> DEFAULT_NUMBER = NaiveNumber.class;
|
||||
/**
|
||||
* The file used for saving and loading configuration.
|
||||
*/
|
||||
public static final File CONFIG_FILE = new File("config.toml");
|
||||
|
||||
/**
|
||||
* The main Abacus UI.
|
||||
*/
|
||||
private Window mainUi;
|
||||
private PluginManager manager;
|
||||
/**
|
||||
* The plugin manager responsible for
|
||||
* loading and unloading plugins,
|
||||
* and getting functions from them.
|
||||
*/
|
||||
private PluginManager pluginManager;
|
||||
/**
|
||||
* Tree builder built from plugin manager,
|
||||
* used to construct parse trees.
|
||||
*/
|
||||
private TreeBuilder treeBuilder;
|
||||
/**
|
||||
* The reducer used to evaluate the tree.
|
||||
*/
|
||||
private NumberReducer numberReducer;
|
||||
/**
|
||||
* The configuration loaded from a file.
|
||||
*/
|
||||
private ConfigurationObject configuration;
|
||||
|
||||
/**
|
||||
* Creates a new instance of the Abacus calculator.
|
||||
*/
|
||||
public Abacus(){
|
||||
init();
|
||||
pluginManager = new PluginManager(this);
|
||||
mainUi = new Window(this);
|
||||
numberReducer = new NumberReducer(this);
|
||||
configuration = new ConfigurationObject(CONFIG_FILE);
|
||||
configuration.save(CONFIG_FILE);
|
||||
|
||||
pluginManager.addListener(this);
|
||||
pluginManager.addInstantiated(new StandardPlugin(pluginManager));
|
||||
try {
|
||||
ClassFinder.loadJars("plugins")
|
||||
.forEach(plugin -> pluginManager.addClass(plugin));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
pluginManager.load();
|
||||
|
||||
mainUi.setVisible(true);
|
||||
}
|
||||
|
||||
private void init() {
|
||||
/**
|
||||
* Gets the current tree builder.
|
||||
* @return the main tree builder in this abacus instance.
|
||||
*/
|
||||
public TreeBuilder getTreeBuilder() {
|
||||
return treeBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current plugin manager,
|
||||
* @return the plugin manager in this abacus instance.
|
||||
*/
|
||||
public PluginManager getPluginManager() {
|
||||
return pluginManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current UI.
|
||||
* @return the UI window in this abacus instance.
|
||||
*/
|
||||
public Window getMainUi() {
|
||||
return mainUi;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the reducer that is responsible for transforming
|
||||
* an expression into a number.
|
||||
* @return the number reducer in this abacus instance.
|
||||
*/
|
||||
public NumberReducer getNumberReducer() {
|
||||
return numberReducer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the configuration object associated with this instance.
|
||||
* @return the configuration object.
|
||||
*/
|
||||
public ConfigurationObject getConfiguration() {
|
||||
return configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a string into a tree structure using the main
|
||||
* tree builder.
|
||||
* @param input the input string to parse
|
||||
* @return the resulting tree, null if the tree builder or the produced tree are null.
|
||||
*/
|
||||
public TreeNode parseString(String input){
|
||||
if(treeBuilder == null) return null;
|
||||
return treeBuilder.fromString(input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evaluates the given tree using the main
|
||||
* number reducer.
|
||||
* @param tree the tree to reduce, must not be null.
|
||||
* @return the resulting number, or null of the reduction failed.
|
||||
*/
|
||||
public NumberInterface evaluateTree(TreeNode tree){
|
||||
return tree.reduce(numberReducer);
|
||||
}
|
||||
|
||||
public NumberInterface numberFromString(String numberString){
|
||||
Class<? extends NumberInterface> toInstantiate =
|
||||
pluginManager.numberFor(configuration.getNumberImplementation());
|
||||
if(toInstantiate == null) toInstantiate = DEFAULT_NUMBER;
|
||||
|
||||
try {
|
||||
return toInstantiate.getConstructor(String.class).newInstance(numberString);
|
||||
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad(PluginManager manager) {
|
||||
treeBuilder = new TreeBuilder(this);
|
||||
for(String function : manager.getAllFunctions()){
|
||||
treeBuilder.registerFunction(function);
|
||||
}
|
||||
for(String operator : manager.getAllOperators()){
|
||||
Operator operatorObject = manager.operatorFor(operator);
|
||||
treeBuilder.registerOperator(operator,
|
||||
operatorObject.getAssociativity(),
|
||||
operatorObject.getType(),
|
||||
operatorObject.getPrecedence());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnload(PluginManager manager) {
|
||||
treeBuilder = null;
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
try {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
manager = new PluginManager();
|
||||
manager.addInstantiated(new StandardPlugin(manager));
|
||||
try {
|
||||
ClassFinder.loadJars("plugins")
|
||||
.forEach(plugin -> manager.addClass(plugin));
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
mainUi = new Window(manager);
|
||||
mainUi.setVisible(true);
|
||||
manager.load();
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
new Abacus();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -6,10 +6,6 @@ package org.nwapw.abacus.config;
|
||||
*/
|
||||
public class Configuration {
|
||||
|
||||
/**
|
||||
* The precision to which the calculator should operator.
|
||||
*/
|
||||
public int decimalPrecision;
|
||||
/**
|
||||
* The type of number this calculator should use.
|
||||
*/
|
||||
|
||||
@@ -2,13 +2,9 @@ package org.nwapw.abacus.config;
|
||||
|
||||
import com.moandjiezana.toml.Toml;
|
||||
import com.moandjiezana.toml.TomlWriter;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A configuration object, which essentially
|
||||
@@ -18,10 +14,6 @@ import java.util.Map;
|
||||
*/
|
||||
public class ConfigurationObject {
|
||||
|
||||
/**
|
||||
* The default implementation to use for instantiating numbers.
|
||||
*/
|
||||
private static final Class<? extends NaiveNumber> DEFAULT_IMPLEMENTATION = NaiveNumber.class;
|
||||
/**
|
||||
* The writer used to store the configuration.
|
||||
*/
|
||||
@@ -30,11 +22,6 @@ public class ConfigurationObject {
|
||||
* The configuration instance being modeled.
|
||||
*/
|
||||
private Configuration configuration;
|
||||
/**
|
||||
* A map of number names to their implementations, which
|
||||
* will be provided by plugins.
|
||||
*/
|
||||
private Map<String, Class<? extends NaiveNumber>> numberImplementations;
|
||||
|
||||
/**
|
||||
* Sets up the ConfigurationObject.
|
||||
@@ -44,7 +31,6 @@ public class ConfigurationObject {
|
||||
*/
|
||||
private void setup(Configuration configuration){
|
||||
this.configuration = configuration;
|
||||
numberImplementations = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,44 +39,17 @@ public class ConfigurationObject {
|
||||
*/
|
||||
private Configuration getDefaultConfig(){
|
||||
configuration = new Configuration();
|
||||
configuration.decimalPrecision = -1;
|
||||
configuration.numberType = "naive";
|
||||
return configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a number implementation.
|
||||
* @param name the name of the number implementation to register the class as.
|
||||
* @param newClass the class that will be used to instantiate the new number.
|
||||
* It is required that this class provides a Number(String) constructor.
|
||||
* Returns the implementation the user has requested to
|
||||
* represent their numbers.
|
||||
* @return the implementation name.
|
||||
*/
|
||||
public void registerImplementation(String name, Class<? extends NaiveNumber> newClass){
|
||||
numberImplementations.put(name, newClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new number with the configured type, passing
|
||||
* it the given string.
|
||||
* @param string the string from which the number should be parsed.
|
||||
* @return the resulting number, or null if an error occurred.
|
||||
*/
|
||||
public NaiveNumber numberFromString(String string) {
|
||||
Class<? extends NaiveNumber> toLoad =
|
||||
numberImplementations.getOrDefault(configuration.numberType, DEFAULT_IMPLEMENTATION);
|
||||
try {
|
||||
return toLoad.getConstructor(String.class).newInstance(string);
|
||||
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the configured, user-requested precision.
|
||||
* @return the precision.
|
||||
*/
|
||||
public int getPrecision(){
|
||||
return configuration.decimalPrecision;
|
||||
public String getNumberImplementation() {
|
||||
return configuration.numberType;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.List;
|
||||
import java.util.jar.JarEntry;
|
||||
import java.util.jar.JarFile;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -24,7 +25,7 @@ public class ClassFinder {
|
||||
* @throws IOException thrown if an error occurred scanning the plugin folder.
|
||||
* @throws ClassNotFoundException thrown if the class listed in the file doesn't get loaded.
|
||||
*/
|
||||
public static ArrayList<Class<?>> loadJars(String filePath) throws IOException, ClassNotFoundException {
|
||||
public static List<Class<?>> loadJars(String filePath) throws IOException, ClassNotFoundException {
|
||||
return loadJars(new File(filePath));
|
||||
}
|
||||
|
||||
@@ -35,7 +36,7 @@ public class ClassFinder {
|
||||
* @throws IOException thrown if an error occurred scanning the plugin folder.
|
||||
* @throws ClassNotFoundException thrown if the class listed in the file doesn't get loaded.
|
||||
*/
|
||||
public static ArrayList<Class<?>> loadJars(File pluginFolderPath) throws IOException, ClassNotFoundException {
|
||||
public static List<Class<?>> loadJars(File pluginFolderPath) throws IOException, ClassNotFoundException {
|
||||
ArrayList<Class<?>> toReturn = new ArrayList<>();
|
||||
if(!pluginFolderPath.exists()) return toReturn;
|
||||
ArrayList<File> files = Files.walk(pluginFolderPath.toPath())
|
||||
@@ -55,7 +56,7 @@ public class ClassFinder {
|
||||
* @throws IOException thrown if there was an error reading the file
|
||||
* @throws ClassNotFoundException thrown if the class could not be loaded.
|
||||
*/
|
||||
public static ArrayList<Class<?>> loadJar(File jarLocation) throws IOException, ClassNotFoundException {
|
||||
public static List<Class<?>> loadJar(File jarLocation) throws IOException, ClassNotFoundException {
|
||||
ArrayList<Class<?>> loadedClasses = new ArrayList<>();
|
||||
String path = jarLocation.getPath();
|
||||
URL[] urls = new URL[]{new URL("jar:file:" + path + "!/")};
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
@@ -24,6 +25,10 @@ public abstract class Plugin {
|
||||
* A hash map of operators mapped to their string names.
|
||||
*/
|
||||
private Map<String, Operator> operators;
|
||||
/**
|
||||
* A hash map of operators mapped to their string names.
|
||||
*/
|
||||
private Map<String, Class<? extends NumberInterface>> numbers;
|
||||
/**
|
||||
* The plugin manager in which to search for functions
|
||||
* not inside this package,
|
||||
@@ -44,6 +49,7 @@ public abstract class Plugin {
|
||||
this.manager = manager;
|
||||
functions = new HashMap<>();
|
||||
operators = new HashMap<>();
|
||||
numbers = new HashMap<>();
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
@@ -63,6 +69,14 @@ public abstract class Plugin {
|
||||
return operators.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of all numbers provided by this plugin.
|
||||
* @return the list of registered numbers.
|
||||
*/
|
||||
public final Set<String> providedNumbers(){
|
||||
return numbers.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a function under the given function name.
|
||||
* @param functionName the name of the function to get
|
||||
@@ -81,6 +95,15 @@ public abstract class Plugin {
|
||||
return operators.get(operatorName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the class under the given name.
|
||||
* @param numberName the name of the class.
|
||||
* @return the class, or null if the plugin doesn't provide it.
|
||||
*/
|
||||
public final Class<? extends NumberInterface> getNumber(String numberName){
|
||||
return numbers.get(numberName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enables the function, loading the necessary instances
|
||||
* of functions.
|
||||
@@ -124,6 +147,18 @@ public abstract class Plugin {
|
||||
operators.put(name, operator);
|
||||
}
|
||||
|
||||
/**
|
||||
* To be used in load(). Registers a number class
|
||||
* with the plugin internally, which makes it possible
|
||||
* for the user to select it as an "implementation" for the
|
||||
* number that they would like to use.
|
||||
* @param name the name to register it under.
|
||||
* @param toRegister the class to register.
|
||||
*/
|
||||
protected final void registerNumber(String name, Class<? extends NumberInterface> toRegister){
|
||||
numbers.put(name, toRegister);
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches the PluginManager for the given function name.
|
||||
* This can be used by the plugins internally in order to call functions
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.nwapw.abacus.plugin;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.*;
|
||||
@@ -26,10 +28,15 @@ public class PluginManager {
|
||||
*/
|
||||
private Map<String, Function> cachedFunctions;
|
||||
/**
|
||||
* List of operators tha have been cached,
|
||||
* List of operators that have been cached,
|
||||
* that is, found in a plugin and returned.
|
||||
*/
|
||||
private Map<String, Operator> cachedOperators;
|
||||
/**
|
||||
* List of registered number implementations that have
|
||||
* been cached, that is, found in a plugin and returned.
|
||||
*/
|
||||
private Map<String, Class<? extends NumberInterface>> cachedNumbers;
|
||||
/**
|
||||
* List of all functions loaded by the plugins.
|
||||
*/
|
||||
@@ -38,21 +45,33 @@ public class PluginManager {
|
||||
* List of all operators loaded by the plugins.
|
||||
*/
|
||||
private Set<String> allOperators;
|
||||
/**
|
||||
* List of all numbers loaded by the plugins.
|
||||
*/
|
||||
private Set<String> allNumbers;
|
||||
/**
|
||||
* The list of plugin listeners attached to this instance.
|
||||
*/
|
||||
private Set<PluginListener> listeners;
|
||||
/**
|
||||
* The instance of Abacus that is used to interact with its other
|
||||
* components.
|
||||
*/
|
||||
private Abacus abacus;
|
||||
|
||||
/**
|
||||
* Creates a new plugin manager.
|
||||
*/
|
||||
public PluginManager(){
|
||||
public PluginManager(Abacus abacus){
|
||||
this.abacus = abacus;
|
||||
loadedPluginClasses = new HashSet<>();
|
||||
plugins = new HashSet<>();
|
||||
cachedFunctions = new HashMap<>();
|
||||
cachedOperators = new HashMap<>();
|
||||
cachedNumbers = new HashMap<>();
|
||||
allFunctions = new HashSet<>();
|
||||
allOperators = new HashSet<>();
|
||||
allNumbers = new HashSet<>();
|
||||
listeners = new HashSet<>();
|
||||
}
|
||||
|
||||
@@ -104,6 +123,15 @@ public class PluginManager {
|
||||
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a numer implementation under the given name.
|
||||
* @param name the name of the implementation.
|
||||
* @return the implementation class
|
||||
*/
|
||||
public Class<? extends NumberInterface> numberFor(String name){
|
||||
return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an instance of Plugin that already has been instantiated.
|
||||
* @param plugin the plugin to add.
|
||||
@@ -136,6 +164,7 @@ public class PluginManager {
|
||||
for(Plugin plugin : plugins){
|
||||
allFunctions.addAll(plugin.providedFunctions());
|
||||
allOperators.addAll(plugin.providedOperators());
|
||||
allNumbers.addAll(plugin.providedNumbers());
|
||||
}
|
||||
listeners.forEach(e -> e.onLoad(this));
|
||||
}
|
||||
@@ -147,6 +176,7 @@ public class PluginManager {
|
||||
for(Plugin plugin : plugins) plugin.disable();
|
||||
allFunctions.clear();
|
||||
allOperators.clear();
|
||||
allNumbers.clear();
|
||||
listeners.forEach(e -> e.onUnload(this));
|
||||
}
|
||||
|
||||
@@ -174,6 +204,14 @@ public class PluginManager {
|
||||
return allOperators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the number implementations loaded by the Plugin Manager
|
||||
* @return the set of all implementations that were loaded
|
||||
*/
|
||||
public Set<String> getAllNumbers() {
|
||||
return allNumbers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a plugin change listener to this plugin manager.
|
||||
* @param listener the listener to add.
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.nwapw.abacus.function.OperatorAssociativity;
|
||||
import org.nwapw.abacus.function.OperatorType;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.number.PreciseNumber;
|
||||
|
||||
import java.util.function.BiFunction;
|
||||
|
||||
@@ -21,6 +22,9 @@ public class StandardPlugin extends Plugin {
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
registerNumber("naive", NaiveNumber.class);
|
||||
registerNumber("precise", PreciseNumber.class);
|
||||
|
||||
registerOperator("+", new Operator(OperatorAssociativity.LEFT, OperatorType.BINARY_INFIX, 0, new Function() {
|
||||
@Override
|
||||
protected boolean matchesParams(NumberInterface[] params) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.function.Function;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
|
||||
/**
|
||||
* A reducer implementation that turns a tree into a single number.
|
||||
@@ -13,14 +13,14 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||
/**
|
||||
* The plugin manager from which to draw the functions.
|
||||
*/
|
||||
private PluginManager manager;
|
||||
private Abacus abacus;
|
||||
|
||||
/**
|
||||
* Creates a new number reducer with the given plugin manager.
|
||||
* @param manager the plugin manager.
|
||||
* Creates a new number reducer.
|
||||
* @param abacus the calculator instance.
|
||||
*/
|
||||
public NumberReducer(PluginManager manager){
|
||||
this.manager = manager;
|
||||
public NumberReducer(Abacus abacus){
|
||||
this.abacus = abacus;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -30,12 +30,12 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||
} else if(node instanceof BinaryInfixNode){
|
||||
NumberInterface left = (NumberInterface) children[0];
|
||||
NumberInterface right = (NumberInterface) children[1];
|
||||
Function function = manager.operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
||||
Function function = abacus.getPluginManager().operatorFor(((BinaryInfixNode) node).getOperation()).getFunction();
|
||||
if(function == null) return null;
|
||||
return function.apply(left, right);
|
||||
} else if(node instanceof UnaryPrefixNode) {
|
||||
NumberInterface child = (NumberInterface) children[0];
|
||||
Function functionn = manager.operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
||||
Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
||||
if(functionn == null) return null;
|
||||
return functionn.apply(child);
|
||||
} else if(node instanceof FunctionNode){
|
||||
@@ -43,7 +43,7 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
||||
for(int i = 0; i < convertedChildren.length; i++){
|
||||
convertedChildren[i] = (NumberInterface) children[i];
|
||||
}
|
||||
Function function = manager.functionFor(((FunctionNode) node).getFunction());
|
||||
Function function = abacus.getPluginManager().functionFor(((FunctionNode) node).getFunction());
|
||||
if(function == null) return null;
|
||||
return function.apply(convertedChildren);
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||
import org.nwapw.abacus.function.OperatorType;
|
||||
import org.nwapw.abacus.lexing.Lexer;
|
||||
import org.nwapw.abacus.lexing.pattern.Match;
|
||||
import org.nwapw.abacus.lexing.pattern.Pattern;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.PreciseNumber;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -31,6 +30,11 @@ public class TreeBuilder {
|
||||
* The map of operator types.
|
||||
*/
|
||||
private Map<String, OperatorType> typeMap;
|
||||
/**
|
||||
* The abacus instance required to interact with
|
||||
* other components of the calculator.
|
||||
*/
|
||||
private Abacus abacus;
|
||||
|
||||
/**
|
||||
* Comparator used to sort token types.
|
||||
@@ -40,7 +44,8 @@ public class TreeBuilder {
|
||||
/**
|
||||
* Creates a new TreeBuilder.
|
||||
*/
|
||||
public TreeBuilder(){
|
||||
public TreeBuilder(Abacus abacus){
|
||||
this.abacus = abacus;
|
||||
lexer = new Lexer<TokenType>(){{
|
||||
register(" ", TokenType.WHITESPACE);
|
||||
register(",", TokenType.COMMA);
|
||||
@@ -172,7 +177,7 @@ public class TreeBuilder {
|
||||
else return new UnaryPrefixNode(operator, applyTo);
|
||||
}
|
||||
} else if(matchType == TokenType.NUM){
|
||||
return new NumberNode(new NaiveNumber(Double.parseDouble(source.substring(match.getFrom(), match.getTo()))));
|
||||
return new NumberNode(abacus.numberFromString(source.substring(match.getFrom(), match.getTo())));
|
||||
} else if(matchType == TokenType.FUNCTION){
|
||||
String functionName = source.substring(match.getFrom(), match.getTo());
|
||||
FunctionNode node = new FunctionNode(functionName);
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
package org.nwapw.abacus.window;
|
||||
|
||||
import org.nwapw.abacus.function.Operator;
|
||||
import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.plugin.PluginListener;
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
import org.nwapw.abacus.tree.NumberReducer;
|
||||
import org.nwapw.abacus.tree.TreeBuilder;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -18,7 +14,7 @@ import java.awt.event.MouseEvent;
|
||||
/**
|
||||
* The main UI window for the calculator.
|
||||
*/
|
||||
public class Window extends JFrame implements PluginListener {
|
||||
public class Window extends JFrame {
|
||||
|
||||
private static final String CALC_STRING = "Calculate";
|
||||
private static final String SYNTAX_ERR_STRING = "Syntax Error";
|
||||
@@ -47,18 +43,10 @@ public class Window extends JFrame implements PluginListener {
|
||||
};
|
||||
|
||||
/**
|
||||
* The plugin manager used to retrieve functions.
|
||||
* The instance of the Abacus class, used
|
||||
* for interaction with plugins and configuration.
|
||||
*/
|
||||
private PluginManager manager;
|
||||
/**
|
||||
* The builder used to construct the parse trees.
|
||||
*/
|
||||
private TreeBuilder builder;
|
||||
/**
|
||||
* The reducer used to evaluate the tree.
|
||||
*/
|
||||
private NumberReducer reducer;
|
||||
|
||||
private Abacus abacus;
|
||||
/**
|
||||
* The last output by the calculator.
|
||||
*/
|
||||
@@ -130,15 +118,14 @@ public class Window extends JFrame implements PluginListener {
|
||||
* Action listener that causes the input to be evaluated.
|
||||
*/
|
||||
private ActionListener evaluateListener = (event) -> {
|
||||
if(builder == null) return;
|
||||
TreeNode parsedExpression = builder.fromString(inputField.getText());
|
||||
TreeNode parsedExpression = abacus.parseString(inputField.getText());
|
||||
if(parsedExpression == null){
|
||||
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
||||
return;
|
||||
}
|
||||
NumberInterface numberInterface = parsedExpression.reduce(reducer);
|
||||
if(numberInterface == null){
|
||||
lastOutputArea.setText(EVAL_ERR_STRING);;
|
||||
NumberInterface numberInterface = abacus.evaluateTree(parsedExpression);
|
||||
if(numberInterface == null) {
|
||||
lastOutputArea.setText(EVAL_ERR_STRING);
|
||||
return;
|
||||
}
|
||||
lastOutput = numberInterface.toString();
|
||||
@@ -159,13 +146,11 @@ public class Window extends JFrame implements PluginListener {
|
||||
|
||||
/**
|
||||
* Creates a new window with the given manager.
|
||||
* @param manager the manager to use.
|
||||
* @param abacus the calculator instance to interact with other components.
|
||||
*/
|
||||
public Window(PluginManager manager){
|
||||
public Window(Abacus abacus){
|
||||
this();
|
||||
this.manager = manager;
|
||||
manager.addListener(this);
|
||||
reducer = new NumberReducer(manager);
|
||||
this.abacus = abacus;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,23 +246,4 @@ public class Window extends JFrame implements PluginListener {
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onLoad(PluginManager manager) {
|
||||
builder = new TreeBuilder();
|
||||
for(String function : manager.getAllFunctions()){
|
||||
builder.registerFunction(function);
|
||||
}
|
||||
for(String operator : manager.getAllOperators()){
|
||||
Operator operatorObject = manager.operatorFor(operator);
|
||||
builder.registerOperator(operator,
|
||||
operatorObject.getAssociativity(),
|
||||
operatorObject.getType(),
|
||||
operatorObject.getPrecedence());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUnload(PluginManager manager) {
|
||||
builder = null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user