more polymorphism, cleanup

master v1.0
Ondřej Hruška 11 years ago
parent cf36cb222f
commit 90477d8d69
  1. 6
      src/net/mightypork/rcalc/IToken.java
  2. 2
      src/net/mightypork/rcalc/RCalcSession.java
  3. 68
      src/net/mightypork/rcalc/TokenList.java
  4. 7
      src/net/mightypork/rcalc/numbers/Fraction.java
  5. 6
      src/net/mightypork/rcalc/operations/Operation.java
  6. 2
      src/net/mightypork/rcalc/tokens/TokenBinaryOperator.java
  7. 12
      src/net/mightypork/rcalc/tokens/TokenOperator.java
  8. 6
      src/net/mightypork/rcalc/tokens/TokenParenthesisLeft.java
  9. 5
      src/net/mightypork/rcalc/tokens/TokenParenthesisRight.java
  10. 2
      src/net/mightypork/rcalc/tokens/TokenUnaryOperatorLeft.java
  11. 2
      src/net/mightypork/rcalc/tokens/TokenUnaryOperatorRight.java

@ -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

Loading…
Cancel
Save