Post Reply 
Python to RPN converter
02-16-2024, 02:35 PM (This post was last modified: 02-16-2024 03:16 PM by Thomas Klemm.)
Post: #56
RE: Python to RPN converter
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}
\)
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Python to RPN converter - tcab - 02-16-2018, 05:27 AM
RE: Python to RPN converter - pier4r - 02-16-2018, 06:26 AM
RE: Python to RPN converter - salvomic - 02-16-2018, 09:47 AM
RE: Python to RPN converter - rprosperi - 02-16-2018, 02:34 PM
RE: Python to RPN converter - charger73 - 02-16-2018, 02:38 PM
RE: Python to RPN converter - Nigel (UK) - 02-16-2018, 03:04 PM
RE: Python to RPN converter - tcab - 02-16-2018, 11:06 PM
RE: Python to RPN converter - compsystems - 02-17-2018, 01:06 AM
RE: Python to RPN converter - StephenG1CMZ - 02-18-2018, 09:21 AM
RE: Python to RPN converter - tcab - 02-18-2018, 10:27 AM
RE: Python to RPN converter - StephenG1CMZ - 02-18-2018, 02:32 PM
RE: Python to RPN converter - tcab - 02-18-2018, 09:25 PM
RE: Python to RPN converter - Thomas Okken - 02-18-2018, 12:59 PM
RE: Python to RPN converter - tcab - 02-18-2018, 01:13 PM
RE: Python to RPN converter - StephenG1CMZ - 02-19-2018, 07:16 AM
RE: Python to RPN converter - tcab - 02-19-2018, 07:29 AM
RE: Python to RPN converter - StephenG1CMZ - 02-19-2018, 07:48 AM
RE: Python to RPN converter - tcab - 02-19-2018, 08:17 AM
RE: Python to RPN converter - pier4r - 02-19-2018, 11:15 AM
RE: Python to RPN converter - StephenG1CMZ - 02-19-2018, 07:36 PM
RE: Python to RPN converter - StephenG1CMZ - 02-20-2018, 02:28 PM
RE: Python to RPN converter - tcab - 02-20-2018, 10:39 PM
RE: Python to RPN converter - MikeOShea - 06-01-2018, 01:39 AM
RE: Python to RPN converter - Thomas Klemm - 06-23-2018, 10:33 PM
RE: Python to RPN converter - tcab - 06-24-2018, 10:56 PM
RE: Python to RPN converter - Thomas Klemm - 06-25-2018, 05:00 PM
RE: Python to RPN converter - Thomas Klemm - 06-28-2018, 04:07 PM
RE: Python to RPN converter - Thomas Klemm - 06-28-2018, 08:04 PM
RE: Python to RPN converter - tcab - 11-02-2018, 04:43 AM
RE: Python to RPN converter - Namir - 11-03-2018, 07:32 PM
RE: Python to RPN converter - tcab - 11-04-2018, 06:06 AM
RE: Python to RPN converter - Thomas Okken - 11-04-2018, 02:02 PM
RE: Python to RPN converter - tcab - 11-04-2018, 10:25 PM
RE: Python to RPN converter - Namir - 11-04-2018, 08:07 PM
RE: Python to RPN converter - tcab - 11-04-2018, 09:59 PM
RE: Python to RPN converter - cdmackay - 12-11-2018, 07:05 PM
RE: Python to RPN converter - tcab - 12-11-2018, 10:46 PM
RE: Python to RPN converter - cdmackay - 12-11-2018, 11:27 PM
RE: Python to RPN converter - compsystems - 06-09-2020, 04:12 PM
RE: Python to RPN converter - tcab - 06-12-2021, 03:42 AM
RE: Python to RPN converter - tcab - 06-12-2021, 03:54 AM
RE: Python to RPN converter - tcab - 09-15-2023, 11:09 AM
RE: Python to RPN converter - dm319 - 09-29-2023, 08:26 PM
RE: Python to RPN converter - Thomas Klemm - 02-16-2024 02:35 PM



User(s) browsing this thread: 5 Guest(s)