evaluate method
Implementation
Future<String?> evaluate(String expression, Map<String?, dynamic> variables) async
{
try
{
// replace non-quoted ');' with ')+' to trick parser into thinking its a binary expression
expression = expression.replaceAll(nonQuotedSemiColons, ")+");
while (expression.contains(")+ ")) {
expression = expression.replaceAll(")+ ",")+");
}
expression = expression.replaceAll("+?", "?");
expression = expression.replaceAll("+:", ":");
if (expression.endsWith("+")) expression = expression.substring(0,expression.length - 1);
// build variable map and modify expression
Map<String, dynamic> variables0 = <String, dynamic>{};
int i = 0;
variables.forEach((key,value)
{
i++;
var myKey = "___V$i";
variables0[myKey] = _isNumeric(value) ? _toNum(value) : _isBool(value) ? _toBool(value) : value;
expression = expression.replaceAll("$key", myKey);
});
variables.clear();
variables.addAll(variables0);
// pre-parse the expression
var parsed = Expression.tryParse(expression);
// Unable to preparse
if (parsed == null) {
Log().debug('Unable to pre-parse conditionals $expression');
return null;
}
// format call and conditional expressions as string variables
if (parsed is ConditionalExpression)
{
// build event expressions as variables
var events = getConditionals(parsed);
events.sort((a, b) => Comparable.compare(b.length, a.length));
expression = parsed.toString();
for (var e in events) {
i++;
var key = "___V$i";
variables0[key] = e;
expression = expression.replaceAll(e, key);
}
// execute the expression and get events string
expression = await Eval.evaluate(expression, variables: variables0);
}
// replace non-quoted + with ;. This might be problematic. Needs testing
expression = expression.replaceAll(nonQuotedPlusSigns, ");");
}
catch(e)
{
Log().exception(e);
return '';
}
return expression;
}