@alu0101312101/constant-folding

1.1.0 • Public • Published

npm version
CI for constant-folding

constant-folding

Constant folding is the process of recognizing and evaluating constant expressions at compile time rather than computing them at runtime. Terms in constant expressions are typically simple literals, such as the integer literal 2, but they may also be variables whose values are known at compile time.

Installation

npm i @alu0101312101/constant-folding

Usage as executable:

./node_modules/@alu0101312101/constant-folding/bin/constant-folding-cli.js input.js output.js
Where input.js is the input program and output.js is where it will be stored the result.

Usage from code:

const constantFolding = require('constant-folding');

const input = '[2,3,4].pop()';

let result = constantFolding(input);

console.log(result); // "4;"

The documentation of the function.

Examples

./bin/constant-folding-cli.js /input.js
File read succesfully
Output written succesfully in output.js

// input.js
//[1,2,3,4].concat(5,6);

// output.js
//[
//    1,
//    2,
//    3,
//    4,
//    5,
//    6
//];

// input2.js
//  [1,2,3+4].pop();
// output2.js
// 7;

// input3,js
// [1,2,3,4,5,6,7].length;
// output.js
// 7;

Author

alu0101312101

Tests

npm run test

 constantFolding tests
    Binary operator tests
      ✔ works correctly with normal functions
      ✔ Test Binary Operator 1: var f = 3 + null; // var f = 3;
      ✔ Test Binary Operator 2: var e = 4 | 3;    // var e = 4;
      ✔ Test Binary Operator 3: var d = 3 + "c";  // var d = '3c';
      ✔ Test Binary Operator 4: var b = 9 + 1;    // var b = 10;
      ✔ Test Binary Operator 5: var a = 2 + 3 * 5 + b; // var a = 25 + b;
    Array tests
      Array.length tests
        ✔ Length test 1: [1,2,3,4,5,6].length // 6;
        ✔ Length test 2: [[1,2],4,5].length // 3;
        ✔ Length test 3: [].length          // 0;
        ✔ Length test 4: var t = ["2",2,"a",5].length  // var t = 4;
      Array.pop tests
        ✔ Pop test 1: [1,2,3,4].pop()  // 4;
        ✔ Pop test 2: [2].pop()        // 2;
        ✔ Pop test 3: [].pop()         // undefined;
        ✔ Pop test 4: [1,2,[3,4,5]].pop() // [3,4,5];
        ✔ Pop test 5: let five = [1,2,3,4,5].pop() // let five = 5;
        ✔ Pop test 6: ["a","b","c"].pop() // "c";
        ✔ Pop test 7: [b,c,a].pop();   // a;
      Array.concat tests
        ✔ Concat test 1: [1,2,3,4,5].concat(6,7,8);   // [1,2,3,4,5,6,7,8];
        ✔ Concat test 2: [1,2,3,4,5].concat([6,7,8]); // [1,2,3,4,5,6,7,8];
        ✔ Concat test 3: ["a","b"].concat("c");       // ["a","b","c"];
        ✔ Concat test 4: [1,2,3].concat([1,[2,3]]);   // [1,2,3,1,[2,3]];
        ✔ Concat test 5: [].concat([]);               // [];
      Mixed functions tests
        ✔ Mixed test 1: [1,2+3].concat([1,2,3].pop()); // [1,5,3];
        ✔ Mixed test 2: 5 + [1,2,3,4].length;          // 9;
        ✔ Mixed test 3: let y = [1,2,3].concat([1,2,[3,4]].pop());  // [1,2,3,3,4];

Package Sidebar

Install

npm i @alu0101312101/constant-folding

Weekly Downloads

24

Version

1.1.0

License

Unlicense

Unpacked Size

16.2 kB

Total Files

10

Last publish

Collaborators

  • alu0101312101