Parsing/RPN calculator algorithm: Difference between revisions

Added Quackery.
(Added 11l)
(Added Quackery.)
Line 4,200:
/ [3.0, 0.0001220703125]
+ [3.0001220703125]</pre>
 
=={{header|Quackery}}==
 
On an historical note, the first step in developing the language Quackery, as is the case with many stack based/reverse Polish/concatenative languages, was coding an RP calculator much like this one. With the minor difference that the exponentiation operator in Quackery is <code>**</code> rather than <code>^</code> (which is bitwise xor) the test string <code>"3 4 2 * 1 5 - 2 3 ^ ^ / +"</code> ''is'' Quackery code, and could be compiled and evaluated (in the Quackery vernacular, "built and done") by passing it to the Quackery word <code>quackery</code>, which is defined in Quackery as
 
<code>[ build do ] is quackery ( $ --> [ )</code>, which neatly sums up the language.
 
Here we do the same thing explicitly, using a <code>switch</code> statement (not actually defined in Quackery, so the code to define it is included), and using the provided ancillary stack <code>temp</code> to make the stack activity explicit.
 
(If the gentle gatekeepers will permit a moment of shameless self-promotion… If you are interested in stack processors or concatenative languages, you may wish to consider that Quackery was designed with the intent of being an entry level language suitable for educational and hobbyist use, accessible at the code level by virtue of being coded in (mostly) non-idiomatic Python (24k source) and Quackery (another 24k of course) with the Python code emphasising a straightforward approach and the use of simple algorithms for the sake of legibility in preference to efficiency.)
 
<lang Quackery>
[ stack ] is switch.arg ( --> [ )
 
[ switch.arg put ] is switch ( x --> )
 
[ switch.arg release ] is otherwise ( --> )
 
[ switch.arg share
!= iff ]else[ done
otherwise
]'[ do ]done[ ] is case ( x --> )
 
[ say "Applying: "
swap echo$ sp
temp take
temp take
swap rot do
temp put ] is apply ( $ x --> )
 
[ say "Pushing: " dup echo$ sp
$->n drop temp put ] is isnumber ( $ --> )
 
[ temp copy echo cr ] is display ( --> )
 
[ nest$ witheach
[ dup switch
[ $ '+' case [ ' + apply ]
$ '-' case [ ' - apply ]
$ '*' case [ ' * apply ]
$ '/' case [ ' / apply ]
$ '^' case [ ' ** apply ]
otherwise [ isnumber ] ]
display ]
temp take ] is rpncalc ( $ --> n )
 
$ "3 4 2 * 1 5 - 2 3 ^ ^ / +" rpncalc
say "Result: " echo</lang>
 
{{out}}
 
<pre>Pushing: 3 [ stack 3 ]
Pushing: 4 [ stack 3 4 ]
Pushing: 2 [ stack 3 4 2 ]
Applying: * [ stack 3 8 ]
Pushing: 1 [ stack 3 8 1 ]
Pushing: 5 [ stack 3 8 1 5 ]
Applying: - [ stack 3 8 -4 ]
Pushing: 2 [ stack 3 8 -4 2 ]
Pushing: 3 [ stack 3 8 -4 2 3 ]
Applying: ^ [ stack 3 8 -4 8 ]
Applying: ^ [ stack 3 8 65536 ]
Applying: / [ stack 3 0 ]
Applying: + [ stack 3 ]
Result: 3</pre>
 
=={{header|Racket}}==
1,462

edits