Infijo | Postfijo |
a = 3+2*4 |
3 2 4 * + &a = |
b = a == 11 |
a 11 == &b = |
&a
indica la dirección de la variable a
y a
indica el valor almacenado en la variable a
.
Añada sentencias
if
...
then
e if
... then
... else
Para realizar la traducción de estas sentencias
añada
instrucciones jmp label
y jmpz label
(por jump if zero)
y etiquetas:
Infijo | Postfijo |
a = (2+5)*3; if a == 0 then b = 5 else b = 3; c = b + 1; |
2 5 + 3 * &a = a 0 == jmpz else1 5 &b = jmp endif0 :else1 3 &b = :endif0 b 1 + &c = |
[~/srcPLgrado/jisoninfix2postfix(master)]$ cat test/test.html <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="UTF-8"> <title>Testing Our Simple Translator</title> <link rel="stylesheet" href="test.css" /> <script type="text/javascript" src="../calculator.js"></script> </head> <body> <h1>Testing Our Simple Translator </h1> <ul id="output"></ul> <script type="text/javascript" src="assert.js"></script> <script type="text/javascript"> var r = calculator.parse("a = 4*8"); assert( /4\s*8\s*[*]\s*a\s*=\s*/.exec(r), "a is 4*8"); r = calculator.parse("a=4;b=a+1"); r = r.replace(/\s+/g,''); var expected = "4a=a1+b="; assert( r == expected, "a = 4;\nb=a+1 translated"); var r = calculator.parse("if a > 0 then b = 1 else b = 2"); r = r.replace(/\s+/g,''); expected = "a 0 > jmpz else1 1 b = jmp endif0 :else1 2 b = :endif0". replace(/\s+/g,''); assert( r == expected, "'if a > 0 then b = 1 else b = 2' translated"); </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>
Casiano Rodríguez León