Post Reply 
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:

"Prompt message"
PROMPT
STO <reg>

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?")
print("sine of", myvar, "is", SIN(myvar))

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.
Find all posts by this user
Quote this message in a reply
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.
Find all posts by this user
Quote this message in a reply
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
Find all posts by this user
Quote this message in a reply
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?

Namir

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:
  • take arbitrarily long strings
  • accept multiple parameters e.g. alpha("Ans: n=", n, "result is", myvar)
  • support the Python keyword parameters append= and sep=
For more information see the help page section entitled "Text and the Alpha register".

-Andy
Find all posts by this user
Quote this message in a reply
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]?
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.

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
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)