expression-compiler
Advanced tools
Comparing version 0.2.0 to 0.3.0
{ | ||
"name": "expression-compiler", | ||
"repository": "git@github.com:uniphil/expression-compiler.git", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "", | ||
"main": "index.js", | ||
"main": "func.js", | ||
"scripts": { | ||
@@ -8,0 +8,0 @@ "test": "gulp test" |
157
readme.md
@@ -1,7 +0,7 @@ | ||
expression | ||
========== | ||
Expression Compiler | ||
=================== | ||
[![Build Status](https://travis-ci.org/uniphil/expression.svg?branch=master)](https://travis-ci.org/uniphil/expression) | ||
[![Build Status](https://travis-ci.org/uniphil/expression-compiler.svg?branch=master)](https://travis-ci.org/uniphil/expression-compiler) | ||
fun with math expressions | ||
Compile math expressions to a useful AST, with built-in compilers to turn the AST into a sanitized javascript function. | ||
@@ -20,12 +20,155 @@ | ||
### Safely execute arbitrary math expression | ||
and also get the raw js generated for the function | ||
```node | ||
> var compile = require('expression'); | ||
> var mkFunc = require('expression-compiler/func'); | ||
undefined | ||
> var var exprFn = compile('c*sin(2*t)+1'); | ||
> var expressionFunc = compile('c*sin(2*t)+1'); | ||
undefined | ||
> exprFn({c: 0.5, sin: Math.sin, t: Math.PI}); | ||
> expressionFunc({c: 0.5}); | ||
0.9999999999999999 | ||
> mkFunc.express('sqrt(x^2 + y^2)') | ||
'(Math.sqrt((Math.pow(symbols["x"],2.0)+Math.pow(symbols["y"],2.0))))' | ||
``` | ||
Note that everything in the global `Math` is made available by the built-in function compiler, and it is assumed that the global `Math` assumed available by its `express` function. | ||
### Get an Abstract Syntax Tree from a math expression | ||
and then echo back the original expression with just the AST | ||
```node | ||
> var parse = require('expression-compiler/parse'); | ||
undefind | ||
> var ast = parse('sin(t)^2 + cos(t)^2'); | ||
unefined | ||
> ast | ||
{ id: 0, | ||
type: 'ASTNode', | ||
node: 'expr', | ||
template: '#', | ||
children: | ||
[ { id: 1, | ||
type: 'ASTNode', | ||
node: 'func', | ||
template: '# +#', | ||
children: [Object], | ||
options: [Object] } ], | ||
options: {} } | ||
> console.log(JSON.stringify(ast, null, 2)); // print out the whole thing | ||
{ | ||
"id": 0, | ||
"type": "ASTNode", | ||
"node": "expr", | ||
"template": "#", | ||
"children": [ | ||
{ | ||
"id": 1, | ||
"type": "ASTNode", | ||
"node": "func", | ||
"template": "# +#", | ||
"children": [ | ||
{ | ||
"id": 2, | ||
"type": "ASTNode", | ||
"node": "func", | ||
"template": "#^#", | ||
"children": [ | ||
{ | ||
"id": 3, | ||
"type": "ASTNode", | ||
"node": "func", | ||
"template": "sin(#)", | ||
"children": [ | ||
{ | ||
"id": 4, | ||
"type": "ASTNode", | ||
"node": "name", | ||
"template": "t", | ||
"children": [], | ||
"options": { | ||
"key": "t" | ||
} | ||
} | ||
], | ||
"options": { | ||
"key": "sin" | ||
} | ||
}, | ||
{ | ||
"id": 5, | ||
"type": "ASTNode", | ||
"node": "literal", | ||
"template": "2", | ||
"children": [], | ||
"options": { | ||
"value": 2 | ||
} | ||
} | ||
], | ||
"options": { | ||
"key": "pow" | ||
} | ||
}, | ||
{ | ||
"id": 6, | ||
"type": "ASTNode", | ||
"node": "func", | ||
"template": "#^#", | ||
"children": [ | ||
{ | ||
"id": 7, | ||
"type": "ASTNode", | ||
"node": "func", | ||
"template": " cos(#)", | ||
"children": [ | ||
{ | ||
"id": 8, | ||
"type": "ASTNode", | ||
"node": "name", | ||
"template": "t", | ||
"children": [], | ||
"options": { | ||
"key": "t" | ||
} | ||
} | ||
], | ||
"options": { | ||
"key": "cos" | ||
} | ||
}, | ||
{ | ||
"id": 9, | ||
"type": "ASTNode", | ||
"node": "literal", | ||
"template": "2", | ||
"children": [], | ||
"options": { | ||
"value": 2 | ||
} | ||
} | ||
], | ||
"options": { | ||
"key": "pow" | ||
} | ||
} | ||
], | ||
"options": { | ||
"key": "sum" | ||
} | ||
} | ||
], | ||
"options": {} | ||
} | ||
undefined | ||
> var echoer = require('expression-compiler/echo'); | ||
undefined | ||
> echoer.fromAST(ast); | ||
'sin(t)^2 + cos(t)^2' | ||
``` | ||
Parsing | ||
@@ -32,0 +175,0 @@ ------- |
@@ -5,5 +5,5 @@ require('mocha'); | ||
var parse = require('./parse'); | ||
var compileE = require('./compile-echo'); | ||
var compileF = require('./compile-func'); | ||
var compileV = require('./compile-values'); | ||
var compileE = require('./echo'); | ||
var compileF = require('./func'); | ||
var compileV = require('./values'); | ||
@@ -10,0 +10,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36432
258
13
778