1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-23 07:50:09 -08:00

Add ignoring whitespace and fix function precedence.

This commit is contained in:
Danila Fedorin 2017-07-27 16:36:13 -07:00
parent 692ba2cdc5
commit 5373d953a6
2 changed files with 10 additions and 6 deletions

View File

@ -7,7 +7,7 @@ package org.nwapw.abacus.tree;
public enum TokenType { public enum TokenType {
INTERNAL_FUNCTION_END(-1), 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. * The priority by which this token gets sorted.

View File

@ -35,6 +35,7 @@ public class TreeBuilder {
*/ */
public TreeBuilder(){ public TreeBuilder(){
lexer = new Lexer<TokenType>(){{ lexer = new Lexer<TokenType>(){{
register(" ", TokenType.WHITESPACE);
register(",", TokenType.COMMA); register(",", TokenType.COMMA);
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM); register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
register("\\(", TokenType.OPEN_PARENTH); register("\\(", TokenType.OPEN_PARENTH);
@ -98,12 +99,14 @@ public class TreeBuilder {
while(!tokenStack.empty()) { while(!tokenStack.empty()) {
Match<TokenType> otherMatch = tokenStack.peek(); Match<TokenType> otherMatch = tokenStack.peek();
TokenType otherMatchType = otherMatch.getType(); 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(otherMatchType == TokenType.OP){
if(otherPrecdence < precedence || int otherPrecdence = precedenceMap.get(source.substring(otherMatch.getFrom(), otherMatch.getTo()));
(associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) { if(otherPrecdence < precedence ||
break; (associativity == OperatorAssociativity.RIGHT && otherPrecdence == precedence)) {
break;
}
} }
output.add(tokenStack.pop()); output.add(tokenStack.pop());
} }
@ -169,6 +172,7 @@ public class TreeBuilder {
public TreeNode fromString(String string){ public TreeNode fromString(String string){
ArrayList<Match<TokenType>> matches = tokenize(string); ArrayList<Match<TokenType>> matches = tokenize(string);
if(matches == null) return null; if(matches == null) return null;
matches.removeIf(m -> m.getType() == TokenType.WHITESPACE);
matches = intoPostfix(string, matches); matches = intoPostfix(string, matches);
if(matches == null) return null; if(matches == null) return null;