There's
a very interesting thread over at HP Museum, where Thomas Klemm has noted that Python is a convenient high-level language which compiles to bytecode and which includes introspection - so you can automatically convert from python, via bytecode, to a machine language of choice. Python can disassemble its own bytecode so the transformations can be text-based.
The whole thing works well if the machine language of choice has a structural resemblance to python bytecode - in this case it's Focal, which runs on the HP-41C calculator. Perhaps one could convert to a virtual machine like Forth or Sweet 16, or perhaps to assembly language, perhaps in a threaded style.
Thomas put up
an example you can run in your browser. His 300 line program converts
Code:
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
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