1
0
mirror of https://github.com/DanilaFe/abacus synced 2024-12-22 23:40:08 -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 {
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.

View File

@ -35,6 +35,7 @@ public class TreeBuilder {
*/
public TreeBuilder(){
lexer = new Lexer<TokenType>(){{
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<TokenType> 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<Match<TokenType>> matches = tokenize(string);
if(matches == null) return null;
matches.removeIf(m -> m.getType() == TokenType.WHITESPACE);
matches = intoPostfix(string, matches);
if(matches == null) return null;