math-codegen 0.3.4 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -23
- package/lib/CodeGenerator.js +10 -2
- package/lib/Interpreter.js +2 -1
- package/package.json +10 -11
package/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
# math-codegen
|
|
1
|
+
# math-codegen
|
|
2
2
|
|
|
3
|
-
[![Build Status][travis-image]][travis-url]
|
|
3
|
+
[![Build Status][travis-image]][travis-url]
|
|
4
4
|
[![NPM][npm-image]][npm-url]
|
|
5
|
-
[]()
|
|
5
|
+
[](https://codecov.io/gh/mauriciopoppe/math-codegen)
|
|
7
6
|
|
|
8
7
|
[](https://github.com/feross/standard)
|
|
9
8
|
|
|
@@ -40,11 +39,11 @@
|
|
|
40
39
|
|
|
41
40
|
An interpreter for mathematical expressions which allows the programmer to change the usual semantic of an
|
|
42
41
|
operator bringing the operator overloading polymorphism to JavaScript (emulated with function calls),
|
|
43
|
-
in addition an expression can be evaluated under any adapted namespace providing expression portability between numeric libraries
|
|
42
|
+
in addition an expression can be evaluated under any adapted namespace providing expression portability between numeric libraries
|
|
44
43
|
|
|
45
44
|
### Lifecycle
|
|
46
45
|
|
|
47
|
-
- `parse`: a mathematical expression is parsed with [`mr-parse`](https://github.com/
|
|
46
|
+
- `parse`: a mathematical expression is parsed with [`mr-parse`](https://github.com/mauriciopoppe/mr-parser), in the ideal scenario
|
|
48
47
|
it would use [math.js expression parser](http://mathjs.org/docs/expressions/index.html) however it's not modularized yet
|
|
49
48
|
and including all math.js is just an overkill, probably `mr-parse` will be replaced with math.js expression parser when
|
|
50
49
|
it reaches npm as a module :)
|
|
@@ -90,7 +89,7 @@ on the namespace to transform these values into values the namespace can operate
|
|
|
90
89
|
|
|
91
90
|
Now that we have a parsed expression we have to compile it against a namespace to produce
|
|
92
91
|
executable JavaScript code
|
|
93
|
-
|
|
92
|
+
|
|
94
93
|
```javascript
|
|
95
94
|
parse('1 + 2 * x').compile(namespace)
|
|
96
95
|
|
|
@@ -105,7 +104,7 @@ parse('1 + 2 * x').compile(namespace)
|
|
|
105
104
|
return ns.add(ns.factory(1), ns.mul(ns.factory(2), (scope["x"] || ns["x"]) ))
|
|
106
105
|
}
|
|
107
106
|
}
|
|
108
|
-
})(definitions) // definitions created by math-codegen
|
|
107
|
+
})(definitions) // definitions created by math-codegen
|
|
109
108
|
```
|
|
110
109
|
|
|
111
110
|
#### Eval
|
|
@@ -122,7 +121,7 @@ Math.js expression parser API is quite similar having the same lifecycle however
|
|
|
122
121
|
important facts I've found:
|
|
123
122
|
|
|
124
123
|
- `math.js` v1.x arrays can represent matrices with `ns.matrix` or as a raw arrays, `math-codegen` doesn't
|
|
125
|
-
make any assumptions of the arrays and treats them just like any other literal allowing the namespace to
|
|
124
|
+
make any assumptions of the arrays and treats them just like any other literal allowing the namespace to
|
|
126
125
|
decide what to do with an array in its `factory` method
|
|
127
126
|
|
|
128
127
|
### Operators
|
|
@@ -189,37 +188,37 @@ new CodeGenerator([options]).parse(code).compile(namespace).eval(scope)
|
|
|
189
188
|
* `interpreter` {Interpreter} Instance of the Interpreter class
|
|
190
189
|
* `defs` {Object} An object with additional definitions available during the compilation
|
|
191
190
|
that exist during the instance lifespan
|
|
192
|
-
|
|
191
|
+
|
|
193
192
|
**params**
|
|
194
193
|
* `options` {Object} Options available for the interpreter
|
|
195
|
-
* `[options.factory="ns.factory"]` {string} factory method under the namespace
|
|
194
|
+
* `[options.factory="ns.factory"]` {string} factory method under the namespace
|
|
196
195
|
* `[options.raw=false]` {boolean} True to interpret OperatorNode, UnaryNode and ArrayNode
|
|
197
196
|
in a raw way without wrapping the operators with identifiers e.g. `-1` will be compiled as
|
|
198
197
|
`-1` instead of `ns.negative(ns.factory(1))`
|
|
199
198
|
* `[options.rawArrayExpressionElements=true]` {boolean} true to interpret the array elements in a raw way
|
|
200
199
|
* `[options.rawCallExpressionElements=false]` {boolean} true to interpret call expression
|
|
201
|
-
|
|
200
|
+
* `[options.applyFactoryToScope=false]` {boolean} true to apply the factory function on non-function values of the scope/namespace
|
|
202
201
|
|
|
203
202
|
### `instance.parse(code)`
|
|
204
203
|
|
|
205
204
|
**chainable**
|
|
206
205
|
**params**
|
|
207
206
|
* `code` {string} string to be parsed
|
|
208
|
-
|
|
209
|
-
Parses a program using [`mr-parse`](https://github.com/
|
|
207
|
+
|
|
208
|
+
Parses a program using [`mr-parse`](https://github.com/mauriciopoppe/mr-parser), each Expression Statement is saved in
|
|
210
209
|
`instance.statements`
|
|
211
210
|
|
|
212
|
-
The documentation for the available nodes is described in [`mr-parse`](https://github.com/
|
|
213
|
-
|
|
211
|
+
The documentation for the available nodes is described in [`mr-parse`](https://github.com/mauriciopoppe/mr-parser)
|
|
212
|
+
|
|
214
213
|
### `instance.compile(namespace)`
|
|
215
|
-
|
|
214
|
+
|
|
216
215
|
**chainable**
|
|
217
216
|
**params**
|
|
218
217
|
* `namespace` {Object}
|
|
219
218
|
|
|
220
219
|
Compiles the code making `namespace`'s properties available during evaluation, **it's required
|
|
221
220
|
to have the `factory` property defined**
|
|
222
|
-
|
|
221
|
+
|
|
223
222
|
**returns** {Object}
|
|
224
223
|
* `return.code` {string} the body of the function to be evaluated with `eval`
|
|
225
224
|
* `return.eval` {Function} Function to be evaluated under a context
|
|
@@ -346,11 +345,7 @@ instance
|
|
|
346
345
|
|
|
347
346
|
## License
|
|
348
347
|
|
|
349
|
-
|
|
348
|
+
MIT
|
|
350
349
|
|
|
351
350
|
[npm-image]: https://img.shields.io/npm/v/math-codegen.svg?style=flat
|
|
352
351
|
[npm-url]: https://npmjs.org/package/math-codegen
|
|
353
|
-
[travis-image]: https://travis-ci.org/maurizzzio/math-codegen.svg?branch=master
|
|
354
|
-
[travis-url]: https://travis-ci.org/maurizzzio/math-codegen
|
|
355
|
-
[coveralls-image]: https://coveralls.io/repos/maurizzzio/math-codegen/badge.svg?branch=master
|
|
356
|
-
[coveralls-url]: https://coveralls.io/r/maurizzzio/math-codegen?branch=master
|
package/lib/CodeGenerator.js
CHANGED
|
@@ -16,6 +16,7 @@ CodeGenerator.prototype.setDefs = function (defs) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
CodeGenerator.prototype.compile = function (namespace) {
|
|
19
|
+
var self = this
|
|
19
20
|
if (!namespace || !(typeof namespace === 'object' || typeof namespace === 'function')) {
|
|
20
21
|
throw TypeError('namespace must be an object')
|
|
21
22
|
}
|
|
@@ -34,11 +35,18 @@ CodeGenerator.prototype.compile = function (namespace) {
|
|
|
34
35
|
this.defs.ns = namespace
|
|
35
36
|
this.defs.$$mathCodegen = {
|
|
36
37
|
getProperty: function (symbol, scope, ns) {
|
|
38
|
+
function applyFactoryIfNeeded (value) {
|
|
39
|
+
if (self.interpreter.options.applyFactoryToScope && typeof value !== 'function') {
|
|
40
|
+
return ns.factory(value)
|
|
41
|
+
}
|
|
42
|
+
return value
|
|
43
|
+
}
|
|
44
|
+
|
|
37
45
|
if (symbol in scope) {
|
|
38
|
-
return scope[symbol]
|
|
46
|
+
return applyFactoryIfNeeded(scope[symbol])
|
|
39
47
|
}
|
|
40
48
|
if (symbol in ns) {
|
|
41
|
-
return ns[symbol]
|
|
49
|
+
return applyFactoryIfNeeded(ns[symbol])
|
|
42
50
|
}
|
|
43
51
|
throw SyntaxError('symbol "' + symbol + '" is undefined')
|
|
44
52
|
},
|
package/lib/Interpreter.js
CHANGED
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "math-codegen",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.1",
|
|
4
4
|
"description": "Generates code from mathematical expressions",
|
|
5
|
-
"bugs": "https://github.com/
|
|
5
|
+
"bugs": "https://github.com/mauricopoppe/math-codegen/issues",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"main": "index.js",
|
|
8
8
|
"author": "Mauricio Poppe <mauricio.poppe@gmail.com>",
|
|
@@ -24,20 +24,19 @@
|
|
|
24
24
|
"parse"
|
|
25
25
|
],
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"extend": "^3.0.
|
|
28
|
-
"mr-parser": "^0.1
|
|
27
|
+
"extend": "^3.0.2",
|
|
28
|
+
"mr-parser": "^0.2.1"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"coveralls": "^2.11.
|
|
31
|
+
"coveralls": "^2.11.4",
|
|
32
32
|
"doctoc": "^0.14.2",
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"standard": "^4.5.4"
|
|
33
|
+
"mocha": "^2.3.3",
|
|
34
|
+
"nodemon": "^1.7.1",
|
|
35
|
+
"standard": "^4.5.4",
|
|
36
|
+
"nyc": "^15.1.0"
|
|
38
37
|
},
|
|
39
38
|
"scripts": {
|
|
40
|
-
"
|
|
39
|
+
"coverage": "nyc npm run test",
|
|
41
40
|
"lint": "standard",
|
|
42
41
|
"test": "mocha -R spec test/",
|
|
43
42
|
"test:watch": "nodemon --watch lib --watch test --watch index.js --exec 'npm test'",
|