This commit is contained in:
rusefi 2017-03-11 22:37:06 -05:00
parent 074521d990
commit 828e7d4129
2 changed files with 32 additions and 7 deletions

View File

@ -115,6 +115,7 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
public static final Function LOG = new Function("log", 1); public static final Function LOG = new Function("log", 1);
public static final Function fsio_setting = new Function("fsio_setting", 1); public static final Function fsio_setting = new Function("fsio_setting", 1);
public static final Function if_function = new Function("if", 3);
/** Returns a pseudo random number */ /** Returns a pseudo random number */
@ -159,7 +160,7 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
/** The whole set of predefined functions */ /** The whole set of predefined functions */
private static final Function[] FUNCTIONS = new Function[]{SINE, COSINE, TANGENT, ASINE, ACOSINE, ATAN, SINEH, COSINEH, TANGENTH, MIN, MAX, SUM, AVERAGE, LN, LOG, ROUND, CEIL, FLOOR, ABS, RANDOM, private static final Function[] FUNCTIONS = new Function[]{SINE, COSINE, TANGENT, ASINE, ACOSINE, ATAN, SINEH, COSINEH, TANGENTH, MIN, MAX, SUM, AVERAGE, LN, LOG, ROUND, CEIL, FLOOR, ABS, RANDOM,
fsio_setting}; if_function, fsio_setting};
/** The whole set of predefined constants */ /** The whole set of predefined constants */
private static final Constant[] CONSTANTS = new Constant[]{TRUE, FALSE}; private static final Constant[] CONSTANTS = new Constant[]{TRUE, FALSE};
@ -196,6 +197,15 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
return result; return result;
} }
public static Function getFunction(String token) {
for (Function function : FUNCTIONS) {
if (function.getName().equalsIgnoreCase(token)) {
return function;
}
}
return null;
}
private static Parameters getParameters() { private static Parameters getParameters() {
if (DEFAULT_PARAMETERS == null) { if (DEFAULT_PARAMETERS == null) {
DEFAULT_PARAMETERS = getDefaultParameters(); DEFAULT_PARAMETERS = getDefaultParameters();
@ -352,7 +362,7 @@ public class DoubleEvaluator extends AbstractEvaluator<Double> {
result = Math.log10(arguments.next()); result = Math.log10(arguments.next());
} else if (RANDOM.equals(function)) { } else if (RANDOM.equals(function)) {
result = Math.random(); result = Math.random();
} else if (fsio_setting.equals(function)) { } else if (fsio_setting.equals(function) || if_function.equals(function)) {
result = 333333.0; result = 333333.0;
} else { } else {
result = super.evaluate(function, arguments, evaluationContext); result = super.evaluate(function, arguments, evaluationContext);

View File

@ -16,11 +16,13 @@ import static org.junit.Assert.assertEquals;
public class ParserTest { public class ParserTest {
@Test @Test
public void testFunctionParameters() { public void testFunctionParameters() {
assertParse("3 log", "log(3)"); assertParseB("3 log", "log(3)");
assertParse("log 3", "log 3 "); // todo: better handling? assertParse("1 2 3 if", "if(1, 2, 3)");
assertParse("0 fsio_setting", "fsio_setting(0)");
assertParse("rpm 2 fsio_setting >", "rpm > fsio_setting(2)"); assertParse("log 3", "log 3 "); // todo: better handling?
assertParseB("0 fsio_setting", "fsio_setting(0)");
assertParseB("rpm 2 fsio_setting >", "rpm > fsio_setting(2)");
} }
@Test @Test
@ -62,7 +64,20 @@ public class ParserTest {
Stack<String> stack = new Stack<>(); Stack<String> stack = new Stack<>();
for (String token : tokens) { for (String token : tokens) {
if (Operator._1_OPERATORS.contains(token)) { if (DoubleEvaluator.getFunction(token) != null) {
int pCount = DoubleEvaluator.getFunction(token).getMaximumArgumentCount();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < pCount; i++) {
if (i != 0)
sb.append(", ");
sb.append(stack.pop());
}
stack.push(token + "(" + sb + ")");
} else if (Operator._1_OPERATORS.contains(token)) {
String a = stack.pop(); String a = stack.pop();
stack.push("(" + token + " " + a + ")"); stack.push("(" + token + " " + a + ")");