mirror of
https://github.com/DanilaFe/abacus
synced 2024-11-17 16:09:32 -08:00
Merge branch 'master' of https://github.com/DanilaFe/abacus
This commit is contained in:
commit
c426cdb892
|
@ -49,8 +49,8 @@ public interface NumberInterface {
|
|||
|
||||
/**
|
||||
* Raises this number to an integer power.
|
||||
* @param exponent
|
||||
* @return
|
||||
* @param exponent the exponent to which to take the number.
|
||||
* @return the resulting value.
|
||||
*/
|
||||
NumberInterface intPow(int exponent);
|
||||
|
||||
|
|
71
src/org/nwapw/abacus/tree/FunctionNode.java
Normal file
71
src/org/nwapw/abacus/tree/FunctionNode.java
Normal file
|
@ -0,0 +1,71 @@
|
|||
package org.nwapw.abacus.tree;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A node that represents a function call.
|
||||
*/
|
||||
public class FunctionNode extends TreeNode {
|
||||
|
||||
/**
|
||||
* The name of the function being called
|
||||
*/
|
||||
private String function;
|
||||
/**
|
||||
* The list of arguments to the function.
|
||||
*/
|
||||
private ArrayList<TreeNode> children;
|
||||
|
||||
/**
|
||||
* Creates a function node with no function.
|
||||
*/
|
||||
private FunctionNode() { }
|
||||
|
||||
/**
|
||||
* Creates a new function node with the given function name.
|
||||
* @param function the function name.
|
||||
*/
|
||||
public FunctionNode(String function){
|
||||
this.function = function;
|
||||
children = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the function name for this node.
|
||||
* @return the function name.
|
||||
*/
|
||||
public String getFunction() {
|
||||
return function;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a child to this node.
|
||||
* @param node the child to add.
|
||||
*/
|
||||
public void addChild(TreeNode node){
|
||||
children.add(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T reduce(Reducer<T> reducer) {
|
||||
Object[] reducedChildren = new Object[children.size()];
|
||||
for(int i = 0; i < reducedChildren.length; i++){
|
||||
reducedChildren[i] = children.get(i).reduce(reducer);
|
||||
if(reducedChildren[i] == null) return null;
|
||||
}
|
||||
return reducer.reduceNode(this, reducedChildren);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
buffer.append(function);
|
||||
buffer.append("(");
|
||||
for(int i = 0; i < children.size(); i++){
|
||||
buffer.append(children.get(i));
|
||||
buffer.append(i == children.size() - 1 ? "" : ", ");
|
||||
}
|
||||
buffer.append(")");
|
||||
return buffer.toString();
|
||||
}
|
||||
}
|
|
@ -1,12 +1,24 @@
|
|||
package org.nwapw.abacus.tree;
|
||||
|
||||
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.
|
||||
* This is not always guaranteed to work.
|
||||
*/
|
||||
public class NumberReducer implements Reducer<NumberInterface> {
|
||||
|
||||
/**
|
||||
* The plugin manager from which to draw the functions.
|
||||
*/
|
||||
private PluginManager manager;
|
||||
|
||||
/**
|
||||
* Creates a new number reducer with the given plugin manager.
|
||||
* @param manager the plugin manager.
|
||||
*/
|
||||
public NumberReducer(PluginManager manager){
|
||||
this.manager = manager;
|
||||
}
|
||||
|
@ -18,7 +30,17 @@ public class NumberReducer implements Reducer<NumberInterface> {
|
|||
} else if(node instanceof OpNode){
|
||||
NumberInterface left = (NumberInterface) children[0];
|
||||
NumberInterface right = (NumberInterface) children[1];
|
||||
return manager.functionFor(((OpNode) node).getOperation()).apply(left, right);
|
||||
Function function = manager.functionFor(((OpNode) node).getOperation());
|
||||
if(function == null) return null;
|
||||
return function.apply(left, right);
|
||||
} else if(node instanceof FunctionNode){
|
||||
NumberInterface[] convertedChildren = new NumberInterface[children.length];
|
||||
for(int i = 0; i < convertedChildren.length; i++){
|
||||
convertedChildren[i] = (NumberInterface) children[i];
|
||||
}
|
||||
Function function = manager.functionFor(((FunctionNode) node).getFunction());
|
||||
if(function == null) return null;
|
||||
return function.apply(convertedChildren);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -86,6 +86,7 @@ public class OpNode extends TreeNode {
|
|||
public <T> T reduce(Reducer<T> reducer) {
|
||||
T leftReduce = left.reduce(reducer);
|
||||
T rightReduce = right.reduce(reducer);
|
||||
if(leftReduce == null || rightReduce == null) return null;
|
||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ package org.nwapw.abacus.tree;
|
|||
*/
|
||||
public enum TokenType {
|
||||
|
||||
ANY(0), OP(1), NUM(2), WORD(3), OPEN_PARENTH(4), CLOSE_PARENTH(5);
|
||||
INTERNAL_FUNCTION_END(-1), INTERNAL_FUNCTION_START(-1), ANY(0), COMMA(1), OP(2), NUM(3), WORD(4), OPEN_PARENTH(5), CLOSE_PARENTH(6);
|
||||
|
||||
/**
|
||||
* The priority by which this token gets sorted.
|
||||
|
|
|
@ -14,6 +14,7 @@ public abstract class TreeNode {
|
|||
* The lexer used to lex tokens.
|
||||
*/
|
||||
protected static Lexer<TokenType> lexer = new Lexer<TokenType>(){{
|
||||
register(",", TokenType.COMMA);
|
||||
register("\\+|-|\\*|/|^", TokenType.OP);
|
||||
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
|
||||
register("[a-zA-Z]+", TokenType.WORD);
|
||||
|
@ -67,9 +68,10 @@ public abstract class TreeNode {
|
|||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||
while(!from.isEmpty()){
|
||||
Match<TokenType> match = from.remove(0);
|
||||
if(match.getType() == TokenType.NUM) {
|
||||
TokenType matchType = match.getType();
|
||||
if(matchType == TokenType.NUM || matchType == TokenType.WORD) {
|
||||
output.add(match);
|
||||
} else if(match.getType() == TokenType.OP){
|
||||
} else if(matchType == TokenType.OP){
|
||||
String tokenString = source.substring(match.getFrom(), match.getTo());
|
||||
int precedence = precedenceMap.get(tokenString);
|
||||
OperatorAssociativity associativity = associativityMap.get(tokenString);
|
||||
|
@ -86,14 +88,24 @@ public abstract class TreeNode {
|
|||
output.add(tokenStack.pop());
|
||||
}
|
||||
tokenStack.push(match);
|
||||
} else if(match.getType() == TokenType.OPEN_PARENTH){
|
||||
} else if(matchType == TokenType.OPEN_PARENTH){
|
||||
if(!output.isEmpty() && output.get(output.size() - 1).getType() == TokenType.WORD){
|
||||
tokenStack.push(output.remove(output.size() - 1));
|
||||
output.add(new Match<>(0, 0, TokenType.INTERNAL_FUNCTION_END));
|
||||
}
|
||||
tokenStack.push(match);
|
||||
} else if(match.getType() == TokenType.CLOSE_PARENTH){
|
||||
} else if(matchType == TokenType.CLOSE_PARENTH || matchType == TokenType.COMMA){
|
||||
while(!tokenStack.empty() && tokenStack.peek().getType() != TokenType.OPEN_PARENTH){
|
||||
output.add(tokenStack.pop());
|
||||
}
|
||||
if(tokenStack.empty()) return null;
|
||||
tokenStack.pop();
|
||||
if(matchType == TokenType.CLOSE_PARENTH){
|
||||
tokenStack.pop();
|
||||
if(!tokenStack.empty() && tokenStack.peek().getType() == TokenType.WORD) {
|
||||
output.add(tokenStack.pop());
|
||||
output.add(new Match<>(0, 0, TokenType.INTERNAL_FUNCTION_START));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
while(!tokenStack.empty()){
|
||||
|
@ -119,6 +131,19 @@ public abstract class TreeNode {
|
|||
else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right);
|
||||
} else if(match.getType() == TokenType.NUM){
|
||||
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
|
||||
} else if(match.getType() == TokenType.INTERNAL_FUNCTION_START){
|
||||
if(matches.isEmpty() || matches.get(0).getType() != TokenType.WORD) return null;
|
||||
Match<TokenType> stringName = matches.remove(0);
|
||||
String functionName = source.substring(stringName.getFrom(), stringName.getTo());
|
||||
FunctionNode node = new FunctionNode(functionName);
|
||||
while(!matches.isEmpty() && matches.get(0).getType() != TokenType.INTERNAL_FUNCTION_END){
|
||||
TreeNode argument = fromStringRecursive(source, matches);
|
||||
if(argument == null) return null;
|
||||
node.addChild(argument);
|
||||
}
|
||||
if(matches.isEmpty()) return null;
|
||||
matches.remove(0);
|
||||
return node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -7,20 +7,34 @@ import javax.swing.table.AbstractTableModel;
|
|||
import javax.swing.table.TableModel;
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* A table model to store data about the history of inputs
|
||||
* in the calculator.
|
||||
*/
|
||||
public class HistoryTableModel extends AbstractTableModel {
|
||||
|
||||
/**
|
||||
* Static array used to get the column names.
|
||||
*/
|
||||
public static final String[] COLUMN_NAMES = {
|
||||
"Input",
|
||||
"Parsed Input",
|
||||
"Output"
|
||||
};
|
||||
|
||||
/**
|
||||
* Static array used to get the class of each column.
|
||||
*/
|
||||
public static final Class[] CLASS_TYPES = {
|
||||
String.class,
|
||||
TreeNode.class,
|
||||
String.class
|
||||
};
|
||||
|
||||
/**
|
||||
* Class used specifically to hold data about
|
||||
* the previous entries into the calculator.
|
||||
*/
|
||||
public static class HistoryEntry {
|
||||
public String input;
|
||||
public TreeNode parsedInput;
|
||||
|
@ -40,17 +54,27 @@ public class HistoryTableModel extends AbstractTableModel {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The list of entries.
|
||||
*/
|
||||
ArrayList<HistoryEntry> entries;
|
||||
|
||||
/**
|
||||
* Creates a new empty history table model
|
||||
*/
|
||||
public HistoryTableModel() {
|
||||
entries = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an entry to the model.
|
||||
* @param entry the entry to add.
|
||||
*/
|
||||
public void addEntry(HistoryEntry entry){
|
||||
entries.add(entry);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
return entries.size();
|
||||
}
|
||||
|
@ -80,18 +104,4 @@ public class HistoryTableModel extends AbstractTableModel {
|
|||
return entries.get(rowIndex).nthValue(columnIndex);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTableModelListener(TableModelListener l) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTableModelListener(TableModelListener l) {
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package org.nwapw.abacus.window;
|
||||
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
import org.nwapw.abacus.tree.NumberReducer;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
@ -17,17 +18,27 @@ import java.awt.event.MouseEvent;
|
|||
public class Window extends JFrame {
|
||||
|
||||
private static final String CALC_STRING = "Calculate";
|
||||
private static final String SELECT_STRING = "Select";
|
||||
private static final String SYNTAX_ERR_STRING = "Syntax Error";
|
||||
private static final String EVAL_ERR_STRING = "Evaluation Error";
|
||||
private static final String NUMBER_SYSTEM_LABEL = "Number Type:";
|
||||
private static final String FUNCTION_LABEL = "Functions:";
|
||||
|
||||
/**
|
||||
* Array of Strings to which the "calculate" button's text
|
||||
* changes. For instance, in the graph tab, the name will
|
||||
* be "Graph" and not "Calculate".
|
||||
*/
|
||||
private static final String[] BUTTON_NAMES = {
|
||||
CALC_STRING,
|
||||
CALC_STRING
|
||||
};
|
||||
|
||||
private static boolean[] BUTTON_ENABLED = {
|
||||
/**
|
||||
* Array of booleans that determine whether the input
|
||||
* field and the input button are enabled at a particular
|
||||
* index.
|
||||
*/
|
||||
private static boolean[] INPUT_ENABLED = {
|
||||
true,
|
||||
false
|
||||
};
|
||||
|
@ -117,13 +128,22 @@ public class Window extends JFrame {
|
|||
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
||||
return;
|
||||
}
|
||||
lastOutput = parsedExpression.reduce(reducer).toString();
|
||||
NumberInterface numberInterface = parsedExpression.reduce(reducer);
|
||||
if(numberInterface == null){
|
||||
lastOutputArea.setText(EVAL_ERR_STRING);;
|
||||
return;
|
||||
}
|
||||
lastOutput = numberInterface.toString();
|
||||
historyModel.addEntry(new HistoryTableModel.HistoryEntry(inputField.getText(), parsedExpression, lastOutput));
|
||||
historyTable.invalidate();
|
||||
lastOutputArea.setText(lastOutput);
|
||||
inputField.setText("");
|
||||
};
|
||||
|
||||
/**
|
||||
* Array of listeners that tell the input button how to behave
|
||||
* at a given input tab.
|
||||
*/
|
||||
private ActionListener[] listeners = {
|
||||
evaluateListener,
|
||||
null
|
||||
|
@ -198,7 +218,7 @@ public class Window extends JFrame {
|
|||
pane.add("Settings", settingsPanel);
|
||||
pane.addChangeListener(e -> {
|
||||
int selectionIndex = pane.getSelectedIndex();
|
||||
boolean enabled = BUTTON_ENABLED[selectionIndex];
|
||||
boolean enabled = INPUT_ENABLED[selectionIndex];
|
||||
ActionListener listener = listeners[selectionIndex];
|
||||
inputEnterButton.setText(BUTTON_NAMES[selectionIndex]);
|
||||
inputField.setEnabled(enabled);
|
||||
|
|
Loading…
Reference in New Issue
Block a user