Subsecciones


Ejemplo en Jison: Calculadora Simple

  1. [~/srcPLgrado/jison/jisoncalc(clase)]$ pwd -P
    /Users/casiano/local/src/javascript/PLgrado/jison/jisoncalc
    [~/srcPLgrado/jison/jisoncalc(clase)]$ git remote -v
    bitbucket       ssh://git@bitbucket.org/casiano/ull-etsii-grado-pl-jisoncalc.git (fetch)
    bitbucket       ssh://git@bitbucket.org/casiano/ull-etsii-grado-pl-jisoncalc.git (push)
    heroku  git@heroku.com:jisoncalc.git (fetch)
    heroku  git@heroku.com:jisoncalc.git (push)
    origin  git@github.com:crguezl/ull-etsii-grado-pl-jisoncalc.git (fetch)
    origin  git@github.com:crguezl/ull-etsii-grado-pl-jisoncalc.git (push)
    
    El repo en github no está actualizado. El repo en bitbucket es privado.
    [~/srcPLgrado/jison/jisoncalc(clase)]$ git branch 
    * clase
      develop
      heroku
      master
    
  2. Enlace al fork del proyecto jison de crguezl (GitHub) (no está completo)
  3. ~/jison/jison-compiler/jison/examples/html_calc_example(develop)]$ pwd -P
    /Users/casiano/local/src/javascript/PLgrado/jison/jison-compiler/jison/examples/html_calc_example
    [~/jison/jison-compiler/jison/examples/html_calc_example(develop)]$ git branch -a
    * develop
      master
      remotes/origin/develop
      remotes/origin/master
    
  4. Una versión en Sinatra se encuentra en:
    [~/srcPLgrado/jison/jison-simple-html-calc(gh-pages)]$ pwd -P
    /Users/casiano/local/src/javascript/PLgrado/jison/jison-simple-html-calc
    [~/srcPLgrado/jison/jison-simple-html-calc(gh-pages)]$ git remote -v
    origin  git@github.com:crguezl/jison-simple-html-calc.git (fetch)
    
  5. Otra versión Sinatra con pruebas puede encontrarse en:
    [~/srcPLgrado/jison/jisoncalc(heroku)]$ git remote -v
    bitbucket       ssh://git@bitbucket.org/casiano/ull-etsii-grado-pl-jisoncalc.git (fetch)
    heroku  git@heroku.com:jisoncalc.git (fetch)
    origin  git@github.com:crguezl/ull-etsii-grado-pl-jisoncalc.git (fetch)
    [~/srcPLgrado/jison/jisoncalc(heroku)]$ ls -l test/
    total 24
    -rw-rw-r--  1 casiano  staff   264 19 mar  2013 assert.js
    -rw-rw-r--  1 casiano  staff   153 19 mar  2013 test.css
    -rw-rw-r--  1 casiano  staff  1021  1 abr  2013 test.html
    [~/srcPLgrado/jison/jisoncalc(heroku)]$ git branch -a
      clase
      develop
    * heroku
      master
      remotes/bitbucket/clase
      remotes/bitbucket/interactive
      remotes/bitbucket/master
      remotes/bitbucket/seplex
      remotes/origin/master
    

calculator.jison

[~/jison/examples/html_calc_example(develop)]$ cat calculator.jison 

/* description: Parses end executes mathematical expressions. */

/* lexical grammar */
%lex
%%

\s+                   /* skip whitespace */
[0-9]+("."[0-9]+)?\b  return 'NUMBER'
"*"                   return '*'
"/"                   return '/'
"-"                   return '-'
"+"                   return '+'
"^"                   return '^'
"!"                   return '!'
"%"                   return '%'
"("                   return '('
")"                   return ')'
"PI"                  return 'PI'
"E"                   return 'E'
<<EOF>>               return 'EOF'
.                     return 'INVALID'

/lex

/* operator associations and precedence */

%left '+' '-'
%left '*' '/'
%left '^'
%right '!'
%right '%'
%left UMINUS

%start expressions

%% /* language grammar */

expressions
    : e EOF
        { typeof console !== 'undefined' ? console.log($1) : print($1);
          return $1; }
    ;

e
    : e '+' e
        {$$ = $1+$3;}
    | e '-' e
        {$$ = $1-$3;}
    | e '*' e
        {$$ = $1*$3;}
    | e '/' e
        {$$ = $1/$3;}
    | e '^' e
        {$$ = Math.pow($1, $3);}
    | e '!'
        {{
          $$ = (function fact (n) { return n==0 ? 1 : fact(n-1) * n })($1);
        }}
    | e '%'
        {$$ = $1/100;}
    | '-' e %prec UMINUS
        {$$ = -$2;}
    | '(' e ')'
        {$$ = $2;}
    | NUMBER
        {$$ = Number(yytext);}
    | E
        {$$ = Math.E;}
    | PI
        {$$ = Math.PI;}
    ;

main.js

[~/jison/examples/html_calc_example(develop)]$ cat main.js
$(document).ready(function () {
  $("button").click(function () {
    try {
      var result = calculator.parse($("input").val())
      $("span").html(result);
    } catch (e) {
      $("span").html(String(e));
    }
  });
});

calculator.html

[~/jison/examples/html_calc_example(develop)]$ cat calculator.html 
<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Calc</title>
    <link rel="stylesheet" href="global.css" type="text/css" media="screen" charset="utf-8" />
  </head>
  <body>
    <h1>Calculator demo</h1>
    <div id="content">
      <script src="jquery/jquery.js"></script> 
      <script src="calculator.js"></script>
      <script src="main.js"></script>
      <p>
        <input type="text" value="PI*4^2 + 5" /> <button>equals</button> 
        <span></span> <!-- Output goes here! -->
      </p>
    </div>
  </body>
</html>

Rakefile

[~/jisoncalc(clase)]$ cat Rakefile 
task :default => %w{calcugly.js} do
  sh "jsbeautify calcugly.js > calculator.js"
  sh "rm -f calcugly.js"
end

file "calcugly.js" => %w{calculator.jison} do
  sh "jison calculator.jison calculator.l -o calculator.js; mv calculator.js calcugly.js"
end

task :testf do
  sh "open -a firefox test/test.html"
end

task :tests do
  sh "open -a safari test/test.html"
end

global.css

[~/jison/examples/html_calc_example(develop)]$ cat global.css
html *
{
   font-size: large; 
   /* The !important ensures that nothing can override what you've set in this style (unless it is also important). */
   font-family: Arial;
}

.thumb {
    height: 75px;
    border: 1px solid #000;
    margin: 10px 5px 0 0;
  }

h1            { text-align: center; font-size: x-large; }
th, td        { vertical-align: top; text-align: left; }   
/* #finaltable  * { color: white; background-color: black; }   */

/* #finaltable table { border-collapse:collapse; } */
/* #finaltable table, td { border:1px solid white; } */
#finaltable:hover td { background-color: blue; } 
tr:nth-child(odd)    { background-color:#eee; }
tr:nth-child(even)    { background-color:#00FF66; }
input        { text-align: right;  border: none;       }     /* Align input to the right  */
textarea     { border: outset; border-color: white;       }                        
table        { border: inset; border-color: white; }
.hidden      { display: none; }
.unhidden    { display: block; }
table.center { margin-left:auto; margin-right:auto; }
#result      { border-color: red; }
tr.error       { background-color: red; }
pre.output   { background-color: white; }
span.repeated { background-color: red }
span.header { background-color: blue }
span.comments { background-color: orange }
span.blanks { background-color: green }
span.nameEqualValue { background-color: cyan }
span.error { background-color: red }

body
{
 background-color:#b0c4de;  /* blue */
}

test/assert.html

$ cat test/assert.js 
var output = document.getElementById('output');

function assert( outcome, description) {
  var li = document.createElement('li');
  li.className = outcome ? 'pass' : 'fail';
  li.appendChild(document.createTextNode(description));
  
  output.appendChild(li);
};

test/test.css

~/jisoncalc(clase)]$ cat test/test.css 
.pass:before {
  content: 'PASS: ';
  color:  blue;
  font-weight: bold;
}

.fail:before {
  content: 'FAIL: ';
  color: red;
  font-weight: bold;
  
}

test/test.html

[~/jisoncalc(clase)]$ cat test/test.html
<!DOCTYPE HTML>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Testing Our Simple Calculator</title>
    <link rel="stylesheet" href="test.css" />
    <script type="text/javascript" src="../calculator.js"></script>

  </head>
  <body>
    <h1>Testing Our Simple Calculator
    </h1>
    
    <ul id="output"></ul>
    <script type="text/javascript" src="______.js"></script>
    
    <script type="text/javascript">
      var r = __________.parse("a = 4*8");
      assert(_______________, "a is 4*8");
      assert(_____________, "32 == 4*8");
      r = calculator.parse("a = 4;\nb=a+1;\nc=b*2");
      assert(____________, "4 is the first computed result ");
      assert(______________, "a is 4");
      assert(______________, "b is 5");
      assert(______________, "c is 10");
    </script>
      See the NetTuts+ tutorial at <a href="http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-quick-and-easy-javascript-testing-with-assert/">Quick and Easy JavaScript Testing</a>
  </body>
</html>


Práctica: Calculadora con Listas de Expresiones y Variables

Modifique la calculadora vista en la sección anterior 5.3 para que el lenguaje cumpla los siguientes requisitos:

Casiano Rodríguez León
2016-03-27