mirror of
https://github.com/DanilaFe/abacus
synced 2026-01-10 17:25:19 +00:00
Add more comments.
This commit is contained in:
@@ -3,22 +3,44 @@ package org.nwapw.abacus.tree;
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.NumberInterface;
|
||||
|
||||
/**
|
||||
* A node implementation that represents a single number.
|
||||
*/
|
||||
public class NumberNode extends TreeNode {
|
||||
|
||||
/**
|
||||
* The number that is represented by this number node.
|
||||
*/
|
||||
private NumberInterface number;
|
||||
|
||||
/**
|
||||
* Creates a number node with no number.
|
||||
*/
|
||||
public NumberNode(){
|
||||
number = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new number node with the given double value.
|
||||
* @param value the value to use.
|
||||
*/
|
||||
public NumberNode(double value){
|
||||
number = new NaiveNumber(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new number node with the given string value, converted
|
||||
* to a double.
|
||||
* @param value the value.
|
||||
*/
|
||||
public NumberNode(String value){
|
||||
this(Double.parseDouble(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the number value of this node.
|
||||
* @return the number value of this node.
|
||||
*/
|
||||
public NumberInterface getNumber() {
|
||||
return number;
|
||||
}
|
||||
|
||||
@@ -1,37 +1,81 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
/**
|
||||
* A tree node that represents an operation being applied to two operands.
|
||||
*/
|
||||
public class OpNode extends TreeNode {
|
||||
|
||||
/**
|
||||
* The operation being applied.
|
||||
*/
|
||||
private String operation;
|
||||
/**
|
||||
* The left node of the operation.
|
||||
*/
|
||||
private TreeNode left;
|
||||
/**
|
||||
* The right node of the operation.
|
||||
*/
|
||||
private TreeNode right;
|
||||
|
||||
/**
|
||||
* Creates a new operation node with the given operation
|
||||
* and no child nodes.
|
||||
* @param operation the operation.
|
||||
*/
|
||||
public OpNode(String operation){
|
||||
this(operation, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new operation node with the given operation
|
||||
* and child nodes.
|
||||
* @param operation the operation.
|
||||
* @param left the left node of the expression.
|
||||
* @param right the right node of the expression.
|
||||
*/
|
||||
public OpNode(String operation, TreeNode left, TreeNode right){
|
||||
this.operation = operation;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the operation in this node.
|
||||
* @return the operation in this node.
|
||||
*/
|
||||
public String getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the left sub-expression of this node.
|
||||
* @return the left node.
|
||||
*/
|
||||
public TreeNode getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the left sub-expression of this node.
|
||||
* @param left the sub-expression to apply.
|
||||
*/
|
||||
public void setLeft(TreeNode left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the right sub-expression of this node.
|
||||
* @return the right node.
|
||||
*/
|
||||
public TreeNode getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the right sub-expression of this node.
|
||||
* @param right the sub-expression to apply.
|
||||
*/
|
||||
public void setRight(TreeNode right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
/**
|
||||
* Enum to represent the associativity of an operator.
|
||||
*/
|
||||
public enum OperatorAssociativity {
|
||||
LEFT, RIGHT
|
||||
}
|
||||
|
||||
@@ -1,11 +1,22 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
/**
|
||||
* Enum to represent the type of the token that has been matched
|
||||
* by the lexer.
|
||||
*/
|
||||
public enum TokenType {
|
||||
|
||||
ANY(0), OP(1), NUM(2), WORD(3), OPEN_PARENTH(4), CLOSE_PARENTH(5);
|
||||
|
||||
/**
|
||||
* The priority by which this token gets sorted.
|
||||
*/
|
||||
public final int priority;
|
||||
|
||||
/**
|
||||
* Creates a new token type with the given priority.
|
||||
* @param priority the priority of this token type.
|
||||
*/
|
||||
TokenType(int priority){
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,14 @@ import org.nwapw.abacus.lexing.pattern.Match;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* An abstract class that represents an expression tree node.
|
||||
*/
|
||||
public abstract class TreeNode {
|
||||
|
||||
/**
|
||||
* The lexer used to lex tokens.
|
||||
*/
|
||||
private static Lexer<TokenType> lexer = new Lexer<TokenType>(){{
|
||||
register(".", TokenType.ANY);
|
||||
register("\\+|-|\\*|/|^", TokenType.OP);
|
||||
@@ -15,6 +21,9 @@ public abstract class TreeNode {
|
||||
register("\\(", TokenType.OPEN_PARENTH);
|
||||
register("\\)", TokenType.CLOSE_PARENTH);
|
||||
}};
|
||||
/**
|
||||
* A map that maps operations to their precedence.
|
||||
*/
|
||||
private static HashMap<String, Integer> precedenceMap = new HashMap<String, Integer>(){{
|
||||
put("+", 0);
|
||||
put("-", 0);
|
||||
@@ -22,6 +31,9 @@ public abstract class TreeNode {
|
||||
put("/", 1);
|
||||
put("^", 2);
|
||||
}};
|
||||
/**
|
||||
* A map that maps operations to their associativity.
|
||||
*/
|
||||
private static HashMap<String, OperatorAssociativity> associativityMap =
|
||||
new HashMap<String, OperatorAssociativity>() {{
|
||||
put("+", OperatorAssociativity.LEFT);
|
||||
@@ -31,12 +43,26 @@ public abstract class TreeNode {
|
||||
put("^", OperatorAssociativity.RIGHT);
|
||||
}};
|
||||
|
||||
/**
|
||||
* Comparator used to sort token types.
|
||||
*/
|
||||
private static Comparator<TokenType> tokenSorter = Comparator.comparingInt(e -> e.priority);
|
||||
|
||||
/**
|
||||
* Tokenizes a string, converting it into matches
|
||||
* @param string the string to tokenize.
|
||||
* @return the list of tokens produced.
|
||||
*/
|
||||
public static ArrayList<Match<TokenType>> tokenize(String string){
|
||||
return lexer.lexAll(string, 0, tokenSorter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rearranges tokens into a postfix list, using Shunting Yard.
|
||||
* @param source the source string.
|
||||
* @param from the tokens to be rearranged.
|
||||
* @return the resulting list of rearranged tokens.
|
||||
*/
|
||||
public static ArrayList<Match<TokenType>> intoPostfix(String source, ArrayList<Match<TokenType>> from){
|
||||
ArrayList<Match<TokenType>> output = new ArrayList<>();
|
||||
Stack<Match<TokenType>> tokenStack = new Stack<>();
|
||||
@@ -78,6 +104,12 @@ public abstract class TreeNode {
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a tree recursively from a list of tokens.
|
||||
* @param source the source string.
|
||||
* @param matches the list of tokens from the source string.
|
||||
* @return the construct tree expression.
|
||||
*/
|
||||
public static TreeNode fromStringRecursive(String source, ArrayList<Match<TokenType>> matches){
|
||||
if(matches.size() == 0) return null;
|
||||
Match<TokenType> match = matches.remove(0);
|
||||
@@ -92,6 +124,11 @@ public abstract class TreeNode {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a tree node from a string.
|
||||
* @param string the string to create a node from.
|
||||
* @return the resulting tree.
|
||||
*/
|
||||
public static TreeNode fromString(String string){
|
||||
ArrayList<Match<TokenType>> matches = intoPostfix(string, tokenize(string));
|
||||
if(matches == null) return null;
|
||||
|
||||
Reference in New Issue
Block a user