mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-25 16:15:19 +00:00
Compare commits
69 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 2ca23fd427 | |||
| efbd6a4c20 | |||
| a211884499 | |||
| f2c280766d | |||
| e6559015b3 | |||
| f931b9f322 | |||
| 78e2d50f89 | |||
| 07dd9d0a1a | |||
| ee1de6dc17 | |||
| 077a34c618 | |||
| 79e85832ce | |||
| 189f8c6e15 | |||
| e8595510b8 | |||
| b09c9c3cb2 | |||
| b0a7c90aa1 | |||
| cf95ed7dc0 | |||
| bc72b4da8a | |||
| 15d7dbd30e | |||
| c8146954c3 | |||
| d18e27bdb4 | |||
| c4eb70999b | |||
| 4a8164631f | |||
| d7caf1cdc7 | |||
| 8754871556 | |||
| b31c1f9624 | |||
| 2b7a68e179 | |||
| e06feaa581 | |||
| 626a2cb514 | |||
|
|
0002f14e61 | ||
|
|
f35f83a92f | ||
|
|
8d9dac1a75 | ||
|
|
aae7e678dd | ||
| 8ada6c32ff | |||
| a446034a92 | |||
| 78033e93b0 | |||
|
|
6038663b3a | ||
| 2074c11095 | |||
| 95bae3befb | |||
| e41c25847b | |||
| 50baf80433 | |||
| f7d4d01bc8 | |||
| ec030607bf | |||
| e816b86a3e | |||
|
|
356084ef61 | ||
| 798ee6f7c3 | |||
| ac153521d4 | |||
| 08999350f4 | |||
|
|
1b9dc5514e | ||
| c19ae3b071 | |||
| ade4eb1035 | |||
| 31b6adecd9 | |||
| 08a462b8f3 | |||
| 989ac80bf4 | |||
| 3cf4f958b0 | |||
| 7e7525cf37 | |||
| b93346ec37 | |||
| cc5d487386 | |||
|
|
27cf6ce64b | ||
|
|
5f60110385 | ||
| 38255b1219 | |||
|
|
1112dafadf | ||
|
|
54340ada63 | ||
|
|
21cd9fd052 | ||
|
|
afcddafd81 | ||
|
|
dbf7d587ed | ||
|
|
07acfefd0b | ||
|
|
e99afa0507 | ||
|
|
67f8c648db | ||
|
|
254276b2af |
@@ -8,3 +8,5 @@
|
|||||||
* Rembulan(5.3) - https://github.com/mjanicek/rembulan
|
* Rembulan(5.3) - https://github.com/mjanicek/rembulan
|
||||||
* LuaJava - https://github.com/jasonsantos/luajava
|
* LuaJava - https://github.com/jasonsantos/luajava
|
||||||
* jnlua - https://code.google.com/archive/p/jnlua/
|
* jnlua - https://code.google.com/archive/p/jnlua/
|
||||||
|
## Gist
|
||||||
|
* https://gist.github.com/rileyJones/1c18338821b88e92a477bfa270344db3
|
||||||
|
|||||||
@@ -1,9 +1,35 @@
|
|||||||
package org.nwapw.abacus;
|
package org.nwapw.abacus;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.plugin.PluginManager;
|
||||||
|
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||||
|
import org.nwapw.abacus.window.Window;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
|
||||||
public class Abacus {
|
public class Abacus {
|
||||||
|
|
||||||
|
private Window mainUi;
|
||||||
|
private PluginManager manager;
|
||||||
|
|
||||||
|
public Abacus(){
|
||||||
|
init();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void init() {
|
||||||
|
try {
|
||||||
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||||
|
} catch (ClassNotFoundException | InstantiationException | UnsupportedLookAndFeelException | IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
manager = new PluginManager();
|
||||||
|
manager.addInstantiated(new StandardPlugin(manager));
|
||||||
|
mainUi = new Window(manager);
|
||||||
|
mainUi.setVisible(true);
|
||||||
|
manager.load();
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args){
|
public static void main(String[] args){
|
||||||
System.out.println("Hello world!");
|
new Abacus();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
47
src/org/nwapw/abacus/function/Function.java
Executable file
47
src/org/nwapw/abacus/function/Function.java
Executable file
@@ -0,0 +1,47 @@
|
|||||||
|
package org.nwapw.abacus.function;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A function that operates on one or more
|
||||||
|
* inputs and returns a single number.
|
||||||
|
*/
|
||||||
|
public abstract class Function {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A map to correctly promote different number implementations to each other.
|
||||||
|
*/
|
||||||
|
private static final HashMap<Class<? extends NumberInterface>, Integer> priorityMap =
|
||||||
|
new HashMap<Class<? extends NumberInterface>, Integer>() {{
|
||||||
|
put(NaiveNumber.class, 0);
|
||||||
|
}};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks whether the given params will work for the given function.
|
||||||
|
* @param params the given params
|
||||||
|
* @return true if the params can be used with this function.
|
||||||
|
*/
|
||||||
|
protected abstract boolean matchesParams(NumberInterface[] params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Internal apply implementation, which already receives appropriately promoted
|
||||||
|
* parameters that have bee run through matchesParams
|
||||||
|
* @param params the promoted parameters.
|
||||||
|
* @return the return value of the function.
|
||||||
|
*/
|
||||||
|
protected abstract NumberInterface applyInternal(NumberInterface[] params);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Function to check, promote arguments and run the function.
|
||||||
|
* @param params the raw input parameters.
|
||||||
|
* @return the return value of the function, or null if an error occurred.
|
||||||
|
*/
|
||||||
|
public NumberInterface apply(NumberInterface...params) {
|
||||||
|
if(!matchesParams(params)) return null;
|
||||||
|
return applyInternal(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
57
src/org/nwapw/abacus/function/Operator.java
Normal file
57
src/org/nwapw/abacus/function/Operator.java
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
package org.nwapw.abacus.function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class that represents a single infix operator.
|
||||||
|
*/
|
||||||
|
public abstract class Operator {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The associativity of the operator.
|
||||||
|
*/
|
||||||
|
private OperatorAssociativity associativity;
|
||||||
|
/**
|
||||||
|
* The precedence of the operator.
|
||||||
|
*/
|
||||||
|
private int precedence;
|
||||||
|
/**
|
||||||
|
* The function that is called by this operator.
|
||||||
|
*/
|
||||||
|
private Function function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new operator with the given parameters.
|
||||||
|
* @param associativity the associativity of the operator.
|
||||||
|
* @param precedence the precedence of the operator.
|
||||||
|
* @param function the function that the operator calls.
|
||||||
|
*/
|
||||||
|
public Operator(OperatorAssociativity associativity, int precedence, Function function){
|
||||||
|
this.associativity = associativity;
|
||||||
|
this.precedence = precedence;
|
||||||
|
this.function = function;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the operator's associativity.
|
||||||
|
* @return the associativity.
|
||||||
|
*/
|
||||||
|
public OperatorAssociativity getAssociativity() {
|
||||||
|
return associativity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the operator's precedence.
|
||||||
|
* @return the precedence.
|
||||||
|
*/
|
||||||
|
public int getPrecedence() {
|
||||||
|
return precedence;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the operator's function.
|
||||||
|
* @return the function.
|
||||||
|
*/
|
||||||
|
public Function getFunction() {
|
||||||
|
return function;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
8
src/org/nwapw/abacus/function/OperatorAssociativity.java
Normal file
8
src/org/nwapw/abacus/function/OperatorAssociativity.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package org.nwapw.abacus.function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enum to represent the associativity of an operator.
|
||||||
|
*/
|
||||||
|
public enum OperatorAssociativity {
|
||||||
|
LEFT, RIGHT
|
||||||
|
}
|
||||||
@@ -5,29 +5,96 @@ import org.nwapw.abacus.lexing.pattern.Match;
|
|||||||
import org.nwapw.abacus.lexing.pattern.Pattern;
|
import org.nwapw.abacus.lexing.pattern.Pattern;
|
||||||
import org.nwapw.abacus.lexing.pattern.PatternNode;
|
import org.nwapw.abacus.lexing.pattern.PatternNode;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Comparator;
|
|
||||||
import java.util.HashSet;
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A lexer that can generate tokens of a given type given a list of regular expressions
|
||||||
|
* to operate on.
|
||||||
|
* @param <T> the type used to identify which match belongs to which pattern.
|
||||||
|
*/
|
||||||
public class Lexer<T> {
|
public class Lexer<T> {
|
||||||
|
|
||||||
private ArrayList<Pattern<T>> patterns;
|
/**
|
||||||
|
* An entry that represents a pattern that has been registered with the lexer.
|
||||||
|
* @param <T> the type used to identify the pattern.
|
||||||
|
*/
|
||||||
|
private static class PatternEntry<T>{
|
||||||
|
/**
|
||||||
|
* The name of the entry.
|
||||||
|
*/
|
||||||
|
public String name;
|
||||||
|
/**
|
||||||
|
* The id of the entry.
|
||||||
|
*/
|
||||||
|
public T id;
|
||||||
|
|
||||||
public Lexer(){
|
/**
|
||||||
patterns = new ArrayList<>();
|
* Creates a new pattern entry with the given name and id.
|
||||||
|
* @param name the name of the pattern entry.
|
||||||
|
* @param id the id of the pattern entry.
|
||||||
|
*/
|
||||||
|
public PatternEntry(String name, T id){
|
||||||
|
this.name = name;
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(name, id);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
return obj instanceof PatternEntry &&
|
||||||
|
((PatternEntry) obj).name.equals(name) &&
|
||||||
|
((PatternEntry) obj).id.equals(id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The registered patterns.
|
||||||
|
*/
|
||||||
|
private HashMap<PatternEntry<T>, Pattern<T>> patterns;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new lexer with no registered patterns.
|
||||||
|
*/
|
||||||
|
public Lexer(){
|
||||||
|
patterns = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a single pattern.
|
||||||
|
* @param pattern the pattern regex
|
||||||
|
* @param id the ID by which to identify the pattern.
|
||||||
|
*/
|
||||||
public void register(String pattern, T id){
|
public void register(String pattern, T id){
|
||||||
Pattern<T> compiledPattern = new Pattern<>(pattern, id);
|
Pattern<T> compiledPattern = new Pattern<>(pattern, id);
|
||||||
if(compiledPattern.getHead() != null) patterns.add(compiledPattern);
|
if(compiledPattern.getHead() != null) patterns.put(new PatternEntry<>(pattern, id), compiledPattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unregisters a pattern.
|
||||||
|
* @param pattern the pattern to unregister
|
||||||
|
* @param id the ID by which to identify the pattern.
|
||||||
|
*/
|
||||||
|
public void unregister(String pattern, T id){
|
||||||
|
patterns.remove(new PatternEntry<>(pattern, id));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads one token from the given string.
|
||||||
|
* @param from the string to read from
|
||||||
|
* @param startAt the index to start at
|
||||||
|
* @param compare the comparator used to sort tokens by their ID.
|
||||||
|
* @return the best match.
|
||||||
|
*/
|
||||||
public Match<T> lexOne(String from, int startAt, Comparator<T> compare){
|
public Match<T> lexOne(String from, int startAt, Comparator<T> compare){
|
||||||
ArrayList<Match<T>> matches = new ArrayList<>();
|
ArrayList<Match<T>> matches = new ArrayList<>();
|
||||||
HashSet<PatternNode<T>> currentSet = new HashSet<>();
|
HashSet<PatternNode<T>> currentSet = new HashSet<>();
|
||||||
HashSet<PatternNode<T>> futureSet = new HashSet<>();
|
HashSet<PatternNode<T>> futureSet = new HashSet<>();
|
||||||
int index = startAt;
|
int index = startAt;
|
||||||
for(Pattern<T> pattern : patterns){
|
for(Pattern<T> pattern : patterns.values()){
|
||||||
pattern.getHead().addInto(currentSet);
|
pattern.getHead().addInto(currentSet);
|
||||||
}
|
}
|
||||||
while(!currentSet.isEmpty()){
|
while(!currentSet.isEmpty()){
|
||||||
@@ -53,15 +120,23 @@ public class Lexer<T> {
|
|||||||
return matches.isEmpty() ? null : matches.get(matches.size() - 1);
|
return matches.isEmpty() ? null : matches.get(matches.size() - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reads all tokens from a string.
|
||||||
|
* @param from the string to start from.
|
||||||
|
* @param startAt the index to start at.
|
||||||
|
* @param compare the comparator used to sort matches by their IDs.
|
||||||
|
* @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 ArrayList<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;
|
||||||
while((lastMatch = lexOne(from, index, compare)) != null && index < from.length()){
|
while(index < from.length() && (lastMatch = lexOne(from, index, compare)) != null){
|
||||||
if(lastMatch.getTo() == lastMatch.getFrom()) return null;
|
if(lastMatch.getTo() == lastMatch.getFrom()) return null;
|
||||||
matches.add(lastMatch);
|
matches.add(lastMatch);
|
||||||
index += lastMatch.getTo() - lastMatch.getFrom();
|
index += lastMatch.getTo() - lastMatch.getFrom();
|
||||||
}
|
}
|
||||||
|
if(lastMatch == null) return null;
|
||||||
return matches;
|
return matches;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
package org.nwapw.abacus.lexing.pattern;
|
package org.nwapw.abacus.lexing.pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A pattern node that matches any character.
|
||||||
|
* @param <T> the type that's used to tell which pattern this node belongs to.
|
||||||
|
*/
|
||||||
public class AnyNode<T> extends PatternNode<T> {
|
public class AnyNode<T> extends PatternNode<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,13 +1,28 @@
|
|||||||
package org.nwapw.abacus.lexing.pattern;
|
package org.nwapw.abacus.lexing.pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A node that represents a successful match.
|
||||||
|
* @param <T> the type that's used to tell which pattern this node belongs to.
|
||||||
|
*/
|
||||||
public class EndNode<T> extends PatternNode<T> {
|
public class EndNode<T> extends PatternNode<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ID of the pattenr that has been matched.
|
||||||
|
*/
|
||||||
private T patternId;
|
private T patternId;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new end node with the given ID.
|
||||||
|
* @param patternId the pattern ID.
|
||||||
|
*/
|
||||||
public EndNode(T patternId){
|
public EndNode(T patternId){
|
||||||
this.patternId = patternId;
|
this.patternId = patternId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the pattern ID.
|
||||||
|
* @return the pattern ID.
|
||||||
|
*/
|
||||||
public T getPatternId(){
|
public T getPatternId(){
|
||||||
return patternId;
|
return patternId;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,10 @@ package org.nwapw.abacus.lexing.pattern;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A node that is used as structural glue in pattern compilation.
|
||||||
|
* @param <T> the type that's used to tell which pattern this node belongs to.
|
||||||
|
*/
|
||||||
public class LinkNode<T> extends PatternNode<T> {
|
public class LinkNode<T> extends PatternNode<T> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,25 +1,56 @@
|
|||||||
package org.nwapw.abacus.lexing.pattern;
|
package org.nwapw.abacus.lexing.pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A match that has been generated by the lexer.
|
||||||
|
* @param <T> the type used to represent the ID of the pattern this match belongs to.
|
||||||
|
*/
|
||||||
public class Match<T> {
|
public class Match<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bottom range of the string, inclusive.
|
||||||
|
*/
|
||||||
private int from;
|
private int from;
|
||||||
|
/**
|
||||||
|
* The top range of the string, exclusive.
|
||||||
|
*/
|
||||||
private int to;
|
private int to;
|
||||||
|
/**
|
||||||
|
* The pattern type this match matched.
|
||||||
|
*/
|
||||||
private T type;
|
private T type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new match with the given parameters.
|
||||||
|
* @param from the bottom range of the string.
|
||||||
|
* @param to the top range of the string.
|
||||||
|
* @param type the type of the match.
|
||||||
|
*/
|
||||||
public Match(int from, int to, T type){
|
public Match(int from, int to, T type){
|
||||||
this.from = from;
|
this.from = from;
|
||||||
this.to = to;
|
this.to = to;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the bottom range bound of the string.
|
||||||
|
* @return the bottom range bound of the string.
|
||||||
|
*/
|
||||||
public int getFrom() {
|
public int getFrom() {
|
||||||
return from;
|
return from;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the top range bound of the string.
|
||||||
|
* @return the top range bound of the string.
|
||||||
|
*/
|
||||||
public int getTo() {
|
public int getTo() {
|
||||||
return to;
|
return to;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the pattern type of the node.
|
||||||
|
* @return the ID of the pattern that this match matched.
|
||||||
|
*/
|
||||||
public T getType() {
|
public T getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,33 @@ import java.util.HashMap;
|
|||||||
import java.util.Stack;
|
import java.util.Stack;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A pattern that can be compiled from a string and used in lexing.
|
||||||
|
* @param <T> the type that is used to identify and sort this pattern.
|
||||||
|
*/
|
||||||
public class Pattern<T> {
|
public class Pattern<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The ID of this pattern.
|
||||||
|
*/
|
||||||
private T id;
|
private T id;
|
||||||
|
/**
|
||||||
|
* The head of this pattern.
|
||||||
|
*/
|
||||||
private PatternNode<T> head;
|
private PatternNode<T> head;
|
||||||
|
/**
|
||||||
|
* The source string of this pattern.
|
||||||
|
*/
|
||||||
private String source;
|
private String source;
|
||||||
|
/**
|
||||||
|
* The index at which the compilation has stopped.
|
||||||
|
*/
|
||||||
private int index;
|
private int index;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A map of regex operator to functions that modify a PatternChain
|
||||||
|
* with the appropriate operation.
|
||||||
|
*/
|
||||||
private HashMap<Character, Function<PatternChain<T>, PatternChain<T>>> operations =
|
private HashMap<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);
|
||||||
@@ -19,11 +39,23 @@ public class Pattern<T> {
|
|||||||
put('?', Pattern.this::transformQuestion);
|
put('?', Pattern.this::transformQuestion);
|
||||||
}};
|
}};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A regex operator function that turns the chain
|
||||||
|
* into a one-or-more chain.
|
||||||
|
* @param chain the chain to transform.
|
||||||
|
* @return the modified chain.
|
||||||
|
*/
|
||||||
private PatternChain<T> transformPlus(PatternChain<T> chain){
|
private PatternChain<T> transformPlus(PatternChain<T> chain){
|
||||||
chain.tail.outputStates.add(chain.head);
|
chain.tail.outputStates.add(chain.head);
|
||||||
return chain;
|
return chain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A regex operator function that turns the chain
|
||||||
|
* into a zero-or-more chain.
|
||||||
|
* @param chain the chain to transform.
|
||||||
|
* @return the modified chain.
|
||||||
|
*/
|
||||||
private PatternChain<T> transformStar(PatternChain<T> chain){
|
private PatternChain<T> transformStar(PatternChain<T> chain){
|
||||||
LinkNode<T> newTail = new LinkNode<>();
|
LinkNode<T> newTail = new LinkNode<>();
|
||||||
LinkNode<T> newHead = new LinkNode<>();
|
LinkNode<T> newHead = new LinkNode<>();
|
||||||
@@ -36,6 +68,12 @@ public class Pattern<T> {
|
|||||||
return chain;
|
return chain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A regex operator function that turns the chain
|
||||||
|
* into a zero-or-one chain.
|
||||||
|
* @param chain the chain to transform.
|
||||||
|
* @return the modified chain.
|
||||||
|
*/
|
||||||
private PatternChain<T> transformQuestion(PatternChain<T> chain){
|
private PatternChain<T> transformQuestion(PatternChain<T> chain){
|
||||||
LinkNode<T> newTail = new LinkNode<>();
|
LinkNode<T> newTail = new LinkNode<>();
|
||||||
LinkNode<T> newHead = new LinkNode<>();
|
LinkNode<T> newHead = new LinkNode<>();
|
||||||
@@ -47,6 +85,11 @@ public class Pattern<T> {
|
|||||||
return chain;
|
return chain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Combines a collection of chains into one OR chain.
|
||||||
|
* @param collection the collection of chains to combine.
|
||||||
|
* @return the resulting OR chain.
|
||||||
|
*/
|
||||||
private PatternChain<T> combineChains(Collection<PatternChain<T>> collection){
|
private PatternChain<T> combineChains(Collection<PatternChain<T>> collection){
|
||||||
LinkNode<T> head = new LinkNode<>();
|
LinkNode<T> head = new LinkNode<>();
|
||||||
LinkNode<T> tail = new LinkNode<>();
|
LinkNode<T> tail = new LinkNode<>();
|
||||||
@@ -58,6 +101,10 @@ public class Pattern<T> {
|
|||||||
return newChain;
|
return newChain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a single value from the input into a chain.
|
||||||
|
* @return the resulting chain, or null on error.
|
||||||
|
*/
|
||||||
private PatternChain<T> parseValue(){
|
private PatternChain<T> parseValue(){
|
||||||
if(index >= source.length()) return null;
|
if(index >= source.length()) return null;
|
||||||
if(source.charAt(index) == '\\'){
|
if(source.charAt(index) == '\\'){
|
||||||
@@ -66,6 +113,10 @@ public class Pattern<T> {
|
|||||||
return new PatternChain<>(new ValueNode<>(source.charAt(index++)));
|
return new PatternChain<>(new ValueNode<>(source.charAt(index++)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a [] range from the input into a chain.
|
||||||
|
* @return the resulting chain, or null on error.
|
||||||
|
*/
|
||||||
private PatternChain<T> parseOr(){
|
private PatternChain<T> parseOr(){
|
||||||
Stack<PatternChain<T>> orStack = new Stack<>();
|
Stack<PatternChain<T>> orStack = new Stack<>();
|
||||||
index++;
|
index++;
|
||||||
@@ -88,6 +139,12 @@ public class Pattern<T> {
|
|||||||
return (orStack.size() == 1) ? orStack.pop() : combineChains(orStack);
|
return (orStack.size() == 1) ? orStack.pop() : combineChains(orStack);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a repeatable segment from the input into a chain
|
||||||
|
* @param isSubsegment whether the segment is a sub-expression "()", and therefore
|
||||||
|
* whether to expect a closing brace.
|
||||||
|
* @return the resulting chain, or null on error.
|
||||||
|
*/
|
||||||
private PatternChain<T> parseSegment(boolean isSubsegment){
|
private PatternChain<T> parseSegment(boolean isSubsegment){
|
||||||
if(index >= source.length() || ((source.charAt(index) != '(') && isSubsegment)) return null;
|
if(index >= source.length() || ((source.charAt(index) != '(') && isSubsegment)) return null;
|
||||||
if(isSubsegment) index++;
|
if(isSubsegment) index++;
|
||||||
@@ -152,6 +209,11 @@ public class Pattern<T> {
|
|||||||
return fullChain;
|
return fullChain;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates / compiles a new pattern with the given id from the given string.
|
||||||
|
* @param from the string to compile a pattern from.
|
||||||
|
* @param id the ID to use.
|
||||||
|
*/
|
||||||
public Pattern(String from, T id){
|
public Pattern(String from, T id){
|
||||||
this.id = id;
|
this.id = id;
|
||||||
index = 0;
|
index = 0;
|
||||||
@@ -166,6 +228,10 @@ public class Pattern<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the head PatternNode, for use in matching
|
||||||
|
* @return the pattern node.
|
||||||
|
*/
|
||||||
public PatternNode<T> getHead() {
|
public PatternNode<T> getHead() {
|
||||||
return head;
|
return head;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,23 +1,52 @@
|
|||||||
package org.nwapw.abacus.lexing.pattern;
|
package org.nwapw.abacus.lexing.pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A chain of nodes that can be treated as a single unit.
|
||||||
|
* Used during pattern compilation.
|
||||||
|
* @param <T> the type used to identify which pattern has been matched.
|
||||||
|
*/
|
||||||
public class PatternChain<T> {
|
public class PatternChain<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The head node of the chain.
|
||||||
|
*/
|
||||||
public PatternNode<T> head;
|
public PatternNode<T> head;
|
||||||
|
/**
|
||||||
|
* The tail node of the chain.
|
||||||
|
*/
|
||||||
public PatternNode<T> tail;
|
public PatternNode<T> tail;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new chain with the given start and end.
|
||||||
|
* @param head the start of the chain.
|
||||||
|
* @param tail the end of the chain.
|
||||||
|
*/
|
||||||
public PatternChain(PatternNode<T> head, PatternNode<T> tail){
|
public PatternChain(PatternNode<T> head, PatternNode<T> tail){
|
||||||
this.head = head;
|
this.head = head;
|
||||||
this.tail = tail;
|
this.tail = tail;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a chain that starts and ends with the same node.
|
||||||
|
* @param node the node to use.
|
||||||
|
*/
|
||||||
public PatternChain(PatternNode<T> node){
|
public PatternChain(PatternNode<T> node){
|
||||||
this(node, node);
|
this(node, node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates an empty chain.
|
||||||
|
*/
|
||||||
public PatternChain(){
|
public PatternChain(){
|
||||||
this(null);
|
this(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends the other chain to this one. This modifies
|
||||||
|
* the nodes, as well.
|
||||||
|
* If this chain is empty, it is set to the other.
|
||||||
|
* @param other the other chain to append.
|
||||||
|
*/
|
||||||
public void append(PatternChain<T> other){
|
public void append(PatternChain<T> other){
|
||||||
if(other.head == null || tail == null) {
|
if(other.head == null || tail == null) {
|
||||||
this.head = other.head;
|
this.head = other.head;
|
||||||
@@ -28,6 +57,12 @@ public class PatternChain<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Appends a single node to this chain. This modifies
|
||||||
|
* the nodes, as well.
|
||||||
|
* If this chain is empty, it is set to the node.
|
||||||
|
* @param node the node to append to this chain.
|
||||||
|
*/
|
||||||
public void append(PatternNode<T> node){
|
public void append(PatternNode<T> node){
|
||||||
if(tail == null){
|
if(tail == null){
|
||||||
head = tail = node;
|
head = tail = node;
|
||||||
|
|||||||
@@ -4,26 +4,58 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A base class for a pattern node. Provides all functions
|
||||||
|
* necessary for matching, and is constructed by a Pattern instance
|
||||||
|
* from a string.
|
||||||
|
* @param <T> the type that's used to tell which pattern this node belongs to.
|
||||||
|
*/
|
||||||
public class PatternNode<T> {
|
public class PatternNode<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The set of states to which the lexer should continue
|
||||||
|
* should this node be correctly matched.
|
||||||
|
*/
|
||||||
protected HashSet<PatternNode<T>> outputStates;
|
protected HashSet<PatternNode<T>> outputStates;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new pattern node.
|
||||||
|
*/
|
||||||
public PatternNode(){
|
public PatternNode(){
|
||||||
outputStates = new HashSet<>();
|
outputStates = new HashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Determines whether the current input character can
|
||||||
|
* be matched by this node.
|
||||||
|
* @param other the character being matched.
|
||||||
|
* @return true if the character can be matched, false otherwise.
|
||||||
|
*/
|
||||||
public boolean matches(char other){
|
public boolean matches(char other){
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If this node can be used as part of a range, returns that value.
|
||||||
|
* @return a NULL terminator if this character cannot be converted
|
||||||
|
* into a range bound, or the appropriate range bound if it can.
|
||||||
|
*/
|
||||||
public char range(){
|
public char range(){
|
||||||
return '\0';
|
return '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds this node in a collection of other nodes.
|
||||||
|
* @param into the collection to add into.
|
||||||
|
*/
|
||||||
public void addInto(Collection<PatternNode<T>> into){
|
public void addInto(Collection<PatternNode<T>> into){
|
||||||
into.add(this);
|
into.add(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds the node's children into a collection of other nodes.
|
||||||
|
* @param into the collection to add into.
|
||||||
|
*/
|
||||||
public void addOutputsInto(Collection<PatternNode<T>> into){
|
public void addOutputsInto(Collection<PatternNode<T>> into){
|
||||||
outputStates.forEach(e -> e.addInto(into));
|
outputStates.forEach(e -> e.addInto(into));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,25 @@
|
|||||||
package org.nwapw.abacus.lexing.pattern;
|
package org.nwapw.abacus.lexing.pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A node that matches a range of characters.
|
||||||
|
* @param <T> the type that's used to tell which pattern this node belongs to.
|
||||||
|
*/
|
||||||
public class RangeNode<T> extends PatternNode<T> {
|
public class RangeNode<T> extends PatternNode<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The bottom bound of the range, inclusive.
|
||||||
|
*/
|
||||||
private char from;
|
private char from;
|
||||||
|
/**
|
||||||
|
* The top bound of the range, inclusive.
|
||||||
|
*/
|
||||||
private char to;
|
private char to;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new range node from the given range.
|
||||||
|
* @param from the bottom bound of the range.
|
||||||
|
* @param to the top bound of hte range.
|
||||||
|
*/
|
||||||
public RangeNode(char from, char to){
|
public RangeNode(char from, char to){
|
||||||
this.from = from;
|
this.from = from;
|
||||||
this.to = to;
|
this.to = to;
|
||||||
|
|||||||
@@ -1,9 +1,20 @@
|
|||||||
package org.nwapw.abacus.lexing.pattern;
|
package org.nwapw.abacus.lexing.pattern;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A node that matches a single value.
|
||||||
|
* @param <T> the type that's used to tell which pattern this node belongs to.
|
||||||
|
*/
|
||||||
public class ValueNode<T> extends PatternNode<T> {
|
public class ValueNode<T> extends PatternNode<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The value this node matches.
|
||||||
|
*/
|
||||||
private char value;
|
private char value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new node that matches the given character.
|
||||||
|
* @param value
|
||||||
|
*/
|
||||||
public ValueNode(char value){
|
public ValueNode(char value){
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
package org.nwapw.abacus.number;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public abstract class Function {
|
|
||||||
|
|
||||||
private static final HashMap<Class<? extends Number>, Integer> priorityMap =
|
|
||||||
new HashMap<Class<? extends Number>, Integer>() {{
|
|
||||||
put(NaiveNumber.class, 0);
|
|
||||||
}};
|
|
||||||
|
|
||||||
protected abstract boolean matchesParams(Number[] params);
|
|
||||||
protected abstract Number applyInternal(Number[] params);
|
|
||||||
|
|
||||||
public Number apply(Number...params) {
|
|
||||||
if(!matchesParams(params)) return null;
|
|
||||||
return applyInternal(params);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,77 +0,0 @@
|
|||||||
package org.nwapw.abacus.number;
|
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
|
|
||||||
public class FunctionDatabase {
|
|
||||||
|
|
||||||
private HashMap<String, Function> functions;
|
|
||||||
|
|
||||||
private void registerDefault(){
|
|
||||||
|
|
||||||
functions.put("+", new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(Number[] params) {
|
|
||||||
return params.length >= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Number applyInternal(Number[] params) {
|
|
||||||
Number sum = params[0];
|
|
||||||
for(int i = 1; i < params.length; i++){
|
|
||||||
sum = sum.add(params[i]);
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
functions.put("-", new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(Number[] params) {
|
|
||||||
return params.length == 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Number applyInternal(Number[] params) {
|
|
||||||
return params[0].subtract(params[1]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
functions.put("*", new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(Number[] params) {
|
|
||||||
return params.length >= 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Number applyInternal(Number[] params) {
|
|
||||||
Number product = params[1];
|
|
||||||
for(int i = 1; i < params.length; i++){
|
|
||||||
product = product.multiply(params[i]);
|
|
||||||
}
|
|
||||||
return product;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
functions.put("/", new Function() {
|
|
||||||
@Override
|
|
||||||
protected boolean matchesParams(Number[] params) {
|
|
||||||
return params.length == 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Number applyInternal(Number[] params) {
|
|
||||||
return params[0].divide(params[1]);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
public FunctionDatabase(){
|
|
||||||
functions = new HashMap<>();
|
|
||||||
registerDefault();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Function getFunction(String name){
|
|
||||||
return functions.get(name);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
66
src/org/nwapw/abacus/number/NaiveNumber.java
Normal file → Executable file
66
src/org/nwapw/abacus/number/NaiveNumber.java
Normal file → Executable file
@@ -1,57 +1,99 @@
|
|||||||
package org.nwapw.abacus.number;
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
public class NaiveNumber implements Number {
|
/**
|
||||||
|
* An implementation of NumberInterface using a double.
|
||||||
|
*/
|
||||||
|
public class NaiveNumber implements NumberInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The value of this number.
|
||||||
|
*/
|
||||||
private double value;
|
private double value;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new NaiveNumber with the given value.
|
||||||
|
* @param value the value to use.
|
||||||
|
*/
|
||||||
public NaiveNumber(double value) {
|
public NaiveNumber(double value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The number zero.
|
||||||
|
*/
|
||||||
|
public static final NaiveNumber ZERO = new NaiveNumber(0);
|
||||||
|
/**
|
||||||
|
* The number one.
|
||||||
|
*/
|
||||||
|
public static final NaiveNumber ONE = new NaiveNumber(1);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int precision() {
|
public int precision() {
|
||||||
return 4;
|
return 15;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number multiply(Number multiplier) {
|
public NumberInterface multiply(NumberInterface multiplier) {
|
||||||
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
|
return new NaiveNumber(value * ((NaiveNumber)multiplier).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number divide(Number divisor) {
|
public NumberInterface divide(NumberInterface divisor) {
|
||||||
return new NaiveNumber(value / ((NaiveNumber)divisor).value);
|
return new NaiveNumber(value / ((NaiveNumber)divisor).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number add(Number summand) {
|
public NumberInterface add(NumberInterface summand) {
|
||||||
return new NaiveNumber(value + ((NaiveNumber)summand).value);
|
return new NaiveNumber(value + ((NaiveNumber)summand).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number subtract(Number subtrahend) {
|
public NumberInterface subtract(NumberInterface subtrahend) {
|
||||||
return new NaiveNumber(value - ((NaiveNumber)subtrahend).value);
|
return new NaiveNumber(value - ((NaiveNumber)subtrahend).value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number negate() {
|
public NumberInterface negate() {
|
||||||
return new NaiveNumber(-value);
|
return new NaiveNumber(-value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number zero() {
|
public NumberInterface intPow(int exponent) {
|
||||||
return new NaiveNumber(0);
|
if(exponent == 0){
|
||||||
|
return NaiveNumber.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 = NaiveNumber.ONE.divide(power);
|
||||||
|
}
|
||||||
|
return power;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number one() {
|
public int compareTo(NumberInterface number) {
|
||||||
return new NaiveNumber(1);
|
NaiveNumber num = (NaiveNumber) number;
|
||||||
|
return Double.compare(value, num.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number promoteTo(Class<? extends Number> toClass) {
|
public int signum() {
|
||||||
|
return this.compareTo(ZERO);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface promoteTo(Class<? extends NumberInterface> toClass) {
|
||||||
if(toClass == this.getClass()) return this;
|
if(toClass == this.getClass()) return this;
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String toString(){
|
||||||
|
return Double.toString(value);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
package org.nwapw.abacus.number;
|
|
||||||
|
|
||||||
public interface Number {
|
|
||||||
|
|
||||||
int precision();
|
|
||||||
Number multiply(Number multiplier);
|
|
||||||
Number divide(Number divisor);
|
|
||||||
Number add(Number summand);
|
|
||||||
Number subtract(Number subtrahend);
|
|
||||||
Number negate();
|
|
||||||
|
|
||||||
Number zero();
|
|
||||||
Number one();
|
|
||||||
|
|
||||||
Number promoteTo(Class<? extends Number> toClass);
|
|
||||||
|
|
||||||
}
|
|
||||||
77
src/org/nwapw/abacus/number/NumberInterface.java
Executable file
77
src/org/nwapw/abacus/number/NumberInterface.java
Executable file
@@ -0,0 +1,77 @@
|
|||||||
|
package org.nwapw.abacus.number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An interface used to represent a number.
|
||||||
|
*/
|
||||||
|
public interface NumberInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The precision to which this number operates.
|
||||||
|
* @return the precision.
|
||||||
|
*/
|
||||||
|
int precision();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Multiplies this number by another, returning
|
||||||
|
* a new number instance.
|
||||||
|
* @param multiplier the multiplier
|
||||||
|
* @return the result of the multiplication.
|
||||||
|
*/
|
||||||
|
NumberInterface multiply(NumberInterface multiplier);
|
||||||
|
/**
|
||||||
|
* Divides this number by another, returning
|
||||||
|
* a new number instance.
|
||||||
|
* @param divisor the divisor
|
||||||
|
* @return the result of the division.
|
||||||
|
*/
|
||||||
|
NumberInterface divide(NumberInterface divisor);
|
||||||
|
/**
|
||||||
|
* Adds this number to another, returning
|
||||||
|
* a new number instance.
|
||||||
|
* @param summand the summand
|
||||||
|
* @return the result of the summation.
|
||||||
|
*/
|
||||||
|
NumberInterface add(NumberInterface summand);
|
||||||
|
/**
|
||||||
|
* Subtracts another number from this number,
|
||||||
|
* a new number instance.
|
||||||
|
* @param subtrahend the subtrahend.
|
||||||
|
* @return the result of the subtraction.
|
||||||
|
*/
|
||||||
|
NumberInterface subtract(NumberInterface subtrahend);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a new instance of this number with
|
||||||
|
* the sign flipped.
|
||||||
|
* @return the new instance.
|
||||||
|
*/
|
||||||
|
NumberInterface negate();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Raises this number to an integer power.
|
||||||
|
* @param exponent the exponent to which to take the number.
|
||||||
|
* @return the resulting value.
|
||||||
|
*/
|
||||||
|
NumberInterface intPow(int exponent);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compares this number to another.
|
||||||
|
* @param number the number to compare to.
|
||||||
|
* @return same as Integer.compare();
|
||||||
|
*/
|
||||||
|
int compareTo(NumberInterface number);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Same as Math.signum().
|
||||||
|
* @return 1 if this number is positive, -1 if this number is negative, 0 if this number is 0.
|
||||||
|
*/
|
||||||
|
int signum();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Promotes this class to another number class.
|
||||||
|
* @param toClass the class to promote to.
|
||||||
|
* @return the resulting new instance.
|
||||||
|
*/
|
||||||
|
NumberInterface promoteTo(Class<? extends NumberInterface> toClass);
|
||||||
|
|
||||||
|
}
|
||||||
161
src/org/nwapw/abacus/plugin/Plugin.java
Normal file
161
src/org/nwapw/abacus/plugin/Plugin.java
Normal file
@@ -0,0 +1,161 @@
|
|||||||
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.function.Function;
|
||||||
|
import org.nwapw.abacus.function.Operator;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A plugin class that can be externally implemented and loaded via the
|
||||||
|
* plugin manager. Plugins provide functionality to the calculator
|
||||||
|
* with the "hasFunction" and "getFunction" functions,
|
||||||
|
* and can use "registerFunction" and "functionFor" for
|
||||||
|
* loading internally.
|
||||||
|
*/
|
||||||
|
public abstract class Plugin {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A hash map of functions mapped to their string names.
|
||||||
|
*/
|
||||||
|
private HashMap<String, Function> functions;
|
||||||
|
/**
|
||||||
|
* A hash map of operators mapped to their string names.
|
||||||
|
*/
|
||||||
|
private HashMap<String, Operator> operators;
|
||||||
|
/**
|
||||||
|
* The plugin manager in which to search for functions
|
||||||
|
* not inside this package,
|
||||||
|
*/
|
||||||
|
private PluginManager manager;
|
||||||
|
/**
|
||||||
|
* Whether this plugin has been loaded.
|
||||||
|
*/
|
||||||
|
private boolean enabled;
|
||||||
|
|
||||||
|
private Plugin(){ }
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new plugin with the given PluginManager.
|
||||||
|
* @param manager the manager controlling this plugin.
|
||||||
|
*/
|
||||||
|
public Plugin(PluginManager manager) {
|
||||||
|
this.manager = manager;
|
||||||
|
functions = new HashMap<>();
|
||||||
|
operators = new HashMap<>();
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of functions provided by this plugin.
|
||||||
|
* @return the list of registered functions.
|
||||||
|
*/
|
||||||
|
public final Set<String> providedFunctions(){
|
||||||
|
return functions.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the list of functions provided by this plugin.
|
||||||
|
* @return the list of registered functions.
|
||||||
|
*/
|
||||||
|
public final Set<String> providedOperators(){
|
||||||
|
return operators.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets a function under the given function name.
|
||||||
|
* @param functionName the name of the function to get
|
||||||
|
* @return the function, or null if this plugin doesn't provide it.
|
||||||
|
*/
|
||||||
|
public final Function getFunction(String functionName) {
|
||||||
|
return functions.get(functionName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an operator under the given operator name.
|
||||||
|
* @param operatorName the name of the operator to get.
|
||||||
|
* @return the operator, or null if this plugin doesn't provide it.
|
||||||
|
*/
|
||||||
|
public final Operator getOperator(String operatorName) {
|
||||||
|
return operators.get(operatorName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enables the function, loading the necessary instances
|
||||||
|
* of functions.
|
||||||
|
*/
|
||||||
|
public final void enable(){
|
||||||
|
if(enabled) return;
|
||||||
|
onEnable();
|
||||||
|
enabled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disables the plugin, clearing loaded data store by default
|
||||||
|
* and calling its disable() method.
|
||||||
|
*/
|
||||||
|
public final void disable(){
|
||||||
|
if(!enabled) return;
|
||||||
|
onDisable();
|
||||||
|
functions.clear();
|
||||||
|
operators.clear();
|
||||||
|
enabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To be used in load(). Registers a function abstract class with the
|
||||||
|
* plugin internally, which makes it accessible to the plugin manager.
|
||||||
|
* @param name the name to register by.
|
||||||
|
* @param toRegister the function implementation.
|
||||||
|
*/
|
||||||
|
protected final void registerFunction(String name, Function toRegister) {
|
||||||
|
functions.put(name, toRegister);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To be used in load(). Registers an operator abstract class
|
||||||
|
* with the plugin internally, which makes it accessible to
|
||||||
|
* the plugin manager.
|
||||||
|
* @param name the name of the operator.
|
||||||
|
* @param operator the operator to register.
|
||||||
|
*/
|
||||||
|
protected final void registerOperator(String name, Operator operator) {
|
||||||
|
operators.put(name, operator);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the PluginManager for the given function name.
|
||||||
|
* This can be used by the plugins internally in order to call functions
|
||||||
|
* they do not provide.
|
||||||
|
* @param name the name for which to search
|
||||||
|
* @return the resulting function, or null if none was found for that name.
|
||||||
|
*/
|
||||||
|
protected final Function functionFor(String name) {
|
||||||
|
return manager.functionFor(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the PluginManager for the given operator name.
|
||||||
|
* This can be used by the plugins internally in order to call
|
||||||
|
* operations they do not provide.
|
||||||
|
* @param name the name for which to search
|
||||||
|
* @return the resulting operator, or null if none was found for that name.
|
||||||
|
*/
|
||||||
|
protected final Operator operatorFor(String name) {
|
||||||
|
return manager.operatorFor(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract method to be overridden by plugin implementation, in which the plugins
|
||||||
|
* are supposed to register the functions they provide and do any other
|
||||||
|
* necessary setup.
|
||||||
|
*/
|
||||||
|
public abstract void onEnable();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstract method overridden by the plugin implementation, in which the plugins
|
||||||
|
* are supposed to dispose of loaded functions, operators, and macros.
|
||||||
|
*/
|
||||||
|
public abstract void onDisable();
|
||||||
|
|
||||||
|
}
|
||||||
20
src/org/nwapw/abacus/plugin/PluginListener.java
Normal file
20
src/org/nwapw/abacus/plugin/PluginListener.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A listener that responds to changes in the PluginManager.
|
||||||
|
*/
|
||||||
|
public interface PluginListener {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the PluginManager loads plugins.
|
||||||
|
* @param manager the manager that fired the event.
|
||||||
|
*/
|
||||||
|
public void onLoad(PluginManager manager);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the PluginManager unloads all its plugins.
|
||||||
|
* @param manager the manager that fired the event.
|
||||||
|
*/
|
||||||
|
public void onUnload(PluginManager manager);
|
||||||
|
|
||||||
|
}
|
||||||
186
src/org/nwapw/abacus/plugin/PluginManager.java
Normal file
186
src/org/nwapw/abacus/plugin/PluginManager.java
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.function.Function;
|
||||||
|
import org.nwapw.abacus.function.Operator;
|
||||||
|
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A class that controls instances of plugins, allowing for them
|
||||||
|
* to interact with each other and the calculator.
|
||||||
|
*/
|
||||||
|
public class PluginManager {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A list of loaded plugins.
|
||||||
|
*/
|
||||||
|
private ArrayList<Plugin> plugins;
|
||||||
|
/**
|
||||||
|
* List of functions that have been cached,
|
||||||
|
* that is, found in a plugin and returned.
|
||||||
|
*/
|
||||||
|
private HashMap<String, Function> cachedFunctions;
|
||||||
|
/**
|
||||||
|
* List of operators tha have been cached,
|
||||||
|
* that is, found in a plugin and returned.
|
||||||
|
*/
|
||||||
|
private HashMap<String, Operator> cachedOperators;
|
||||||
|
/**
|
||||||
|
* List of all functions loaded by the plugins.
|
||||||
|
*/
|
||||||
|
private HashSet<String> allFunctions;
|
||||||
|
/**
|
||||||
|
* List of all operators loaded by the plugins.
|
||||||
|
*/
|
||||||
|
private HashSet<String> allOperators;
|
||||||
|
/**
|
||||||
|
* The list of plugin listeners attached to this instance.
|
||||||
|
*/
|
||||||
|
private HashSet<PluginListener> listeners;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new plugin manager.
|
||||||
|
*/
|
||||||
|
public PluginManager(){
|
||||||
|
plugins = new ArrayList<>();
|
||||||
|
cachedFunctions = new HashMap<>();
|
||||||
|
cachedOperators = new HashMap<>();
|
||||||
|
allFunctions = new HashSet<>();
|
||||||
|
allOperators = new HashSet<>();
|
||||||
|
listeners = new HashSet<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Searches the plugin list for a certain value, retrieving the Plugin's
|
||||||
|
* list of items of the type using the setFunction and getting the value
|
||||||
|
* of it is available via getFunction. If the value is contained
|
||||||
|
* in the cache, it returns the cached value instead.
|
||||||
|
* @param plugins the plugin list to search.
|
||||||
|
* @param cache the cache to use
|
||||||
|
* @param setFunction the function to retrieve a set of available T's from the plugin
|
||||||
|
* @param getFunction the function to get the T value under the given name
|
||||||
|
* @param name the name to search for
|
||||||
|
* @param <T> the type of element being search
|
||||||
|
* @return the retrieved element, or null if it was not found.
|
||||||
|
*/
|
||||||
|
private static <T> T searchCached(Collection<Plugin> plugins, Map<String, T> cache,
|
||||||
|
java.util.function.Function<Plugin, Set<String>> setFunction,
|
||||||
|
java.util.function.BiFunction<Plugin, String, T> getFunction,
|
||||||
|
String name){
|
||||||
|
if(cache.containsKey(name)) return cache.get(name);
|
||||||
|
|
||||||
|
T loadedValue = null;
|
||||||
|
for(Plugin plugin : plugins){
|
||||||
|
if(setFunction.apply(plugin).contains(name)){
|
||||||
|
loadedValue = getFunction.apply(plugin, name);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.put(name, loadedValue);
|
||||||
|
return loadedValue;
|
||||||
|
}
|
||||||
|
/**
|
||||||
|
* Gets a function under the given name.
|
||||||
|
* @param name the name of the function
|
||||||
|
* @return the function under the given name.
|
||||||
|
*/
|
||||||
|
public Function functionFor(String name){
|
||||||
|
return searchCached(plugins, cachedFunctions, Plugin::providedFunctions, Plugin::getFunction, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets an operator under the given name.
|
||||||
|
* @param name the name of the operator.
|
||||||
|
* @return the operator under the given name.
|
||||||
|
*/
|
||||||
|
public Operator operatorFor(String name){
|
||||||
|
return searchCached(plugins, cachedOperators, Plugin::providedOperators, Plugin::getOperator, name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds an instance of Plugin that already has been instantiated.
|
||||||
|
* @param plugin the plugin to add.
|
||||||
|
*/
|
||||||
|
public void addInstantiated(Plugin plugin){
|
||||||
|
plugins.add(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates a class of plugin, and adds it to this
|
||||||
|
* plugin manager.
|
||||||
|
* @param newClass the new class to instantiate.
|
||||||
|
*/
|
||||||
|
public void addClass(Class<?> newClass){
|
||||||
|
if(!Plugin.class.isAssignableFrom(newClass)) return;
|
||||||
|
try {
|
||||||
|
addInstantiated((Plugin) newClass.getConstructor(PluginManager.class).newInstance(this));
|
||||||
|
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Loads all the plugins in the PluginManager.
|
||||||
|
*/
|
||||||
|
public void load(){
|
||||||
|
for(Plugin plugin : plugins) plugin.enable();
|
||||||
|
for(Plugin plugin : plugins){
|
||||||
|
allFunctions.addAll(plugin.providedFunctions());
|
||||||
|
allOperators.addAll(plugin.providedOperators());
|
||||||
|
}
|
||||||
|
listeners.forEach(e -> e.onLoad(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unloads all the plugins in the PluginManager.
|
||||||
|
*/
|
||||||
|
public void unload(){
|
||||||
|
for(Plugin plugin : plugins) plugin.disable();
|
||||||
|
allFunctions.clear();
|
||||||
|
allOperators.clear();
|
||||||
|
listeners.forEach(e -> e.onUnload(this));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reloads all the plugins in the PluginManager.
|
||||||
|
*/
|
||||||
|
public void reload(){
|
||||||
|
unload();
|
||||||
|
reload();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the functions loaded by the Plugin Manager.
|
||||||
|
* @return the set of all functions that were loaded.
|
||||||
|
*/
|
||||||
|
public HashSet<String> getAllFunctions() {
|
||||||
|
return allFunctions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets all the operators loaded by the Plugin Manager.
|
||||||
|
* @return the set of all operators that were loaded.
|
||||||
|
*/
|
||||||
|
public HashSet<String> getAllOperators() {
|
||||||
|
return allOperators;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a plugin change listener to this plugin manager.
|
||||||
|
* @param listener the listener to add.
|
||||||
|
*/
|
||||||
|
public void addListener(PluginListener listener){
|
||||||
|
listeners.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove the plugin change listener from this plugin manager.
|
||||||
|
* @param listener the listener to remove.
|
||||||
|
*/
|
||||||
|
public void removeListener(PluginListener listener){
|
||||||
|
listeners.remove(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
171
src/org/nwapw/abacus/plugin/StandardPlugin.java
Executable file
171
src/org/nwapw/abacus/plugin/StandardPlugin.java
Executable file
@@ -0,0 +1,171 @@
|
|||||||
|
package org.nwapw.abacus.plugin;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.function.Function;
|
||||||
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
|
||||||
|
import java.util.function.BiFunction;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The plugin providing standard functions such as addition and subtraction to
|
||||||
|
* the calculator.
|
||||||
|
*/
|
||||||
|
public class StandardPlugin extends Plugin {
|
||||||
|
|
||||||
|
public StandardPlugin(PluginManager manager) {
|
||||||
|
super(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onEnable() {
|
||||||
|
registerFunction("+", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length >= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
NumberInterface sum = params[0];
|
||||||
|
for(int i = 1; i < params.length; i++){
|
||||||
|
sum = sum.add(params[i]);
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
registerFunction("-", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return params[0].subtract(params[1]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
registerFunction("*", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length >= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
NumberInterface product = params[0];
|
||||||
|
for(int i = 1; i < params.length; i++){
|
||||||
|
product = product.multiply(params[i]);
|
||||||
|
}
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
registerFunction("/", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return params[0].divide(params[1]);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
registerFunction("!", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
if(params[0].signum() == 0){
|
||||||
|
return (new NaiveNumber(1)).promoteTo(params[0].getClass());
|
||||||
|
}
|
||||||
|
NumberInterface factorial = params[0];
|
||||||
|
NumberInterface multiplier = params[0];
|
||||||
|
//It is necessary to later prevent calls of factorial on anything but non-negative integers.
|
||||||
|
while((multiplier = multiplier.subtract(NaiveNumber.ONE.promoteTo(multiplier.getClass()))).signum() == 1){
|
||||||
|
factorial = factorial.multiply(multiplier);
|
||||||
|
}
|
||||||
|
return factorial;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
registerFunction("exp", new Function() {
|
||||||
|
@Override
|
||||||
|
protected boolean matchesParams(NumberInterface[] params) {
|
||||||
|
return params.length == 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected NumberInterface applyInternal(NumberInterface[] params) {
|
||||||
|
return sumSeries(params[0], StandardPlugin.this::getExpSeriesTerm, getNTermsExp(getMaxError(params[0]), params[0]));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onDisable() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the nth term of the Taylor series (centered at 0) of e^x
|
||||||
|
* @param n the term required (n >= 0).
|
||||||
|
* @param x the real number at which the series is evaluated.
|
||||||
|
* @return the nth term of the series.
|
||||||
|
*/
|
||||||
|
private NumberInterface getExpSeriesTerm(int n, NumberInterface x){
|
||||||
|
return x.intPow(n).divide(this.getFunction("!").apply((new NaiveNumber(n)).promoteTo(x.getClass())));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the number of terms needed to evaluate the exponential function (at x)
|
||||||
|
* such that the error is at most maxError.
|
||||||
|
* @param maxError Maximum error permissible (This should probably be positive.)
|
||||||
|
* @param x where the function is evaluated.
|
||||||
|
* @return the number of terms needed to evaluated the exponential function.
|
||||||
|
*/
|
||||||
|
private int getNTermsExp(NumberInterface maxError, NumberInterface x){
|
||||||
|
//We need n such that x^(n+2) <= (n+1)! * maxError
|
||||||
|
//The variables LHS and RHS refer to the above inequality.
|
||||||
|
int n = 0;
|
||||||
|
NumberInterface LHS = x.intPow(2), RHS = maxError;
|
||||||
|
while(LHS.compareTo(RHS) > 0){
|
||||||
|
n++;
|
||||||
|
LHS = LHS.multiply(x);
|
||||||
|
RHS = RHS.multiply(new NaiveNumber(n).promoteTo(RHS.getClass()));
|
||||||
|
}
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a partial sum of a series whose terms are given by the nthTermFunction, evaluated at x.
|
||||||
|
* @param x the value at which the series is evaluated.
|
||||||
|
* @param nthTermFunction the function that returns the nth term of the series, in the format term(x, n).
|
||||||
|
* @param n the number of terms in the partial sum.
|
||||||
|
* @return the value of the partial sum that has the same class as x.
|
||||||
|
*/
|
||||||
|
private NumberInterface sumSeries(NumberInterface x, BiFunction<Integer, NumberInterface, NumberInterface> nthTermFunction, int n){
|
||||||
|
NumberInterface sum = NaiveNumber.ZERO.promoteTo(x.getClass());
|
||||||
|
for(int i = 0; i <= n; i++){
|
||||||
|
sum = sum.add(nthTermFunction.apply(i, x));
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the maximum error based on the precision of the class of number.
|
||||||
|
* @param number Any instance of the NumberInterface in question (should return an appropriate precision).
|
||||||
|
* @return the maximum error.
|
||||||
|
*/
|
||||||
|
private NumberInterface getMaxError(NumberInterface number){
|
||||||
|
return (new NaiveNumber(10)).promoteTo(number.getClass()).intPow(-number.precision());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
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,25 +1,57 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
import org.nwapw.abacus.number.NaiveNumber;
|
import org.nwapw.abacus.number.NaiveNumber;
|
||||||
import org.nwapw.abacus.number.Number;
|
import org.nwapw.abacus.number.NumberInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A node implementation that represents a single number.
|
||||||
|
*/
|
||||||
public class NumberNode extends TreeNode {
|
public class NumberNode extends TreeNode {
|
||||||
|
|
||||||
private Number number;
|
/**
|
||||||
|
* The number that is represented by this number node.
|
||||||
|
*/
|
||||||
|
private NumberInterface number;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a number node with no number.
|
||||||
|
*/
|
||||||
public NumberNode(){
|
public NumberNode(){
|
||||||
number = null;
|
number = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new number node with the given double value.
|
||||||
|
* @param value the value to use.
|
||||||
|
*/
|
||||||
public NumberNode(double value){
|
public NumberNode(double value){
|
||||||
number = new NaiveNumber(value);
|
number = new NaiveNumber(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new number node with the given string value, converted
|
||||||
|
* to a double.
|
||||||
|
* @param value the value.
|
||||||
|
*/
|
||||||
public NumberNode(String value){
|
public NumberNode(String value){
|
||||||
this(Double.parseDouble(value));
|
this(Double.parseDouble(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
public Number getNumber() {
|
/**
|
||||||
|
* Gets the number value of this node.
|
||||||
|
* @return the number value of this node.
|
||||||
|
*/
|
||||||
|
public NumberInterface getNumber() {
|
||||||
return number;
|
return number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
|
return reducer.reduceNode(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return number != null ? number.toString() : "null";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
48
src/org/nwapw/abacus/tree/NumberReducer.java
Normal file
48
src/org/nwapw/abacus/tree/NumberReducer.java
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public NumberInterface reduceNode(TreeNode node, Object... children) {
|
||||||
|
if(node instanceof NumberNode) {
|
||||||
|
return ((NumberNode) node).getNumber();
|
||||||
|
} else if(node instanceof OpNode){
|
||||||
|
NumberInterface left = (NumberInterface) children[0];
|
||||||
|
NumberInterface right = (NumberInterface) children[1];
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,38 +1,100 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A tree node that represents an operation being applied to two operands.
|
||||||
|
*/
|
||||||
public class OpNode extends TreeNode {
|
public class OpNode extends TreeNode {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The operation being applied.
|
||||||
|
*/
|
||||||
private String operation;
|
private String operation;
|
||||||
|
/**
|
||||||
|
* The left node of the operation.
|
||||||
|
*/
|
||||||
private TreeNode left;
|
private TreeNode left;
|
||||||
|
/**
|
||||||
|
* The right node of the operation.
|
||||||
|
*/
|
||||||
private TreeNode right;
|
private TreeNode right;
|
||||||
|
|
||||||
|
private OpNode() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new operation node with the given operation
|
||||||
|
* and no child nodes.
|
||||||
|
* @param operation the operation.
|
||||||
|
*/
|
||||||
public OpNode(String operation){
|
public OpNode(String operation){
|
||||||
this(operation, null, null);
|
this(operation, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new operation node with the given operation
|
||||||
|
* and child nodes.
|
||||||
|
* @param operation the operation.
|
||||||
|
* @param left the left node of the expression.
|
||||||
|
* @param right the right node of the expression.
|
||||||
|
*/
|
||||||
public OpNode(String operation, TreeNode left, TreeNode right){
|
public OpNode(String operation, TreeNode left, TreeNode right){
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the operation in this node.
|
||||||
|
* @return the operation in this node.
|
||||||
|
*/
|
||||||
public String getOperation() {
|
public String getOperation() {
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the left sub-expression of this node.
|
||||||
|
* @return the left node.
|
||||||
|
*/
|
||||||
public TreeNode getLeft() {
|
public TreeNode getLeft() {
|
||||||
return left;
|
return left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the left sub-expression of this node.
|
||||||
|
* @param left the sub-expression to apply.
|
||||||
|
*/
|
||||||
public void setLeft(TreeNode left) {
|
public void setLeft(TreeNode left) {
|
||||||
this.left = left;
|
this.left = left;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the right sub-expression of this node.
|
||||||
|
* @return the right node.
|
||||||
|
*/
|
||||||
public TreeNode getRight() {
|
public TreeNode getRight() {
|
||||||
return right;
|
return right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the right sub-expression of this node.
|
||||||
|
* @param right the sub-expression to apply.
|
||||||
|
*/
|
||||||
public void setRight(TreeNode right) {
|
public void setRight(TreeNode right) {
|
||||||
this.right = right;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String leftString = left != null ? left.toString() : "null";
|
||||||
|
String rightString = right != null ? right.toString() : "null";
|
||||||
|
|
||||||
|
return "(" + leftString + operation + rightString + ")";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
package org.nwapw.abacus.tree;
|
|
||||||
|
|
||||||
public enum OperatorAssociativity {
|
|
||||||
LEFT, RIGHT
|
|
||||||
}
|
|
||||||
17
src/org/nwapw/abacus/tree/Reducer.java
Normal file
17
src/org/nwapw/abacus/tree/Reducer.java
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface used to reduce a tree into a single value.
|
||||||
|
* @param <T> the value to reduce into.
|
||||||
|
*/
|
||||||
|
public interface Reducer<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reduces the given tree into a single value of type T.
|
||||||
|
* @param node the node being passed in to be reduced.
|
||||||
|
* @param children the already-reduced children of this node.
|
||||||
|
* @return the resulting value from the reduce.
|
||||||
|
*/
|
||||||
|
public T reduceNode(TreeNode node, Object...children);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,11 +1,23 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enum to represent the type of the token that has been matched
|
||||||
|
* by the lexer.
|
||||||
|
*/
|
||||||
public enum TokenType {
|
public enum TokenType {
|
||||||
|
|
||||||
ANY(0), OP(1), NUM(2), WORD(3), OPEN_PARENTH(4), CLOSE_PARENTH(5);
|
INTERNAL_FUNCTION_END(-1),
|
||||||
|
ANY(0), COMMA(1), OP(2), NUM(3), FUNCTION(4), OPEN_PARENTH(5), CLOSE_PARENTH(6);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The priority by which this token gets sorted.
|
||||||
|
*/
|
||||||
public final int priority;
|
public final int priority;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new token type with the given priority.
|
||||||
|
* @param priority the priority of this token type.
|
||||||
|
*/
|
||||||
TokenType(int priority){
|
TokenType(int priority){
|
||||||
this.priority = priority;
|
this.priority = priority;
|
||||||
}
|
}
|
||||||
|
|||||||
178
src/org/nwapw/abacus/tree/TreeBuilder.java
Normal file
178
src/org/nwapw/abacus/tree/TreeBuilder.java
Normal file
@@ -0,0 +1,178 @@
|
|||||||
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
|
import org.nwapw.abacus.lexing.Lexer;
|
||||||
|
import org.nwapw.abacus.lexing.pattern.Match;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The builder responsible for turning strings into trees.
|
||||||
|
*/
|
||||||
|
public class TreeBuilder {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The lexer used to get the input tokens.
|
||||||
|
*/
|
||||||
|
private Lexer<TokenType> lexer;
|
||||||
|
/**
|
||||||
|
* The map of operator precedences.
|
||||||
|
*/
|
||||||
|
private HashMap<String, Integer> precedenceMap;
|
||||||
|
/**
|
||||||
|
* The map of operator associativity.
|
||||||
|
*/
|
||||||
|
private HashMap<String, OperatorAssociativity> associativityMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comparator used to sort token types.
|
||||||
|
*/
|
||||||
|
protected static Comparator<TokenType> tokenSorter = Comparator.comparingInt(e -> e.priority);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new TreeBuilder.
|
||||||
|
*/
|
||||||
|
public TreeBuilder(){
|
||||||
|
lexer = new Lexer<TokenType>(){{
|
||||||
|
register(",", TokenType.COMMA);
|
||||||
|
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
|
||||||
|
register("\\(", TokenType.OPEN_PARENTH);
|
||||||
|
register("\\)", TokenType.CLOSE_PARENTH);
|
||||||
|
}};
|
||||||
|
precedenceMap = new HashMap<>();
|
||||||
|
associativityMap = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers a function with the TreeBuilder.
|
||||||
|
* @param function the function to register.
|
||||||
|
*/
|
||||||
|
public void registerFunction(String function){
|
||||||
|
lexer.register(function, TokenType.FUNCTION);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Registers an operator with the TreeBuilder.
|
||||||
|
* @param operator the operator to register.
|
||||||
|
* @param precedence the precedence of the operator.
|
||||||
|
* @param associativity the associativity of the operator.
|
||||||
|
*/
|
||||||
|
public void registerOperator(String operator, int precedence, OperatorAssociativity associativity){
|
||||||
|
lexer.register(operator, TokenType.OP);
|
||||||
|
precedenceMap.put(operator, precedence);
|
||||||
|
associativityMap.put(operator, associativity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tokenizes a string, converting it into matches
|
||||||
|
* @param string the string to tokenize.
|
||||||
|
* @return the list of tokens produced.
|
||||||
|
*/
|
||||||
|
public ArrayList<Match<TokenType>> tokenize(String string){
|
||||||
|
return lexer.lexAll(string, 0, tokenSorter);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Rearranges tokens into a postfix list, using Shunting Yard.
|
||||||
|
* @param source the source string.
|
||||||
|
* @param from the tokens to be rearranged.
|
||||||
|
* @return the resulting list of rearranged tokens.
|
||||||
|
*/
|
||||||
|
public ArrayList<Match<TokenType>> intoPostfix(String source, ArrayList<Match<TokenType>> from){
|
||||||
|
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
||||||
|
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||||
|
while(!from.isEmpty()){
|
||||||
|
Match<TokenType> match = from.remove(0);
|
||||||
|
TokenType matchType = match.getType();
|
||||||
|
if(matchType == TokenType.NUM) {
|
||||||
|
output.add(match);
|
||||||
|
} else if(matchType == TokenType.FUNCTION) {
|
||||||
|
output.add(new Match<>(0, 0, TokenType.INTERNAL_FUNCTION_END));
|
||||||
|
tokenStack.push(match);
|
||||||
|
} else if(matchType == TokenType.OP){
|
||||||
|
String tokenString = source.substring(match.getFrom(), match.getTo());
|
||||||
|
int precedence = precedenceMap.get(tokenString);
|
||||||
|
OperatorAssociativity associativity = associativityMap.get(tokenString);
|
||||||
|
|
||||||
|
while(!tokenStack.empty()) {
|
||||||
|
Match<TokenType> otherMatch = tokenStack.peek();
|
||||||
|
TokenType otherMatchType = otherMatch.getType();
|
||||||
|
if(otherMatchType != TokenType.OP) break;
|
||||||
|
|
||||||
|
int otherPrecdence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo()));
|
||||||
|
if(otherPrecdence < precedence ||
|
||||||
|
(associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
output.add(tokenStack.pop());
|
||||||
|
}
|
||||||
|
tokenStack.push(match);
|
||||||
|
} else if(matchType == TokenType.OPEN_PARENTH){
|
||||||
|
tokenStack.push(match);
|
||||||
|
} 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;
|
||||||
|
if(matchType == TokenType.CLOSE_PARENTH){
|
||||||
|
tokenStack.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while(!tokenStack.empty()){
|
||||||
|
Match<TokenType> match = tokenStack.peek();
|
||||||
|
TokenType matchType = match.getType();
|
||||||
|
if(!(matchType == TokenType.OP || matchType == TokenType.FUNCTION)) return null;
|
||||||
|
output.add(tokenStack.pop());
|
||||||
|
}
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a tree recursively from a list of tokens.
|
||||||
|
* @param source the source string.
|
||||||
|
* @param matches the list of tokens from the source string.
|
||||||
|
* @return the construct tree expression.
|
||||||
|
*/
|
||||||
|
public TreeNode fromStringRecursive(String source, ArrayList<Match<TokenType>> matches){
|
||||||
|
if(matches.size() == 0) return null;
|
||||||
|
Match<TokenType> match = matches.remove(0);
|
||||||
|
TokenType matchType = match.getType();
|
||||||
|
if(matchType == TokenType.OP){
|
||||||
|
TreeNode right = fromStringRecursive(source, matches);
|
||||||
|
TreeNode left = fromStringRecursive(source, matches);
|
||||||
|
if(left == null || right == null) return null;
|
||||||
|
else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right);
|
||||||
|
} else if(matchType == TokenType.NUM){
|
||||||
|
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
|
||||||
|
} else if(matchType == TokenType.FUNCTION){
|
||||||
|
String functionName = source.substring(match.getFrom(), match.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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a tree node from a string.
|
||||||
|
* @param string the string to create a node from.
|
||||||
|
* @return the resulting tree.
|
||||||
|
*/
|
||||||
|
public TreeNode fromString(String string){
|
||||||
|
ArrayList<Match<TokenType>> matches = tokenize(string);
|
||||||
|
if(matches == null) return null;
|
||||||
|
matches = intoPostfix(string, matches);
|
||||||
|
if(matches == null) return null;
|
||||||
|
|
||||||
|
Collections.reverse(matches);
|
||||||
|
return fromStringRecursive(string, matches);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,103 +1,22 @@
|
|||||||
package org.nwapw.abacus.tree;
|
package org.nwapw.abacus.tree;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.function.OperatorAssociativity;
|
||||||
import org.nwapw.abacus.lexing.Lexer;
|
import org.nwapw.abacus.lexing.Lexer;
|
||||||
import org.nwapw.abacus.lexing.pattern.Match;
|
import org.nwapw.abacus.lexing.pattern.Match;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* An abstract class that represents an expression tree node.
|
||||||
|
*/
|
||||||
public abstract class TreeNode {
|
public abstract class TreeNode {
|
||||||
|
|
||||||
private static Lexer<TokenType> lexer = new Lexer<TokenType>(){{
|
/**
|
||||||
register(".", TokenType.ANY);
|
* The function that reduces a tree to a single vale.
|
||||||
register("\\+|-|\\*|/|^", TokenType.OP);
|
* @param reducer the reducer used to reduce the tree.
|
||||||
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
|
* @param <T> the type the reducer produces.
|
||||||
register("[a-zA-Z]+", TokenType.WORD);
|
* @return the result of the reduction, or null on error.
|
||||||
register("\\(", TokenType.OPEN_PARENTH);
|
*/
|
||||||
register("\\)", TokenType.CLOSE_PARENTH);
|
public abstract <T> T reduce(Reducer<T> reducer);
|
||||||
}};
|
|
||||||
private static HashMap<String, Integer> precedenceMap = new HashMap<String, Integer>(){{
|
|
||||||
put("+", 0);
|
|
||||||
put("-", 0);
|
|
||||||
put("*", 1);
|
|
||||||
put("/", 1);
|
|
||||||
put("^", 2);
|
|
||||||
}};
|
|
||||||
private static HashMap<String, OperatorAssociativity> associativityMap =
|
|
||||||
new HashMap<String, OperatorAssociativity>() {{
|
|
||||||
put("+", OperatorAssociativity.LEFT);
|
|
||||||
put("-", OperatorAssociativity.LEFT);
|
|
||||||
put("*", OperatorAssociativity.LEFT);
|
|
||||||
put("/", OperatorAssociativity.LEFT);
|
|
||||||
put("^", OperatorAssociativity.RIGHT);
|
|
||||||
}};
|
|
||||||
|
|
||||||
private static Comparator<TokenType> tokenSorter = Comparator.comparingInt(e -> e.priority);
|
|
||||||
|
|
||||||
public static ArrayList<Match<TokenType>> tokenize(String string){
|
|
||||||
return lexer.lexAll(string, 0, tokenSorter);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static ArrayList<Match<TokenType>> intoPostfix(String source, ArrayList<Match<TokenType>> from){
|
|
||||||
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
|
||||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
|
||||||
while(!from.isEmpty()){
|
|
||||||
Match<TokenType> match = from.remove(0);
|
|
||||||
if(match.getType() == TokenType.NUM) {
|
|
||||||
output.add(match);
|
|
||||||
} else if(match.getType() == TokenType.OP){
|
|
||||||
String tokenString = source.substring(match.getFrom(), match.getTo());
|
|
||||||
int precedence = precedenceMap.get(tokenString);
|
|
||||||
OperatorAssociativity associativity = associativityMap.get(tokenString);
|
|
||||||
|
|
||||||
while(!tokenStack.empty()) {
|
|
||||||
Match<TokenType> otherMatch = tokenStack.peek();
|
|
||||||
if(otherMatch.getType() != TokenType.OP) break;
|
|
||||||
|
|
||||||
int otherPrecdence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo()));
|
|
||||||
if(otherPrecdence < precedence ||
|
|
||||||
(associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
output.add(tokenStack.pop());
|
|
||||||
}
|
|
||||||
tokenStack.push(match);
|
|
||||||
} else if(match.getType() == TokenType.OPEN_PARENTH){
|
|
||||||
tokenStack.push(match);
|
|
||||||
} else if(match.getType() == TokenType.CLOSE_PARENTH){
|
|
||||||
while(!tokenStack.empty() && tokenStack.peek().getType() != TokenType.OPEN_PARENTH){
|
|
||||||
output.add(tokenStack.pop());
|
|
||||||
}
|
|
||||||
if(tokenStack.empty()) return null;
|
|
||||||
tokenStack.pop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while(!tokenStack.empty()){
|
|
||||||
if(!(tokenStack.peek().getType() == TokenType.OP)) return null;
|
|
||||||
output.add(tokenStack.pop());
|
|
||||||
}
|
|
||||||
return output;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TreeNode fromStringRecursive(String source, ArrayList<Match<TokenType>> matches){
|
|
||||||
if(matches.size() == 0) return null;
|
|
||||||
Match<TokenType> match = matches.remove(0);
|
|
||||||
if(match.getType() == TokenType.OP){
|
|
||||||
TreeNode right = fromStringRecursive(source, matches);
|
|
||||||
TreeNode left = fromStringRecursive(source, matches);
|
|
||||||
if(left == null || right == null) return null;
|
|
||||||
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())));
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TreeNode fromString(String string){
|
|
||||||
ArrayList<Match<TokenType>> matches = intoPostfix(string, tokenize(string));
|
|
||||||
if(matches == null) return null;
|
|
||||||
|
|
||||||
Collections.reverse(matches);
|
|
||||||
return fromStringRecursive(string, matches);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
107
src/org/nwapw/abacus/window/HistoryTableModel.java
Normal file
107
src/org/nwapw/abacus/window/HistoryTableModel.java
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
package org.nwapw.abacus.window;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.tree.TreeNode;
|
||||||
|
|
||||||
|
import javax.swing.event.TableModelListener;
|
||||||
|
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;
|
||||||
|
public String output;
|
||||||
|
|
||||||
|
public HistoryEntry(String input, TreeNode parsedInput, String output){
|
||||||
|
this.input = input;
|
||||||
|
this.parsedInput = parsedInput;
|
||||||
|
this.output = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
Object nthValue(int n){
|
||||||
|
if(n == 0) return input;
|
||||||
|
if(n == 1) return parsedInput;
|
||||||
|
if(n == 2) return output;
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
public int getRowCount() {
|
||||||
|
return entries.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getColumnCount() {
|
||||||
|
return 3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getColumnName(int columnIndex) {
|
||||||
|
return COLUMN_NAMES[columnIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Class<?> getColumnClass(int columnIndex) {
|
||||||
|
return CLASS_TYPES[columnIndex];
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCellEditable(int rowIndex, int columnIndex) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getValueAt(int rowIndex, int columnIndex) {
|
||||||
|
return entries.get(rowIndex).nthValue(columnIndex);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
280
src/org/nwapw/abacus/window/Window.java
Normal file
280
src/org/nwapw/abacus/window/Window.java
Normal file
@@ -0,0 +1,280 @@
|
|||||||
|
package org.nwapw.abacus.window;
|
||||||
|
|
||||||
|
import org.nwapw.abacus.function.Operator;
|
||||||
|
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.*;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.datatransfer.StringSelection;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.awt.event.MouseAdapter;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The main UI window for the calculator.
|
||||||
|
*/
|
||||||
|
public class Window extends JFrame implements PluginListener {
|
||||||
|
|
||||||
|
private static final String CALC_STRING = "Calculate";
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The plugin manager used to retrieve functions.
|
||||||
|
*/
|
||||||
|
private PluginManager manager;
|
||||||
|
/**
|
||||||
|
* The builder used to construct the parse trees.
|
||||||
|
*/
|
||||||
|
private TreeBuilder builder;
|
||||||
|
/**
|
||||||
|
* The reducer used to evaluate the tree.
|
||||||
|
*/
|
||||||
|
private NumberReducer reducer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The last output by the calculator.
|
||||||
|
*/
|
||||||
|
private String lastOutput;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The tabbed pane that separates calculator contexts.
|
||||||
|
*/
|
||||||
|
private JTabbedPane pane;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The panel where the output occurs.
|
||||||
|
*/
|
||||||
|
private JPanel calculationPanel;
|
||||||
|
/**
|
||||||
|
* The text area reserved for the last output.
|
||||||
|
*/
|
||||||
|
private JTextArea lastOutputArea;
|
||||||
|
/**
|
||||||
|
* The table used for storing history results.
|
||||||
|
*/
|
||||||
|
private JTable historyTable;
|
||||||
|
/**
|
||||||
|
* The table model used for managing history.
|
||||||
|
*/
|
||||||
|
private HistoryTableModel historyModel;
|
||||||
|
/**
|
||||||
|
* The scroll pane for the history area.
|
||||||
|
*/
|
||||||
|
private JScrollPane historyScroll;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The panel where the input occurs.
|
||||||
|
*/
|
||||||
|
private JPanel inputPanel;
|
||||||
|
/**
|
||||||
|
* The input text field.
|
||||||
|
*/
|
||||||
|
private JTextField inputField;
|
||||||
|
/**
|
||||||
|
* The "submit" button.
|
||||||
|
*/
|
||||||
|
private JButton inputEnterButton;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The side panel for separate configuration.
|
||||||
|
*/
|
||||||
|
private JPanel settingsPanel;
|
||||||
|
/**
|
||||||
|
* Panel for elements relating to number
|
||||||
|
* system selection.
|
||||||
|
*/
|
||||||
|
private JPanel numberSystemPanel;
|
||||||
|
/**
|
||||||
|
* The possible list of number systems.
|
||||||
|
*/
|
||||||
|
private JComboBox<String> numberSystemList;
|
||||||
|
/**
|
||||||
|
* The panel for elements relating to
|
||||||
|
* function selection.
|
||||||
|
*/
|
||||||
|
private JPanel functionSelectPanel;
|
||||||
|
/**
|
||||||
|
* The list of functions available to the user.
|
||||||
|
*/
|
||||||
|
private JComboBox<String> functionList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Action listener that causes the input to be evaluated.
|
||||||
|
*/
|
||||||
|
private ActionListener evaluateListener = (event) -> {
|
||||||
|
if(builder == null) return;
|
||||||
|
TreeNode parsedExpression = builder.fromString(inputField.getText());
|
||||||
|
if(parsedExpression == null){
|
||||||
|
lastOutputArea.setText(SYNTAX_ERR_STRING);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
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
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new window with the given manager.
|
||||||
|
* @param manager the manager to use.
|
||||||
|
*/
|
||||||
|
public Window(PluginManager manager){
|
||||||
|
this();
|
||||||
|
this.manager = manager;
|
||||||
|
manager.addListener(this);
|
||||||
|
reducer = new NumberReducer(manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new window.
|
||||||
|
*/
|
||||||
|
private Window() {
|
||||||
|
super();
|
||||||
|
|
||||||
|
lastOutput = "";
|
||||||
|
|
||||||
|
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||||
|
setSize(320, 480);
|
||||||
|
|
||||||
|
inputField = new JTextField();
|
||||||
|
inputEnterButton = new JButton(CALC_STRING);
|
||||||
|
|
||||||
|
inputPanel = new JPanel();
|
||||||
|
inputPanel.setLayout(new BorderLayout());
|
||||||
|
inputPanel.add(inputField, BorderLayout.CENTER);
|
||||||
|
inputPanel.add(inputEnterButton, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
historyModel = new HistoryTableModel();
|
||||||
|
historyTable = new JTable(historyModel);
|
||||||
|
historyScroll = new JScrollPane(historyTable);
|
||||||
|
lastOutputArea = new JTextArea(lastOutput);
|
||||||
|
lastOutputArea.setEditable(false);
|
||||||
|
|
||||||
|
calculationPanel = new JPanel();
|
||||||
|
calculationPanel.setLayout(new BorderLayout());
|
||||||
|
calculationPanel.add(historyScroll, BorderLayout.CENTER);
|
||||||
|
calculationPanel.add(lastOutputArea, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
numberSystemList = new JComboBox<>();
|
||||||
|
|
||||||
|
numberSystemPanel = new JPanel();
|
||||||
|
numberSystemPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
|
numberSystemPanel.setLayout(new FlowLayout());
|
||||||
|
numberSystemPanel.add(new JLabel(NUMBER_SYSTEM_LABEL));
|
||||||
|
numberSystemPanel.add(numberSystemList);
|
||||||
|
numberSystemPanel.setMaximumSize(numberSystemPanel.getPreferredSize());
|
||||||
|
|
||||||
|
functionList = new JComboBox<>();
|
||||||
|
|
||||||
|
functionSelectPanel = new JPanel();
|
||||||
|
functionSelectPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
|
functionSelectPanel.setLayout(new FlowLayout());
|
||||||
|
functionSelectPanel.add(new JLabel(FUNCTION_LABEL));
|
||||||
|
functionSelectPanel.add(functionList);
|
||||||
|
functionSelectPanel.setMaximumSize(functionSelectPanel.getPreferredSize());
|
||||||
|
|
||||||
|
settingsPanel = new JPanel();
|
||||||
|
settingsPanel.setAlignmentX(Component.LEFT_ALIGNMENT);
|
||||||
|
settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.PAGE_AXIS));
|
||||||
|
settingsPanel.add(numberSystemPanel);
|
||||||
|
settingsPanel.add(functionSelectPanel);
|
||||||
|
|
||||||
|
pane = new JTabbedPane();
|
||||||
|
pane.add("Calculator", calculationPanel);
|
||||||
|
pane.add("Settings", settingsPanel);
|
||||||
|
pane.addChangeListener(e -> {
|
||||||
|
int selectionIndex = pane.getSelectedIndex();
|
||||||
|
boolean enabled = INPUT_ENABLED[selectionIndex];
|
||||||
|
ActionListener listener = listeners[selectionIndex];
|
||||||
|
inputEnterButton.setText(BUTTON_NAMES[selectionIndex]);
|
||||||
|
inputField.setEnabled(enabled);
|
||||||
|
inputEnterButton.setEnabled(enabled);
|
||||||
|
|
||||||
|
for(ActionListener removingListener : inputEnterButton.getActionListeners()){
|
||||||
|
inputEnterButton.removeActionListener(removingListener);
|
||||||
|
inputField.removeActionListener(removingListener);
|
||||||
|
}
|
||||||
|
if(listener != null){
|
||||||
|
inputEnterButton.addActionListener(listener);
|
||||||
|
inputField.addActionListener(listener);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
add(pane, BorderLayout.CENTER);
|
||||||
|
add(inputPanel, BorderLayout.SOUTH);
|
||||||
|
|
||||||
|
inputEnterButton.addActionListener(evaluateListener);
|
||||||
|
inputField.addActionListener(evaluateListener);
|
||||||
|
historyTable.addMouseListener(new MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(MouseEvent e) {
|
||||||
|
Point clickPoint = e.getPoint();
|
||||||
|
if(e.getClickCount() == 2){
|
||||||
|
int row = historyTable.rowAtPoint(clickPoint);
|
||||||
|
int column = historyTable.columnAtPoint(clickPoint);
|
||||||
|
String toCopy = historyTable.getValueAt(row, column).toString();
|
||||||
|
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(new StringSelection(toCopy), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@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.getPrecedence(), operatorObject.getAssociativity());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onUnload(PluginManager manager) {
|
||||||
|
builder = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user