From b9c88b9d241723e9c5ada69956eaa851a60c0777 Mon Sep 17 00:00:00 2001 From: Riley Jones Date: Mon, 7 Aug 2017 15:03:14 -0700 Subject: [PATCH] recognise variables --- src/main/java/org/nwapw/abacus/fx/AbacusController.java | 6 ++---- src/main/java/org/nwapw/abacus/lexing/Lexer.java | 5 +++++ src/main/java/org/nwapw/abacus/lexing/pattern/Match.java | 2 ++ src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java | 1 + .../java/org/nwapw/abacus/parsing/ShuntingYardParser.java | 6 +++++- src/main/java/org/nwapw/abacus/tree/TokenType.java | 2 +- 6 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/nwapw/abacus/fx/AbacusController.java b/src/main/java/org/nwapw/abacus/fx/AbacusController.java index 06b10ac..178cc7d 100644 --- a/src/main/java/org/nwapw/abacus/fx/AbacusController.java +++ b/src/main/java/org/nwapw/abacus/fx/AbacusController.java @@ -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) { diff --git a/src/main/java/org/nwapw/abacus/lexing/Lexer.java b/src/main/java/org/nwapw/abacus/lexing/Lexer.java index 1b7553b..e95064c 100644 --- a/src/main/java/org/nwapw/abacus/lexing/Lexer.java +++ b/src/main/java/org/nwapw/abacus/lexing/Lexer.java @@ -57,6 +57,7 @@ public class Lexer { * @return the best match. */ public Match lexOne(String from, int startAt, Comparator compare) { + //boolean variable = true; ArrayList> matches = new ArrayList<>(); HashSet> currentSet = new HashSet<>(); HashSet> futureSet = new HashSet<>(); @@ -70,6 +71,7 @@ public class Lexer { node.addOutputsInto(futureSet); } else if (node instanceof EndNode) { matches.add(new Match<>(from.substring(startAt, index), ((EndNode) node).getPatternId())); + //variable = false; } } @@ -84,6 +86,9 @@ public class Lexer { if (compare != null) { matches.sort(Comparator.comparingInt(a -> a.getContent().length())); } + //if(variable&&) { + // matches.add(new Match<>(from.substring(startAt, index), ((EndNode) node).getPatternId())); + //} return matches.isEmpty() ? null : matches.get(matches.size() - 1); } diff --git a/src/main/java/org/nwapw/abacus/lexing/pattern/Match.java b/src/main/java/org/nwapw/abacus/lexing/pattern/Match.java index 90edc82..1b9a6e0 100644 --- a/src/main/java/org/nwapw/abacus/lexing/pattern/Match.java +++ b/src/main/java/org/nwapw/abacus/lexing/pattern/Match.java @@ -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. * diff --git a/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java b/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java index 790882a..b199f5f 100644 --- a/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java +++ b/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java @@ -36,6 +36,7 @@ public class LexerTokenizer implements Tokenizer>, PluginListen register("[0-9]*(\\.[0-9]+)?", TokenType.NUM); register("\\(", TokenType.OPEN_PARENTH); register("\\)", TokenType.CLOSE_PARENTH); + register("[a-zA-Z]+",TokenType.VARIABLE); }}; } diff --git a/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java b/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java index 5d35032..40a185b 100644 --- a/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java +++ b/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java @@ -63,7 +63,9 @@ public class ShuntingYardParser implements Parser>, 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>, 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); diff --git a/src/main/java/org/nwapw/abacus/tree/TokenType.java b/src/main/java/org/nwapw/abacus/tree/TokenType.java index ebb8861..aa1957a 100644 --- a/src/main/java/org/nwapw/abacus/tree/TokenType.java +++ b/src/main/java/org/nwapw/abacus/tree/TokenType.java @@ -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.