@alu0101244488/constant-folding
v1.0.2
Published
Constant Folding javascript code
Downloads
6
Maintainers
Readme
constant-folding
Installation
For installation it is only required to use the next command:
npm install @alu0101244488/constant-folding
You can see the npm package on the following link:
https://www.npmjs.com/package/@alu0101244488/constant-folding
Then you can install the package dependencies with:
npm i
Usage as executable:
To execute you can use the following command where [inputFile]
is the file with the javasctipt code to compute and [outputFile]
is the file to print the corresponding code with the transformations applied. Note is that the -o [outputFile]
or --output [outputFile]
is optional and the resulting code will be displayed on terminal in any case (a test file is given to try).
cf [inputFile] -o [outputFile]
Other options suported are -h
or --help
, and -V
or --version
which shows all the corresponding information or the version of the package correspondingly.
Example of the execution. You can see more details about the result of the execution in the Examples section.
Usage from code:
const constantFolding = require('constant-folding');
//call the function
Functions implemented to to apply the constant folding:
- constantFolding
/**
* Function to traverse the ast and modify the corresponding nodes to apply
* the transformation in the tree (constant folding)
* @param {string} code String with the hole code
* @return {string} String with the resulting javascript code
* after appliying the corresponding ast transformations
*/
function constantFolding(code) {
...
}
- replaceByLiteral
/**
* Function to modify the ast for binary expressions for two literals
* @param {Node} node Node with two literals and the operator (binary expresion)
*/
function replaceByLiteral(node) {
...
}
- replaceByMemberExpression
/**
* Function to replace any member function depending if its an array or a literal
* (a string) with its corresponding result.
* @param {Node} node Node to apply the constant folding
*/
function replaceByMemberExpression(node) {
...
}
- replaceByCallExpression
/**
* Function to alter an ast call expression. This is applyed when a methods
* call is encountered. This is for exmple for a object method or literals like
* strings or arrays.
* @param {Node} node Node to apply the transformation
*/
function replaceByCallExpression(node) {
...
}
- generateArray
/**
* Receives a node and generates an array from the elements of that node.
* Function that given a node (which hods the information of an array) creates
* the corresponding array and returns it
* @param {Node} node Node with the information of the array
* @return {Array} Resulting array
*/
function generateArray(node) {
...
}
- generateOutput
/**
* Function to formatt the resulting code with spaces and new lines
* @param {string} code Resulting code
* @return {string} Formatted code
*/
function generateOutput(code) {
...
}
Examples
Here is an example of the execution of a very simple code where we want to transform the sum of two literals (numbers).
Input file:
3 + 1;
Output:
4 + 1;
The program can also work with other types like arrays and strings and with other operations as for methods and properties of those types. An example can be seen in the next example:
[1, 2, 3].length
["a", "b", "c"].join();
"abc"[0];
output
3
"a,b,c";
"a";
Author
Aram Pérez Dios alu0101244488
Tests
The tests are splited in three different sections.
- The first one is related to binary operators with. There are tests with two or more operands literals and non literals.
- Then tests about methods and properties. This si splited further into two categories, arrays and strings.
- Tests for properties and methods of literal arrays.
- Tests for properties and methods of literal strings.
- The last test is reseved to try different blocks of code and try multiple sentences.