From 1c751353f1aeefd9cd3005d6093f826765982ce2 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 18 Aug 2017 14:21:14 -0700 Subject: [PATCH] Lex and parse variables. --- .../main/java/org/nwapw/abacus/parsing/LexerTokenizer.java | 1 + .../java/org/nwapw/abacus/parsing/ShuntingYardParser.java | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java b/core/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java index 790882a..b7bdd1f 100644 --- a/core/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java +++ b/core/src/main/java/org/nwapw/abacus/parsing/LexerTokenizer.java @@ -34,6 +34,7 @@ public class LexerTokenizer implements Tokenizer>, PluginListen register(" ", TokenType.WHITESPACE); register(",", TokenType.COMMA); register("[0-9]*(\\.[0-9]+)?", TokenType.NUM); + register("[a-zA-Z]+", TokenType.VARIABLE); register("\\(", TokenType.OPEN_PARENTH); register("\\)", TokenType.CLOSE_PARENTH); }}; diff --git a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java index c7a5615..af9936b 100644 --- a/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java +++ b/core/src/main/java/org/nwapw/abacus/parsing/ShuntingYardParser.java @@ -61,7 +61,7 @@ public class ShuntingYardParser implements Parser>, PluginListe Match match = from.remove(0); previousType = matchType; matchType = match.getType(); - if (matchType == TokenType.NUM) { + if (matchType == TokenType.NUM || matchType == TokenType.VARIABLE) { output.add(match); } else if (matchType == TokenType.FUNCTION) { output.add(new Match<>("", TokenType.INTERNAL_FUNCTION_END)); @@ -144,6 +144,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);