mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-26 08:35:20 +00:00
Compare commits
9 Commits
negatives
...
constant-p
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f02867a4e | |||
| 691118c206 | |||
| 819fff6391 | |||
|
|
9b71f9aaf4 | ||
|
|
cf953da40a | ||
|
|
27ad10c0f1 | ||
|
|
601c4fea55 | ||
|
|
52fbfd5134 | ||
|
|
b31151384d |
@@ -1,7 +1,6 @@
|
|||||||
package org.nwapw.abacus;
|
package org.nwapw.abacus;
|
||||||
|
|
||||||
import org.nwapw.abacus.config.Configuration;
|
import org.nwapw.abacus.config.ConfigurationObject;
|
||||||
import org.nwapw.abacus.fx.AbacusApplication;
|
|
||||||
import org.nwapw.abacus.number.NaiveNumber;
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.parsing.LexerTokenizer;
|
import org.nwapw.abacus.parsing.LexerTokenizer;
|
||||||
@@ -12,7 +11,9 @@ import org.nwapw.abacus.plugin.PluginManager;
|
|||||||
import org.nwapw.abacus.plugin.StandardPlugin;
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
import org.nwapw.abacus.tree.NumberReducer;
|
import org.nwapw.abacus.tree.NumberReducer;
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
import org.nwapw.abacus.window.Window;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
@@ -46,7 +47,7 @@ public class Abacus {
|
|||||||
/**
|
/**
|
||||||
* The configuration loaded from a file.
|
* The configuration loaded from a file.
|
||||||
*/
|
*/
|
||||||
private Configuration configuration;
|
private ConfigurationObject configuration;
|
||||||
/**
|
/**
|
||||||
* The tree builder used to construct a tree
|
* The tree builder used to construct a tree
|
||||||
* from a string.
|
* from a string.
|
||||||
@@ -59,8 +60,8 @@ public class Abacus {
|
|||||||
public Abacus() {
|
public Abacus() {
|
||||||
pluginManager = new PluginManager();
|
pluginManager = new PluginManager();
|
||||||
numberReducer = new NumberReducer(this);
|
numberReducer = new NumberReducer(this);
|
||||||
configuration = new Configuration(CONFIG_FILE);
|
configuration = new ConfigurationObject(CONFIG_FILE);
|
||||||
configuration.saveTo(CONFIG_FILE);
|
configuration.save(CONFIG_FILE);
|
||||||
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
LexerTokenizer lexerTokenizer = new LexerTokenizer();
|
||||||
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
ShuntingYardParser shuntingYardParser = new ShuntingYardParser(this);
|
||||||
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
treeBuilder = new TreeBuilder<>(lexerTokenizer, shuntingYardParser);
|
||||||
@@ -78,7 +79,13 @@ public class Abacus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
AbacusApplication.launch(AbacusApplication.class, args);
|
try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
|
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
new Window(new Abacus()).setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -114,7 +121,7 @@ public class Abacus {
|
|||||||
*
|
*
|
||||||
* @return the configuration object.
|
* @return the configuration object.
|
||||||
*/
|
*/
|
||||||
public Configuration getConfiguration() {
|
public ConfigurationObject getConfiguration() {
|
||||||
return configuration;
|
return configuration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,83 +1,14 @@
|
|||||||
package org.nwapw.abacus.config;
|
package org.nwapw.abacus.config;
|
||||||
|
|
||||||
import com.moandjiezana.toml.Toml;
|
|
||||||
import com.moandjiezana.toml.TomlWriter;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The configuration object that stores
|
* Serializable class that will be used to load TOML
|
||||||
* options that the user can change.
|
* configurations.
|
||||||
*/
|
*/
|
||||||
public class Configuration {
|
public class Configuration {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The TOML writer used to write this configuration to a file.
|
* The type of number this calculator should use.
|
||||||
*/
|
*/
|
||||||
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
public String numberType;
|
||||||
/**
|
|
||||||
* The TOML reader used to load this config from a file.
|
|
||||||
*/
|
|
||||||
private static final Toml TOML_READER = new Toml();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The implementation of the number that should be used.
|
|
||||||
*/
|
|
||||||
private String numberImplementation = "naive";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new configuration with the given values.
|
|
||||||
* @param numberImplementation the number implementation, like "naive" or "precise"
|
|
||||||
*/
|
|
||||||
public Configuration(String numberImplementation){
|
|
||||||
this.numberImplementation = numberImplementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Loads a configuration from a given file, keeping non-specified fields default.
|
|
||||||
* @param fromFile the file to load from.
|
|
||||||
*/
|
|
||||||
public Configuration(File fromFile){
|
|
||||||
if(!fromFile.exists()) return;
|
|
||||||
copyFrom(TOML_READER.read(fromFile).to(Configuration.class));
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Copies the values from the given configuration into this one.
|
|
||||||
* @param otherConfiguration the configuration to copy from.
|
|
||||||
*/
|
|
||||||
public void copyFrom(Configuration otherConfiguration){
|
|
||||||
this.numberImplementation = otherConfiguration.numberImplementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Saves this configuration to the given file, creating
|
|
||||||
* any directories that do not exist.
|
|
||||||
* @param file the file to save to.
|
|
||||||
*/
|
|
||||||
public void saveTo(File file){
|
|
||||||
if(file.getParentFile() != null) file.getParentFile().mkdirs();
|
|
||||||
try {
|
|
||||||
TOML_WRITER.write(this, file);
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the number implementation from this configuration.
|
|
||||||
* @return the number implementation.
|
|
||||||
*/
|
|
||||||
public String getNumberImplementation() {
|
|
||||||
return numberImplementation;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the number implementation for the configuration
|
|
||||||
* @param numberImplementation the number implementation.
|
|
||||||
*/
|
|
||||||
public void setNumberImplementation(String numberImplementation) {
|
|
||||||
this.numberImplementation = numberImplementation;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
111
src/main/java/org/nwapw/abacus/config/ConfigurationObject.java
Normal file
111
src/main/java/org/nwapw/abacus/config/ConfigurationObject.java
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
package org.nwapw.abacus.config;
|
||||||
|
|
||||||
|
import com.moandjiezana.toml.Toml;
|
||||||
|
import com.moandjiezana.toml.TomlWriter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A configuration object, which essentially
|
||||||
|
* manages saving, loading, and getting values
|
||||||
|
* from the configuration. While Configuration is
|
||||||
|
* the data model, this is the interface with it.
|
||||||
|
*/
|
||||||
|
public class ConfigurationObject {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The writer used to store the configuration.
|
||||||
|
*/
|
||||||
|
private static final TomlWriter TOML_WRITER = new TomlWriter();
|
||||||
|
/**
|
||||||
|
* The configuration instance being modeled.
|
||||||
|
*/
|
||||||
|
private Configuration configuration;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new configuration object with the given config.
|
||||||
|
*
|
||||||
|
* @param config the config to use.
|
||||||
|
*/
|
||||||
|
public ConfigurationObject(Configuration config) {
|
||||||
|
setup(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a configuration object by attempting to
|
||||||
|
* load a config from the given path, using the
|
||||||
|
* default configuration otherwise.
|
||||||
|
*
|
||||||
|
* @param path the path to attempt to load.
|
||||||
|
*/
|
||||||
|
public ConfigurationObject(File path) {
|
||||||
|
Configuration config;
|
||||||
|
if (!path.exists()) {
|
||||||
|
config = getDefaultConfig();
|
||||||
|
} else {
|
||||||
|
Toml parse = new Toml();
|
||||||
|
parse.read(path);
|
||||||
|
config = parse.to(Configuration.class);
|
||||||
|
}
|
||||||
|
setup(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new configuration object with the
|
||||||
|
* default configuration.
|
||||||
|
*/
|
||||||
|
public ConfigurationObject() {
|
||||||
|
setup(getDefaultConfig());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets up the ConfigurationObject.
|
||||||
|
* different constructors do different things,
|
||||||
|
* but they all lead here.
|
||||||
|
*
|
||||||
|
* @param configuration the configuration to set up with.
|
||||||
|
*/
|
||||||
|
private void setup(Configuration configuration) {
|
||||||
|
this.configuration = configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a default configuration.
|
||||||
|
*
|
||||||
|
* @return the newly created default configuration.
|
||||||
|
*/
|
||||||
|
private Configuration getDefaultConfig() {
|
||||||
|
configuration = new Configuration();
|
||||||
|
configuration.numberType = "naive";
|
||||||
|
return configuration;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the implementation the user has requested to
|
||||||
|
* represent their numbers.
|
||||||
|
*
|
||||||
|
* @return the implementation name.
|
||||||
|
*/
|
||||||
|
public String getNumberImplementation() {
|
||||||
|
return configuration.numberType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Saves the ConfigurationObject to the given file.
|
||||||
|
*
|
||||||
|
* @param toFile the file to save ot.
|
||||||
|
* @return true if the save succeed, false if otherwise.
|
||||||
|
*/
|
||||||
|
public boolean save(File toFile) {
|
||||||
|
if (toFile.getParentFile() != null) toFile.getParentFile().mkdirs();
|
||||||
|
try {
|
||||||
|
TOML_WRITER.write(configuration, toFile);
|
||||||
|
return true;
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -4,5 +4,5 @@ package org.nwapw.abacus.function;
|
|||||||
* The type of an operator, describing how it should behave.
|
* The type of an operator, describing how it should behave.
|
||||||
*/
|
*/
|
||||||
public enum OperatorType {
|
public enum OperatorType {
|
||||||
BINARY_INFIX, UNARY_POSTFIX, UNARY_PREFIX
|
BINARY_INFIX, UNARY_POSTFIX
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +0,0 @@
|
|||||||
package org.nwapw.abacus.fx;
|
|
||||||
|
|
||||||
import javafx.application.Application;
|
|
||||||
import javafx.fxml.FXMLLoader;
|
|
||||||
import javafx.scene.Parent;
|
|
||||||
import javafx.scene.Scene;
|
|
||||||
import javafx.stage.Stage;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The main application class for JavaFX responsible for loading
|
|
||||||
* and displaying the fxml file.
|
|
||||||
*/
|
|
||||||
public class AbacusApplication extends Application {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start(Stage primaryStage) throws Exception {
|
|
||||||
Parent parent = FXMLLoader.load(getClass().getResource("/abacus.fxml"));
|
|
||||||
Scene mainScene = new Scene(parent, 320, 480);
|
|
||||||
primaryStage.setScene(mainScene);
|
|
||||||
primaryStage.setTitle("Abacus");
|
|
||||||
primaryStage.show();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,110 +0,0 @@
|
|||||||
package org.nwapw.abacus.fx;
|
|
||||||
|
|
||||||
import javafx.collections.FXCollections;
|
|
||||||
import javafx.collections.ObservableList;
|
|
||||||
import javafx.fxml.FXML;
|
|
||||||
import javafx.scene.control.*;
|
|
||||||
import javafx.scene.text.Text;
|
|
||||||
import javafx.util.Callback;
|
|
||||||
import org.nwapw.abacus.Abacus;
|
|
||||||
import org.nwapw.abacus.number.NumberInterface;
|
|
||||||
import org.nwapw.abacus.tree.TreeNode;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The controller for the abacus FX UI, responsible
|
|
||||||
* for all the user interaction.
|
|
||||||
*/
|
|
||||||
public class AbacusController {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Constant string that is displayed if the text could not be lexed or parsed.
|
|
||||||
*/
|
|
||||||
private static final String ERR_SYNTAX = "Syntax Error";
|
|
||||||
/**
|
|
||||||
* Constant string that is displayed if the tree could not be reduced.
|
|
||||||
*/
|
|
||||||
private static final String ERR_EVAL = "Evaluation Error";
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
private TableView<HistoryModel> historyTable;
|
|
||||||
@FXML
|
|
||||||
private TableColumn<HistoryModel, String> inputColumn;
|
|
||||||
@FXML
|
|
||||||
private TableColumn<HistoryModel, String> parsedColumn;
|
|
||||||
@FXML
|
|
||||||
private TableColumn<HistoryModel, String> outputColumn;
|
|
||||||
@FXML
|
|
||||||
private Text outputText;
|
|
||||||
@FXML
|
|
||||||
private TextField inputField;
|
|
||||||
@FXML
|
|
||||||
private Button inputButton;
|
|
||||||
@FXML
|
|
||||||
private ComboBox<String> numberImplementationBox;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The list of history entries, created by the users.
|
|
||||||
*/
|
|
||||||
private ObservableList<HistoryModel> historyData;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The abacus instance used for calculations and all
|
|
||||||
* other main processing code.
|
|
||||||
*/
|
|
||||||
private ObservableList<String> numberImplementationOptions;
|
|
||||||
|
|
||||||
private Abacus abacus;
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
public void initialize(){
|
|
||||||
Callback<TableColumn<HistoryModel, String>, TableCell<HistoryModel, String>> cellFactory =
|
|
||||||
param -> new CopyableCell<>();
|
|
||||||
|
|
||||||
historyData = FXCollections.observableArrayList();
|
|
||||||
historyTable.setItems(historyData);
|
|
||||||
numberImplementationOptions = FXCollections.observableArrayList();
|
|
||||||
numberImplementationBox.setItems(numberImplementationOptions);
|
|
||||||
numberImplementationBox.valueProperty().addListener((observable, oldValue, newValue)
|
|
||||||
-> {
|
|
||||||
abacus.getConfiguration().setNumberImplementation(newValue);
|
|
||||||
abacus.getConfiguration().saveTo(Abacus.CONFIG_FILE);
|
|
||||||
});
|
|
||||||
historyTable.getSelectionModel().setCellSelectionEnabled(true);
|
|
||||||
inputColumn.setCellFactory(cellFactory);
|
|
||||||
inputColumn.setCellValueFactory(cell -> cell.getValue().inputProperty());
|
|
||||||
parsedColumn.setCellFactory(cellFactory);
|
|
||||||
parsedColumn.setCellValueFactory(cell -> cell.getValue().parsedProperty());
|
|
||||||
outputColumn.setCellFactory(cellFactory);
|
|
||||||
outputColumn.setCellValueFactory(cell -> cell.getValue().outputProperty());
|
|
||||||
|
|
||||||
abacus = new Abacus();
|
|
||||||
numberImplementationOptions.addAll(abacus.getPluginManager().getAllNumbers());
|
|
||||||
String actualImplementation = abacus.getConfiguration().getNumberImplementation();
|
|
||||||
String toSelect = (numberImplementationOptions.contains(actualImplementation)) ? actualImplementation : "naive";
|
|
||||||
numberImplementationBox.getSelectionModel().select(toSelect);
|
|
||||||
}
|
|
||||||
|
|
||||||
@FXML
|
|
||||||
private void performCalculation(){
|
|
||||||
inputButton.setDisable(true);
|
|
||||||
TreeNode constructedTree = abacus.parseString(inputField.getText());
|
|
||||||
if(constructedTree == null){
|
|
||||||
outputText.setText(ERR_SYNTAX);
|
|
||||||
inputButton.setDisable(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
NumberInterface evaluatedNumber = abacus.evaluateTree(constructedTree);
|
|
||||||
if(evaluatedNumber == null){
|
|
||||||
outputText.setText(ERR_EVAL);
|
|
||||||
inputButton.setDisable(false);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
outputText.setText(evaluatedNumber.toString());
|
|
||||||
historyData.add(new HistoryModel(inputField.getText(), constructedTree.toString(), evaluatedNumber.toString()));
|
|
||||||
|
|
||||||
inputButton.setDisable(false);
|
|
||||||
inputField.setText("");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
package org.nwapw.abacus.fx;
|
|
||||||
|
|
||||||
import javafx.scene.control.TableCell;
|
|
||||||
import javafx.scene.input.MouseEvent;
|
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.awt.datatransfer.StringSelection;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A cell that copies its value to the clipboard
|
|
||||||
* when double clicked.
|
|
||||||
* @param <S> The type of the table view generic type.
|
|
||||||
* @param <T> The type of the value contained in the cell.
|
|
||||||
*/
|
|
||||||
public class CopyableCell<S, T> extends TableCell<S, T> {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new copyable cell.
|
|
||||||
*/
|
|
||||||
public CopyableCell(){
|
|
||||||
addEventFilter(MouseEvent.MOUSE_CLICKED, event -> {
|
|
||||||
if(event.getClickCount() == 2){
|
|
||||||
Toolkit.getDefaultToolkit().getSystemClipboard()
|
|
||||||
.setContents(new StringSelection(getText()), null);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void updateItem(T item, boolean empty) {
|
|
||||||
super.updateItem(item, empty);
|
|
||||||
setText((empty || item == null) ? null : item.toString());
|
|
||||||
setGraphic(null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,87 +0,0 @@
|
|||||||
package org.nwapw.abacus.fx;
|
|
||||||
|
|
||||||
import javafx.beans.property.SimpleStringProperty;
|
|
||||||
import javafx.beans.property.StringProperty;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The data model used for storing history entries.
|
|
||||||
*/
|
|
||||||
public class HistoryModel {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The property used for displaying the column
|
|
||||||
* for the user input.
|
|
||||||
*/
|
|
||||||
private final StringProperty input;
|
|
||||||
/**
|
|
||||||
* The property used for displaying the column
|
|
||||||
* that contains the parsed input.
|
|
||||||
*/
|
|
||||||
private final StringProperty parsed;
|
|
||||||
/**
|
|
||||||
* The property used for displaying the column
|
|
||||||
* that contains the program output.
|
|
||||||
*/
|
|
||||||
private final StringProperty output;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Creates a new history model with the given variables.
|
|
||||||
* @param input the user input
|
|
||||||
* @param parsed the parsed input
|
|
||||||
* @param output the program output.
|
|
||||||
*/
|
|
||||||
public HistoryModel(String input, String parsed, String output){
|
|
||||||
this.input = new SimpleStringProperty();
|
|
||||||
this.parsed = new SimpleStringProperty();
|
|
||||||
this.output = new SimpleStringProperty();
|
|
||||||
this.input.setValue(input);
|
|
||||||
this.parsed.setValue(parsed);
|
|
||||||
this.output.setValue(output);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the input property.
|
|
||||||
* @return the input property.
|
|
||||||
*/
|
|
||||||
public StringProperty inputProperty() {
|
|
||||||
return input;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Gets the input.
|
|
||||||
* @return the input.
|
|
||||||
*/
|
|
||||||
public String getInput() {
|
|
||||||
return input.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the parsed input property.
|
|
||||||
* @return the parsed input property.
|
|
||||||
*/
|
|
||||||
public StringProperty parsedProperty() {
|
|
||||||
return parsed;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Gets the parsed input.
|
|
||||||
* @return the parsed input.
|
|
||||||
*/
|
|
||||||
public String getParsed() {
|
|
||||||
return parsed.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the output property.
|
|
||||||
* @return the output property.
|
|
||||||
*/
|
|
||||||
public StringProperty outputProperty() {
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Gets the program output.
|
|
||||||
* @return the output.
|
|
||||||
*/
|
|
||||||
public String getOutput() {
|
|
||||||
return output.get();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -94,8 +94,23 @@ public class NaiveNumber implements NumberInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int ceiling() {
|
public NumberInterface ceiling() {
|
||||||
return (int) Math.ceil(value);
|
return new NaiveNumber(Math.ceil(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface floor() {
|
||||||
|
return new NaiveNumber(Math.floor(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface fractionalPart() {
|
||||||
|
return new NaiveNumber(value - Math.floor(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int intValue() {
|
||||||
|
return (int)value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -83,7 +83,26 @@ public interface NumberInterface {
|
|||||||
* Returns the least integer greater than or equal to the number.
|
* Returns the least integer greater than or equal to the number.
|
||||||
* @return the least integer >= the number, if int can hold the value.
|
* @return the least integer >= the number, if int can hold the value.
|
||||||
*/
|
*/
|
||||||
int ceiling();
|
NumberInterface ceiling();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the greatest integer less than or equal to the number.
|
||||||
|
* @return the greatest int >= the number, if int can hold the value.
|
||||||
|
*/
|
||||||
|
NumberInterface floor();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the fractional part of the number.
|
||||||
|
* @return the fractional part of the number.
|
||||||
|
*/
|
||||||
|
NumberInterface fractionalPart();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the integer representation of this number, discarding any fractional part,
|
||||||
|
* if int can hold the value.
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
int intValue();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Promotes this class to another number class.
|
* Promotes this class to another number class.
|
||||||
|
|||||||
@@ -1,12 +1,9 @@
|
|||||||
package org.nwapw.abacus.number;
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.math.MathContext;
|
||||||
import java.math.RoundingMode;
|
import java.math.RoundingMode;
|
||||||
|
|
||||||
/**
|
|
||||||
* A number that uses a BigDecimal to store its value,
|
|
||||||
* leading to infinite possible precision.
|
|
||||||
*/
|
|
||||||
public class PreciseNumber implements NumberInterface {
|
public class PreciseNumber implements NumberInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -99,8 +96,38 @@ public class PreciseNumber implements NumberInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int ceiling() {
|
public NumberInterface ceiling() {
|
||||||
return (int) Math.ceil(value.doubleValue());
|
String str = value.toPlainString();
|
||||||
|
int decimalIndex = str.indexOf('.');
|
||||||
|
if(decimalIndex != -1){
|
||||||
|
return this.floor().add(ONE);
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface floor() {
|
||||||
|
String str = value.toPlainString();
|
||||||
|
int decimalIndex = str.indexOf('.');
|
||||||
|
if(decimalIndex != -1){
|
||||||
|
return new PreciseNumber(str.substring(0, decimalIndex));
|
||||||
|
}
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface fractionalPart() {
|
||||||
|
String str = value.toPlainString();
|
||||||
|
int decimalIndex = str.indexOf('.');
|
||||||
|
if(decimalIndex != -1){
|
||||||
|
return new PreciseNumber(str.substring(decimalIndex + 1));
|
||||||
|
}
|
||||||
|
return ZERO;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int intValue() {
|
||||||
|
return value.intValue();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -55,12 +55,9 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
public List<Match<TokenType>> intoPostfix(List<Match<TokenType>> from) {
|
public List<Match<TokenType>> intoPostfix(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<>();
|
||||||
TokenType previousType;
|
|
||||||
TokenType matchType = null;
|
|
||||||
while (!from.isEmpty()) {
|
while (!from.isEmpty()) {
|
||||||
Match<TokenType> match = from.remove(0);
|
Match<TokenType> match = from.remove(0);
|
||||||
previousType = matchType;
|
TokenType matchType = match.getType();
|
||||||
matchType = match.getType();
|
|
||||||
if (matchType == TokenType.NUM) {
|
if (matchType == TokenType.NUM) {
|
||||||
output.add(match);
|
output.add(match);
|
||||||
} else if (matchType == TokenType.FUNCTION) {
|
} else if (matchType == TokenType.FUNCTION) {
|
||||||
@@ -77,13 +74,7 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tokenString.equals("-") && (previousType == null || previousType == TokenType.OP ||
|
while (!tokenStack.empty()) {
|
||||||
previousType == TokenType.OPEN_PARENTH)){
|
|
||||||
from.add(0, new Match<>("`", TokenType.OP));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!tokenStack.empty() && type == OperatorType.BINARY_INFIX) {
|
|
||||||
Match<TokenType> otherMatch = tokenStack.peek();
|
Match<TokenType> otherMatch = tokenStack.peek();
|
||||||
TokenType otherMatchType = otherMatch.getType();
|
TokenType otherMatchType = otherMatch.getType();
|
||||||
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
if (!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break;
|
||||||
@@ -112,8 +103,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
}
|
}
|
||||||
while (!tokenStack.empty()) {
|
while (!tokenStack.empty()) {
|
||||||
Match<TokenType> match = tokenStack.peek();
|
Match<TokenType> match = tokenStack.peek();
|
||||||
TokenType newMatchType = match.getType();
|
TokenType matchType = match.getType();
|
||||||
if (!(newMatchType == TokenType.OP || newMatchType == TokenType.FUNCTION)) return null;
|
if (!(matchType == TokenType.OP || matchType == TokenType.FUNCTION)) return null;
|
||||||
output.add(tokenStack.pop());
|
output.add(tokenStack.pop());
|
||||||
}
|
}
|
||||||
return output;
|
return output;
|
||||||
@@ -136,11 +127,11 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
|||||||
TreeNode right = constructRecursive(matches);
|
TreeNode right = constructRecursive(matches);
|
||||||
TreeNode left = constructRecursive(matches);
|
TreeNode left = constructRecursive(matches);
|
||||||
if (left == null || right == null) return null;
|
if (left == null || right == null) return null;
|
||||||
else return new BinaryNode(operator, left, right);
|
else return new BinaryInfixNode(operator, left, right);
|
||||||
} else {
|
} else {
|
||||||
TreeNode applyTo = constructRecursive(matches);
|
TreeNode applyTo = constructRecursive(matches);
|
||||||
if (applyTo == null) return null;
|
if (applyTo == null) return null;
|
||||||
else return new UnaryNode(operator, applyTo);
|
else return new UnaryPrefixNode(operator, applyTo);
|
||||||
}
|
}
|
||||||
} else if (matchType == TokenType.NUM) {
|
} else if (matchType == TokenType.NUM) {
|
||||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ public abstract class Plugin {
|
|||||||
* A hash map of operators mapped to their string names.
|
* A hash map of operators mapped to their string names.
|
||||||
*/
|
*/
|
||||||
private Map<String, Class<? extends NumberInterface>> numbers;
|
private Map<String, Class<? extends NumberInterface>> numbers;
|
||||||
|
/**
|
||||||
|
* A hash map of constant providers for each number type.
|
||||||
|
*/
|
||||||
|
private Map<Class<?>, java.util.function.Function<String, NumberInterface>> constantProviders;
|
||||||
/**
|
/**
|
||||||
* 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,
|
||||||
@@ -52,6 +56,7 @@ public abstract class Plugin {
|
|||||||
functions = new HashMap<>();
|
functions = new HashMap<>();
|
||||||
operators = new HashMap<>();
|
operators = new HashMap<>();
|
||||||
numbers = new HashMap<>();
|
numbers = new HashMap<>();
|
||||||
|
constantProviders = new HashMap<>();
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +87,14 @@ public abstract class Plugin {
|
|||||||
return numbers.keySet();
|
return numbers.keySet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of all constant providers provided by this plugin.
|
||||||
|
* @return the list of constant providers.
|
||||||
|
*/
|
||||||
|
public final Set<Class<?>> providedConstantProviders() {
|
||||||
|
return constantProviders.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Gets a function under the given function name.
|
* Gets a function under the given function name.
|
||||||
*
|
*
|
||||||
@@ -112,6 +125,16 @@ public abstract class Plugin {
|
|||||||
return numbers.get(numberName);
|
return numbers.get(numberName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the constant provider for the given class.
|
||||||
|
*
|
||||||
|
* @param pluginClass the class for which to provide constants.
|
||||||
|
* @return the provider, or null, if the plugin doesn't provide it.
|
||||||
|
*/
|
||||||
|
public final java.util.function.Function<String, NumberInterface> getConstantProvider(Class<?> pluginClass){
|
||||||
|
return constantProviders.get(pluginClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enables the function, loading the necessary instances
|
* Enables the function, loading the necessary instances
|
||||||
* of functions.
|
* of functions.
|
||||||
@@ -131,6 +154,8 @@ public abstract class Plugin {
|
|||||||
onDisable();
|
onDisable();
|
||||||
functions.clear();
|
functions.clear();
|
||||||
operators.clear();
|
operators.clear();
|
||||||
|
numbers.clear();
|
||||||
|
constantProviders.clear();
|
||||||
enabled = false;
|
enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -170,6 +195,19 @@ public abstract class Plugin {
|
|||||||
numbers.put(name, toRegister);
|
numbers.put(name, toRegister);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To be used in load(). Registers a constant provider
|
||||||
|
* with the plugin internally, which makes it possible
|
||||||
|
* for the calculations to look up constants for each different
|
||||||
|
* number type.
|
||||||
|
* @param providerFor the class the provider works with.
|
||||||
|
* @param constantProvider the provider to register.
|
||||||
|
*/
|
||||||
|
protected final void registerConstantProvider(Class<?> providerFor,
|
||||||
|
java.util.function.Function<String, NumberInterface> constantProvider) {
|
||||||
|
constantProviders.put(providerFor, constantProvider);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Searches the PluginManager for the given function name.
|
* Searches the PluginManager for the given function name.
|
||||||
* This can be used by the plugins internally in order to call functions
|
* This can be used by the plugins internally in order to call functions
|
||||||
@@ -194,6 +232,30 @@ public abstract class Plugin {
|
|||||||
return manager.operatorFor(name);
|
return manager.operatorFor(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the PluginManager for the given number implementation.
|
||||||
|
* This can be used by the plugins internally in order to
|
||||||
|
* find classes by name that they do not provide.
|
||||||
|
*
|
||||||
|
* @param name the name for which to search
|
||||||
|
* @return the resulting number class.
|
||||||
|
*/
|
||||||
|
protected final Class<? extends NumberInterface> numberFor(String name) {
|
||||||
|
return manager.numberFor(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the PluginManager for the given constant provider.
|
||||||
|
* This can be used by the plugins internally in order
|
||||||
|
* to find constant providers for number provider they do not provide.
|
||||||
|
*
|
||||||
|
* @param forClass the class for which to get a generator for.
|
||||||
|
* @return the resulting generator
|
||||||
|
*/
|
||||||
|
protected final java.util.function.Function<String, NumberInterface> constantProviderFor(Class<?> forClass){
|
||||||
|
return manager.constantProviderFor(forClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Abstract method to be overridden by plugin implementation, in which the plugins
|
* Abstract method to be overridden by plugin implementation, in which the plugins
|
||||||
* are supposed to register the functions they provide and do any other
|
* are supposed to register the functions they provide and do any other
|
||||||
|
|||||||
@@ -36,6 +36,11 @@ public class PluginManager {
|
|||||||
* been cached, that is, found in a plugin and returned.
|
* been cached, that is, found in a plugin and returned.
|
||||||
*/
|
*/
|
||||||
private Map<String, Class<? extends NumberInterface>> cachedNumbers;
|
private Map<String, Class<? extends NumberInterface>> cachedNumbers;
|
||||||
|
/**
|
||||||
|
* List of registered constant providers for every
|
||||||
|
* number class.
|
||||||
|
*/
|
||||||
|
private Map<Class<?>, java.util.function.Function<String, NumberInterface>> cachedConstantProviders;
|
||||||
/**
|
/**
|
||||||
* List of all functions loaded by the plugins.
|
* List of all functions loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
@@ -48,6 +53,10 @@ public class PluginManager {
|
|||||||
* List of all numbers loaded by the plugins.
|
* List of all numbers loaded by the plugins.
|
||||||
*/
|
*/
|
||||||
private Set<String> allNumbers;
|
private Set<String> allNumbers;
|
||||||
|
/**
|
||||||
|
* List of all the constant providers loaded by the plugins.
|
||||||
|
*/
|
||||||
|
private Set<Class<?>> allConstantProviders;
|
||||||
/**
|
/**
|
||||||
* The list of plugin listeners attached to this instance.
|
* The list of plugin listeners attached to this instance.
|
||||||
*/
|
*/
|
||||||
@@ -62,9 +71,11 @@ public class PluginManager {
|
|||||||
cachedFunctions = new HashMap<>();
|
cachedFunctions = new HashMap<>();
|
||||||
cachedOperators = new HashMap<>();
|
cachedOperators = new HashMap<>();
|
||||||
cachedNumbers = new HashMap<>();
|
cachedNumbers = new HashMap<>();
|
||||||
|
cachedConstantProviders = new HashMap<>();
|
||||||
allFunctions = new HashSet<>();
|
allFunctions = new HashSet<>();
|
||||||
allOperators = new HashSet<>();
|
allOperators = new HashSet<>();
|
||||||
allNumbers = new HashSet<>();
|
allNumbers = new HashSet<>();
|
||||||
|
allConstantProviders = new HashSet<>();
|
||||||
listeners = new HashSet<>();
|
listeners = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,12 +91,13 @@ public class PluginManager {
|
|||||||
* @param getFunction the function to get the T value under the given name
|
* @param getFunction the function to get the T value under the given name
|
||||||
* @param name the name to search for
|
* @param name the name to search for
|
||||||
* @param <T> the type of element being search
|
* @param <T> the type of element being search
|
||||||
|
* @param <K> the type of key that the cache is indexed by.
|
||||||
* @return the retrieved element, or null if it was not found.
|
* @return the retrieved element, or null if it was not found.
|
||||||
*/
|
*/
|
||||||
private static <T> T searchCached(Collection<Plugin> plugins, Map<String, T> cache,
|
private static <T, K> T searchCached(Collection<Plugin> plugins, Map<K, T> cache,
|
||||||
java.util.function.Function<Plugin, Set<String>> setFunction,
|
java.util.function.Function<Plugin, Set<K>> setFunction,
|
||||||
java.util.function.BiFunction<Plugin, String, T> getFunction,
|
java.util.function.BiFunction<Plugin, K, T> getFunction,
|
||||||
String name) {
|
K name) {
|
||||||
if (cache.containsKey(name)) return cache.get(name);
|
if (cache.containsKey(name)) return cache.get(name);
|
||||||
|
|
||||||
T loadedValue = null;
|
T loadedValue = null;
|
||||||
@@ -130,6 +142,15 @@ public class PluginManager {
|
|||||||
return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name);
|
return searchCached(plugins, cachedNumbers, Plugin::providedNumbers, Plugin::getNumber, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the constant provider for the given class.
|
||||||
|
* @param forClass the class to get the provider for.
|
||||||
|
* @return the provider.
|
||||||
|
*/
|
||||||
|
public java.util.function.Function<String, NumberInterface> constantProviderFor(Class<?> forClass){
|
||||||
|
return searchCached(plugins, cachedConstantProviders, Plugin::providedConstantProviders, Plugin::getConstantProvider, forClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds an instance of Plugin that already has been instantiated.
|
* Adds an instance of Plugin that already has been instantiated.
|
||||||
*
|
*
|
||||||
@@ -165,6 +186,7 @@ public class PluginManager {
|
|||||||
allFunctions.addAll(plugin.providedFunctions());
|
allFunctions.addAll(plugin.providedFunctions());
|
||||||
allOperators.addAll(plugin.providedOperators());
|
allOperators.addAll(plugin.providedOperators());
|
||||||
allNumbers.addAll(plugin.providedNumbers());
|
allNumbers.addAll(plugin.providedNumbers());
|
||||||
|
allConstantProviders.addAll(plugin.providedConstantProviders());
|
||||||
}
|
}
|
||||||
listeners.forEach(e -> e.onLoad(this));
|
listeners.forEach(e -> e.onLoad(this));
|
||||||
}
|
}
|
||||||
@@ -177,6 +199,7 @@ public class PluginManager {
|
|||||||
allFunctions.clear();
|
allFunctions.clear();
|
||||||
allOperators.clear();
|
allOperators.clear();
|
||||||
allNumbers.clear();
|
allNumbers.clear();
|
||||||
|
allConstantProviders.clear();
|
||||||
listeners.forEach(e -> e.onUnload(this));
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,6 +238,14 @@ public class PluginManager {
|
|||||||
return allNumbers;
|
return allNumbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the constant providers loaded by the Plugin Manager.
|
||||||
|
* @return the set of all constant providers that were loaded.
|
||||||
|
*/
|
||||||
|
public Set<Class<?>> getAllConstantProviders() {
|
||||||
|
return allConstantProviders;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a plugin change listener to this plugin manager.
|
* Adds a plugin change listener to this plugin manager.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import org.nwapw.abacus.number.NaiveNumber;
|
|||||||
import org.nwapw.abacus.number.NumberInterface;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
import org.nwapw.abacus.number.PreciseNumber;
|
import org.nwapw.abacus.number.PreciseNumber;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.function.BiFunction;
|
import java.util.function.BiFunction;
|
||||||
@@ -19,6 +20,7 @@ import java.util.function.BiFunction;
|
|||||||
public class StandardPlugin extends Plugin {
|
public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
private static HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> factorialLists = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
private static HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>> factorialLists = new HashMap<Class<? extends NumberInterface>, ArrayList<NumberInterface>>();
|
||||||
|
static HashMap<Class<? extends NumberInterface>, NumberInterface> piValues = new HashMap<Class<? extends NumberInterface>, NumberInterface>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The addition operator, +
|
* The addition operator, +
|
||||||
@@ -52,20 +54,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
return params[0].subtract(params[1]);
|
return params[0].subtract(params[1]);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
/**
|
|
||||||
* The negation operator, -
|
|
||||||
*/
|
|
||||||
public static final Operator OP_NEGATE = new Operator(OperatorAssociativity.LEFT, OperatorType.UNARY_PREFIX, 0, new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(NumberInterface[] params) {
|
|
||||||
return params.length == 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected NumberInterface applyInternal(NumberInterface[] params) {
|
|
||||||
return params[0].negate();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
/**
|
/**
|
||||||
* The multiplication operator, *
|
* The multiplication operator, *
|
||||||
*/
|
*/
|
||||||
@@ -186,7 +174,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
//right and left refer to lhs and rhs in the above inequality.
|
//right and left refer to lhs and rhs in the above inequality.
|
||||||
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
NumberInterface sum = NaiveNumber.ONE.promoteTo(params[0].getClass());
|
||||||
NumberInterface nextNumerator = params[0];
|
NumberInterface nextNumerator = params[0];
|
||||||
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling())), right = maxError;
|
NumberInterface left = params[0].multiply((new NaiveNumber(3)).promoteTo(params[0].getClass()).intPow(params[0].ceiling().intValue())), right = maxError;
|
||||||
do{
|
do{
|
||||||
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
sum = sum.add(nextNumerator.divide(factorial(params[0].getClass(), n+1)));
|
||||||
n++;
|
n++;
|
||||||
@@ -294,6 +282,32 @@ public class StandardPlugin extends Plugin {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The sine function (the argument is interpreted in radians).
|
||||||
|
*/
|
||||||
|
public static final Function FUNCTION_SIN = new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
NumberInterface pi = getPi(params[0].getClass());
|
||||||
|
NumberInterface twoPi = pi.multiply(new NaiveNumber(2).promoteTo(pi.getClass()));
|
||||||
|
NumberInterface theta = getSmallAngle(params[0]);
|
||||||
|
//System.out.println(theta);
|
||||||
|
if(theta.compareTo(pi.multiply(new NaiveNumber(1.5).promoteTo(twoPi.getClass()))) >= 0){
|
||||||
|
theta = theta.subtract(twoPi);
|
||||||
|
}
|
||||||
|
else if(theta.compareTo(pi.divide(new NaiveNumber(2).promoteTo(pi.getClass()))) > 0){
|
||||||
|
theta = pi.subtract(theta);
|
||||||
|
}
|
||||||
|
//System.out.println(theta);
|
||||||
|
return sinTaylor(theta);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
public StandardPlugin(PluginManager manager) {
|
public StandardPlugin(PluginManager manager) {
|
||||||
super(manager);
|
super(manager);
|
||||||
}
|
}
|
||||||
@@ -331,7 +345,6 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
registerOperator("+", OP_ADD);
|
registerOperator("+", OP_ADD);
|
||||||
registerOperator("-", OP_SUBTRACT);
|
registerOperator("-", OP_SUBTRACT);
|
||||||
registerOperator("`", OP_NEGATE);
|
|
||||||
registerOperator("*", OP_MULTIPLY);
|
registerOperator("*", OP_MULTIPLY);
|
||||||
registerOperator("/", OP_DIVIDE);
|
registerOperator("/", OP_DIVIDE);
|
||||||
registerOperator("^", OP_CARET);
|
registerOperator("^", OP_CARET);
|
||||||
@@ -341,6 +354,7 @@ public class StandardPlugin extends Plugin {
|
|||||||
registerFunction("exp", FUNCTION_EXP);
|
registerFunction("exp", FUNCTION_EXP);
|
||||||
registerFunction("ln", FUNCTION_LN);
|
registerFunction("ln", FUNCTION_LN);
|
||||||
registerFunction("sqrt", FUNCTION_SQRT);
|
registerFunction("sqrt", FUNCTION_SQRT);
|
||||||
|
registerFunction("sin", FUNCTION_SIN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -348,9 +362,16 @@ public class StandardPlugin extends Plugin {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A factorial function that uses memoization for each number class; it efficiently
|
||||||
|
* computes factorials of non-negative integers.
|
||||||
|
* @param numberClass type of number to return.
|
||||||
|
* @param n non-negative integer.
|
||||||
|
* @return a number of numClass with value n factorial.
|
||||||
|
*/
|
||||||
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
public static NumberInterface factorial(Class<? extends NumberInterface> numberClass, int n){
|
||||||
if(!factorialLists.containsKey(numberClass)){
|
if(!factorialLists.containsKey(numberClass)){
|
||||||
factorialLists.put(numberClass, new ArrayList<>());
|
factorialLists.put(numberClass, new ArrayList<NumberInterface>());
|
||||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
factorialLists.get(numberClass).add(NaiveNumber.ONE.promoteTo(numberClass));
|
||||||
}
|
}
|
||||||
@@ -363,4 +384,71 @@ public class StandardPlugin extends Plugin {
|
|||||||
return list.get(n);
|
return list.get(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the value of the Taylor series for sin (centered at 0) at x.
|
||||||
|
* @param x where the series is evaluated.
|
||||||
|
* @return the value of the series
|
||||||
|
*/
|
||||||
|
private static NumberInterface sinTaylor(NumberInterface x){
|
||||||
|
NumberInterface power = x, multiplier = x.multiply(x).negate(), currentTerm = x, sum = x;
|
||||||
|
NumberInterface maxError = getMaxError(x);
|
||||||
|
int n = 1;
|
||||||
|
do{
|
||||||
|
n += 2;
|
||||||
|
power = power.multiply(multiplier);
|
||||||
|
currentTerm = power.divide(factorial(x.getClass(), n));
|
||||||
|
sum = sum.add(currentTerm);
|
||||||
|
} while (FUNCTION_ABS.apply(currentTerm).compareTo(maxError) > 0);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an approximation of Pi, with appropriate accuracy for given number class.
|
||||||
|
* @param numClass type of number.
|
||||||
|
* @return A number of class numClass, with value approximately Pi = 3.1415...
|
||||||
|
*/
|
||||||
|
public static NumberInterface getPi(Class<? extends NumberInterface> numClass){
|
||||||
|
if(!piValues.containsKey(numClass)){
|
||||||
|
//https://en.wikipedia.org/wiki/Chudnovsky_algorithm
|
||||||
|
NumberInterface C = FUNCTION_SQRT.apply(new NaiveNumber(10005).promoteTo(numClass)).multiply(new NaiveNumber(426880).promoteTo(numClass));
|
||||||
|
NumberInterface M = NaiveNumber.ONE.promoteTo(numClass);
|
||||||
|
NumberInterface L = new NaiveNumber(13591409).promoteTo(numClass);
|
||||||
|
NumberInterface X = M;
|
||||||
|
NumberInterface sum = L;
|
||||||
|
int termsNeeded = C.getMaxPrecision()/13 + 1;
|
||||||
|
|
||||||
|
NumberInterface lSummand = new NaiveNumber(545140134).promoteTo(L.getClass());
|
||||||
|
NumberInterface xMultiplier = new NaiveNumber(262537412).promoteTo(X.getClass())
|
||||||
|
.multiply(new NaiveNumber(1000000000).promoteTo(X.getClass()))
|
||||||
|
.add(new NaiveNumber(640768000).promoteTo(X.getClass()))
|
||||||
|
.negate();
|
||||||
|
for(int i = 0; i < termsNeeded; i++){
|
||||||
|
M = M
|
||||||
|
.multiply(new NaiveNumber(12*i+2).promoteTo(M.getClass()))
|
||||||
|
.multiply(new NaiveNumber(12*i+6).promoteTo(M.getClass()))
|
||||||
|
.multiply(new NaiveNumber(12*i+10).promoteTo(M.getClass()))
|
||||||
|
.divide(new NaiveNumber(Math.pow(i+1,3)).promoteTo(M.getClass()));
|
||||||
|
L = L.add(lSummand);
|
||||||
|
X = X.multiply(xMultiplier);
|
||||||
|
sum = sum.add(M.multiply(L).divide(X));
|
||||||
|
}
|
||||||
|
piValues.put(numClass, C.divide(sum));
|
||||||
|
}
|
||||||
|
return piValues.get(numClass);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an equivalent angle in the interval [0, 2pi)
|
||||||
|
* @param phi an angle (in radians).
|
||||||
|
* @return theta in [0, 2pi) that differs from phi by a multiple of 2pi.
|
||||||
|
*/
|
||||||
|
private static NumberInterface getSmallAngle(NumberInterface phi){
|
||||||
|
NumberInterface twoPi = getPi(phi.getClass()).multiply(new NaiveNumber("2").promoteTo(phi.getClass()));
|
||||||
|
NumberInterface theta = FUNCTION_ABS.apply(phi).subtract(twoPi
|
||||||
|
.multiply(FUNCTION_ABS.apply(phi).divide(twoPi).floor())); //Now theta is in [0, 2pi).
|
||||||
|
if(phi.signum() < 0){
|
||||||
|
theta = twoPi.subtract(theta);
|
||||||
|
}
|
||||||
|
return theta;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 BinaryNode extends TreeNode {
|
public class BinaryInfixNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation being applied.
|
* The operation being applied.
|
||||||
@@ -18,7 +18,7 @@ public class BinaryNode extends TreeNode {
|
|||||||
*/
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
private BinaryNode() {
|
private BinaryInfixNode() {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -27,7 +27,7 @@ public class BinaryNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param operation the operation.
|
* @param operation the operation.
|
||||||
*/
|
*/
|
||||||
public BinaryNode(String operation) {
|
public BinaryInfixNode(String operation) {
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,7 +39,7 @@ public class BinaryNode 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 BinaryNode(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;
|
||||||
@@ -28,15 +28,15 @@ 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 BinaryNode) {
|
} 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 = abacus.getPluginManager().operatorFor(((BinaryNode) node).getOperation()).getFunction();
|
Function function = abacus.getPluginManager().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 UnaryNode) {
|
} else if (node instanceof UnaryPrefixNode) {
|
||||||
NumberInterface child = (NumberInterface) children[0];
|
NumberInterface child = (NumberInterface) children[0];
|
||||||
Function functionn = abacus.getPluginManager().operatorFor(((UnaryNode) node).getOperation()).getFunction();
|
Function functionn = abacus.getPluginManager().operatorFor(((UnaryPrefixNode) node).getOperation()).getFunction();
|
||||||
if (functionn == null) return null;
|
if (functionn == null) return null;
|
||||||
return functionn.apply(child);
|
return functionn.apply(child);
|
||||||
} else if (node instanceof FunctionNode) {
|
} else if (node instanceof FunctionNode) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
public class UnaryNode extends TreeNode {
|
public class UnaryPrefixNode extends TreeNode {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The operation this node will apply.
|
* The operation this node will apply.
|
||||||
@@ -16,7 +16,7 @@ public class UnaryNode extends TreeNode {
|
|||||||
*
|
*
|
||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
*/
|
*/
|
||||||
public UnaryNode(String operation) {
|
public UnaryPrefixNode(String operation) {
|
||||||
this(operation, null);
|
this(operation, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@ public class UnaryNode extends TreeNode {
|
|||||||
* @param operation the operation for this node.
|
* @param operation the operation for this node.
|
||||||
* @param applyTo the node to apply the function to.
|
* @param applyTo the node to apply the function to.
|
||||||
*/
|
*/
|
||||||
public UnaryNode(String operation, TreeNode applyTo) {
|
public UnaryPrefixNode(String operation, TreeNode applyTo) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.applyTo = applyTo;
|
this.applyTo = applyTo;
|
||||||
}
|
}
|
||||||
@@ -1,54 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
|
|
||||||
<?import javafx.geometry.Insets?>
|
|
||||||
<?import javafx.scene.control.*?>
|
|
||||||
<?import javafx.scene.layout.BorderPane?>
|
|
||||||
<?import javafx.scene.layout.VBox?>
|
|
||||||
<?import javafx.scene.text.Text?>
|
|
||||||
<?import javafx.scene.layout.HBox?>
|
|
||||||
<?import javafx.scene.layout.GridPane?>
|
|
||||||
<BorderPane xmlns="http://javafx.com/javafx"
|
|
||||||
xmlns:fx="http://javafx.com/fxml"
|
|
||||||
fx:controller="org.nwapw.abacus.fx.AbacusController">
|
|
||||||
<center>
|
|
||||||
<TabPane>
|
|
||||||
<Tab text="Calculator" closable="false">
|
|
||||||
<BorderPane>
|
|
||||||
<center>
|
|
||||||
<TableView fx:id="historyTable">
|
|
||||||
<columnResizePolicy>
|
|
||||||
<TableView fx:constant="CONSTRAINED_RESIZE_POLICY"/>
|
|
||||||
</columnResizePolicy>
|
|
||||||
<columns>
|
|
||||||
<TableColumn fx:id="inputColumn" text="Input" sortable="false"/>
|
|
||||||
<TableColumn fx:id="parsedColumn" text="Parsed" sortable="false"/>
|
|
||||||
<TableColumn fx:id="outputColumn" text="Output" sortable="false"/>
|
|
||||||
</columns>
|
|
||||||
</TableView>
|
|
||||||
</center>
|
|
||||||
<bottom>
|
|
||||||
<VBox>
|
|
||||||
<ScrollPane prefHeight="50" vbarPolicy="NEVER">
|
|
||||||
<padding>
|
|
||||||
<Insets top="10" bottom="10" left="10" right="10"/>
|
|
||||||
</padding>
|
|
||||||
<Text fx:id="outputText"/>
|
|
||||||
</ScrollPane>
|
|
||||||
<TextField fx:id="inputField" onAction="#performCalculation"/>
|
|
||||||
<Button fx:id="inputButton" text="Calculate" maxWidth="Infinity"
|
|
||||||
onAction="#performCalculation"/>
|
|
||||||
</VBox>
|
|
||||||
</bottom>
|
|
||||||
</BorderPane>
|
|
||||||
</Tab>
|
|
||||||
<Tab text="Settings" closable="false">
|
|
||||||
<GridPane hgap="10" vgap="10">
|
|
||||||
<padding><Insets left="10" right="10" top="10" bottom="10"/></padding>
|
|
||||||
<Label text="Number Implementation" GridPane.columnIndex="0" GridPane.rowIndex="0"/>
|
|
||||||
<ComboBox fx:id="numberImplementationBox" GridPane.columnIndex="1" GridPane.rowIndex="0"/>
|
|
||||||
</GridPane>
|
|
||||||
</Tab>
|
|
||||||
</TabPane>
|
|
||||||
</center>
|
|
||||||
|
|
||||||
</BorderPane>
|
|
||||||
Reference in New Issue
Block a user