mirror of
https://github.com/DanilaFe/abacus
synced 2025-01-09 15:54:13 -08:00
recognise variables
This commit is contained in:
parent
6a15c266c4
commit
b9c88b9d24
@ -13,10 +13,7 @@ import org.nwapw.abacus.Abacus;
|
||||
import org.nwapw.abacus.config.Configuration;
|
||||
import org.nwapw.abacus.number.ComputationInterruptedException;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
import org.nwapw.abacus.plugin.ClassFinder;
|
||||
import org.nwapw.abacus.plugin.PluginListener;
|
||||
import org.nwapw.abacus.plugin.PluginManager;
|
||||
import org.nwapw.abacus.plugin.StandardPlugin;
|
||||
import org.nwapw.abacus.plugin.*;
|
||||
import org.nwapw.abacus.tree.TreeNode;
|
||||
|
||||
import java.io.File;
|
||||
@ -217,6 +214,7 @@ public class AbacusController implements PluginListener {
|
||||
PluginManager abacusPluginManager = abacus.getPluginManager();
|
||||
abacusPluginManager.addListener(this);
|
||||
abacusPluginManager.addInstantiated(new StandardPlugin(abacus.getPluginManager()));
|
||||
abacusPluginManager.addInstantiated(new VariablePlugin(abacus.getPluginManager()));
|
||||
try {
|
||||
ClassFinder.loadJars("plugins").forEach(abacusPluginManager::addClass);
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
|
@ -57,6 +57,7 @@ public class Lexer<T> {
|
||||
* @return the best match.
|
||||
*/
|
||||
public Match<T> lexOne(String from, int startAt, Comparator<T> compare) {
|
||||
//boolean variable = true;
|
||||
ArrayList<Match<T>> matches = new ArrayList<>();
|
||||
HashSet<PatternNode<T>> currentSet = new HashSet<>();
|
||||
HashSet<PatternNode<T>> futureSet = new HashSet<>();
|
||||
@ -70,6 +71,7 @@ public class Lexer<T> {
|
||||
node.addOutputsInto(futureSet);
|
||||
} else if (node instanceof EndNode) {
|
||||
matches.add(new Match<>(from.substring(startAt, index), ((EndNode<T>) node).getPatternId()));
|
||||
//variable = false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -84,6 +86,9 @@ public class Lexer<T> {
|
||||
if (compare != null) {
|
||||
matches.sort(Comparator.comparingInt(a -> a.getContent().length()));
|
||||
}
|
||||
//if(variable&&) {
|
||||
// matches.add(new Match<>(from.substring(startAt, index), ((EndNode<T>) node).getPatternId()));
|
||||
//}
|
||||
return matches.isEmpty() ? null : matches.get(matches.size() - 1);
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,7 @@
|
||||
package org.nwapw.abacus.lexing.pattern;
|
||||
|
||||
import org.nwapw.abacus.tree.TokenType;
|
||||
|
||||
/**
|
||||
* A match that has been generated by the lexer.
|
||||
*
|
||||
|
@ -36,6 +36,7 @@ public class LexerTokenizer implements Tokenizer<Match<TokenType>>, PluginListen
|
||||
register("[0-9]*(\\.[0-9]+)?", TokenType.NUM);
|
||||
register("\\(", TokenType.OPEN_PARENTH);
|
||||
register("\\)", TokenType.CLOSE_PARENTH);
|
||||
register("[a-zA-Z]+",TokenType.VARIABLE);
|
||||
}};
|
||||
}
|
||||
|
||||
|
@ -63,7 +63,9 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
matchType = match.getType();
|
||||
if (matchType == TokenType.NUM) {
|
||||
output.add(match);
|
||||
} else if (matchType == TokenType.FUNCTION) {
|
||||
}else if(matchType == TokenType.VARIABLE) {
|
||||
output.add(match);
|
||||
}else if (matchType == TokenType.FUNCTION) {
|
||||
output.add(new Match<>("", TokenType.INTERNAL_FUNCTION_END));
|
||||
tokenStack.push(match);
|
||||
} else if (matchType == TokenType.OP) {
|
||||
@ -144,6 +146,8 @@ public class ShuntingYardParser implements Parser<Match<TokenType>>, PluginListe
|
||||
}
|
||||
} else if (matchType == TokenType.NUM) {
|
||||
return new NumberNode(abacus.numberFromString(match.getContent()));
|
||||
} else if (matchType == TokenType.VARIABLE){
|
||||
return new VariableNode(match.getContent());
|
||||
} else if (matchType == TokenType.FUNCTION) {
|
||||
String functionName = match.getContent();
|
||||
FunctionNode node = new FunctionNode(functionName);
|
||||
|
@ -7,7 +7,7 @@ package org.nwapw.abacus.tree;
|
||||
public enum TokenType {
|
||||
|
||||
INTERNAL_FUNCTION_END(-1),
|
||||
ANY(0), WHITESPACE(1), COMMA(2), OP(3), NUM(4), FUNCTION(5), OPEN_PARENTH(6), CLOSE_PARENTH(7);
|
||||
ANY(0), WHITESPACE(1), COMMA(2), OP(3), NUM(4), VARIABLE(5), FUNCTION(6), OPEN_PARENTH(7), CLOSE_PARENTH(8);
|
||||
|
||||
/**
|
||||
* The priority by which this token gets sorted.
|
||||
|
Loading…
Reference in New Issue
Block a user