@alu0101130507/constant-folding
v1.3.1
Published
Constant Folding javascript code
Downloads
7
Maintainers
Readme
Constant folding
This module provides functionalities to perform the transformation of the constant folding for a JavaScript input, producing an output which has the same code as the input but with some of the constants in methods, arrays or operations are folded.
Functions
constantFolding(code) ⇒ string
A function that receives a piece of code and returns the same code but with its constants folded.
Kind: global function Returns: string - The modified code with its constants folded
| Param | Type | Description | | --- | --- | --- | | code | string | The code to receive |
replaceByLiteral(n)
Replaces a node that represents the operation between two children literals with a single node that has the result of said operation
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replaceConcat(n)
Replaces a node that represents the concat() method of an array with a single node that has the result of said method
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replaceJoin(n)
Replaces a node that represents the join() method of an array with a single node that has the result of said method
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replaceLength(n)
Replaces a node that represents the length member expression of an array with a single node that has the result of that length.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replaceBracketOperator(n)
Replaces a node that represents the bracket operator [] member expression of an array with a single node that has the result of that operator.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replaceShift(n)
Replaces a node that represents the shift method of an array with a single node that has the result of that method.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replaceSlice(n)
Replaces a node that represents the slice method of an array with a single node that has the result of that method.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replacePop(n)
Replaces a node that represents the pop method of an array with a single node that has the result of that method.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
replaceReverse(n)
Replaces a node that represents the reverse method of an array with a single node that has the result of that method.
Kind: global function
| Param | Type | Description | | --- | --- | --- | | n | ASTnode | The AST father node. |
Installation
To install this module, simply run the following command:
npm i @ULL-ESIT-PL-2122/constant-folding
Usage as executable:
The module provides an executable that can run as:
Usage: cf [options] <filename>
Constant Folding javascript code
Arguments:
filename file with the original code
Options:
-V, --version output the version number
-o, --output <filename> name of the file to write the result (default: "output.js")
-h, --help display help for command
Usage from code:
The module also provides one function, constantFolding(), which can be used to parse any piece of code and perform the required transformations. The usage of this function is described in the Functions section.
const constantFolding = require('@ULL-ESIT-PL-2122/constant-folding');
//call the function
let outputCode = constantFolding(inputCode);
Examples
Example 1: Binary expressions
// Example input with binary expressions:
let f = 3 + null;
let e = 4 | 3;
let d = 3 + "c";
let b = 9 +1;
let a = 2 + 3 * 5 + b;
// Will produce the following output:
let f = 3;
let e = 7;
let d = '3c';
let b = 10;
let a = 17 + b;
Example 2: Member expressions
// Example input with member expressions:
['a', 'b', 'c'].concat(['d', 'e'], 'f', 'g', ['h']);
['a', 'b', 'c'].join();
['a', 'b', 'c'].join('@');
[1, 2, 3].length;
[1, 2, 3][2-1];
[1, 2, 3].shift();
[1, 2, 3].slice(0, 1+1);
['a', 'b', 'c'].pop();
['a', 'b', 'c'].reverse();
// Will produce the following output:
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
'a,b,c';
'a@b@c';
3;
2;
1;
[1, 2];
'c';
['c', 'b', 'a'];
Author
Markus Schüller Perdigón - alu0101130507
Tests
The tests can be found in test/test.js
and run with npm run test
. It contains tests for the
examples found in the Examples section.