Python to RPN converter
|
11-04-2018, 06:06 AM
Post: #41
|
|||
|
|||
RE: Python to RPN converter
(11-03-2018 07:32 PM)Namir Wrote: How can I write Python code that translates into: myvar = PROMPT("Prompt message") You cannot strictly speaking store into a specific register, because from the Python point of view registers don't exist. You just have to think in Python and stick to Python variables - and treat the generated RPN as "machine code" ;-) Thus: Code: myvar = PROMPT("length?") You can however store into a HP42S named variable - they are ok to specify. Check out the the "Prompting for input" section in the online examples help. |
|||
11-04-2018, 02:02 PM
Post: #42
|
|||
|
|||
RE: Python to RPN converter
Can't you access registers like REGS[index]?
You could also implement a special case for REGS, so that REGS[0] can be translated directly to RCL 00 or STO 00, avoiding the use of STOIJ and RCLEL/STOEL. |
|||
11-04-2018, 08:07 PM
Post: #43
|
|||
|
|||
RE: Python to RPN converter
In displaying the contents of the Alpha register (translated from a Python print statement), would it be better to generate PROMPT statements instead of AVIEW?
Namir |
|||
11-04-2018, 09:59 PM
Post: #44
|
|||
|
|||
RE: Python to RPN converter
(11-04-2018 08:07 PM)Namir Wrote: In displaying the contents of the Alpha register (translated from a Python print statement), would it be better to generate PROMPT statements instead of AVIEW? You are correct that print() is a synonym for AVIEW(). In Python print doesn't stop program execution so I wanted to keep that behaviour. If you want to stop program execution so that the user can take a good look at something, then simply use PROMPT(msg) and don't worry about assigning the return value of PROMPT to a variable. Keep in mind that in the converter, print/AVIEW, PROMPT, PRA and alpha all:
-Andy |
|||
11-04-2018, 10:25 PM
Post: #45
|
|||
|
|||
RE: Python to RPN converter
(11-04-2018 02:02 PM)Thomas Okken Wrote: Can't you access registers like REGS[index]? I suppose I could open up access to the registers in the way you suggest. Keep in mind however that by default, numbered registers are used to store Python variables, so registers are being pretty much being overwritten all the time and thus don't have much value. Specifically, all lowercase python variables are mapped to RPN registers in the order that the converter parser sees them. Thus a = 10 and b = 20 will be mapped to registers 00 and 01 respectively. The Python to RPN converter does offer ways of mapping Python variables to RPN named variables, by using uppercase Python variable names, or the special comment directive 'named' e.g. Code: y = 100 # rpn: named however the default behaviour is to use registers, to avoid polluting the HP42S/Free42 global namespace with dozens of variable names from your Python programs. In retrospect, perhaps the better implementation would have been to store all the Python variables in a dedicated HP42S matrix and grow the matrix as necessary to house new variables etc. This would have been more work to implement and would result in more verbose generated RPN. However now that the converter is built, it's an area that I may revisit one day. -Andy |
|||
12-11-2018, 07:05 PM
Post: #46
|
|||
|
|||
RE: Python to RPN converter
Has the converter moved? I'm getting 404 errors for all the links…
Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot/C47 Casio, Rockwell 18R |
|||
12-11-2018, 10:46 PM
Post: #47
|
|||
|
|||
RE: Python to RPN converter
(12-11-2018 07:05 PM)cdmackay Wrote: Has the converter moved? I'm getting 404 errors for all the links… Try http://pyrpn.atug.com/ The www. prefixed address was misconfigured due to a server change I did the other week, and should rectify itself shortly. Sorry for the scare! -Andy |
|||
12-11-2018, 11:27 PM
Post: #48
|
|||
|
|||
RE: Python to RPN converter
(12-11-2018 10:46 PM)tcab Wrote: Try http://pyrpn.atug.com/ phew! thanks Andy Cambridge, UK 41CL/DM41X 12/15C/16C DM15/16 17B/II/II+ 28S 42S/DM42 32SII 48GX 50g 35s WP34S PrimeG2 WP43S/pilot/C47 Casio, Rockwell 18R |
|||
05-22-2020, 05:53 PM
Post: #49
|
|||
|
|||
RE: Python to RPN converter
hello all,
as you can see I'm not a specialist ;-). I would like to know if there is a way to convert in the opposite way: RPN to python please? Thanks for help Pascal |
|||
06-09-2020, 04:12 PM
Post: #50
|
|||
|
|||
RE: Python to RPN converter | |||
06-12-2020, 03:05 PM
Post: #51
|
|||
|
|||
RE: Python to RPN converter
This is excellent. I love this.
Two problems I ran into: * the lack of ROUND equivalent for the HP 42S * how to handle the int command (set number as an integer) |
|||
06-12-2021, 03:42 AM
Post: #52
|
|||
|
|||
RE: Python to RPN converter
(06-12-2020 03:05 PM)Eddie W. Shore Wrote: This is excellent. I love this. > This is excellent. I love this. Thanks Eddie! - sorry for the late reply, somehow I just saw your post now. > * the lack of ROUND equivalent for the HP 42S HP42 has the built in command RND which you can invoke from Python e.g. you type RND(1.5) and convert it via https://pyrpn.atug.com which generates 1.5 RND but since the built in HP42S/Free42 RND works via the current display format, we seem to have to call FIX as well to pull this off. Thus a full example might be Python code: Code: LBL("untitled") which generates: Code: 01 LBL "untitle" > * how to handle the int command (set number as an integer) Perhaps type IP(myvar) into the Python to RPN converter - which generates the built in Free42 command IP. That's the closest command I can find. Here is a Python program to do both Code: def rndip(n): After conversion, and pasting into Free42, run with 1.5 XEQ RNDIP * Reminder: the Python to RPN converter is not related to Python in HP Prime CAS, nor the beta MicroPython App in HP Prime. It is a online website which converts Python into RPN which you can paste or type into Free42/HP42S/HP41. Help is https://pyrpn.atug.com/help |
|||
06-12-2021, 03:54 AM
Post: #53
|
|||
|
|||
RE: Python to RPN converter
A new release of the converter today:
Code: def sumsigma(_from, _to): Use: On Free42 type 1 Enter 10 XEQ SUMSIGMA and you'll get the answer 385. Its just a fun exercise - easily accomplished on the HP Prime with the one liner e.g. Σ(X^2,X,1,10) |
|||
09-15-2023, 11:09 AM
Post: #54
|
|||
|
|||
RE: Python to RPN converter
The code behind this project is now open source and available at https://github.com/abulka/pyrpn
You can run it locally on your own machine via docker e.g. Code: git clone git@github.com:abulka/pyrpn.git Plenty of information in the README. Enjoy! |
|||
09-29-2023, 08:26 PM
Post: #55
|
|||
|
|||
RE: Python to RPN converter
Thank you for opening it up!
|
|||
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): 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 } It is based on the disassembly of the d5 function: Code: 1 0 RESUME 0 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. 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} \) |
|||
10-18-2024, 12:14 PM
Post: #57
|
|||
|
|||
Bug Report: Python to RPN converter
The following function is not translated properly:
Code: def f(a, b): This is the generated code for the HP-42S: Code: LBL "f" However CHS should be executed after the addition of a and b. This calculates -a + b instead. Compare it with the disassembly of the function f: Code: 1 0 RESUME 0 This is the abstract syntax tree of the expression -(a + b): Code: Module( It looks good to me. Therefore, I assume that it is a problem in the method visit_UnaryOp of RecursiveRpnVisitor: Code: def visit_UnaryOp(self, node): |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 4 Guest(s)