Translating Arithmetic Expressions to JavaScript

Objetivos

Usando Jison generalice el ejemplo en la carpeta ast del Repo hello-jison constituido por los ficheros:

para escribir un programa que, recibiendo como entrada una cadena que contiene una secuencia de expresiones aritméticas de enteros separados por comas, produzca un AST compatible con el del parser espree. Use escodegen para generar el código JavaScript correspondiente (vea ast2js.js). Sigue un ejemplo de una posible ejecución:

✗ cat test/data/test1.calc 
4 - 2 - 1                                                                                       
✗ bin/calc2js.mjs test/data/test1.calc 
console.log(4 - 2 - 1);
✗ bin/calc2js.js test/data/test1.calc | node -
1

Las expresiones aritméticas deben soportar además de suma, resta, multiplicación y división el menos unario -(2+3)*2 un operador de factorial !, un operador de potencia ** y paréntesis. Ejemplo: -(2 + 3) * 4! - 5 ** 6.

Opciones en línea de comandos

Use commander para procesar la línea de argumentos:

$ bin/calc2js.mjs --help
Usage: calc2js [options] <filename>

Arguments:
  filename                 file with the original code

Options:
  -V, --version            output the version number
  -o, --output <filename>  file in which to write the output
  -h, --help               display help for command

Dealing with Ambiguity

Para tratar con los temas de ambigüedad en la gramática, puede consultar

Unary minus and exponentiation in escodegen

See unary minus section

Pruebas

Añada pruebas a este proyecto usando mocha

Mocking and Stubbing

Covering

You can use nyc to do the covering of your mocha tests. See the notes in covering.

Activate the GitHub pages of your repo (use the default branch and the docs folder) and be sure to include your covering report in the docs folder.

✗ npm run cov

> hello-jison@1.0.0 cov
> nyc npm test


> hello-jison@1.0.0 test
> npm run compile; mocha test/test.mjs


> hello-jison@1.0.0 compile
> jison src/grammar.jison src/lexer.l -o src/calc.js



  ✔ transpile(test1.calc, out1.js)
  ✔ transpile(test2.calc, out2.js)
  ✔ transpile(test3.calc, out3.js)

  3 passing (20ms)

--------------|---------|----------|---------|---------|-----------------------------------------
File          | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s                       
--------------|---------|----------|---------|---------|-----------------------------------------
All files     |   58.92 |    45.83 |   47.22 |   56.63 |                                         
 ast-build.js |     100 |      100 |     100 |     100 |                                         
 calc.js      |   57.44 |    45.78 |   40.62 |   54.92 | ...,530-539,548-569,578,580,602-607,610 
 transpile.js |   81.81 |       50 |     100 |   81.81 | 11-12                                   
--------------|---------|----------|---------|---------|-----------------------------------------

References

Essentials for this lab

Jison and Syntax Analysis

Have a look