Parsing/RPN calculator algorithm: Difference between revisions

Dialects of BASIC moved to the BASIC section.
(Added XPL0 example.)
(Dialects of BASIC moved to the BASIC section.)
Line 586:
</pre>
 
=={{header|ANSI Standard BASICANTLR}}==
[[File:Rpn.png|left|rpnC]]
[[File:rpnCNum.png|left|rpnC]]
[[File:RpnCop.png|left|rpnC]]
<br clear=both>
===Java===
<syntaxhighlight lang="java">
grammar rpnC ;
//
// rpn Calculator
//
// Nigel Galloway - April 7th., 2012
//
@members {
Stack<Double> s = new Stack<Double>();
}
rpn : (WS* (num|op) (WS | WS* NEWLINE {System.out.println(s.pop());}))*;
num : '-'? Digit+ ('.' Digit+)? {s.push(Double.parseDouble($num.text));};
Digit : '0'..'9';
op : '-' {double x = s.pop(); s.push(s.pop() - x);}
| '/' {double x = s.pop(); s.push(s.pop() / x);}
| '*' {s.push(s.pop() * s.pop());}
| '^' {double x = s.pop(); s.push(Math.pow(s.pop(), x));}
| '+' {s.push(s.pop() + s.pop());};
WS : (' ' | '\t'){skip()};
NEWLINE : '\r'? '\n';
</syntaxhighlight>
Produces:
<pre>
>java Test
3 4 2 * 1 5 - 2 3 ^ ^ / +
^Z
3.0001220703125
</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
Output is in clipboard.
<syntaxhighlight lang="ahk">evalRPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
evalRPN(s){
stack := []
out := "For RPN expression: '" s "'`r`n`r`nTOKEN`t`tACTION`t`t`tSTACK`r`n"
Loop Parse, s
If A_LoopField is number
t .= A_LoopField
else
{
If t
stack.Insert(t)
, out .= t "`tPush num onto top of stack`t" stackShow(stack) "`r`n"
, t := ""
If InStr("+-/*^", l := A_LoopField)
{
a := stack.Remove(), b := stack.Remove()
stack.Insert( l = "+" ? b + a
:l = "-" ? b - a
:l = "*" ? b * a
:l = "/" ? b / a
:l = "^" ? b **a
:0 )
out .= l "`tApply op " l " to top of stack`t" stackShow(stack) "`r`n"
}
}
r := stack.Remove()
out .= "`r`n The final output value is: '" r "'"
clipboard := out
return r
}
StackShow(stack){
for each, value in stack
out .= A_Space value
return subStr(out, 2)
}</syntaxhighlight>
{{out}}
<pre>For RPN expression: '3 4 2 * 1 5 - 2 3 ^ ^ / +'
 
TOKEN ACTION STACK
3 Push num onto top of stack 3
4 Push num onto top of stack 3 4
2 Push num onto top of stack 3 4 2
* Apply op * to top of stack 3 8
1 Push num onto top of stack 3 8 1
5 Push num onto top of stack 3 8 1 5
- Apply op - to top of stack 3 8 -4
2 Push num onto top of stack 3 8 -4 2
3 Push num onto top of stack 3 8 -4 2 3
^ Apply op ^ to top of stack 3 8 -4 8
^ Apply op ^ to top of stack 3 8 65536
/ Apply op / to top of stack 3 0.000122
+ Apply op + to top of stack 3.000122
 
The final output value is: '3.000122'</pre>
 
=={{header|BASIC}}==
==={{header|ANSI Standard BASIC}}===
<syntaxhighlight lang="ansi standard basic">1000 DECLARE EXTERNAL SUB rpn
1010 PUBLIC NUMERIC R(64) ! stack
Line 712 ⟶ 806:
2230 END SUB</syntaxhighlight>
 
==={{header|ANTLRBBC BASIC}}===
[[File:Rpn.png|left|rpnC]]
[[File:rpnCNum.png|left|rpnC]]
[[File:RpnCop.png|left|rpnC]]
<br clear=both>
===Java===
<syntaxhighlight lang="java">
grammar rpnC ;
//
// rpn Calculator
//
// Nigel Galloway - April 7th., 2012
//
@members {
Stack<Double> s = new Stack<Double>();
}
rpn : (WS* (num|op) (WS | WS* NEWLINE {System.out.println(s.pop());}))*;
num : '-'? Digit+ ('.' Digit+)? {s.push(Double.parseDouble($num.text));};
Digit : '0'..'9';
op : '-' {double x = s.pop(); s.push(s.pop() - x);}
| '/' {double x = s.pop(); s.push(s.pop() / x);}
| '*' {s.push(s.pop() * s.pop());}
| '^' {double x = s.pop(); s.push(Math.pow(s.pop(), x));}
| '+' {s.push(s.pop() + s.pop());};
WS : (' ' | '\t'){skip()};
NEWLINE : '\r'? '\n';
</syntaxhighlight>
Produces:
<pre>
>java Test
3 4 2 * 1 5 - 2 3 ^ ^ / +
^Z
3.0001220703125
</pre>
 
=={{header|AutoHotkey}}==
{{works with|AutoHotkey_L}}
Output is in clipboard.
<syntaxhighlight lang="ahk">evalRPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
evalRPN(s){
stack := []
out := "For RPN expression: '" s "'`r`n`r`nTOKEN`t`tACTION`t`t`tSTACK`r`n"
Loop Parse, s
If A_LoopField is number
t .= A_LoopField
else
{
If t
stack.Insert(t)
, out .= t "`tPush num onto top of stack`t" stackShow(stack) "`r`n"
, t := ""
If InStr("+-/*^", l := A_LoopField)
{
a := stack.Remove(), b := stack.Remove()
stack.Insert( l = "+" ? b + a
:l = "-" ? b - a
:l = "*" ? b * a
:l = "/" ? b / a
:l = "^" ? b **a
:0 )
out .= l "`tApply op " l " to top of stack`t" stackShow(stack) "`r`n"
}
}
r := stack.Remove()
out .= "`r`n The final output value is: '" r "'"
clipboard := out
return r
}
StackShow(stack){
for each, value in stack
out .= A_Space value
return subStr(out, 2)
}</syntaxhighlight>
{{out}}
<pre>For RPN expression: '3 4 2 * 1 5 - 2 3 ^ ^ / +'
 
TOKEN ACTION STACK
3 Push num onto top of stack 3
4 Push num onto top of stack 3 4
2 Push num onto top of stack 3 4 2
* Apply op * to top of stack 3 8
1 Push num onto top of stack 3 8 1
5 Push num onto top of stack 3 8 1 5
- Apply op - to top of stack 3 8 -4
2 Push num onto top of stack 3 8 -4 2
3 Push num onto top of stack 3 8 -4 2 3
^ Apply op ^ to top of stack 3 8 -4 8
^ Apply op ^ to top of stack 3 8 65536
/ Apply op / to top of stack 3 0.000122
+ Apply op + to top of stack 3.000122
 
The final output value is: '3.000122'</pre>
 
=={{header|BBC BASIC}}==
<syntaxhighlight lang="bbcbasic"> @% = &60B
RPN$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
Line 860 ⟶ 861:
+ : 3.00012
</pre>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">#define NULL 0
 
type node
'implement the stack as a linked list
n as double
p as node ptr
end type
 
function spctok( byref s as string ) as string
'returns everything in the string up to the first space
'modifies the original string to begin at the fist non-space char after the first space
dim as string r
dim as double i = 1
while mid(s,i,1)<>" " and i<=len(s)
r += mid(s,i,1)
i+=1
wend
do
i+=1
loop until mid(s,i,1)<>" " or i >= len(s)
s = right(s,len(s)-i+1)
return r
end function
 
sub print_stack( byval S as node ptr )
'display everything on the stack
print "Stack <--- ";
while S->p <> NULL
S = S->p
print S->n;" ";
wend
print
end sub
 
sub push( byval S as node ptr, v as double )
'push a value onto the stack
dim as node ptr x
x = allocate(sizeof(node))
x->n = v
x->p = S->p
S->p = x
end sub
 
function pop( byval S as node ptr ) as double
'pop a value from the stack
if s->P = NULL then return -99999
dim as double r = S->p->n
dim as node ptr junk = S->p
S->p = S->p->p
deallocate(junk)
return r
end function
 
dim as string s = "3 4 2 * 1 5 - 2 3 ^ ^ / +", c
dim as node StackHead
 
while len(s) > 0
c = spctok(s)
print "Token: ";c;" ";
select case c
case "+"
push(@StackHead, pop(@StackHead) + pop(@StackHead))
print "Operation + ";
case "-"
push(@StackHead, -(pop(@StackHead) - pop(@StackHead)))
print "Operation - ";
case "/"
push(@StackHead, 1./(pop(@StackHead) / pop(@StackHead)))
print "Operation / ";
case "*"
push(@StackHead, pop(@StackHead) * pop(@StackHead))
print "Operation * ";
case "^"
push(@StackHead, pop(@StackHead) ^ pop(@StackHead))
print "Operation ^ ";
case else
push(@StackHead, val(c))
print "Operation push ";
end select
print_stack(@StackHead)
wend</syntaxhighlight>
{{out}}<pre>
Token: 3 Operation push Stack <--- 3
Token: 4 Operation push Stack <--- 4 3
Token: 2 Operation push Stack <--- 2 4 3
Token: * Operation * Stack <--- 8 3
Token: 1 Operation push Stack <--- 1 8 3
Token: 5 Operation push Stack <--- 5 1 8 3
Token: - Operation - Stack <--- -4 8 3
Token: 2 Operation push Stack <--- 2 -4 8 3
Token: 3 Operation push Stack <--- 3 2 -4 8 3
Token: ^ Operation ^ Stack <--- 8 -4 8 3
Token: ^ Operation ^ Stack <--- 65536 8 3
Token: / Operation / Stack <--- 0.0001220703125 3
Token: + Operation + Stack <--- 3.0001220703125
</pre>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">
global stack$
 
expr$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
print "Expression:"
print expr$
print
 
print "Input","Operation","Stack after"
 
stack$=""
token$ = "#"
i = 1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
 
do
print "Token ";i;": ";token$,
select case
'operation
case instr("+-*/^",token$)<>0
print "operate",
op2$=pop$()
op1$=pop$()
if op1$="" then
print "Error: stack empty for ";i;"-th token: ";token$
end
end if
 
op1=val(op1$)
op2=val(op2$)
 
select case token$
case "+"
res = op1+op2
case "-"
res = op1-op2
case "*"
res = op1*op2
case "/"
res = op1/op2
case "^"
res = op1^op2
end select
 
call push str$(res)
'default:number
case else
print "push",
call push token$
end select
print "Stack: ";reverse$(stack$)
i = i+1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
loop until token$ =""
 
res$=pop$()
print
print "Result:" ;res$
extra$=pop$()
if extra$<>"" then
print "Error: extra things on a stack: ";extra$
end if
end
 
'---------------------------------------
function reverse$(s$)
reverse$ = ""
token$="#"
while token$<>""
i=i+1
token$=word$(s$,i,"|")
reverse$ = token$;" ";reverse$
wend
end function
'---------------------------------------
sub push s$
stack$=s$+"|"+stack$ 'stack
end sub
 
function pop$()
'it does return empty on empty stack
pop$=word$(stack$,1,"|")
stack$=mid$(stack$,instr(stack$,"|")+1)
end function
</syntaxhighlight>
 
{{out}}
<pre>
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Input Operation Stack after
Token 1: 3 push Stack: 3
Token 2: 4 push Stack: 3 4
Token 3: 2 push Stack: 3 4 2
Token 4: * operate Stack: 3 8
Token 5: 1 push Stack: 3 8 1
Token 6: 5 push Stack: 3 8 1 5
Token 7: - operate Stack: 3 8 -4
Token 8: 2 push Stack: 3 8 -4 2
Token 9: 3 push Stack: 3 8 -4 2 3
Token 10: ^ operate Stack: 3 8 -4 8
Token 11: ^ operate Stack: 3 8 65536
Token 12: / operate Stack: 3 0.12207031e-3
Token 13: + operate Stack: 3.00012207
 
Result:3.00012207
</pre>
 
==={{header|Run BASIC}}===
<syntaxhighlight lang="runbasic">prn$ = "3 4 2 * 1 5 - 2 3 ^ ^ / + "
 
j = 0
while word$(prn$,i + 1," ") <> ""
i = i + 1
n$ = word$(prn$,i," ")
if n$ < "0" or n$ > "9" then
num1 = val(word$(stack$,s," "))
num2 = val(word$(stack$,s-1," "))
n = op(n$,num2,num1)
s = s - 1
stack$ = stk$(stack$,s -1,str$(n))
print "Push Opr ";n$;" to stack: ";stack$
else
s = s + 1
stack$ = stack$ + n$ + " "
print "Push Num ";n$;" to stack: ";stack$
end if
wend
 
function stk$(stack$,s,a$)
for i = 1 to s
stk$ = stk$ + word$(stack$,i," ") + " "
next i
stk$ = stk$ + a$ + " "
end function
 
FUNCTION op(op$,a,b)
if op$ = "*" then op = a * b
if op$ = "/" then op = a / b
if op$ = "^" then op = a ^ b
if op$ = "+" then op = a + b
if op$ = "-" then op = a - b
end function</syntaxhighlight>
<pre>Push Num 3 to stack: 3
Push Num 4 to stack: 3 4
Push Num 2 to stack: 3 4 2
Push Opr * to stack: 3 8
Push Num 1 to stack: 3 8 1
Push Num 5 to stack: 3 8 1 5
Push Opr - to stack: 3 8 -4
Push Num 2 to stack: 3 8 -4 2
Push Num 3 to stack: 3 8 -4 2 3
Push Opr ^ to stack: 3 8 -4 8
Push Opr ^ to stack: 3 8 65536
Push Opr / to stack: 3 1.22070312e-4
Push Opr + to stack: 3.00012207</pre>
 
==={{header|Sinclair ZX81 BASIC}}===
If you only have 1k of RAM, this program will correctly evaluate the test expression with fewer than 10 bytes to spare. (I know that because I tried running it with the first line modified to allow a stack depth of 7, i.e. allocating space for two more 40-bit floats, and it crashed with an "out of memory" error code before it could print the result of the final addition.) If we desperately needed a few extra bytes there are ways they could be shaved out of the current program; but this version works, and editing a program that takes up almost all your available RAM isn't very comfortable, and to make it really useful for practical purposes you would still want to have 2k or more anyway.
 
The ZX81 character set doesn't include <code>^</code>, so we have to use <code>**</code> instead. Note that this is not two separate stars, although that's what it looks like: you have to enter it by typing <code>SHIFT</code>+<code>H</code>.
 
No attempt is made to check for invalid syntax, stack overflow or underflow, etc.
 
<syntaxhighlight lang="basic"> 10 DIM S(5)
20 LET P=1
30 INPUT E$
40 LET I=0
50 LET I=I+1
60 IF E$(I)=" " THEN GOTO 110
70 IF I<LEN E$ THEN GOTO 50
80 LET W$=E$
90 GOSUB 150
100 STOP
110 LET W$=E$( TO I-1)
120 LET E$=E$(I+1 TO )
130 GOSUB 150
140 GOTO 40
150 IF W$="+" OR W$="-" OR W$="*" OR W$="/" OR W$="**" THEN GOTO 250
160 LET S(P)=VAL W$
170 LET P=P+1
180 PRINT W$;
190 PRINT ":";
200 FOR I=P-1 TO 1 STEP -1
210 PRINT " ";S(I);
220 NEXT I
230 PRINT
240 RETURN
250 IF W$="**" THEN LET S(P-2)=ABS S(P-2)
260 LET S(P-2)=VAL (STR$ S(P-2)+W$+STR$ S(P-1))
270 LET P=P-1
280 GOTO 180</syntaxhighlight>
{{in}}
<pre>3 4 2 * 1 5 - 2 3 ** ** / +</pre>
{{out}}
<pre>3: 3
4: 4 3
2: 2 4 3
*: 8 3
1: 1 8 3
5: 5 1 8 3
-: -4 8 3
2: 2 -4 8 3
3: 3 2 -4 8 3
**: 8 -4 8 3
**: 65536 8 3
/: .00012207031 3
+: 3.0001221</pre>
 
==={{header|VBA}}===
{{trans|Liberty BASIC}}
<syntaxhighlight lang="vba">Global stack$
Function RPN(expr$)
Debug.Print "Expression:"
Debug.Print expr$
Debug.Print "Input", "Operation", "Stack after"
stack$ = ""
token$ = "#"
i = 1
token$ = Split(expr$)(i - 1) 'split is base 0
token2$ = " " + token$ + " "
Do
Debug.Print "Token "; i; ": "; token$,
'operation
If InStr("+-*/^", token$) <> 0 Then
Debug.Print "operate",
op2$ = pop$()
op1$ = pop$()
If op1$ = "" Then
Debug.Print "Error: stack empty for "; i; "-th token: "; token$
End
End If
op1 = Val(op1$)
op2 = Val(op2$)
Select Case token$
Case "+"
res = CDbl(op1) + CDbl(op2)
Case "-"
res = CDbl(op1) - CDbl(op2)
Case "*"
res = CDbl(op1) * CDbl(op2)
Case "/"
res = CDbl(op1) / CDbl(op2)
Case "^"
res = CDbl(op1) ^ CDbl(op2)
End Select
Call push2(str$(res))
'default:number
Else
Debug.Print "push",
Call push2(token$)
End If
Debug.Print "Stack: "; reverse$(stack$)
i = i + 1
If i > Len(Join(Split(expr, " "), "")) Then
token$ = ""
Else
token$ = Split(expr$)(i - 1) 'base 0
token2$ = " " + token$ + " "
End If
Loop Until token$ = ""
Debug.Print
Debug.Print "Result:"; pop$()
'extra$ = pop$()
If stack <> "" Then
Debug.Print "Error: extra things on a stack: "; stack$
End If
End
End Function
'---------------------------------------
Function reverse$(s$)
reverse$ = ""
token$ = "#"
While token$ <> ""
i = i + 1
token$ = Split(s$, "|")(i - 1) 'split is base 0
reverse$ = token$ & " " & reverse$
Wend
End Function
'---------------------------------------
Sub push2(s$)
stack$ = s$ + "|" + stack$ 'stack
End Sub
Function pop$()
'it does return empty on empty stack
pop$ = Split(stack$, "|")(0)
stack$ = Mid$(stack$, InStr(stack$, "|") + 1)
End Function</syntaxhighlight>
 
{{out}}
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
Input Operation Stack after
Token 1 : 3 push Stack: 3
Token 2 : 4 push Stack: 3 4
Token 3 : 2 push Stack: 3 4 2
Token 4 : * operate Stack: 3 8
Token 5 : 1 push Stack: 3 8 1
Token 6 : 5 push Stack: 3 8 1 5
Token 7 : - operate Stack: 3 8 -4
Token 8 : 2 push Stack: 3 8 -4 2
Token 9 : 3 push Stack: 3 8 -4 2 3
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .0001220703125
Token 13 : + operate Stack: 3.0001220703125
 
Result: 3.0001220703125</pre>
 
==={{header|Xojo}}===
{{trans|VBA}}
<syntaxhighlight lang="xojo">
Function RPN(expr As String) As String
Dim tokenArray() As String
Dim stack() As String
Dim Wert1 As Double
Dim Wert2 As Double
'Initialize array (removed later)
ReDim tokenArray(1)
ReDim stack(1)
tokenArray = Split(expr, " ")
Dim i As integer
i = 0
 
While i <= tokenArray.Ubound
If tokenArray(i) = "+" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1+Wert2))
ElseIf tokenArray(i) = "-" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1-Wert2))
ElseIf tokenArray(i) = "*" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1*Wert2))
ElseIf tokenArray(i) = "/" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1/Wert2))
ElseIf tokenArray(i) = "^" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(pow(Wert1,Wert2)))
Else
stack.Append(tokenArray(i))
End If
i = i +1
Wend
Return stack(2)
End Function</syntaxhighlight>
 
 
 
{{out}}
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Input Operation Stack after
Token 1 : 3 push Stack: 3
Token 2 : 4 push Stack: 3 4
Token 3 : 2 push Stack: 3 4 2
Token 4 : * operate Stack: 3 8
Token 5 : 1 push Stack: 3 8 1
Token 6 : 5 push Stack: 3 8 1 5
Token 7 : - operate Stack: 3 8 -4
Token 8 : 2 push Stack: 3 8 -4 2
Token 9 : 3 push Stack: 3 8 -4 2 3
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .000122
Token 13 : + operate Stack: 3.000122
 
Result: 3.000122</pre>
 
=={{header|Bracmat}}==
Line 2,042 ⟶ 2,540:
25 + XEQ 1: 3.000122
Result is... 3.000122</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">#define NULL 0
 
type node
'implement the stack as a linked list
n as double
p as node ptr
end type
 
function spctok( byref s as string ) as string
'returns everything in the string up to the first space
'modifies the original string to begin at the fist non-space char after the first space
dim as string r
dim as double i = 1
while mid(s,i,1)<>" " and i<=len(s)
r += mid(s,i,1)
i+=1
wend
do
i+=1
loop until mid(s,i,1)<>" " or i >= len(s)
s = right(s,len(s)-i+1)
return r
end function
 
sub print_stack( byval S as node ptr )
'display everything on the stack
print "Stack <--- ";
while S->p <> NULL
S = S->p
print S->n;" ";
wend
print
end sub
 
sub push( byval S as node ptr, v as double )
'push a value onto the stack
dim as node ptr x
x = allocate(sizeof(node))
x->n = v
x->p = S->p
S->p = x
end sub
 
function pop( byval S as node ptr ) as double
'pop a value from the stack
if s->P = NULL then return -99999
dim as double r = S->p->n
dim as node ptr junk = S->p
S->p = S->p->p
deallocate(junk)
return r
end function
 
dim as string s = "3 4 2 * 1 5 - 2 3 ^ ^ / +", c
dim as node StackHead
 
while len(s) > 0
c = spctok(s)
print "Token: ";c;" ";
select case c
case "+"
push(@StackHead, pop(@StackHead) + pop(@StackHead))
print "Operation + ";
case "-"
push(@StackHead, -(pop(@StackHead) - pop(@StackHead)))
print "Operation - ";
case "/"
push(@StackHead, 1./(pop(@StackHead) / pop(@StackHead)))
print "Operation / ";
case "*"
push(@StackHead, pop(@StackHead) * pop(@StackHead))
print "Operation * ";
case "^"
push(@StackHead, pop(@StackHead) ^ pop(@StackHead))
print "Operation ^ ";
case else
push(@StackHead, val(c))
print "Operation push ";
end select
print_stack(@StackHead)
wend</syntaxhighlight>
{{out}}<pre>
Token: 3 Operation push Stack <--- 3
Token: 4 Operation push Stack <--- 4 3
Token: 2 Operation push Stack <--- 2 4 3
Token: * Operation * Stack <--- 8 3
Token: 1 Operation push Stack <--- 1 8 3
Token: 5 Operation push Stack <--- 5 1 8 3
Token: - Operation - Stack <--- -4 8 3
Token: 2 Operation push Stack <--- 2 -4 8 3
Token: 3 Operation push Stack <--- 3 2 -4 8 3
Token: ^ Operation ^ Stack <--- 8 -4 8 3
Token: ^ Operation ^ Stack <--- 65536 8 3
Token: / Operation / Stack <--- 0.0001220703125 3
Token: + Operation + Stack <--- 3.0001220703125
</pre>
 
=={{header|FunL}}==
Line 2,911 ⟶ 3,311:
- and the "or" boolean function.
</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">
global stack$
 
expr$ = "3 4 2 * 1 5 - 2 3 ^ ^ / +"
print "Expression:"
print expr$
print
 
print "Input","Operation","Stack after"
 
stack$=""
token$ = "#"
i = 1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
 
do
print "Token ";i;": ";token$,
select case
'operation
case instr("+-*/^",token$)<>0
print "operate",
op2$=pop$()
op1$=pop$()
if op1$="" then
print "Error: stack empty for ";i;"-th token: ";token$
end
end if
 
op1=val(op1$)
op2=val(op2$)
 
select case token$
case "+"
res = op1+op2
case "-"
res = op1-op2
case "*"
res = op1*op2
case "/"
res = op1/op2
case "^"
res = op1^op2
end select
 
call push str$(res)
'default:number
case else
print "push",
call push token$
end select
print "Stack: ";reverse$(stack$)
i = i+1
token$ = word$(expr$, i)
token2$ = " "+token$+" "
loop until token$ =""
 
res$=pop$()
print
print "Result:" ;res$
extra$=pop$()
if extra$<>"" then
print "Error: extra things on a stack: ";extra$
end if
end
 
'---------------------------------------
function reverse$(s$)
reverse$ = ""
token$="#"
while token$<>""
i=i+1
token$=word$(s$,i,"|")
reverse$ = token$;" ";reverse$
wend
end function
'---------------------------------------
sub push s$
stack$=s$+"|"+stack$ 'stack
end sub
 
function pop$()
'it does return empty on empty stack
pop$=word$(stack$,1,"|")
stack$=mid$(stack$,instr(stack$,"|")+1)
end function
</syntaxhighlight>
 
{{out}}
<pre>
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Input Operation Stack after
Token 1: 3 push Stack: 3
Token 2: 4 push Stack: 3 4
Token 3: 2 push Stack: 3 4 2
Token 4: * operate Stack: 3 8
Token 5: 1 push Stack: 3 8 1
Token 6: 5 push Stack: 3 8 1 5
Token 7: - operate Stack: 3 8 -4
Token 8: 2 push Stack: 3 8 -4 2
Token 9: 3 push Stack: 3 8 -4 2 3
Token 10: ^ operate Stack: 3 8 -4 8
Token 11: ^ operate Stack: 3 8 65536
Token 12: / operate Stack: 3 0.12207031e-3
Token 13: + operate Stack: 3.00012207
 
Result:3.00012207
</pre>
 
=={{header|Lua}}==
Line 5,063 ⟶ 5,351:
+ ADD [3.0001220703125]
Value = 3.0001220703125</pre>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">prn$ = "3 4 2 * 1 5 - 2 3 ^ ^ / + "
 
j = 0
while word$(prn$,i + 1," ") <> ""
i = i + 1
n$ = word$(prn$,i," ")
if n$ < "0" or n$ > "9" then
num1 = val(word$(stack$,s," "))
num2 = val(word$(stack$,s-1," "))
n = op(n$,num2,num1)
s = s - 1
stack$ = stk$(stack$,s -1,str$(n))
print "Push Opr ";n$;" to stack: ";stack$
else
s = s + 1
stack$ = stack$ + n$ + " "
print "Push Num ";n$;" to stack: ";stack$
end if
wend
 
function stk$(stack$,s,a$)
for i = 1 to s
stk$ = stk$ + word$(stack$,i," ") + " "
next i
stk$ = stk$ + a$ + " "
end function
 
FUNCTION op(op$,a,b)
if op$ = "*" then op = a * b
if op$ = "/" then op = a / b
if op$ = "^" then op = a ^ b
if op$ = "+" then op = a + b
if op$ = "-" then op = a - b
end function</syntaxhighlight>
<pre>Push Num 3 to stack: 3
Push Num 4 to stack: 3 4
Push Num 2 to stack: 3 4 2
Push Opr * to stack: 3 8
Push Num 1 to stack: 3 8 1
Push Num 5 to stack: 3 8 1 5
Push Opr - to stack: 3 8 -4
Push Num 2 to stack: 3 8 -4 2
Push Num 3 to stack: 3 8 -4 2 3
Push Opr ^ to stack: 3 8 -4 8
Push Opr ^ to stack: 3 8 65536
Push Opr / to stack: 3 1.22070312e-4
Push Opr + to stack: 3.00012207</pre>
 
=={{header|Rust}}==
Line 5,297 ⟶ 5,536:
3.0001220703125
</pre>
 
=={{header|Sinclair ZX81 BASIC}}==
If you only have 1k of RAM, this program will correctly evaluate the test expression with fewer than 10 bytes to spare. (I know that because I tried running it with the first line modified to allow a stack depth of 7, i.e. allocating space for two more 40-bit floats, and it crashed with an "out of memory" error code before it could print the result of the final addition.) If we desperately needed a few extra bytes there are ways they could be shaved out of the current program; but this version works, and editing a program that takes up almost all your available RAM isn't very comfortable, and to make it really useful for practical purposes you would still want to have 2k or more anyway.
 
The ZX81 character set doesn't include <code>^</code>, so we have to use <code>**</code> instead. Note that this is not two separate stars, although that's what it looks like: you have to enter it by typing <code>SHIFT</code>+<code>H</code>.
 
No attempt is made to check for invalid syntax, stack overflow or underflow, etc.
 
<syntaxhighlight lang="basic"> 10 DIM S(5)
20 LET P=1
30 INPUT E$
40 LET I=0
50 LET I=I+1
60 IF E$(I)=" " THEN GOTO 110
70 IF I<LEN E$ THEN GOTO 50
80 LET W$=E$
90 GOSUB 150
100 STOP
110 LET W$=E$( TO I-1)
120 LET E$=E$(I+1 TO )
130 GOSUB 150
140 GOTO 40
150 IF W$="+" OR W$="-" OR W$="*" OR W$="/" OR W$="**" THEN GOTO 250
160 LET S(P)=VAL W$
170 LET P=P+1
180 PRINT W$;
190 PRINT ":";
200 FOR I=P-1 TO 1 STEP -1
210 PRINT " ";S(I);
220 NEXT I
230 PRINT
240 RETURN
250 IF W$="**" THEN LET S(P-2)=ABS S(P-2)
260 LET S(P-2)=VAL (STR$ S(P-2)+W$+STR$ S(P-1))
270 LET P=P-1
280 GOTO 180</syntaxhighlight>
{{in}}
<pre>3 4 2 * 1 5 - 2 3 ** ** / +</pre>
{{out}}
<pre>3: 3
4: 4 3
2: 2 4 3
*: 8 3
1: 1 8 3
5: 5 1 8 3
-: -4 8 3
2: 2 -4 8 3
3: 3 2 -4 8 3
**: 8 -4 8 3
**: 65536 8 3
/: .00012207031 3
+: 3.0001221</pre>
 
=={{header|Swift}}==
Line 5,567 ⟶ 5,754:
+ : 3
3</syntaxhighlight>
 
=={{header|VBA}}==
 
{{trans|Liberty BASIC}}
 
<syntaxhighlight lang="vba">Global stack$
Function RPN(expr$)
Debug.Print "Expression:"
Debug.Print expr$
Debug.Print "Input", "Operation", "Stack after"
stack$ = ""
token$ = "#"
i = 1
token$ = Split(expr$)(i - 1) 'split is base 0
token2$ = " " + token$ + " "
Do
Debug.Print "Token "; i; ": "; token$,
'operation
If InStr("+-*/^", token$) <> 0 Then
Debug.Print "operate",
op2$ = pop$()
op1$ = pop$()
If op1$ = "" Then
Debug.Print "Error: stack empty for "; i; "-th token: "; token$
End
End If
op1 = Val(op1$)
op2 = Val(op2$)
Select Case token$
Case "+"
res = CDbl(op1) + CDbl(op2)
Case "-"
res = CDbl(op1) - CDbl(op2)
Case "*"
res = CDbl(op1) * CDbl(op2)
Case "/"
res = CDbl(op1) / CDbl(op2)
Case "^"
res = CDbl(op1) ^ CDbl(op2)
End Select
Call push2(str$(res))
'default:number
Else
Debug.Print "push",
Call push2(token$)
End If
Debug.Print "Stack: "; reverse$(stack$)
i = i + 1
If i > Len(Join(Split(expr, " "), "")) Then
token$ = ""
Else
token$ = Split(expr$)(i - 1) 'base 0
token2$ = " " + token$ + " "
End If
Loop Until token$ = ""
Debug.Print
Debug.Print "Result:"; pop$()
'extra$ = pop$()
If stack <> "" Then
Debug.Print "Error: extra things on a stack: "; stack$
End If
End
End Function
'---------------------------------------
Function reverse$(s$)
reverse$ = ""
token$ = "#"
While token$ <> ""
i = i + 1
token$ = Split(s$, "|")(i - 1) 'split is base 0
reverse$ = token$ & " " & reverse$
Wend
End Function
'---------------------------------------
Sub push2(s$)
stack$ = s$ + "|" + stack$ 'stack
End Sub
Function pop$()
'it does return empty on empty stack
pop$ = Split(stack$, "|")(0)
stack$ = Mid$(stack$, InStr(stack$, "|") + 1)
End Function</syntaxhighlight>
 
{{out}}
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
Input Operation Stack after
Token 1 : 3 push Stack: 3
Token 2 : 4 push Stack: 3 4
Token 3 : 2 push Stack: 3 4 2
Token 4 : * operate Stack: 3 8
Token 5 : 1 push Stack: 3 8 1
Token 6 : 5 push Stack: 3 8 1 5
Token 7 : - operate Stack: 3 8 -4
Token 8 : 2 push Stack: 3 8 -4 2
Token 9 : 3 push Stack: 3 8 -4 2 3
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .0001220703125
Token 13 : + operate Stack: 3.0001220703125
 
Result: 3.0001220703125</pre>
 
=={{header|V (Vlang)}}==
Line 5,849 ⟶ 5,924:
The final value is 3.0001220703125
</pre>
 
=={{header|Xojo}}==
 
{{trans|VBA}}
 
<syntaxhighlight lang="xojo">
Function RPN(expr As String) As String
 
Dim tokenArray() As String
Dim stack() As String
Dim Wert1 As Double
Dim Wert2 As Double
'Initialize array (removed later)
ReDim tokenArray(1)
ReDim stack(1)
tokenArray = Split(expr, " ")
Dim i As integer
i = 0
While i <= tokenArray.Ubound
If tokenArray(i) = "+" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1+Wert2))
ElseIf tokenArray(i) = "-" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1-Wert2))
ElseIf tokenArray(i) = "*" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1*Wert2))
ElseIf tokenArray(i) = "/" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(Wert1/Wert2))
ElseIf tokenArray(i) = "^" Then
Wert2 = Val(stack.pop)
Wert1 = Val(stack.pop)
stack.Append(Str(pow(Wert1,Wert2)))
Else
stack.Append(tokenArray(i))
End If
i = i +1
Wend
Return stack(2)
End Function</syntaxhighlight>
 
 
 
{{out}}
<pre>?RPN("3 4 2 * 1 5 - 2 3 ^ ^ / +")
Expression:
3 4 2 * 1 5 - 2 3 ^ ^ / +
 
Input Operation Stack after
Token 1 : 3 push Stack: 3
Token 2 : 4 push Stack: 3 4
Token 3 : 2 push Stack: 3 4 2
Token 4 : * operate Stack: 3 8
Token 5 : 1 push Stack: 3 8 1
Token 6 : 5 push Stack: 3 8 1 5
Token 7 : - operate Stack: 3 8 -4
Token 8 : 2 push Stack: 3 8 -4 2
Token 9 : 3 push Stack: 3 8 -4 2 3
Token 10 : ^ operate Stack: 3 8 -4 8
Token 11 : ^ operate Stack: 3 8 65536
Token 12 : / operate Stack: 3 .000122
Token 13 : + operate Stack: 3.000122
 
Result: 3.000122</pre>
 
=={{header|XPL0}}==
512

edits