1
0
mirror of https://github.com/DanilaFe/abacus synced 2026-01-27 17:15:21 +00:00

Compare commits

...

3 Commits

11 changed files with 143 additions and 26 deletions

View File

@@ -54,7 +54,7 @@ public class Lexer<T> {
/** /**
* The registered patterns. * The registered patterns.
*/ */
private HashMap<PatternEntry<T>, Pattern<T>> patterns; private Map<PatternEntry<T>, Pattern<T>> patterns;
/** /**
* Creates a new lexer with no registered patterns. * Creates a new lexer with no registered patterns.
@@ -127,7 +127,7 @@ public class Lexer<T> {
* @param compare the comparator used to sort matches by their IDs. * @param compare the comparator used to sort matches by their IDs.
* @return the resulting list of matches, in order, or null on error. * @return the resulting list of matches, in order, or null on error.
*/ */
public ArrayList<Match<T>> lexAll(String from, int startAt, Comparator<T> compare){ public List<Match<T>> lexAll(String from, int startAt, Comparator<T> compare){
int index = startAt; int index = startAt;
ArrayList<Match<T>> matches = new ArrayList<>(); ArrayList<Match<T>> matches = new ArrayList<>();
Match<T> lastMatch = null; Match<T> lastMatch = null;

View File

@@ -2,6 +2,7 @@ package org.nwapw.abacus.lexing.pattern;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Stack; import java.util.Stack;
import java.util.function.Function; import java.util.function.Function;
@@ -32,7 +33,7 @@ public class Pattern<T> {
* A map of regex operator to functions that modify a PatternChain * A map of regex operator to functions that modify a PatternChain
* with the appropriate operation. * with the appropriate operation.
*/ */
private HashMap<Character, Function<PatternChain<T>, PatternChain<T>>> operations = private Map<Character, Function<PatternChain<T>, PatternChain<T>>> operations =
new HashMap<Character, Function<PatternChain<T>, PatternChain<T>>>() {{ new HashMap<Character, Function<PatternChain<T>, PatternChain<T>>>() {{
put('+', Pattern.this::transformPlus); put('+', Pattern.this::transformPlus);
put('*', Pattern.this::transformStar); put('*', Pattern.this::transformStar);

View File

@@ -3,6 +3,7 @@ package org.nwapw.abacus.lexing.pattern;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set;
/** /**
* A base class for a pattern node. Provides all functions * A base class for a pattern node. Provides all functions
@@ -16,7 +17,7 @@ public class PatternNode<T> {
* The set of states to which the lexer should continue * The set of states to which the lexer should continue
* should this node be correctly matched. * should this node be correctly matched.
*/ */
protected HashSet<PatternNode<T>> outputStates; protected Set<PatternNode<T>> outputStates;
/** /**
* Creates a new pattern node. * Creates a new pattern node.

View File

@@ -88,6 +88,9 @@ public class NaiveNumber implements NumberInterface {
@Override @Override
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) { public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
if(toClass == this.getClass()) return this; if(toClass == this.getClass()) return this;
else if(toClass == PreciseNumber.class){
return new PreciseNumber(Double.toString(value));
}
return null; return null;
} }

View File

@@ -0,0 +1,112 @@
package org.nwapw.abacus.number;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;
import java.math.RoundingMode;
public class PreciseNumber implements NumberInterface{
/**
* The number one.
*/
static final PreciseNumber ONE = new PreciseNumber(BigDecimal.ONE);
/**
* The number zero.
*/
static final PreciseNumber ZERO = new PreciseNumber(BigDecimal.ZERO);
/**
* The number ten.
*/
static final PreciseNumber TEN = new PreciseNumber(BigDecimal.TEN);
BigDecimal value = new BigDecimal("0");
/**
* Constructs a precise number from the given string.
* @param string a string representation of the number meeting the same conditions
* as the BidDecimal(String) constructor.
*/
public PreciseNumber(String string){
value = new BigDecimal(string);
}
/**
* Constructs a precise number from the given BigDecimal.
* @param value a BigDecimal object representing the value of the number.
*/
public PreciseNumber(BigDecimal value){
this.value = value;
}
@Override
public int precision() {
return 54;
}
@Override
public NumberInterface multiply(NumberInterface multiplier) {
return new PreciseNumber(value.multiply(((PreciseNumber) multiplier).value));
}
@Override
public NumberInterface divide(NumberInterface divisor) {
return new PreciseNumber(value.divide(((PreciseNumber) divisor).value, this.precision(), RoundingMode.HALF_UP));
}
@Override
public NumberInterface add(NumberInterface summand) {
return new PreciseNumber(value.add(((PreciseNumber) summand).value));
}
@Override
public NumberInterface subtract(NumberInterface subtrahend) {
return new PreciseNumber(value.subtract(((PreciseNumber) subtrahend).value));
}
@Override
public NumberInterface intPow(int exponent) {
if(exponent == 0){
return PreciseNumber.ONE;
}
boolean takeReciprocal = exponent < 0;
exponent = Math.abs(exponent);
NumberInterface power = this;
for(int currentExponent = 1; currentExponent < exponent; currentExponent++){
power = power.multiply(this);
}
if(takeReciprocal){
power = PreciseNumber.ONE.divide(power);
}
return power;
}
@Override
public int compareTo(NumberInterface number) {
return value.compareTo(((PreciseNumber) number).value);
}
@Override
public int signum() {
return value.signum();
}
@Override
public NumberInterface negate(){
return new PreciseNumber(value.negate());
}
@Override
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
if(toClass == this.getClass()){
return this;
}
return null;
}
@Override
public String toString() {
BigDecimal rounded = value.setScale(precision() - 4, RoundingMode.HALF_UP);
return rounded.stripTrailingZeros().toPlainString();
}
}

View File

@@ -4,6 +4,7 @@ import org.nwapw.abacus.function.Function;
import org.nwapw.abacus.function.Operator; import org.nwapw.abacus.function.Operator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.Set; import java.util.Set;
/** /**
@@ -18,11 +19,11 @@ public abstract class Plugin {
/** /**
* A hash map of functions mapped to their string names. * A hash map of functions mapped to their string names.
*/ */
private HashMap<String, Function> functions; private Map<String, Function> functions;
/** /**
* A hash map of operators mapped to their string names. * A hash map of operators mapped to their string names.
*/ */
private HashMap<String, Operator> operators; private Map<String, Operator> operators;
/** /**
* 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,

View File

@@ -15,29 +15,29 @@ public class PluginManager {
/** /**
* A list of loaded plugins. * A list of loaded plugins.
*/ */
private ArrayList<Plugin> plugins; private List<Plugin> plugins;
/** /**
* List of functions that have been cached, * List of functions that have been cached,
* that is, found in a plugin and returned. * that is, found in a plugin and returned.
*/ */
private HashMap<String, Function> cachedFunctions; private Map<String, Function> cachedFunctions;
/** /**
* List of operators tha have been cached, * List of operators tha have been cached,
* that is, found in a plugin and returned. * that is, found in a plugin and returned.
*/ */
private HashMap<String, Operator> cachedOperators; private Map<String, Operator> cachedOperators;
/** /**
* List of all functions loaded by the plugins. * List of all functions loaded by the plugins.
*/ */
private HashSet<String> allFunctions; private Set<String> allFunctions;
/** /**
* List of all operators loaded by the plugins. * List of all operators loaded by the plugins.
*/ */
private HashSet<String> allOperators; private Set<String> allOperators;
/** /**
* The list of plugin listeners attached to this instance. * The list of plugin listeners attached to this instance.
*/ */
private HashSet<PluginListener> listeners; private Set<PluginListener> listeners;
/** /**
* Creates a new plugin manager. * Creates a new plugin manager.
@@ -155,7 +155,7 @@ public class PluginManager {
* Gets all the functions loaded by the Plugin Manager. * Gets all the functions loaded by the Plugin Manager.
* @return the set of all functions that were loaded. * @return the set of all functions that were loaded.
*/ */
public HashSet<String> getAllFunctions() { public Set<String> getAllFunctions() {
return allFunctions; return allFunctions;
} }
@@ -163,7 +163,7 @@ public class PluginManager {
* Gets all the operators loaded by the Plugin Manager. * Gets all the operators loaded by the Plugin Manager.
* @return the set of all operators that were loaded. * @return the set of all operators that were loaded.
*/ */
public HashSet<String> getAllOperators() { public Set<String> getAllOperators() {
return allOperators; return allOperators;
} }

View File

@@ -6,9 +6,6 @@ import org.nwapw.abacus.function.OperatorAssociativity;
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 javax.print.attribute.standard.MediaSize;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.BiFunction; import java.util.function.BiFunction;
/** /**
@@ -231,7 +228,7 @@ public class StandardPlugin extends Plugin {
@Override @Override
protected NumberInterface applyInternal(NumberInterface[] params) { protected NumberInterface applyInternal(NumberInterface[] params) {
return StandardPlugin.this.getFunction("pow").apply(params[0], (new NaiveNumber(0.5))); return StandardPlugin.this.getOperator("^").getFunction().apply(params[0], ((new NaiveNumber(0.5)).promoteTo(params[0].getClass())));
} }
}); });
} }

View File

@@ -1,6 +1,7 @@
package org.nwapw.abacus.tree; package org.nwapw.abacus.tree;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* A node that represents a function call. * A node that represents a function call.
@@ -14,7 +15,7 @@ public class FunctionNode extends TreeNode {
/** /**
* The list of arguments to the function. * The list of arguments to the function.
*/ */
private ArrayList<TreeNode> children; private List<TreeNode> children;
/** /**
* Creates a function node with no function. * Creates a function node with no function.

View File

@@ -19,11 +19,11 @@ public class TreeBuilder {
/** /**
* The map of operator precedences. * The map of operator precedences.
*/ */
private HashMap<String, Integer> precedenceMap; private Map<String, Integer> precedenceMap;
/** /**
* The map of operator associativity. * The map of operator associativity.
*/ */
private HashMap<String, OperatorAssociativity> associativityMap; private Map<String, OperatorAssociativity> associativityMap;
/** /**
* Comparator used to sort token types. * Comparator used to sort token types.
@@ -70,7 +70,7 @@ public class TreeBuilder {
* @param string the string to tokenize. * @param string the string to tokenize.
* @return the list of tokens produced. * @return the list of tokens produced.
*/ */
public ArrayList<Match<TokenType>> tokenize(String string){ public List<Match<TokenType>> tokenize(String string){
return lexer.lexAll(string, 0, tokenSorter); return lexer.lexAll(string, 0, tokenSorter);
} }
@@ -80,7 +80,7 @@ public class TreeBuilder {
* @param from the tokens to be rearranged. * @param from the tokens to be rearranged.
* @return the resulting list of rearranged tokens. * @return the resulting list of rearranged tokens.
*/ */
public ArrayList<Match<TokenType>> intoPostfix(String source, ArrayList<Match<TokenType>> from){ public List<Match<TokenType>> intoPostfix(String source, 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<>();
while(!from.isEmpty()){ while(!from.isEmpty()){
@@ -138,7 +138,7 @@ public class TreeBuilder {
* @param matches the list of tokens from the source string. * @param matches the list of tokens from the source string.
* @return the construct tree expression. * @return the construct tree expression.
*/ */
public TreeNode fromStringRecursive(String source, ArrayList<Match<TokenType>> matches){ public TreeNode fromStringRecursive(String source, List<Match<TokenType>> matches){
if(matches.size() == 0) return null; if(matches.size() == 0) return null;
Match<TokenType> match = matches.remove(0); Match<TokenType> match = matches.remove(0);
TokenType matchType = match.getType(); TokenType matchType = match.getType();
@@ -170,7 +170,7 @@ public class TreeBuilder {
* @return the resulting tree. * @return the resulting tree.
*/ */
public TreeNode fromString(String string){ public TreeNode fromString(String string){
ArrayList<Match<TokenType>> matches = tokenize(string); List<Match<TokenType>> matches = tokenize(string);
if(matches == null) return null; if(matches == null) return null;
matches.removeIf(m -> m.getType() == TokenType.WHITESPACE); matches.removeIf(m -> m.getType() == TokenType.WHITESPACE);
matches = intoPostfix(string, matches); matches = intoPostfix(string, matches);

View File

@@ -6,6 +6,7 @@ import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel; import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel; import javax.swing.table.TableModel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
/** /**
* A table model to store data about the history of inputs * A table model to store data about the history of inputs
@@ -57,7 +58,7 @@ public class HistoryTableModel extends AbstractTableModel {
/** /**
* The list of entries. * The list of entries.
*/ */
ArrayList<HistoryEntry> entries; List<HistoryEntry> entries;
/** /**
* Creates a new empty history table model * Creates a new empty history table model