NOTE: The latest versions of escodegen support the exponentiation operator:
➜ arith2js-parallel-computing-group-parallel git:(essay-2023-02-15-miercoles) npm ls escodegen
hello-jison@1.0.0 /Users/casianorodriguezleon/campus-virtual/2223/pl2223/practicas/arith2js/arith2js-parallel-computing-group-parallel
└─┬ jison@0.4.18
└── escodegen@1.3.3
➜ arith2js-parallel-computing-group-parallel git:(essay-2023-02-15-miercoles) npm i escodegen@latest
➜ arith2js-parallel-computing-group-parallel git:(essay-2023-02-15-miercoles) ✗ npm ls escodegen
hello-jison@1.0.0 /Users/casianorodriguezleon/campus-virtual/2223/pl2223/practicas/arith2js/arith2js-parallel-computing-group-parallel
├── escodegen@2.1.0
└─┬ jison@0.4.18
└── escodegen@1.3.3
➜ arith2js-parallel-computing-group-parallel git:(essay-2023-02-15-miercoles) ✗ node
Welcome to Node.js v21.2.0.
Type ".help" for more information.
> let espree = require('espree')
> let ast3 = espree.parse('(-2)**2', { ecmaVersion: 7})
> let escodegen = require('escodegen')
> let gc = escodegen.generate(ast3)
> gc
'(-2) ** 2;'
⚠️
Danger: Exponentiation is ECMAScript 2016
New Features in ECMAScript 2016: JavaScript Exponentiation (**) See for instance: https://www.w3schools.com/js/js_2016.asp (opens in a new tab) and https://262.ecma-international.org/7.0/ (opens in a new tab)
... This specification also includes support for a new exponentiation operator and adds a new method to Array.prototype called includes
Since the old version of escodegen do not support this Feature the combination of unary minus and exponentiation for those you an have:
➜ arith2js-parallel-computing-group-parallel git:(essay-2023-02-15-miercoles) ✗ node
Welcome to Node.js v18.8.0.
Type ".help" for more information.
> let espree = require('espree')
> let ast = espree.parse('(-2)**2') // gives an error since the default compiler is ecma 5
Uncaught [SyntaxError: Unexpected token *
> let ast3 = espree.parse('(-2)**2', { ecmaVersion: 7}) // no errors. Right AST
> let escodegen = require('escodegen')
> let gc = escodegen.generate(ast3) // escodegen does not support this feature. The code generated is wrong
> gc
'-2 ** 2;'
> eval(gc) // the evaluation of the code produces errors
Uncaught:
SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence
> -2 ** 2 # JS does not accept this expression
-2 ** 2
^^^^^
Uncaught:
SyntaxError: Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence
> (-2) ** 2 # ... But this code works
4
Therefore, the following continuation of the former node session suggest the correct translation:
💡
> let ast5 = espree.parse('Math.pow(-2,2)')
undefined
> gc = escodegen.generate(ast5)
'Math.pow(-2, 2);'
> eval(gc)
4