Python to RPN converter

Discussion around the Swiss Micros DM42 calculator.
wawachief
Posts: 30
Joined: Tue Dec 12, 2017 6:39 pm
Location: France, Normandie

Re: Python to RPN converter

Post by wawachief » Tue Feb 20, 2018 1:55 pm

I am trying another program, this time using lists :

Code: Select all

LBL("ampoule")
ampoules=[]
nb=276
somme=0

for i in range(nb):
    ampoules.append(0)
    
for e in range(1,26):
    i=e-1 
    while i<nb:
        ampoules[i]=1-ampoules[i]
        i+=e 

for i in range(nb):
  somme += ampoules[i]
    
print(somme)
This time, the program is converted into RPN but I have a Dimension Error during execution. This program works fine in Python.
DM42 SN:00218
HP-11c - HP-19b - HP25 - HP 45 - HP42s - HP48gx

toml_12953
Posts: 447
Joined: Wed May 03, 2017 5:46 pm
Location: Malone, NY USA

Re: Python to RPN converter

Post by toml_12953 » Tue Feb 20, 2018 2:30 pm

wawachief wrote:
Tue Feb 20, 2018 1:36 pm
This is an incredible work ! thank you.

I try to convert a simple program to find the sum of all divisors of a given integer, just for testing :

Code: Select all

LBL("test")
print(sommediv(2016))

def sommediv(n):
  res = 0
  for d in range(2, n):
    if n%d == 0:
      res += d
  return res
But unfortunatly, I get an error :
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Am I doing something wrong ?
If you're doing something wrong, then so am I. I get the same error.
Tom L

Ducator meus nihil agit sine lagunculae leynidae accedunt

DM42 SN: 00025 (Beta)
SN: 00221 (Shipping)

tcab
Posts: 13
Joined: Sun Jan 14, 2018 11:03 am

Re: Python to RPN converter

Post by tcab » Tue Feb 20, 2018 11:32 pm

I try to convert a simple program to find the sum of all divisors of a given integer, just for testing
The Python % operator (modulo) was not being converted to RPN’s MOD - this has now been fixed. Please try it again!

Note that at runtime you will get an error about for loops being limited to a thousand, so sommediv(2016) will hit this limit but sommediv(1000) will be OK and give the answer 1339. This is because under the hood, for range loops are implemented with RPN's ISG command, which seems to have a range limit of 1000 due to the ISG ccccccc.fffii format of fff.  If you want to loop more than 1000 times then using a while loop instead of a for loop, which has no such limitations. e.g.

Code: Select all

LBL("test")
print(sommediv(2016))

def sommediv(n):
  res = 0
  d = 2
  while d < n:
    if n%d == 0:
      res += d
    d += 1
  return res

tcab
Posts: 13
Joined: Sun Jan 14, 2018 11:03 am

Re: Python to RPN converter

Post by tcab » Tue Feb 20, 2018 11:43 pm

I am trying another program, this time using lists...LBL("ampoule")
Converts and runs fine for me on Free42, giving the answer 154. Can you please tell me which line number of the RPN it is failing on? Thanks.

wawachief
Posts: 30
Joined: Tue Dec 12, 2017 6:39 pm
Location: France, Normandie

Re: Python to RPN converter

Post by wawachief » Wed Feb 21, 2018 1:12 pm

Everything works fine now. I tried the "ampoule" program again and it works on free42 and on the dm42. I don't know what I did wrong yesterday :(
The sommediv program works fine too, the pb is fixed. Thank you.

By the way, maybe it would be useful to open a new subforum to post Python programs ? something like "Python Software Library" ? I think this Python2RPN converter is really useful to design quickly lots of new programs.
DM42 SN:00218
HP-11c - HP-19b - HP25 - HP 45 - HP42s - HP48gx

tcab
Posts: 13
Joined: Sun Jan 14, 2018 11:03 am

Re: Python to RPN converter

Post by tcab » Wed Feb 21, 2018 10:30 pm

new subforum to post Python programs
Sounds good - not sure how to set that up - have PM'd you to discuss.

Alternatively, people can create examples on the Python to RPN website itself, and when they are voted for (there is a link under each user created example to vote), they can become part of the permanent collection of examples. Feel free to add examples and vote on your own examples!

User avatar
Vitasam
Posts: 215
Joined: Thu Jun 01, 2017 9:51 am
Location: Finland
Contact:

Re: Python to RPN converter

Post by Vitasam » Mon Sep 03, 2018 5:58 am

By the way, maybe it would be useful to open a new subforum to post Python programs ? something like "Python Software Library" ? I think this Python2RPN converter is really useful to design quickly lots of new programs.
Any progress about it? Now, after Numworks and the announcement of Casio Graph 90+E (or fxCG50 outside of France) to have Python updates it will be a good idea.

P.S. Both - Numworks and fxCG50 do have Micropython 1.9.x inside. Just wonder how difficult is to port Micropython to DM42 now, when DM42 SDK is available.
Elektronika MK-61, Elektronika MK-52, HP15c LE, DM42 SN#16 FW 3.11

keithdalby
Posts: 498
Joined: Mon Apr 24, 2017 6:38 pm

Re: Python to RPN converter

Post by keithdalby » Mon Sep 03, 2018 7:52 pm

More importantly, is there a decent implementation of HP-like RPN for the numworks machine yet?

User avatar
Vitasam
Posts: 215
Joined: Thu Jun 01, 2017 9:51 am
Location: Finland
Contact:

Re: Python to RPN converter

Post by Vitasam » Mon Sep 03, 2018 8:08 pm

Seems that someone did an experiment:
https://github.com/boricj/numworks-rpn
Elektronika MK-61, Elektronika MK-52, HP15c LE, DM42 SN#16 FW 3.11

sambutoki
Posts: 3
Joined: Tue Sep 12, 2017 8:16 pm

Re: Python to RPN converter

Post by sambutoki » Sat Dec 08, 2018 10:50 pm

tcab wrote:
Fri Feb 16, 2018 5:30 am
I am pleased to announce the Python to HP42S RPN converter website is online.
http://www.pyrpn.atug.com

Image

You write code in a high level structured language (which happens to be Python 3 syntax), hit a button and RPN is generated.

Image

You then paste the RPN into Free42 or transfer it to your DM42 (by creating a raw) - and it runs.
  1. Examples: http://www.pyrpn.atug.com/examples
  2. User Guide: http://www.pyrpn.atug.com/help
  3. Canvas for 42S Simulator: http://www.pyrpn.atug.com/canvas
  4. List of HP42S Commands Supported Reference: http://www.pyrpn.atug.com/cmds
The converter supports core Python syntax (which is very powerful), but does not implement the built in Python libraries that you would get in desktop Python. You have to rely on the ability to call HP42S commands from Python to do your work - which of course you can do. Specifically, it has the following capabilities:
  1. Variables
  2. Functions, Multiple functions, nested functions
  3. Parameter passing, receiving return values, multiple return values
  4. if elif else
  5. Comparison operators == != > < >= <=
  6. Booleans True, False and operators not or and
  7. for loops, range(), for..in iteration through lists and dictionary keys
  8. while loops, while...else
  9. continue and break operations in for loops and while loops
  10. Lists and Dictionaries (basic operations only).
  11. Matrices, Pythonic matrix element access syntax [row,col]
  12. NumPy compatible slicing syntax for sub-matrices
  13. Complex numbers using either 42S or Python native syntax
  14. Expressions involving nested brackets
  15. assert
  16. Testing and clearing of flags
  17. Access most HP42S commands as function calls e.g. FIX(2)
  18. Some enhanced functions to make life easier e.g. varmenu() automates and simplifies the generation of MVAR based code.
Included in the examples are some graphic primitive routines originally written for the Raspberry Pi in C rewritten into Python and converted to RPN. This now gives the 42S a small graphics library to generate lines, circles, rectangles, filled shapes etc. More information in this related thread. This would be of particular interest to owners of the DM42 calculator which has the larger screen, crying out to be taken advantage of.

Image

and targeting the larger screen size of the DM42 - this image:

Image

The purpose of the Python to RPN converter is not to usurp the beauty and role of handcrafted RPN, it is to provide an alternative way of programming the HP42S for those who prefer to to use if statements and for loops etc. rather than GTO's and ISG. The purpose is also to provide powerful new features to make programming the 42S easier: lists, dictionaries, matrix access syntax, native complex number syntax, alpha messages easily built with multiple parameters in one line, simpler menu support using aview() etc.

My hope is that the Python to RPN converter will contribute to keeping this wonderfully designed calculator alive, just as Free42 and the DM42 have done. I look forward to what people build, using the increased ease of use and power of a structured programming language and hope to see some great new programs developed for the DM42.

I hope people like it.
I went to check out your python to rpn page, and I get a 404 error. Is the website down or did you move it?
Thanks for making this to begin with.

Post Reply