more polymorphism, cleanup
This commit is contained in:
@@ -7,5 +7,9 @@ package net.mightypork.rcalc;
|
|||||||
* @author Ondrej Hruska
|
* @author Ondrej Hruska
|
||||||
*/
|
*/
|
||||||
public interface IToken {
|
public interface IToken {
|
||||||
|
/**
|
||||||
|
* Wrap this token in a tokenList (return itself if already instance of a TokenList)
|
||||||
|
* @return the wrapping TokenList
|
||||||
|
*/
|
||||||
|
public TokenList wrapInTokenList();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -76,7 +76,7 @@ public class RCalcSession implements IDebugable {
|
|||||||
|
|
||||||
if (expression.length() == 0) return null; // no operation
|
if (expression.length() == 0) return null; // no operation
|
||||||
|
|
||||||
expression.replace(":=", "="); // normalize assignment sign
|
expression = expression.replace(":=", "="); // normalize assignment sign
|
||||||
|
|
||||||
String assignedVariable = null;
|
String assignedVariable = null;
|
||||||
String assignmentOperation = "";
|
String assignmentOperation = "";
|
||||||
|
|||||||
@@ -35,6 +35,15 @@ public class TokenList extends ArrayList<IToken> implements IToken {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a tokenList with single item
|
||||||
|
* @param token the item
|
||||||
|
*/
|
||||||
|
public TokenList(IToken token) {
|
||||||
|
add(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the token list.<br>
|
* Parse the token list.<br>
|
||||||
* After running this method, the list is typically reduced to a single
|
* After running this method, the list is typically reduced to a single
|
||||||
@@ -121,29 +130,12 @@ public class TokenList extends ArrayList<IToken> implements IToken {
|
|||||||
|
|
||||||
TokenBinaryOperator opToken = (TokenBinaryOperator) operatorToken;
|
TokenBinaryOperator opToken = (TokenBinaryOperator) operatorToken;
|
||||||
|
|
||||||
// variables for tokenLists of the operands
|
// get operands
|
||||||
TokenList leftTL, rightTL;
|
TokenList leftArg = get(pos - 1).wrapInTokenList();
|
||||||
|
TokenList rightArg = get(pos + 1).wrapInTokenList();
|
||||||
// get left operand
|
|
||||||
IToken leftArg = get(pos - 1);
|
|
||||||
if (leftArg instanceof TokenList) {
|
|
||||||
leftTL = (TokenList) leftArg;
|
|
||||||
} else {
|
|
||||||
leftTL = new TokenList(1);
|
|
||||||
leftTL.add(leftArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get right operand
|
|
||||||
IToken rightArg = get(pos + 1);
|
|
||||||
if (rightArg instanceof TokenList) {
|
|
||||||
rightTL = (TokenList) rightArg;
|
|
||||||
} else {
|
|
||||||
rightTL = new TokenList(1);
|
|
||||||
rightTL.add(rightArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// build an operation
|
// build an operation
|
||||||
Operation op = opToken.toOperation(leftTL, rightTL);
|
Operation op = opToken.toOperation(leftArg, rightArg);
|
||||||
|
|
||||||
// discard used tokens
|
// discard used tokens
|
||||||
subList(pos - 1, pos + 2).clear();
|
subList(pos - 1, pos + 2).clear();
|
||||||
@@ -156,20 +148,11 @@ public class TokenList extends ArrayList<IToken> implements IToken {
|
|||||||
|
|
||||||
TokenUnaryOperatorLeft opToken = (TokenUnaryOperatorLeft) operatorToken;
|
TokenUnaryOperatorLeft opToken = (TokenUnaryOperatorLeft) operatorToken;
|
||||||
|
|
||||||
// variable for left operand
|
// get operand
|
||||||
TokenList leftTL;
|
TokenList leftArg = get(pos - 1).wrapInTokenList();
|
||||||
|
|
||||||
// get left operand (the only one)
|
|
||||||
IToken leftArg = get(pos - 1);
|
|
||||||
if (leftArg instanceof TokenList) {
|
|
||||||
leftTL = (TokenList) leftArg;
|
|
||||||
} else {
|
|
||||||
leftTL = new TokenList(1);
|
|
||||||
leftTL.add(leftArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// build an operation
|
// build an operation
|
||||||
Operation op = opToken.toOperation(leftTL);
|
Operation op = opToken.toOperation(leftArg);
|
||||||
|
|
||||||
// discard used tokens
|
// discard used tokens
|
||||||
subList(pos - 1, pos + 1).clear();
|
subList(pos - 1, pos + 1).clear();
|
||||||
@@ -183,19 +166,10 @@ public class TokenList extends ArrayList<IToken> implements IToken {
|
|||||||
TokenUnaryOperatorRight opToken = (TokenUnaryOperatorRight) operatorToken;
|
TokenUnaryOperatorRight opToken = (TokenUnaryOperatorRight) operatorToken;
|
||||||
|
|
||||||
// variable for left operand
|
// variable for left operand
|
||||||
TokenList rightTL;
|
TokenList rightArg = get(pos + 1).wrapInTokenList();
|
||||||
|
|
||||||
// get right operand (the only one)
|
|
||||||
IToken rightArg = get(pos + 1);
|
|
||||||
if (rightArg instanceof TokenList) {
|
|
||||||
rightTL = (TokenList) rightArg;
|
|
||||||
} else {
|
|
||||||
rightTL = new TokenList(1);
|
|
||||||
rightTL.add(rightArg);
|
|
||||||
}
|
|
||||||
|
|
||||||
// build an operation
|
// build an operation
|
||||||
Operation op = opToken.toOperation(rightTL);
|
Operation op = opToken.toOperation(rightArg);
|
||||||
|
|
||||||
// discard used tokens
|
// discard used tokens
|
||||||
subList(pos, pos + 2).clear();
|
subList(pos, pos + 2).clear();
|
||||||
@@ -265,4 +239,10 @@ public class TokenList extends ArrayList<IToken> implements IToken {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TokenList wrapInTokenList() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import java.math.BigInteger;
|
|||||||
|
|
||||||
import net.mightypork.rcalc.IEvaluableToken;
|
import net.mightypork.rcalc.IEvaluableToken;
|
||||||
import net.mightypork.rcalc.ParseError;
|
import net.mightypork.rcalc.ParseError;
|
||||||
|
import net.mightypork.rcalc.TokenList;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -461,4 +462,10 @@ public class Fraction implements IEvaluableToken {
|
|||||||
return new Fraction(result, BigInteger.ONE);
|
return new Fraction(result, BigInteger.ONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TokenList wrapInTokenList() {
|
||||||
|
return new TokenList(this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.mightypork.rcalc.operations;
|
|||||||
|
|
||||||
|
|
||||||
import net.mightypork.rcalc.IEvaluableToken;
|
import net.mightypork.rcalc.IEvaluableToken;
|
||||||
|
import net.mightypork.rcalc.TokenList;
|
||||||
import net.mightypork.rcalc.numbers.Fraction;
|
import net.mightypork.rcalc.numbers.Fraction;
|
||||||
|
|
||||||
|
|
||||||
@@ -14,4 +15,9 @@ public abstract class Operation implements IEvaluableToken {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract Fraction evaluate();
|
public abstract Fraction evaluate();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TokenList wrapInTokenList() {
|
||||||
|
return new TokenList(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import net.mightypork.rcalc.operations.Operation;
|
|||||||
*
|
*
|
||||||
* @author Ondrej Hruska
|
* @author Ondrej Hruska
|
||||||
*/
|
*/
|
||||||
public abstract class TokenBinaryOperator implements IOperatorToken {
|
public abstract class TokenBinaryOperator extends TokenOperator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert to operation
|
* Convert to operation
|
||||||
|
|||||||
@@ -0,0 +1,12 @@
|
|||||||
|
package net.mightypork.rcalc.tokens;
|
||||||
|
|
||||||
|
import net.mightypork.rcalc.TokenList;
|
||||||
|
|
||||||
|
public class TokenOperator implements IOperatorToken {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TokenList wrapInTokenList() {
|
||||||
|
return new TokenList(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,6 +2,7 @@ package net.mightypork.rcalc.tokens;
|
|||||||
|
|
||||||
|
|
||||||
import net.mightypork.rcalc.IToken;
|
import net.mightypork.rcalc.IToken;
|
||||||
|
import net.mightypork.rcalc.TokenList;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,4 +18,9 @@ public class TokenParenthesisLeft implements IToken {
|
|||||||
return "(";
|
return "(";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TokenList wrapInTokenList() {
|
||||||
|
throw new RuntimeException("Cannot wrap a parenthesis in a TokenList!");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package net.mightypork.rcalc.tokens;
|
|||||||
|
|
||||||
|
|
||||||
import net.mightypork.rcalc.IToken;
|
import net.mightypork.rcalc.IToken;
|
||||||
|
import net.mightypork.rcalc.TokenList;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -17,4 +18,8 @@ public class TokenParenthesisRight implements IToken {
|
|||||||
return ")";
|
return ")";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TokenList wrapInTokenList() {
|
||||||
|
throw new RuntimeException("Cannot wrap a parenthesis in a TokenList!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import net.mightypork.rcalc.operations.Operation;
|
|||||||
*
|
*
|
||||||
* @author Ondrej Hruska
|
* @author Ondrej Hruska
|
||||||
*/
|
*/
|
||||||
public abstract class TokenUnaryOperatorLeft implements IOperatorToken {
|
public abstract class TokenUnaryOperatorLeft extends TokenOperator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert to operation
|
* Convert to operation
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import net.mightypork.rcalc.operations.Operation;
|
|||||||
*
|
*
|
||||||
* @author Ondrej Hruska
|
* @author Ondrej Hruska
|
||||||
*/
|
*/
|
||||||
public abstract class TokenUnaryOperatorRight implements IOperatorToken {
|
public abstract class TokenUnaryOperatorRight extends TokenOperator {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert to operation
|
* Convert to operation
|
||||||
|
|||||||
Reference in New Issue
Block a user