A light, fast equation parser that supports functions and variables.
- Supports many number formats. E.g.:
12
-1.234
1.234e5
0xff12
0b1101101
- Mathematical operators and brackets
+
-
*
/
^
%
(
)
- Common functions are built-in
sin
,acos
,max
,min
,floor
,round
,log
, etc
- Supports user-defined functions
- Supports user-defined variables (references)
- User-defined error handling
- Add
eq_parser
to yourpubspec.yaml
- Add
import 'package:eq_parser/eq_parser.dart';
to your code
num result = EqParser()..parse('12 + 3 ^ (5.12 * sin(pi/2))');
var parser = EqParser();
parser.onError = (m, p)=>throw Exception('$m at position $p');
parser.references.addAll({
'x': 3,
'y': 4,
});
parser.functions['multiply'] = FunctionDef((a, b)=>a * b, 2);
num result = parser.parse('multiply(x, y)');
This parser is not based on the shunting yard algorithm or reverse polish notation. I am not sure if it actually matches any existing algorithm or if it is novel. It's based on a single stack, and does not use recursion. Tokens are pushed to the stack just once, and popped/processed just once, so performance is fast and linear with respect to the length of the equation.
There is a minimal implementation included for reference purposes. It can be used like this:
import 'package:eq_parser/src/eq_parser_lite.dart';
num result = EqParserLite()..parse('(17 - 7.12) * 2 ^ 4');