mirror of
https://github.com/DanilaFe/abacus
synced 2025-01-09 07:44:14 -08:00
Implement parsing a postfix expression into a tree.
This commit is contained in:
parent
3684673362
commit
e4e9e2ce21
25
src/org/nwapw/abacus/tree/NumberNode.java
Normal file
25
src/org/nwapw/abacus/tree/NumberNode.java
Normal file
@ -0,0 +1,25 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
import org.nwapw.abacus.number.NaiveNumber;
|
||||
import org.nwapw.abacus.number.Number;
|
||||
|
||||
public class NumberNode extends TreeNode {
|
||||
|
||||
private Number number;
|
||||
|
||||
public NumberNode(){
|
||||
number = null;
|
||||
}
|
||||
|
||||
public NumberNode(double value){
|
||||
number = new NaiveNumber(value);
|
||||
}
|
||||
|
||||
public NumberNode(String value){
|
||||
this(Double.parseDouble(value));
|
||||
}
|
||||
|
||||
public Number getNumber() {
|
||||
return number;
|
||||
}
|
||||
}
|
38
src/org/nwapw/abacus/tree/OpNode.java
Normal file
38
src/org/nwapw/abacus/tree/OpNode.java
Normal file
@ -0,0 +1,38 @@
|
||||
package org.nwapw.abacus.tree;
|
||||
|
||||
public class OpNode extends TreeNode {
|
||||
|
||||
private String operation;
|
||||
private TreeNode left;
|
||||
private TreeNode right;
|
||||
|
||||
public OpNode(String operation){
|
||||
this(operation, null, null);
|
||||
}
|
||||
|
||||
public OpNode(String operation, TreeNode left, TreeNode right){
|
||||
this.operation = operation;
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public String getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public TreeNode getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(TreeNode left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public TreeNode getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(TreeNode right) {
|
||||
this.right = right;
|
||||
}
|
||||
}
|
@ -78,8 +78,26 @@ public abstract class TreeNode {
|
||||
return output;
|
||||
}
|
||||
|
||||
public static TreeNode fromString(String string){
|
||||
public static TreeNode fromStringRecursive(String source, ArrayList<Match<TokenType>> matches){
|
||||
if(matches.size() == 0) return null;
|
||||
Match<TokenType> match = matches.remove(0);
|
||||
if(match.getType() == TokenType.OP){
|
||||
TreeNode right = fromStringRecursive(source, matches);
|
||||
TreeNode left = fromStringRecursive(source, matches);
|
||||
if(left == null || right == null) return null;
|
||||
else return new OpNode(source.substring(match.getFrom(), match.getTo()), left, right);
|
||||
} else if(match.getType() == TokenType.NUM){
|
||||
return new NumberNode(Double.parseDouble(source.substring(match.getFrom(), match.getTo())));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static TreeNode fromString(String string){
|
||||
ArrayList<Match<TokenType>> matches = intoPostfix(string, tokenize(string));
|
||||
if(matches == null) return null;
|
||||
|
||||
Collections.reverse(matches);
|
||||
return fromStringRecursive(string, matches);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user