Python to FOCAL Compiler
|
10-05-2015, 10:58 PM
Post: #1
|
|||
|
|||
Python to FOCAL Compiler
Python Byte-Code
Ever since I noticed the similarity of the generated byte-code of a Python-program with a FOCAL program I wondered if this could be used to create programs for the HP-41C. Let's start with an example that calculates the area of a circle with radius r: Code: def circle(r): This will compile to byte-code which then can be disassembled: Code: 2 0 LOAD_GLOBAL 0 (pi) The goal is to generate the following FOCAL-program: Code: LBL "CIRCLE" Of course this can't work in general due to the limitations of the HP-41C. But nonetheless the results so far are promising: Celsius to Fahrenheit conversion Code: def fahrenheit(celsius): Code: LBL "FAHRENH" But can we deal with complex expressions involving mathematical functions? Spherical Law of Cosines Code: def spherical_law_of_cosines(a, C, b): Code: LBL "SPHERIC" Mach Number Sometimes we have to rearrange the expression a little to avoid stack-overflow as with the famous formula for the mach number: Code: def mach(): Code: LBL "MACH" You may notice that the result isn't exactly how it is solved in the HP-67 manual as the expression 6.875E-6*25500 is simplified to 0.1753125. Or then we can avoid stack-overflow by using local variables. Quadratic Equation Code: def qe(a, b, c): Code: LBL "QE" In some cases the RDN command after each STO command could be removed but not in all. Thus the generated code isn't optimized but that can easily be done manually to shave off a byte here and there. Fizz Buzz The famous simple coding interview question: Code: def fizbuz(n): Code: LBL "FIZBUZ" A print statement is currently just mapped to the AVIEW command. But the last one should be VIEW X instead. I didn't come up with a simple solution for this but think this can easily be fixed manually. You may notice that all branches go to LBL 03 as a common exit-point. You can of course just use RTN instead. Greatest Common Divisor What about loops, you may wonder. Code: def gcd(a, b): Code: LBL "GCD" While the code is correct we can certainly remove LBL 02 as it isn't used. And then there's no need to swap a and b before storing them. Of course we're far away from the optimized solution below but it might be a good starting point. Code: LBL "GCD" Conditionals How would you translate the following conditional? Code: def conditional(n): Isn't it nice that you can let the compiler do the hard work? Code: LBL "CONDITI" Nested loop and break This primitive program lists all prime factors of a given number: Code: def factor(n): Here again we have to replace AVIEW by VIEW X: Code: LBL "FACTOR" Mutual Inductunce of Coil Pair This example stems from a recent thread: Code: def mutind(r, R, x): We assume that we can use functions to calculate the complete elliptic integrals. For this we have to add a customized mapping: Code: function = { The generated code has some similarities to the listing for the HP-67 in APPENDIX A: Code: LBL "MUTIND" Conclusions There are some limitations:
It was fun to tinker a little with Python byte-code. I hope the result may be inspiring. Kind regards Thomas Code: Archive: python_to_focal_compiler.zip To translate the examples in examples.py just run: python compiler.py The result is listed in examples.hp. |
|||
« Next Oldest | Next Newest »
|
Messages In This Thread |
Python to FOCAL Compiler - Thomas Klemm - 10-05-2015 10:58 PM
RE: Python to FOCAL Compiler - Sukiari - 10-06-2015, 03:36 AM
RE: Python to FOCAL Compiler - Massimo Gnerucci - 10-06-2015, 05:50 AM
RE: Python to FOCAL Compiler - Ángel Martin - 10-06-2015, 11:16 AM
RE: Python to FOCAL Compiler - Gerson W. Barbosa - 10-06-2015, 03:58 PM
RE: Python to FOCAL Compiler - Thomas Klemm - 10-06-2015, 07:11 PM
RE: Python to FOCAL Compiler - Gerson W. Barbosa - 10-06-2015, 08:19 PM
RE: Python to FOCAL Compiler - Thomas Klemm - 10-06-2015, 07:55 PM
RE: Python to FOCAL Compiler - Thomas Klemm - 10-06-2015, 08:28 PM
RE: Python to FOCAL Compiler - Gerson W. Barbosa - 10-07-2015, 01:47 AM
RE: Python to FOCAL Compiler - Thomas Klemm - 10-07-2015, 04:34 AM
RE: Python to FOCAL Compiler - Massimo Gnerucci - 10-07-2015, 05:38 AM
Java to FOCAL Compiler - Thomas Klemm - 10-07-2015, 04:38 PM
|
User(s) browsing this thread: 4 Guest(s)