math-codegen
Advanced tools
Comparing version 0.2.2 to 0.2.3
@@ -5,4 +5,3 @@ 'use strict'; | ||
var id = node.name; | ||
return '(scope["' + id + '"] || ' + | ||
'ns["' + id + '"] || ' + id + ' )'; | ||
return '("' + id + '" in scope ? scope["' + id + '"] : ns["' + id + '"])'; | ||
}; |
{ | ||
"name": "math-codegen", | ||
"version": "0.2.2", | ||
"version": "0.2.3", | ||
"description": "Generates code from mathematical expressions", | ||
@@ -5,0 +5,0 @@ "bugs": "https://github.com/maurizzzio/math-codegen/issues", |
@@ -55,4 +55,4 @@ # math-codegen | ||
The expression can be emulated with function calls instead of operators (the parser identifies operators as | ||
[Binary Expressions](https://github.com/estree/estree/blob/master/spec.md#binaryexpression)) | ||
the expression can be emulated with function calls instead of operators (the parser identifies the addition and multiplication | ||
expression as [Binary Expressions](https://github.com/estree/estree/blob/master/spec.md#binaryexpression)) | ||
@@ -116,6 +116,7 @@ ```javascript | ||
- `math.js` has a custom expression parser, `math-codegen` uses Esprima which support the ES5 grammar | ||
- `math.js` has a custom expression parser (which means it has additional types of nodes), | ||
`math-codegen` uses Esprima which support the ES5 grammar only | ||
[(ESTree AST nodes)](https://github.com/estree/estree/blob/master/spec.md) | ||
- `math.js` v1.x arrays can represent matrices with `ns.matrix` or as a raw arrays, `math-codegen` doesn't | ||
make any assumptions of arrays and treats them just like any other literal allowing the namespace to | ||
make any assumptions of the arrays and treats them just like any other literal allowing the namespace to | ||
decide what to do with an array in its `factory` method | ||
@@ -149,3 +150,4 @@ | ||
* `[options.raw=false]` {boolean} True to interpret BinaryExpression, UnaryExpression and ArrayExpression | ||
in a raw way without wrapping the operators with the factory method | ||
in a raw way without wrapping the operators with identifiers, e.g. `-1` will be compiled as | ||
`-1` instead of `ns.negative(ns.factory(1))` | ||
* `[options.rawArrayExpressionElements=true]` {boolean} true to interpret the array elements in a raw way | ||
@@ -211,9 +213,16 @@ * `[options.rawCallExpressionElements=false]` {boolean} true to interpret call expression | ||
### Native math | ||
### built-in math | ||
```javascript | ||
var numeric = { | ||
factory: function (a) { return a; }, | ||
add: function (a, b) { return a + b; }, | ||
mul: function (a, b) { return a * b; } | ||
factory: function (a) { | ||
// anything is a number :) | ||
return Number(a); | ||
}, | ||
add: function (a, b) { | ||
return a + b; | ||
}, | ||
mul: function (a, b) { | ||
return a * b; | ||
} | ||
}; | ||
@@ -225,3 +234,3 @@ | ||
console.log(instance.eval({x : 3})); // 1 + 2 * 3 = 7 | ||
instance.eval({x : 3}); // 1 + 2 * 3 = 7 | ||
``` | ||
@@ -232,2 +241,4 @@ | ||
```javascript | ||
var instance = new CodeGenerator(); | ||
var imaginary = { | ||
@@ -253,10 +264,18 @@ factory: function (a) { | ||
var instance = new CodeGenerator() | ||
// [1, 0] + [2, 0] * [1, 1] | ||
// [1, 0] + [2, 2] | ||
// [3, 2] | ||
instance | ||
.parse('1 + 2 * x') | ||
.compile(imaginary); | ||
.compile(imaginary) | ||
.eval({x : [1, 1]}) | ||
// because of the way the factory works it can also receive an array as a parameter | ||
// [1, 0] + [2, 0] * [1, 1] | ||
// [1, 0] + [2, 2] | ||
// [3, 2] | ||
console.log(instance.eval({x : [1, 1]})); | ||
instance | ||
.parse('[1, 0] + [2, 0] * x') | ||
.compile(imaginary) | ||
.eval({x : [1, 1]}); | ||
``` | ||
@@ -273,3 +292,3 @@ | ||
} | ||
return [a[0], a[1]]; | ||
return a; | ||
}, | ||
@@ -288,10 +307,9 @@ add: function (x, y) { | ||
var instance = new CodeGenerator() | ||
.parse('1 + 2 * x') | ||
.compile(interval); | ||
// [1, 1] + [2, 2] * [-1, 2] | ||
// [1, 1] + [-2, 4] | ||
// [-1, 5] | ||
console.log(instance.eval({x : [-1, 2]})); | ||
var instance = new CodeGenerator() | ||
.parse('1 + 2 * x') | ||
.compile(interval) | ||
.eval({x : [-1, 2]}); | ||
``` | ||
@@ -298,0 +316,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
21517
324
288