RosettaCodeData/Task/Parsing-RPN-calculator-algorithm/N-t-roff/parsing-rpn-calculator-algorithm-3.n
2018-06-22 20:57:24 +00:00

89 lines
1.3 KiB
Text

.ig
===========================
Array implementation
===========================
..
.de end
..
.de array
. nr \\$1.c 0 1
. de \\$1.push end
. nr \\$1..\\\\n+[\\$1.c] \\\\$1
. end
. de \\$1.pop end
. if \\\\n[\\$1.c]>0 \{ \
. rr \\$1..\\\\n[\\$1.c]
. nr \\$1.c -1\
. \}
. end
. de \\$1.dump end
. nr i 0 1
. rm ou
. while \\\\n+i<=\\\\n[\\$1.c] \{ \
. as ou "\\\\n[\\$1..\\\\ni]
. \}
. tm \\\\*(ou
. rr i
. end
..
.ig
==========================
End array implementation
==========================
..
.array stack
.de hyper3
. nr rs 1
. nr i 0 1
. while \\n+i<=\\$2 .nr rs \\n(rs*\\$1
. rr i
..
.de pop2
. nr O2 \\n[\\$1..\\n[\\$1.c]]
. \\$1.pop
. nr O1 \\n[\\$1..\\n[\\$1.c]]
. \\$1.pop
..
.de rpn
. ie '\\$1'+' \{ \
. pop2 stack
. nr rs \\n(O1+\\n(O2
. \}
. el \{ \
. ie '\\$1'-' \{ \
. pop2 stack
. nr rs \\n(O1-\\n(O2
. \}
. el \{ \
. ie '\\$1'*' \{ \
. pop2 stack
. nr rs \\n(O1*\\n(O2
. \}
. el \{ \
. ie '\\$1'/' \{ \
. pop2 stack
. nr rs \\n(O1/\\n(O2
. \}
. el \{ \
. ie '\\$1'%' \{ \
. pop2 stack
. nr rs \\n(O1%\\n(O2
. \}
. el \{ \
. ie '\\$1'^' \{ \
. pop2 stack
. hyper3 \\n(O1 \\n(O2
. \}
. el .nr rs \\$1
. \}\}\}\}\}
.
. stack.push \\n(rs
. stack.dump
.
. if \\n(.$>1 \{ \
. shift
. rpn \\$@
. \}
..
.rpn 3 4 2 * 1 5 - 2 3 ^ ^ / +
.stack.dump