Python to RPN converter - Printable Version +- HP Forums (https://www.hpmuseum.org/forum) +-- Forum: Not HP Calculators (/forum-7.html) +--- Forum: Not quite HP Calculators - but related (/forum-8.html) +--- Thread: Python to RPN converter (/thread-10178.html) |
RE: Python to RPN converter - sa-penguin - 02-19-2018 02:29 AM (02-17-2018 02:33 PM)rprosperi Wrote: At least half the members here prefer RPN over RPL. Thanks for the memories. My first programming "experience" was RPN - and I learned from the books that came with the HP-41. loved it. Eventually, I bought a 48. Hated RPL. To this day, I still don't know whether to blame RPL itself, or the documentation that came with the HP-48. RE: Python to RPN converter - polbit - 02-19-2018 02:54 AM (02-19-2018 02:29 AM)sa-penguin Wrote:(02-17-2018 02:33 PM)rprosperi Wrote: At least half the members here prefer RPN over RPL. I have figured out the perfect response when somebody asks me if I prefer RPN or RPL HPs: "How about that sweet 71b! What a BASIC!" RE: Python to RPN converter - sa-penguin - 02-19-2018 06:27 AM (02-19-2018 02:54 AM)polbit Wrote: "How about that sweet 71b! What a BASIC!" Not blasphemy - must be heresy. RE: Python to RPN converter - StephenG1CMZ - 02-19-2018 07:16 AM Tcab, I am puzzled when you ask for source code without the line numbers "that I added". Those line numbers are actually part of your original demo, and were not deleted after pasting here RE: Python to RPN converter - tcab - 02-19-2018 07:29 AM Ah I see what might have happened with your copy paste fragment getting those strange line numbers... Python of course has no line numbers, but there are indeed line numbers in the margin of the Python editor (like in most programming editors) but they are not part of the source code and usually impossible to copy out - at least in desktop browsers and iOS browsers. Somehow you managed to select them and copy them! It might better to use a desktop browser to become familiar with the site - I haven't tested the site under android yet sorry. RE: Python to RPN converter - StephenG1CMZ - 02-19-2018 07:48 AM Its only on this forum the line numbers combine with the source - Convert didn't complain until line 7 of my demo2 so I guess wasn't seeing the line numbers. Right now, I only have Android available - the PC needs fixing. Thanks for your help. RE: Python to RPN converter - tcab - 02-19-2018 08:17 AM No worries. I've gone ahead and implemented a Clear button and a New Project link - as well as removing the initial demo code from the converter. Other features released today include a new LBL main start point for programs, allowing access to the Python global scope area - great for sharing variables between functions. Programs written with this paradigm are likely to be more appealing to RPN programmers and look like: Code:
Variable references now look outwards from functions into their outer functions (if any)... all the way to global scope - until they find the variable. This is how nested scope works in most programming languages, including Python. Read all about scope in the help file. Other features released today are the ability to edit and delete cloned example snippets, as well as vote on them. A new simplified menu() command has been added - there are a couple of examples showing how you can rapidly build custom menus and submenus. RE: Python to RPN converter - pier4r - 02-19-2018 11:15 AM Would be interesting to see how "optimized" is the code generated by the converter (that is not a primary objective here, it is already great to get a conversion). For example with a summation: http://www.hpmuseum.org/forum/thread-9750.html Or some array work: http://www.hpmuseum.org/cgi-sys/cgiwrap/hpmuseum/articles.cgi?read=700 From the last one the code for the dm42 is Code:
RE: Python to RPN converter - StephenG1CMZ - 02-19-2018 07:36 PM Thanks Tcab, that new UI is now usable on Android...much better. RE: Python to RPN converter - StephenG1CMZ - 02-20-2018 02:28 PM I have a couple of queries, which may be obvious to anyone that has used HP42 before. First, with any code formatted like this, I am finding that the converted code starts with a label followed by a RTN. I seem to have to delete the RTN for the program to execute. I presume I am doing something wrong. I tried moving the LBL but so far without success. Second, if I include LOCAT() in the source, I get a Label Not Found error executing the program. I guess that means free42's LOCAT is not available to Python. I am sure there must be some way of using it, but being new to the 42 I haven't yet figured it out. I tried creating LOC8 which called LOCAT, but haven't figured out the 42 usage yet. Code:
RE: Python to RPN converter - tcab - 02-20-2018 10:39 PM The reason your program is not running when you call "H1" is that it is not calling the "Here" function. You have to call Python functions to run them. So your code should be: Code:
Another way to write this is to get rid of the LBL and simply Code:
Or you can even run your code in the global namespace like this: Code:
As for your calls to LOCAT and LOC8 - they are not defined anywhere and don't exist on the HP42S (see cmd reference) and so are being converted to local label calls starting with single letters e.g. XEQ B - which won't exist if you try to run the code. Finally - a small point - you don't need the AVIEW after the print() because print() already generates an AVIEW for you. See the section on text and the alpha register in the help page. RE: Python to RPN converter - MikeOShea - 06-01-2018 01:39 AM Very nice work! RE: Python to RPN converter - Thomas Klemm - 06-23-2018 10:33 PM 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): It gets translated to: Code: LBL "GCD" The generated code by your website is similar but a bit longer: Code: LBL "gcd" 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): Just out of curiosity: Does your program rely on the Python compiler as well or did you write one on your own? RE: Python to RPN converter - tcab - 06-24-2018 10:56 PM Hi Thomas, How interesting that you built something similar! You program scans Python generated Bytecode whilst my program works earlier in the pipeline, at the AST stage: So yes, my program does rely on the Python compiler. Specifically my approach has been to use Python's built in ability to parse itself into an AST Abstract Syntax Tree, then to traverse this tree using the visitor design pattern to generate the RPN. For example, the following Python code: Code: import ast Code: Module(body=[Assign( If you are interested, here are the slides of a recent talk I gave to a local Python User Group in Melbourne, Australia, about how I used the Python AST capabilities to build the Python to Rpn converter. The reception to the talk was good, but became fun and buoyant once the audience realised I had targeted an old HP calculator with this fancy Python technology - especially when I pulled out my HP calcs for all to see and touch. -Andy Bulka RE: Python to RPN converter - Vincent Weber - 06-25-2018 07:13 AM Hi all, Reading this thread reactivated an idea I had but never had the courage to implement: build the algebraic equation parser and solver from the HP-27S into the 42S. Since Thomas won't build this kind of far-off feature in free42, the only way to do it... is FOCAL. While in theory FOCAL is Turing-complete and should be able to implement anything, especially with the virtually unlimited speed and memory of Free42, in practice this is pretty heavy stuff. I thought of using matrices as nodes to build trees, using their variable names as pointers, but you need a strong stomach... Then I saw this Python to RPN thing and thought "wow ! In Python this is almost trivial to do !". Then I read the converter documentation and my enthousiasm scaled down... It seems that the converter still relied on the 4-level stack to evaluate expressions, without using registers to store temporary computations, and that therefore too complex algebraic computations are doomed to failure. And since I want to do recursive tree evaluation... I don't want to start this if this has no chance of success. What do you think ? Thanks in advance and cheers, Vincent RE: Python to RPN converter - Thomas Klemm - 06-25-2018 05:00 PM (06-24-2018 10:56 PM)tcab Wrote: If you are interested, here are the slides of a recent talk I gave to a local Python User Group in Melbourne, Australia, about how I used the Python AST capabilities to build the Python to Rpn converter. Thanks a lot for your answer and sharing the slides. I guess I'll have to dive a bit deeper into the innards of Python. I published the program here and to my astonishment it's still present. Cheers Thomas RE: Python to RPN converter - Thomas Klemm - 06-28-2018 04:07 PM Just watched the talk Hy: A Lisp that transforms itself into the Python AST that you mentioned in the slides. This made me wonder if I could translate hy to FOCAL as well? Let's give it a try with a function that calculates the area of circle given the radius r: Code: (defn circle [r] Code: hy 0.14.0+196.g4de18d3 using CPython(default) 2.7.10 on Darwin How cool is that? Since hy produces an AST I assume it would be easy to provide a frontend for your application. For those of you who want to try hy. [attachment=6050] RE: Python to RPN converter - Thomas Klemm - 06-28-2018 08:04 PM While the syntax of hy is similar to Clojure we can still use a while-loop and mutate local variables. This makes the translation from Python straight forward. We can even break out of a loop. I've translated a few of the other examples: Celsius to Fahrenheit Code: (defn fahrenheit [celsius] Quadratic Equation Code: (defn qe [a b c] Greatest Common Divisor Code: (defn gcd [a b] Factors of a Number Code: (defn factor [n] Circumference of an Ellipse Code: (defn ellipse [a b] It appears that print is handled as a function in hy. Thus I had to extend the compiler a bit. Otherwise translating the factor function would lead to errors. It is mapped to AVIEW: Code: function = { RE: Python to RPN converter - tcab - 11-02-2018 04:43 AM The website for the Python to RPN converter has moved to a different url and faster server. Apologies for any inconvenience. -Andy Python to RPN converter: for HP42S/Free42/DM42 http://www.pyrpn.atug.com Print42: Echo Free42 virtual tape to Epson thermal printer http://www.print42.atug.com RE: Python to RPN converter - Namir - 11-03-2018 07:32 PM How can I write Python code that translates into: "Prompt message" PROMPT STO <reg> |