@adrianepi/constant-folding
Version:
197 lines (158 loc) • 4.35 kB
Markdown
[](https://badge.fury.io/js/@adrianepi%2Fconstant-folding)
[](https://github.com/ULL-ESIT-PL-2122/constant-folding-module-adrian-rdguez-hdez-alu0101158280/actions/workflows/nodejs.yml)
A small library providing utility methods to `constant-folding`. This allow to use constant folding with array, strings and number methods. For using this, the JS code is parsed into an AST and then modified for allowing the execution of the methods.
```
npm install @adrianepi/constant-folding --save
```
Usage help:
```
Constant Folding javascript code
Arguments:
filename file with the original code
Options:
-V, --version output the version number
-o, --output <filename> file where the output is going to be stored (default: "output.js")
-p, --pattern <pattern> other parameters (--js | -j) for only store the js output and (--ast | -a) for only storing the ast output.
-h, --help display help for command
```
Examples of execution:
```sh
cf inputFile -o outputFile -p pattern
```
```sh
cf examples/example1.js -o output.js -p --js
```
## Usage from code:
```javascript
const constantFolding = require('constant-folding');
// Input must be js code.
const input = `
var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;
[].concat([d, e], f, g, [h]);
["a", "b", "c"].join();
["a", "b", "c"].join('@');
[].length;
[][2-1];
[].shift();
[].slice(0, 1+1);
[].pop();
[].reverse();
"abc"[0];
"abc".charAt();
"abc".charAt(1);
"abc".length;
"a,b,c".split(",");
(100 + 23).toString();
`;
const pattern = "--js";
// Patterns can be one of the following:
// (-j | --js) for js output.
// (-a | --ast) for ast output.
// If no pattern received, it will return both of them, AST code + JS code.
console.log(constantFolding(input, pattern));
```
[](https://github.com/ULL-ESIT-PL-2122/constant-folding-module-adrian-rdguez-hdez-alu0101158280/tree/master/docs).
Simple input examples:
```js
var f = 3+null;
var e = 4 | 3;
var d = 3+"c";
var b = 9 +1;
var a = 2+3*5+b;
```
Output of simple examples:
```js
var f = 3;
var e = 7;
var d = 3c;
var b = 10;
var a = 17 + b;
```
Array input examples:
```js
[].concat([d, e], f, g, [h]);
["a", "b", "c"].join();
["a", "b", "c"].join('@');
[].length;
[][2-1];
[].shift();
[].slice(0, 1+1);
[].pop();
[].reverse();
```
Output of array examples:
```js
[ a, b, c, d, e, f, g, h];
"a,b,c";
"a@b@c";
3;
2;
1;
[ 1, 2];
c;
[ c, b, a];
```
String input examples:
```js
"abc"[0];
"abc".charAt();
"abc".charAt(1);
"abc".length;
"a,b,c".split(",");
```
Output of string examples:
```js
"a";
"a";
"b";
3;
"a", "b", "c";
```
Adrián Epifanio Rodríguez Hernández (alu0101158280)
For executing the test there are some examples in the *examples/* folder which can be used for testing the different ways of applying constant folding (array, strings, numbers).
For running tests the command is:
```sh
npm test
```
Output for the tests:
```
> @adrianepi/constant-folding@1.3.0 test
> mocha
ConstantFolding simple tests
✔ Testing var f = 3+null;
✔ Testing var e = 4 | 3;
✔ Testing var d = 3+"c";
✔ Testing var b = 9 +1;
✔ Testing var a = 2+3*5+b;
ConstantFolding array tests
✔ Testing [a, b, c].concat([d, e], f, g, [h]);
✔ Testing ["a", "b", "c"].join();
✔ Testing ["a", "b", "c"].join('@');
✔ Testing [1, 2, 3].length;
✔ Testing [1, 2, 3][2-1];
✔ Testing [1, 2, 3].shift();
✔ Testing [1, 2, 3].slice(0, 1+1);
✔ Testing [a, b, c].pop();
✔ Testing [a, b, c].reverse();
ConstantFolding string tests
✔ Testing "abc"[0];
✔ Testing "abc".charAt();
✔ Testing "abc".charAt(1);
✔ Testing "abc".length;
✔ Testing "a,b,c".split(",");
ConstantFolding AST tests
✔ Testing example1 AST
✔ Testing example2 AST
✔ Testing example3 AST
22 passing (46ms)
```