From 5373d953a6ff3e796280c2df86a6de5034f4eba6 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Thu, 27 Jul 2017 16:36:13 -0700 Subject: [PATCH] Add ignoring whitespace and fix function precedence. --- src/org/nwapw/abacus/tree/TokenType.java | 2 +- src/org/nwapw/abacus/tree/TreeBuilder.java | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/org/nwapw/abacus/tree/TokenType.java b/src/org/nwapw/abacus/tree/TokenType.java index 1d70998..b5629b5 100644 --- a/src/org/nwapw/abacus/tree/TokenType.java +++ b/src/org/nwapw/abacus/tree/TokenType.java @@ -7,7 +7,7 @@ package org.nwapw.abacus.tree; public enum TokenType { INTERNAL_FUNCTION_END(-1), - ANY(0), COMMA(1), OP(2), NUM(3), FUNCTION(4), OPEN_PARENTH(5), CLOSE_PARENTH(6); + ANY(0), WHITESPACE(1), COMMA(2), OP(3), NUM(4), FUNCTION(5), OPEN_PARENTH(6), CLOSE_PARENTH(7); /** * The priority by which this token gets sorted. diff --git a/src/org/nwapw/abacus/tree/TreeBuilder.java b/src/org/nwapw/abacus/tree/TreeBuilder.java index 30e9a5d..ea269af 100644 --- a/src/org/nwapw/abacus/tree/TreeBuilder.java +++ b/src/org/nwapw/abacus/tree/TreeBuilder.java @@ -35,6 +35,7 @@ public class TreeBuilder { */ public TreeBuilder(){ lexer = new Lexer(){{ + register(" ", TokenType.WHITESPACE); register(",", TokenType.COMMA); register("[0-9]+(\\.[0-9]+)?", TokenType.NUM); register("\\(", TokenType.OPEN_PARENTH); @@ -98,12 +99,14 @@ public class TreeBuilder { while(!tokenStack.empty()) { Match otherMatch = tokenStack.peek(); TokenType otherMatchType = otherMatch.getType(); - if(otherMatchType != TokenType.OP) break; + if(!(otherMatchType == TokenType.OP || otherMatchType == TokenType.FUNCTION)) break; - int otherPrecdence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo())); - if(otherPrecdence < precedence || - (associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) { - break; + if(otherMatchType == TokenType.OP){ + int otherPrecdence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo())); + if(otherPrecdence < precedence || + (associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) { + break; + } } output.add(tokenStack.pop()); } @@ -169,6 +172,7 @@ public class TreeBuilder { public TreeNode fromString(String string){ ArrayList> matches = tokenize(string); if(matches == null) return null; + matches.removeIf(m -> m.getType() == TokenType.WHITESPACE); matches = intoPostfix(string, matches); if(matches == null) return null;