evaluateArithmetic function
Evaluates a BODMAS arithmetic expression with variable substitution.
Identifiers in expr resolve from values; only variables whose types
entry equals "number" may be used as operands. The grammar is + - * /
with chained unary minus and no parentheses (matching the dashboard). Returns
null on any error — unknown or non-numeric variable, division by zero, or
malformed syntax.
Implementation
String? evaluateArithmetic(
String expr,
Map<String, String> values,
Map<String, String> types,
) {
// Resolve eagerly: every operand becomes a `double`, every operator a
// single-char `String`. `null` means a bad character or an unusable variable.
final tokens = _tokenize(expr, values, types);
if (tokens == null) return null;
try {
final parser = _Parser(tokens);
final result = parser.expression();
if (!parser.atEnd) return null; // leftover tokens, e.g. "3 4"
return _formatResult(result);
} on _EvalError {
return null;
}
}