Parsing/RPN calculator algorithm: Difference between revisions

Added Forth entry
(Add CLU)
(Added Forth entry)
Line 1,929:
+ { 3+1/8192 }
</pre>
 
 
=={{header|Forth}}==
 
{{works with|gforth|0.7.3}}
Forth is stack-based, so evaluation is direct:
<lang forth>: ^ over swap 1 ?do over * loop nip ;
s" 3 4 2 * 1 5 - 2 3 ^ ^ / +" evaluate .</lang>
 
To show intermediate steps:
<lang forth>: ^ over swap 1 ?do over * loop nip ;
: detail
begin
cr ." stack: " .s
bl word count dup
0<> while
." , read: " 2dup type evaluate
repeat
2drop
;
detail 3 4 2 * 1 5 - 2 3 ^ ^ / +</lang>
 
{{out}}
<pre>stack: <0> , read: 3
stack: <1> 3 , read: 4
stack: <2> 3 4 , read: 2
stack: <3> 3 4 2 , read: *
stack: <2> 3 8 , read: 1
stack: <3> 3 8 1 , read: 5
stack: <4> 3 8 1 5 , read: -
stack: <3> 3 8 -4 , read: 2
stack: <4> 3 8 -4 2 , read: 3
stack: <5> 3 8 -4 2 3 , read: ^
stack: <4> 3 8 -4 8 , read: ^
stack: <3> 3 8 65536 , read: /
stack: <2> 3 0 , read: +
stack: <1> 3 ok</pre>
 
 
 
=={{header|Fortran}}==
Anonymous user