diff --git a/java_console/models/src/com/fathzer/soft/javaluator/AbstractEvaluator.java b/java_console/models/src/com/fathzer/soft/javaluator/AbstractEvaluator.java index ae66658ee2..cd91fe1e14 100644 --- a/java_console/models/src/com/fathzer/soft/javaluator/AbstractEvaluator.java +++ b/java_console/models/src/com/fathzer/soft/javaluator/AbstractEvaluator.java @@ -135,7 +135,7 @@ public abstract class AbstractEvaluator { values.push(value!=null ? value : toValue(literal, evaluationContext)); } else if (token.isOperator()) { Operator operator = token.getOperator(); - rpnPush("deq", operator.getRpnSymbol()); + rpnPush("deq", operator.getSymbol()); values.push(evaluate(operator, getArguments(values, operator.getOperandCount()), evaluationContext)); } else { throw new IllegalArgumentException(); 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 eea1391297..b6210b2660 100644 --- a/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java +++ b/java_console/models/src/com/fathzer/soft/javaluator/DoubleEvaluator.java @@ -118,7 +118,7 @@ public class DoubleEvaluator extends AbstractEvaluator { public static final Function RANDOM = new Function("random", 0); /** The negate unary operator in the standard operator precedence.*/ - public static final Operator NEGATE = new Operator("-", 1, Operator.Associativity.RIGHT, 3, "negate"); + 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); @@ -219,9 +219,9 @@ public class DoubleEvaluator extends AbstractEvaluator { protected Double toValue(String literal, Object evaluationContext) { ParsePosition p = new ParsePosition(0); Number result = FORMATTER.get().parse(literal, p); - if (p.getIndex()==0 || p.getIndex()!=literal.length()) { - return 666.0; -// throw new IllegalArgumentException(literal+" is not a number"); + if (p.getIndex() == 0 || p.getIndex() != literal.length()) { + return 6666666.0; // let's return this magic in case of any function call +// throw new IllegalArgumentException(literal + " is not a number"); } return result.doubleValue(); } 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 949750b4b6..f175246689 100644 --- a/java_console/models/src/com/fathzer/soft/javaluator/Operator.java +++ b/java_console/models/src/com/fathzer/soft/javaluator/Operator.java @@ -7,8 +7,9 @@ import java.util.List; * @author Jean-Marc Astesana * @see License information */ +@SuppressWarnings("WeakerAccess") public class Operator { - public final String rpnSymbol; + private final String rpnSymbol; public static final List _2_OPERATORS = new ArrayList<>(); /** An Operator's associativity. @@ -36,11 +37,11 @@ public class Operator { * @throws IllegalArgumentException if operandCount if not 1 or 2 or if associativity is none * @throws NullPointerException if symbol or associativity are null */ - public Operator(String symbol, int operandCount, Associativity associativity, int precedence) { + Operator(String symbol, int operandCount, Associativity associativity, int precedence) { this(symbol, operandCount, associativity, precedence, null); } - public Operator(String symbol, int operandCount, Associativity associativity, int precedence, String rpnSymbol) { + Operator(String symbol, int operandCount, Associativity associativity, int precedence, String rpnSymbol) { this.rpnSymbol = rpnSymbol; if (symbol==null || associativity==null) { throw new NullPointerException(); @@ -59,7 +60,7 @@ public class Operator { this.associativity = associativity; this.precedence = precedence; if (operandCount == 2) { - _2_OPERATORS.add(getRpnSymbol()); + _2_OPERATORS.add(symbol); } } @@ -70,14 +71,6 @@ public class Operator { return this.symbol; } - - public String getRpnSymbol() { - if (rpnSymbol != null) - return rpnSymbol; - return symbol; - } - - /** Gets the operator's operand count. * @return an integer */ @@ -146,7 +139,6 @@ public class Operator { @Override public String toString() { return "Operator{" + - "rpnSymbol='" + rpnSymbol + '\'' + ", symbol='" + symbol + '\'' + ", precedence=" + precedence + ", operandCount=" + operandCount + diff --git a/java_console/models/src/com/rusefi/test/ParserTest.java b/java_console/models/src/com/rusefi/test/ParserTest.java index 2fe62d7dff..e9d5ad32e2 100644 --- a/java_console/models/src/com/rusefi/test/ParserTest.java +++ b/java_console/models/src/com/rusefi/test/ParserTest.java @@ -20,9 +20,9 @@ public class ParserTest { assertParseB("2 1 >", "2 > 1"); assertParse("rpm 0 >", "rpm > false"); assertParse("rpm 0 >", "(rpm > false)"); - assertParse("rpm user0 > clt user2 > |", "(rpm > user0) or (clt > user2)"); + assertParse("rpm user0 > clt user2 > or", "(rpm > user0) or (clt > user2)"); assertParse("1 2 | 3 |", "1 | 2 | 3"); - assertParse("rpm user0 > clt user2 > | vbatt user1 > |", "(rpm > user0) or (clt > user2) or (vbatt > user1)"); + assertParse("rpm user0 > clt user2 > or vbatt user1 > or", "(rpm > user0) or (clt > user2) or (vbatt > user1)"); } private void assertParseB(String rpn, String expression) { @@ -71,16 +71,22 @@ public class ParserTest { @Test - public void test() { - assertValue("2 3 6 ^ negate +", "2+-3^6", -727.0); + public void testUnaryMinus() { + /** + * do we even need to and/or should to support unary minus? + * + * http://stackoverflow.com/questions/20246787/handling-unary-minus-for-shunting-yard-algorithm + */ + assertValue("3 negate", "negate3", -3); + assertValue("2 3 6 ^ negate +", "2+negate 3^6", -727.0); } @Test public void testBooleanNot2() throws Exception { assertValue("0 !", "! 0", 1); - assertValue("0 !", "not 0", 1); - assertValue("1 !", "not(1)", 0); - assertValue("0 !", "not(0)", 1); + assertValue("0 not", "not 0", 1); + assertValue("1 not", "not(1)", 0); + assertValue("0 not", "not(0)", 1); // assertValue("1 0 | not 0 & 1 0 | |", "(((true | false) & not(false)) | (true | false))", 1); } @@ -92,31 +98,30 @@ public class ParserTest { @Test public void testRusEfi() { - assertParse("1 4 |", "1 or 4"); - assertParse("1 4 |", "1 OR 4"); + assertParse("1 4 or", "1 or 4"); + assertParse("1 4 or", "1 OR 4"); assertParseB("time_since_boot 4 <", "(time_since_boot < 4)"); assertParseB("1 4 |", "1 | 4"); assertParse("time_since_boot 4 < rpm 0 > |", "(time_since_boot < 4) | (rpm > 0)"); - assertParse("1 4 &", "1 and 4"); - assertParse("1 4 &", "1 AND 4"); + assertParse("1 4 and", "1 and 4"); + assertParse("1 4 and", "1 AND 4"); assertParseB("1 4 &", "1 & 4"); assertParse("coolant fan_off_setting >", "(coolant > fan_off_setting)"); - assertParse("1 3 |", "1 OR 3"); - assertParse("1 3 &", "1 and 3"); - assertParse("1 coolant fan_on_setting > |", "1 OR (coolant > fan_on_setting)"); - assertParse("fan coolant fan_off_setting > & coolant fan_on_setting > |", "(fan and (coolant > fan_off_setting)) OR (coolant > fan_on_setting)"); + + assertParse("1 coolant fan_on_setting > or", "1 OR (coolant > fan_on_setting)"); + assertParse("fan coolant fan_off_setting > and coolant fan_on_setting > or", "(fan and (coolant > fan_off_setting)) OR (coolant > fan_on_setting)"); assertParse("time_since_boot 4 <= rpm 0 > |", "(time_since_boot <= 4) | (rpm > 0)"); assertParse("time_since_boot 4 <= rpm 0 > |", "(time_since_boot <= 4) | (rpm > 0)"); - assertParse("time_since_boot 4 <= rpm 0 > |", "(time_since_boot <= 4) OR (rpm > 0)"); + assertParse("time_since_boot 4 <= rpm 0 > or", "(time_since_boot <= 4) OR (rpm > 0)"); - assertParse("self rpm 4800 >= & rpm 5000 > |", "(self and (rpm >= 4800)) OR (rpm > 5000)"); + assertParse("self rpm 4800 >= and rpm 5000 > or", "(self and (rpm >= 4800)) OR (rpm > 5000)"); }