Browse Source

more polymorphism, cleanup

master v1.0
Ondřej Hruška 9 years ago
parent
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

6
src/net/mightypork/rcalc/IToken.java

@ -7,5 +7,9 @@ package net.mightypork.rcalc;
* @author Ondrej Hruska
*/
public interface IToken {
/**
* Wrap this token in a tokenList (return itself if already instance of a TokenList)
* @return the wrapping TokenList
*/
public TokenList wrapInTokenList();
}

2
src/net/mightypork/rcalc/RCalcSession.java

@ -76,7 +76,7 @@ public class RCalcSession implements IDebugable {
if (expression.length() == 0) return null; // no operation
expression.replace(":=", "="); // normalize assignment sign
expression = expression.replace(":=", "="); // normalize assignment sign
String assignedVariable = null;
String assignmentOperation = "";

68
src/net/mightypork/rcalc/TokenList.java

@ -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>
* 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;
// variables for tokenLists of the operands
TokenList leftTL, rightTL;
// 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);
}
// get operands
TokenList leftArg = get(pos - 1).wrapInTokenList();
TokenList rightArg = get(pos + 1).wrapInTokenList();
// build an operation
Operation op = opToken.toOperation(leftTL, rightTL);
Operation op = opToken.toOperation(leftArg, rightArg);
// discard used tokens
subList(pos - 1, pos + 2).clear();
@ -156,20 +148,11 @@ public class TokenList extends ArrayList<IToken> implements IToken {
TokenUnaryOperatorLeft opToken = (TokenUnaryOperatorLeft) operatorToken;
// variable for left operand
TokenList leftTL;
// 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);
}
// get operand
TokenList leftArg = get(pos - 1).wrapInTokenList();
// build an operation
Operation op = opToken.toOperation(leftTL);
Operation op = opToken.toOperation(leftArg);
// discard used tokens
subList(pos - 1, pos + 1).clear();
@ -183,19 +166,10 @@ public class TokenList extends ArrayList<IToken> implements IToken {
TokenUnaryOperatorRight opToken = (TokenUnaryOperatorRight) operatorToken;
// variable for left operand
TokenList rightTL;
// 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);
}
TokenList rightArg = get(pos + 1).wrapInTokenList();
// build an operation
Operation op = opToken.toOperation(rightTL);
Operation op = opToken.toOperation(rightArg);
// discard used tokens
subList(pos, pos + 2).clear();
@ -265,4 +239,10 @@ public class TokenList extends ArrayList<IToken> implements IToken {
}
@Override
public TokenList wrapInTokenList() {
return this;
}
}

7
src/net/mightypork/rcalc/numbers/Fraction.java

@ -5,6 +5,7 @@ import java.math.BigInteger;
import net.mightypork.rcalc.IEvaluableToken;
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);
}
@Override
public TokenList wrapInTokenList() {
return new TokenList(this);
}
}

6
src/net/mightypork/rcalc/operations/Operation.java

@ -2,6 +2,7 @@ package net.mightypork.rcalc.operations;
import net.mightypork.rcalc.IEvaluableToken;
import net.mightypork.rcalc.TokenList;
import net.mightypork.rcalc.numbers.Fraction;
@ -14,4 +15,9 @@ public abstract class Operation implements IEvaluableToken {
@Override
public abstract Fraction evaluate();
@Override
public TokenList wrapInTokenList() {
return new TokenList(this);
}
}

2
src/net/mightypork/rcalc/tokens/TokenBinaryOperator.java

@ -10,7 +10,7 @@ import net.mightypork.rcalc.operations.Operation;
*
* @author Ondrej Hruska
*/
public abstract class TokenBinaryOperator implements IOperatorToken {
public abstract class TokenBinaryOperator extends TokenOperator {
/**
* Convert to operation

12
src/net/mightypork/rcalc/tokens/TokenOperator.java

@ -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);
}
}

6
src/net/mightypork/rcalc/tokens/TokenParenthesisLeft.java

@ -2,6 +2,7 @@ package net.mightypork.rcalc.tokens;
import net.mightypork.rcalc.IToken;
import net.mightypork.rcalc.TokenList;
/**
@ -17,4 +18,9 @@ public class TokenParenthesisLeft implements IToken {
return "(";
}
@Override
public TokenList wrapInTokenList() {
throw new RuntimeException("Cannot wrap a parenthesis in a TokenList!");
}
}

5
src/net/mightypork/rcalc/tokens/TokenParenthesisRight.java

@ -2,6 +2,7 @@ package net.mightypork.rcalc.tokens;
import net.mightypork.rcalc.IToken;
import net.mightypork.rcalc.TokenList;
/**
@ -17,4 +18,8 @@ public class TokenParenthesisRight implements IToken {
return ")";
}
@Override
public TokenList wrapInTokenList() {
throw new RuntimeException("Cannot wrap a parenthesis in a TokenList!");
}
}

2
src/net/mightypork/rcalc/tokens/TokenUnaryOperatorLeft.java

@ -10,7 +10,7 @@ import net.mightypork.rcalc.operations.Operation;
*
* @author Ondrej Hruska
*/
public abstract class TokenUnaryOperatorLeft implements IOperatorToken {
public abstract class TokenUnaryOperatorLeft extends TokenOperator {
/**
* Convert to operation

2
src/net/mightypork/rcalc/tokens/TokenUnaryOperatorRight.java

@ -10,7 +10,7 @@ import net.mightypork.rcalc.operations.Operation;
*
* @author Ondrej Hruska
*/
public abstract class TokenUnaryOperatorRight implements IOperatorToken {
public abstract class TokenUnaryOperatorRight extends TokenOperator {
/**
* Convert to operation

Loading…
Cancel
Save