projectitis / eq_parser

A light, fast equation parser that supports functions and variables written in dart

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

A light, fast equation parser that supports functions and variables.

Features

  • 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

Getting started

  • Add eq_parser to your pubspec.yaml
  • Add import 'package:eq_parser/eq_parser.dart'; to your code

Usage

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)');

Additional information

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');

About

A light, fast equation parser that supports functions and variables written in dart

License:MIT License


Languages

Language:Dart 100.0%