staying human-readable

This commit is contained in:
rusefi 2017-03-08 23:47:04 -05:00
parent 9fce2116ed
commit 107923c4f8
3 changed files with 25 additions and 8 deletions

View File

@ -120,7 +120,7 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
/** The negate unary operator in the standard operator precedence.*/
public static final Operator NEGATE = new Operator("negate", 1, Operator.Associativity.RIGHT, 3, "negate");
/** The negate unary operator in the Excel like operator precedence.*/
public static final Operator NEGATE_HIGH = new Operator("-", 1, Operator.Associativity.RIGHT, 5);
// public static final Operator NEGATE_HIGH = new Operator("-", 1, Operator.Associativity.RIGHT, 5);
/** The negate unary operator in the standard operator precedence.*/
public static final Operator NOT = new Operator("!", 1, Operator.Associativity.RIGHT, 3);
@ -245,7 +245,7 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
*/
@Override
protected Double evaluate(Operator operator, Iterator<Double> operands, Object evaluationContext) {
if (NEGATE.equals(operator) || NEGATE_HIGH.equals(operator)) {
if (NEGATE.equals(operator)) {
return -operands.next();
} else if (NOT.equals(operator) || NOT2.equals(operator)) {
return boolean2double(operands.next() != 1.0);

View File

@ -2,6 +2,8 @@ package com.fathzer.soft.javaluator;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
/** An <a href="http://en.wikipedia.org/wiki/Operator_(mathematics)">operator</a>.
* @author Jean-Marc Astesana
@ -10,8 +12,11 @@ import java.util.List;
@SuppressWarnings("WeakerAccess")
public class Operator {
private final String rpnSymbol;
public static final List<String> _1_OPERATORS = new ArrayList<>();
public static final List<String> _2_OPERATORS = new ArrayList<>();
private static Set<String> symbols = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
/** An Operator's <a href="http://en.wikipedia.org/wiki/Operator_associativity">associativity</a>.
*/
public enum Associativity {
@ -59,9 +64,17 @@ public class Operator {
this.operandCount = operandCount;
this.associativity = associativity;
this.precedence = precedence;
if (operandCount == 2) {
if (operandCount == 1) {
_1_OPERATORS.add(symbol);
} else if (operandCount == 2) {
_2_OPERATORS.add(symbol);
} else {
throw new IllegalStateException("Unexpected operand count: " + symbol);
}
if (symbols.contains(symbol))
throw new IllegalStateException("Not unique symbol " + symbol);
symbols.add(symbol);
}
/** Gets the operator's symbol.

View File

@ -12,12 +12,12 @@ import static org.junit.Assert.assertEquals;
public class ParserTest {
@Test
public void testBooleanConversion() throws ParseException {
assertParseB("2 1 >", "2 > 1");
assertParseB("1 2 + 3 -", "1 + 2 - 3");
assertParseB("1 2 | 3 |", "1 | 2 | 3");
assertParseB("1 2 + 3 4 5 + / +", "1 + 2 + 3 / (4 +5 )");
assertParseB("2 1 >", "2 > 1");
assertParse("rpm 0 >", "rpm > false");
assertParse("rpm 0 >", "(rpm > false)");
assertParse("rpm user0 > clt user2 > or", "(rpm > user0) or (clt > user2)");
@ -49,8 +49,12 @@ public class ParserTest {
Stack<String> stack = new Stack<>();
for (String token : tokens) {
if (Operator._1_OPERATORS.contains(token)) {
String a = stack.pop();
if (takesTwoParams(token)) {
stack.push("(" + token + " " + a + ")");
} else if (takesTwoParameters(token)) {
if (stack.size() < 2)
throw new IllegalStateException("Not enough " + token);
String a = stack.pop();
@ -61,11 +65,11 @@ public class ParserTest {
}
}
if (stack.size() != 1)
throw new IllegalStateException("Unexpected: " + stack);
throw new IllegalStateException("Unexpected stack content: " + stack);
return stack.pop();
}
private boolean takesTwoParams(String token) {
private boolean takesTwoParameters(String token) {
return Operator._2_OPERATORS.contains(token);
}
@ -93,7 +97,7 @@ public class ParserTest {
private void assertValue(String expectedRpn, String expression, double expectedValue) {
DoubleEvaluator evaluator = new DoubleEvaluator();
assertEquals(expectedValue, evaluator.evaluate(expression), 0.001);
assertParse(expectedRpn, expression);
assertParseB(expectedRpn, expression);
}
@Test