expression
fun with math expressions
Install
$ npm install expression-compiler
Usage
> var compile = require('expression');
undefined
> var var exprFn = compile('c*sin(2*t)+1');
undefined
> exprFn({c: 0.5, sin: Math.sin, t: Math.PI});
0.9999999999999999
Parsing
New AST node design:
{
id: 0,
type: 'ASTNode',
node: 'expr',
template: '#',
children: [],
options: {}
}
id
must be uniquetype
must be a valid node typeoptions
contains different properties depending on the node type
AST Node Types
expr
options:
{}
func
options:
{
key: 'funcName'
}
Infix functions should be normal function nodes. Here are some examples for +
with 2 or 3 operands:
{
id: 0,
type: 'ASTNode',
node: 'func',
template: '# + # + #',
children: [someNode, anotherNode, andAnother],
options: {
key: 'sum'
}
}
Normal functions can take multiple arguments
{
id: 0,
type: 'ASTNode',
node: 'func',
template: 'min(#, #)',
children: [someNode, anotherNode],
options: {
key: 'min'
}
}
name
options:
{
key: 'varName'
}
literal
options:
{
value: 1
}