diff --git a/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java b/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java index b6210b2660..c7beed1239 100644 --- a/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java +++ b/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java @@ -120,7 +120,7 @@ public class DoubleEvaluator extends AbstractEvaluator { /** 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 { */ @Override protected Double evaluate(Operator operator, Iterator 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); diff --git a/java_console/models/src/com/fathzer/soft/javaluator/Operator.java b/java_console/models/src/com/fathzer/soft/javaluator/Operator.java index f175246689..65fa4c628b 100644 --- a/java_console/models/src/com/fathzer/soft/javaluator/Operator.java +++ b/java_console/models/src/com/fathzer/soft/javaluator/Operator.java @@ -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 operator. * @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 _1_OPERATORS = new ArrayList<>(); public static final List _2_OPERATORS = new ArrayList<>(); + private static Set symbols = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); + /** An Operator's associativity. */ 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. diff --git a/java_console/models/src/com/rusefi/test/ParserTest.java b/java_console/models/src/com/rusefi/test/ParserTest.java index e9d5ad32e2..cdae964033 100644 --- a/java_console/models/src/com/rusefi/test/ParserTest.java +++ b/java_console/models/src/com/rusefi/test/ParserTest.java @@ -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 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