mirror of
https://github.com/DanilaFe/abacus
synced 2024-12-24 08:12:39 -08:00
Merge branch 'master' into ui-touchup
This commit is contained in:
commit
367abb4157
|
@ -49,4 +49,9 @@ public class NumberNode extends TreeNode {
|
||||||
public <T> T reduce(Reducer<T> reducer) {
|
public <T> T reduce(Reducer<T> reducer) {
|
||||||
return reducer.reduceNode(this);
|
return reducer.reduceNode(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return number != null ? number.toString() : "null";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,4 +88,19 @@ public class OpNode extends TreeNode {
|
||||||
T rightReduce = right.reduce(reducer);
|
T rightReduce = right.reduce(reducer);
|
||||||
return reducer.reduceNode(this, leftReduce, rightReduce);
|
return reducer.reduceNode(this, leftReduce, rightReduce);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String leftString = left != null ? left.toString() : "null";
|
||||||
|
String rightString = right != null ? right.toString() : "null";
|
||||||
|
|
||||||
|
if(right != null && right instanceof OpNode){
|
||||||
|
if(TreeNode.precedenceMap.get(((OpNode) right).getOperation()) >
|
||||||
|
TreeNode.precedenceMap.get(operation)) {
|
||||||
|
rightString = "(" + rightString + ")";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return leftString + operation + rightString;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ public abstract class TreeNode {
|
||||||
/**
|
/**
|
||||||
* The lexer used to lex tokens.
|
* The lexer used to lex tokens.
|
||||||
*/
|
*/
|
||||||
private static Lexer<TokenType> lexer = new Lexer<TokenType>(){{
|
protected static Lexer<TokenType> lexer = new Lexer<TokenType>(){{
|
||||||
register("\\+|-|\\*|/|^", TokenType.OP);
|
register("\\+|-|\\*|/|^", TokenType.OP);
|
||||||
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
|
register("[0-9]+(\\.[0-9]+)?", TokenType.NUM);
|
||||||
register("[a-zA-Z]+", TokenType.WORD);
|
register("[a-zA-Z]+", TokenType.WORD);
|
||||||
|
@ -23,7 +23,7 @@ public abstract class TreeNode {
|
||||||
/**
|
/**
|
||||||
* A map that maps operations to their precedence.
|
* A map that maps operations to their precedence.
|
||||||
*/
|
*/
|
||||||
private static HashMap<String, Integer> precedenceMap = new HashMap<String, Integer>(){{
|
protected static HashMap<String, Integer> precedenceMap = new HashMap<String, Integer>(){{
|
||||||
put("+", 0);
|
put("+", 0);
|
||||||
put("-", 0);
|
put("-", 0);
|
||||||
put("*", 1);
|
put("*", 1);
|
||||||
|
@ -33,7 +33,7 @@ public abstract class TreeNode {
|
||||||
/**
|
/**
|
||||||
* A map that maps operations to their associativity.
|
* A map that maps operations to their associativity.
|
||||||
*/
|
*/
|
||||||
private static HashMap<String, OperatorAssociativity> associativityMap =
|
protected static HashMap<String, OperatorAssociativity> associativityMap =
|
||||||
new HashMap<String, OperatorAssociativity>() {{
|
new HashMap<String, OperatorAssociativity>() {{
|
||||||
put("+", OperatorAssociativity.LEFT);
|
put("+", OperatorAssociativity.LEFT);
|
||||||
put("-", OperatorAssociativity.LEFT);
|
put("-", OperatorAssociativity.LEFT);
|
||||||
|
@ -45,7 +45,7 @@ public abstract class TreeNode {
|
||||||
/**
|
/**
|
||||||
* Comparator used to sort token types.
|
* Comparator used to sort token types.
|
||||||
*/
|
*/
|
||||||
private static Comparator<TokenType> tokenSorter = Comparator.comparingInt(e -> e.priority);
|
protected static Comparator<TokenType> tokenSorter = Comparator.comparingInt(e -> e.priority);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tokenizes a string, converting it into matches
|
* Tokenizes a string, converting it into matches
|
||||||
|
|
Loading…
Reference in New Issue
Block a user