2017-07-24 10:04:00 -07:00
|
|
|
package org.nwapw.abacus;
|
|
|
|
|
2017-08-01 10:24:32 -07:00
|
|
|
import org.nwapw.abacus.config.Configuration;
|
2017-08-01 09:53:38 -07:00
|
|
|
import org.nwapw.abacus.fx.AbacusApplication;
|
2017-07-28 21:25:02 -07:00
|
|
|
import org.nwapw.abacus.number.NumberInterface;
|
2017-07-29 21:37:55 -07:00
|
|
|
import org.nwapw.abacus.parsing.LexerTokenizer;
|
|
|
|
import org.nwapw.abacus.parsing.ShuntingYardParser;
|
|
|
|
import org.nwapw.abacus.parsing.TreeBuilder;
|
2017-08-03 21:05:25 -07:00
|
|
|
import org.nwapw.abacus.plugin.NumberImplementation;
|
2017-07-25 21:52:23 -07:00
|
|
|
import org.nwapw.abacus.plugin.PluginManager;
|
2017-07-28 15:18:23 -07:00
|
|
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
2017-07-28 21:25:02 -07:00
|
|
|
import org.nwapw.abacus.tree.NumberReducer;
|
|
|
|
import org.nwapw.abacus.tree.TreeNode;
|
2017-07-25 21:11:36 -07:00
|
|
|
|
2017-07-28 21:37:47 -07:00
|
|
|
import java.io.File;
|
2017-07-25 21:11:36 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* The main calculator class. This is responsible
|
|
|
|
* for piecing together all of the components, allowing
|
|
|
|
* their interaction with each other.
|
|
|
|
*/
|
2017-07-29 21:37:55 -07:00
|
|
|
public class Abacus {
|
2017-07-24 10:04:00 -07:00
|
|
|
|
2017-08-04 13:21:28 -07:00
|
|
|
/**
|
|
|
|
* The default number implementation to be used if no other one is available / selected.
|
|
|
|
*/
|
2017-08-03 21:05:25 -07:00
|
|
|
public static final NumberImplementation DEFAULT_IMPLEMENTATION = StandardPlugin.IMPLEMENTATION_NAIVE;
|
2017-07-28 21:37:47 -07:00
|
|
|
/**
|
|
|
|
* The file used for saving and loading configuration.
|
|
|
|
*/
|
2017-07-28 22:17:22 -07:00
|
|
|
public static final File CONFIG_FILE = new File("config.toml");
|
2017-07-28 21:37:47 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* The plugin manager responsible for
|
|
|
|
* loading and unloading plugins,
|
|
|
|
* and getting functions from them.
|
|
|
|
*/
|
|
|
|
private PluginManager pluginManager;
|
|
|
|
/**
|
|
|
|
* The reducer used to evaluate the tree.
|
|
|
|
*/
|
|
|
|
private NumberReducer numberReducer;
|
2017-07-28 21:37:47 -07:00
|
|
|
/**
|
|
|
|
* The configuration loaded from a file.
|
|
|
|
*/
|
2017-08-01 10:24:32 -07:00
|
|
|
private Configuration configuration;
|
2017-07-29 21:37:55 -07:00
|
|
|
/**
|
|
|
|
* The tree builder used to construct a tree
|
|
|
|
* from a string.
|
|
|
|
*/
|
|
|
|
private TreeBuilder treeBuilder;
|
2017-07-25 21:11:36 -07:00
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* Creates a new instance of the Abacus calculator.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public Abacus() {
|
2017-08-02 19:06:16 -07:00
|
|
|
pluginManager = new PluginManager(this);
|
2017-07-28 21:25:02 -07:00
|
|
|
numberReducer = new NumberReducer(this);
|
2017-08-01 10:24:32 -07:00
|
|
|
configuration = new Configuration(CONFIG_FILE);
|
|
|
|
configuration.saveTo(CONFIG_FILE);
|
2017-07-29 21:37:55 -07:00
|
|
|
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
|
|
|
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
|
|
|
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
2017-07-25 21:11:36 -07:00
|
|
|
|
2017-07-29 21:37:55 -07:00
|
|
|
pluginManager.addListener(shuntingYardParser);
|
2017-08-05 16:21:02 -07:00
|
|
|
pluginManager.addListener(lexerTokenizer);
|
2017-07-28 21:25:02 -07:00
|
|
|
}
|
|
|
|
|
2017-07-30 21:11:32 -07:00
|
|
|
public static void main(String[] args) {
|
2017-08-01 09:53:38 -07:00
|
|
|
AbacusApplication.launch(AbacusApplication.class, args);
|
2017-07-30 21:11:32 -07:00
|
|
|
}
|
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* Gets the current tree builder.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 21:25:02 -07:00
|
|
|
* @return the main tree builder in this abacus instance.
|
|
|
|
*/
|
|
|
|
public TreeBuilder getTreeBuilder() {
|
|
|
|
return treeBuilder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current plugin manager,
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 21:25:02 -07:00
|
|
|
* @return the plugin manager in this abacus instance.
|
|
|
|
*/
|
|
|
|
public PluginManager getPluginManager() {
|
|
|
|
return pluginManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the reducer that is responsible for transforming
|
|
|
|
* an expression into a number.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 21:25:02 -07:00
|
|
|
* @return the number reducer in this abacus instance.
|
|
|
|
*/
|
|
|
|
public NumberReducer getNumberReducer() {
|
|
|
|
return numberReducer;
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:37:47 -07:00
|
|
|
/**
|
|
|
|
* Gets the configuration object associated with this instance.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 21:37:47 -07:00
|
|
|
* @return the configuration object.
|
|
|
|
*/
|
2017-08-01 10:24:32 -07:00
|
|
|
public Configuration getConfiguration() {
|
2017-07-28 21:37:47 -07:00
|
|
|
return configuration;
|
|
|
|
}
|
|
|
|
|
2017-07-28 21:25:02 -07:00
|
|
|
/**
|
|
|
|
* Parses a string into a tree structure using the main
|
|
|
|
* tree builder.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 21:25:02 -07:00
|
|
|
* @param input the input string to parse
|
|
|
|
* @return the resulting tree, null if the tree builder or the produced tree are null.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public TreeNode parseString(String input) {
|
2017-07-28 21:25:02 -07:00
|
|
|
return treeBuilder.fromString(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates the given tree using the main
|
|
|
|
* number reducer.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-28 21:25:02 -07:00
|
|
|
* @param tree the tree to reduce, must not be null.
|
|
|
|
* @return the resulting number, or null of the reduction failed.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public NumberInterface evaluateTree(TreeNode tree) {
|
2017-07-28 21:25:02 -07:00
|
|
|
return tree.reduce(numberReducer);
|
|
|
|
}
|
|
|
|
|
2017-07-30 14:59:20 -07:00
|
|
|
/**
|
|
|
|
* Creates a number from a string.
|
2017-07-30 21:11:32 -07:00
|
|
|
*
|
2017-07-30 14:59:20 -07:00
|
|
|
* @param numberString the string to create the number from.
|
|
|
|
* @return the resulting number.
|
|
|
|
*/
|
2017-07-30 21:11:32 -07:00
|
|
|
public NumberInterface numberFromString(String numberString) {
|
2017-08-03 21:05:25 -07:00
|
|
|
NumberImplementation toInstantiate =
|
|
|
|
pluginManager.numberImplementationFor(configuration.getNumberImplementation());
|
|
|
|
if (toInstantiate == null) toInstantiate = DEFAULT_IMPLEMENTATION;
|
2017-07-28 22:17:22 -07:00
|
|
|
|
2017-08-03 21:05:25 -07:00
|
|
|
return toInstantiate.instanceForString(numberString);
|
2017-07-28 22:17:22 -07:00
|
|
|
}
|
2017-07-24 10:04:00 -07:00
|
|
|
}
|