PUSH and POP stack routines

Contributions to this software library are always welcome. Please ensure that you post program listings rather than .raw files. They give a reasonable idea of what your program does without having to load them into a DM42 and you can also include comments in your code. Check out the following link for a decoder/encoder: https://technical.swissmicros.com/decoders/dm42/

You can then copy/paste the listing and post it in "code" tags.
dlachieze
Posts: 613
Joined: Thu May 04, 2017 12:20 pm
Location: France

Re: PUSH and POP stack routines

Post by dlachieze »

lispm wrote:
Sun Mar 20, 2022 9:19 am
Dear dlachieze

Code: Select all

 1
 XEQ "PUSH"
 XEQ "POP"
returns error

"too few Arguements" in NSTK
"Invalid Type" in 4STK

at least with my Free42 3.0.10 on Ubuntu and Android
It works fine with Free42 3.0.10 on Android. At which step do you get this error message?
DM42: 00425 - DM41X: β00066 - WP43: 00042
User avatar
lispm
Posts: 27
Joined: Tue Jun 29, 2021 2:23 pm
Location: Germany

Re: PUSH and POP stack routines

Post by lispm »

The new version works fine. Great idea to use NEWLIST APPEND RCL X<>Y EXTEND to the implement the First in Last Out. Did not think of that!
Last edited by lispm on Tue Mar 22, 2022 12:50 pm, edited 1 time in total.

TI 55, HP16C, HP41C, DM42, APPLE ][, TRS-80, ATARI 800XL, ATARI 520ST, XEROX 1186 DANDELION, SYMBOLICS 3640, TI EXPLORER II
BLOG: https://yazuu.org/en/showblog_public.php?user=1&blog=
Thomas Okken
Posts: 1102
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: PUSH and POP stack routines

Post by Thomas Okken »

I just realized that my version had a bug: it needed a CF 25 in the case HEAD returned NO, that is, when trying to pop from an existing but empty stack. In its original form, it would leave flag 25 set when it returned in that case, which of course is Not Good™.

I updated my earlier post, adding the missing CF 25.

The POP function is a bit tricky because you can't use FUNC for it. With FUNC, the function will either return an error or consume its declared number parameters and return its declared number of results; it cannot return one number of results in one non-error scenario and a different number of results in another non-error scenario. And that means you can't use it to implement something like HEAD or POP... But then that also means you have to be careful with flag 25. FUNC takes care of saving and restoring flag 25, and so if you're not using FUNC, at least you should clear flag 25.

What's really needed here are RTN/RTNYES/RTNNO variants that allow you to override the parameter and result counts specified in the FUNC call. Something I'll have to think about, I was already planning to go back to the drawing board for FUNC in NSTK mode...

(Or you could say that trying to pop from an empty stack should be considered an error. In that case, POP becomes a lot simpler: LBL "POP" FUNC 01 SF 25 HEAD "μ" FC? 25 RTNERR 6 END.)
User avatar
lispm
Posts: 27
Joined: Tue Jun 29, 2021 2:23 pm
Location: Germany

Re: PUSH and POP stack routines

Post by lispm »

Hi Thomas. do you mind if I Update your SW on my original post, because it is much better and I don't want the reader to install an inferior version?
All I did was to remove the RTNYES from your code because it requires an additional keypress when in CUSTOM mode to# see the result.

TI 55, HP16C, HP41C, DM42, APPLE ][, TRS-80, ATARI 800XL, ATARI 520ST, XEROX 1186 DANDELION, SYMBOLICS 3640, TI EXPLORER II
BLOG: https://yazuu.org/en/showblog_public.php?user=1&blog=
Thomas Okken
Posts: 1102
Joined: Tue May 02, 2017 5:48 pm
Location: Netherlands
Contact:

Re: PUSH and POP stack routines

Post by Thomas Okken »

Sure, go ahead!

I would recommend not removing the RTNYES, though. As long as you're only calling POP from programs, it doesn't matter, of course; in running programs, RTNYES has exactly the same effect as RTN or END. But for consistency when called from the keyboard, I feel that a function that can return No should return only Yes or No. It's a matter of taste, I'll admit, but that is my take. (Or instead of returning No when the stack is empty, return an error. That's the shortest version of the code, and I think the most appropriate for interactive use as well.)
Post Reply