mirror of https://github.com/rusefi/rusefi-1.git
postfix 2 infix
This commit is contained in:
parent
e563021963
commit
bf837065ab
|
@ -1,11 +1,15 @@
|
||||||
package com.fathzer.soft.javaluator;
|
package com.fathzer.soft.javaluator;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/** An <a href="http://en.wikipedia.org/wiki/Operator_(mathematics)">operator</a>.
|
/** An <a href="http://en.wikipedia.org/wiki/Operator_(mathematics)">operator</a>.
|
||||||
* @author Jean-Marc Astesana
|
* @author Jean-Marc Astesana
|
||||||
* @see <a href="../../../license.html">License information</a>
|
* @see <a href="../../../license.html">License information</a>
|
||||||
*/
|
*/
|
||||||
public class Operator {
|
public class Operator {
|
||||||
public final String rpnSymbol;
|
public final String rpnSymbol;
|
||||||
|
public static final List<String> _2_OPERATORS = new ArrayList<>();
|
||||||
|
|
||||||
/** An Operator's <a href="http://en.wikipedia.org/wiki/Operator_associativity">associativity</a>.
|
/** An Operator's <a href="http://en.wikipedia.org/wiki/Operator_associativity">associativity</a>.
|
||||||
*/
|
*/
|
||||||
|
@ -54,6 +58,9 @@ public class Operator {
|
||||||
this.operandCount = operandCount;
|
this.operandCount = operandCount;
|
||||||
this.associativity = associativity;
|
this.associativity = associativity;
|
||||||
this.precedence = precedence;
|
this.precedence = precedence;
|
||||||
|
if (operandCount == 2) {
|
||||||
|
_2_OPERATORS.add(getRpnSymbol());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Gets the operator's symbol.
|
/** Gets the operator's symbol.
|
||||||
|
|
|
@ -1,19 +1,23 @@
|
||||||
package com.rusefi.test;
|
package com.rusefi.test;
|
||||||
|
|
||||||
import com.fathzer.soft.javaluator.DoubleEvaluator;
|
import com.fathzer.soft.javaluator.DoubleEvaluator;
|
||||||
|
import com.fathzer.soft.javaluator.Operator;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.text.ParseException;
|
import java.text.ParseException;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
public class ParserTest {
|
public class ParserTest {
|
||||||
@Test
|
@Test
|
||||||
public void testBooleanConversion() throws ParseException {
|
public void testBooleanConversion() throws ParseException {
|
||||||
assertParse("1 2 + 3 -", "1 + 2 - 3");
|
assertParseB("1 2 + 3 -", "1 + 2 - 3");
|
||||||
assertParse("1 2 | 3 |", "1 | 2 | 3");
|
assertParseB("1 2 | 3 |", "1 | 2 | 3");
|
||||||
|
|
||||||
assertParse("2 1 >", "2 > 1");
|
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 0 >", "(rpm > false)");
|
assertParse("rpm 0 >", "(rpm > false)");
|
||||||
assertParse("rpm user0 > clt user2 > |", "(rpm > user0) or (clt > user2)");
|
assertParse("rpm user0 > clt user2 > |", "(rpm > user0) or (clt > user2)");
|
||||||
|
@ -21,6 +25,18 @@ public class ParserTest {
|
||||||
assertParse("rpm user0 > clt user2 > | vbatt user1 > |", "(rpm > user0) or (clt > user2) or (vbatt > user1)");
|
assertParse("rpm user0 > clt user2 > | vbatt user1 > |", "(rpm > user0) or (clt > user2) or (vbatt > user1)");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void assertParseB(String rpn, String expression) {
|
||||||
|
assertParse(rpn, expression);
|
||||||
|
String h = getInfix(rpn);
|
||||||
|
System.out.println(h);
|
||||||
|
|
||||||
|
assertEquals(getReplace(expression), getReplace(h));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getReplace(String h) {
|
||||||
|
return h.replaceAll("[\\(\\)]", "").replace(" ", "");
|
||||||
|
}
|
||||||
|
|
||||||
private void assertParse(String rpn, String expression) {
|
private void assertParse(String rpn, String expression) {
|
||||||
DoubleEvaluator evaluator = new DoubleEvaluator();
|
DoubleEvaluator evaluator = new DoubleEvaluator();
|
||||||
evaluator.evaluate(expression.toLowerCase());
|
evaluator.evaluate(expression.toLowerCase());
|
||||||
|
@ -28,6 +44,31 @@ public class ParserTest {
|
||||||
assertEquals(rpn, evaluator.getRusEfi());
|
assertEquals(rpn, evaluator.getRusEfi());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String getInfix(String rpn) {
|
||||||
|
String tokens[] = rpn.split(" ");
|
||||||
|
Stack<String> stack = new Stack<>();
|
||||||
|
|
||||||
|
for (String token : tokens) {
|
||||||
|
|
||||||
|
if (takesTwoParams(token)) {
|
||||||
|
if (stack.size() < 2)
|
||||||
|
throw new IllegalStateException("Not enough " + token);
|
||||||
|
String a = stack.pop();
|
||||||
|
String b = stack.pop();
|
||||||
|
stack.push("(" + b + " " + token + " " + a + ")");
|
||||||
|
} else {
|
||||||
|
stack.push(token);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (stack.size() != 1)
|
||||||
|
throw new IllegalStateException("Unexpected: " + stack);
|
||||||
|
return stack.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean takesTwoParams(String token) {
|
||||||
|
return Operator._2_OPERATORS.contains(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void test() {
|
||||||
|
|
Loading…
Reference in New Issue