This is a dumb parser. The opposite of what a parser should be, but it was a lot of fun to build! 😊
Consider using petiteparser instead.
var calculator = new Calculator();
calculator.addFunction('sin', (arg) => math.sin(arg));
calculator.addFunction('pi', (arg) => math.pi * arg);
var text = 'sin(pi(1/2))*2**1**2';
print(calculator.parse(text).evaluate());
Calculator is a pre-written parser included in this library. Check it out for an example of how to use this.
var parser = new Parser(symbolRules, tokenRules, transformRules);
The parsers are just collections of pattern matching rules.
- Symbol Rules match characters to Symbols
- Token Rules match Symbols to Tokens (Symbols and Tokens are interchangeable)
- Transform Rules turn Tokens into a result
(1) and (2) happen in parse()
and (3) happens in evaluate()
.