The following
Python program leads to an error:
Code:
def d5(a, b):
if a < 5:
if b < 5:
return (a + b) % 5
else:
return (a + b) % 5 + 5
else:
if b < 5:
return (a - b) % 5 + 5
else:
return (a - b) % 5
Potential RPN stack overflow detected - expression too complex for 4 level stack - simplify! ['b', 5, '_result_', 'a', 'b'], line: 11
return (a - b) % 5
This is a possible translation for the
HP-42S:
Code:
00 { 63-Byte Prgm }
01▸LBL "D5"
02 STO 01
03 R↓
04 STO 00
05 RCL 00
06 5
07 X≤Y?
08 GTO 01
09 RCL 01
10 5
11 X≤Y?
12 GTO 00
13 RCL 00
14 RCL 01
15 +
16 5
17 MOD
18 RTN
19▸LBL 00
20 RCL 00
21 RCL 01
22 +
23 5
24 MOD
25 5
26 +
27 RTN
28▸LBL 01
29 RCL 01
30 5
31 X≤Y?
32 GTO 02
33 RCL 00
34 RCL 01
35 -
36 5
37 MOD
38 5
39 +
40 RTN
41▸LBL 02
42 RCL 00
43 RCL 01
44 -
45 5
46 MOD
47 END
It is based on the disassembly of the
d5 function:
Code:
1 0 RESUME 0
2 2 LOAD_FAST 0 (a)
4 LOAD_CONST 1 (5)
6 COMPARE_OP 2 (<)
10 POP_JUMP_IF_FALSE 24 (to 60)
3 12 LOAD_FAST 1 (b)
14 LOAD_CONST 1 (5)
16 COMPARE_OP 2 (<)
20 POP_JUMP_IF_FALSE 8 (to 38)
4 22 LOAD_FAST 0 (a)
24 LOAD_FAST 1 (b)
26 BINARY_OP 0 (+)
30 LOAD_CONST 1 (5)
32 BINARY_OP 6 (%)
36 RETURN_VALUE
6 >> 38 LOAD_FAST 0 (a)
40 LOAD_FAST 1 (b)
42 BINARY_OP 0 (+)
46 LOAD_CONST 1 (5)
48 BINARY_OP 6 (%)
52 LOAD_CONST 1 (5)
54 BINARY_OP 0 (+)
58 RETURN_VALUE
8 >> 60 LOAD_FAST 1 (b)
62 LOAD_CONST 1 (5)
64 COMPARE_OP 2 (<)
68 POP_JUMP_IF_FALSE 11 (to 92)
9 70 LOAD_FAST 0 (a)
72 LOAD_FAST 1 (b)
74 BINARY_OP 10 (-)
78 LOAD_CONST 1 (5)
80 BINARY_OP 6 (%)
84 LOAD_CONST 1 (5)
86 BINARY_OP 0 (+)
90 RETURN_VALUE
11 >> 92 LOAD_FAST 0 (a)
94 LOAD_FAST 1 (b)
96 BINARY_OP 10 (-)
100 LOAD_CONST 1 (5)
102 BINARY_OP 6 (%)
106 RETURN_VALUE
Therefore I don't think a stack overflow can happen.
Suggestion: I can imagine that it's not trivial to detect a potential stack overflow.
But in such cases I would rather just get a warning and still get the generated code.
Addendum:
This is the generated code when the exception is not thrown:
Code:
Generated 84 lines.
01 LBL "d5"
02 XEQ 65
03 STO 00
04 RDN
05 STO 01
06 RDN
07 RCL 00
08 5
09 XEQ 79
10 X≠0?
11 GTO 00
12 GTO 03
13 LBL 00
14 RCL 01
15 5
16 XEQ 79
17 X≠0?
18 GTO 04
19 GTO 06
20 LBL 04
21 RCL 00
22 RCL 01
23 +
24 5
25 MOD
26 RTN
27 GTO 05
28 LBL 06
29 RCL 00
30 RCL 01
31 +
32 5
33 MOD
34 5
35 +
36 RTN
37 LBL 05
38 GTO 01
39 LBL 03
40 RCL 01
41 5
42 XEQ 79
43 X≠0?
44 GTO 07
45 GTO 02
46 LBL 07
47 RCL 00
48 RCL 01
49 -
50 5
51 MOD
52 5
53 +
54 RTN
55 GTO 01
56 LBL 02
57 RCL 00
58 RCL 01
59 -
60 5
61 MOD
62 RTN
63 LBL 01
64 RTN
65 LBL 50
66 "-Utility Funcs-"
67 RTN
68 LBL 65
69 X<>Y
70 RTN
71 LBL 79
72 CF 00
73 X>Y?
74 SF 00
75 XEQ 61
76 RTN
77 LBL 61
78 RDN
79 RDN
80 FS? 00
81 1
82 FC? 00
83 0
84 RTN
For those interested, this function implements the
Dihedral Group \(D_5\):
\(
\begin{bmatrix}
0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9\\
1 & 2 & 3 & 4 & 0 & 6 & 7 & 8 & 9 & 5\\
2 & 3 & 4 & 0 & 1 & 7 & 8 & 9 & 5 & 6\\
3 & 4 & 0 & 1 & 2 & 8 & 9 & 5 & 6 & 7\\
4 & 0 & 1 & 2 & 3 & 9 & 5 & 6 & 7 & 8\\
5 & 9 & 8 & 7 & 6 & 0 & 4 & 3 & 2 & 1\\
6 & 5 & 9 & 8 & 7 & 1 & 0 & 4 & 3 & 2\\
7 & 6 & 5 & 9 & 8 & 2 & 1 & 0 & 4 & 3\\
8 & 7 & 6 & 5 & 9 & 3 & 2 & 1 & 0 & 4\\
9 & 8 & 7 & 6 & 5 & 4 & 3 & 2 & 1 & 0
\end{bmatrix}
\)