Post Reply 
Python to RPN converter
06-23-2018, 10:33 PM
Post: #33
RE: Python to RPN converter
Some time ago I wrote this Python to FOCAL Compiler.

This Python program calculates the greatest common divisor of two integers a and b:
Code:
def gcd(a, b):
    while b != 0:
        a, b = b, a % b
    return a

It gets translated to:
Code:
LBL "GCD"
STO 01 ; b
RDN
STO 00 ; a
RDN
LBL 00
RCL 01 ; b
0
X=Y?
GTO 01
RCL 01 ; b
RCL 00 ; a
RCL 01 ; b
MOD
X<>Y
STO 00 ; a
RDN
STO 01 ; b
RDN
GTO 00
LBL 01
LBL 02
RCL 00 ; a
RTN

The generated code by your website is similar but a bit longer:
Code:
LBL "gcd"
XEQ 65          // reorder 2 params for storage
STO 00          // param: a
RDN
STO 01          // param: b
RDN
LBL 00          // while
RCL 01          // b
0
XEQ 86          // compare, return bool
X≠0?            // while true?
GTO 01          // while body
GTO 02          // resume
LBL 01          // while body
RCL 01          // b
RCL 00          // a
RCL 01          // b
MOD
STO 01          // b 
RDN
STO 00          // a 
GTO 00          // while
LBL 02          // resume
RCL 00          // a
RTN             // return
LBL 50          // PyRPN Support Library of
"-Utility Funcs-"
RTN             // ---------------------------
LBL 65          // Reverse params. (a,b) -> (b,a)
X<>Y
RTN
LBL 86          // != (y:a, x:b) -> boolean of a != b
CF 00
X≠Y?
SF 00
XEQ 61
RTN
LBL 61          // Util used by comparison ops. (a,b) -> (boolean) - whether flag 00 is set, plus RDNs
RDN
RDN             // params dropped
FS? 00
1
FC? 00
0
RTN

My program relies on the generated byte-code that has some resemblance to FOCAL since Python uses a stack-machine to interpret it. So it was never more than a quick and dirty hacked together proof of concept. Nothing compared to your listed capabilities. Still I was pleased with the result.

It appears that both implementations don't support recursion as in this case:
Code:
def gcd(a, b):
    return a if b == 0 else gcd(b, a % b)

Just out of curiosity: Does your program rely on the Python compiler as well or did you write one on your own?
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: 2 Guest(s)