Question

i am starting programming using CL on IBM system i. My task is to implement RPN calculator using CL language. Typically, this calculator uses stack. But I have no idea have to implement it in CL. Can someone give me some advice? Maybe *PTR variables are solution, but can someone give me an example how to use it?

Thanks in advance JS

edit:

CL language is imposed by my tutor.

My program should receives signs from user (digits/number, signs of mathematical operations).
After receiving each sign program should analize what was received and then take some action, according to pseudo-code:

Do when new sign was entered:
if: entered sign is a number
    then push it into stack
else if: entered sign is a sign of mathematical operation 
    then 
    - pop two elements from stack 
    - make operation using this elements and entered sign 
    - push result into stack

My problem is how to implement or substitude this stack. A need to reach element in LIFO order and I do not know how many elements the structure finally will contain.

Was it helpful?

Solution

Understand that CL is ill-suited for this. Following is untested:

DCL &ARRAY *CHAR 50
DCL &PTR *PTR ADDRESS(&ARRAY)
DCL &ELEM *CHAR 10 STG(*BASED) BASPTR(&PTR)
DCL &i *DEC (9 0)
...
/* Loop through the array */
DOFOR VAR(&i) FROM(1) TO(5) BY(1)
  CHGVAR %OFFSET(&PTR) (%OFFSET(&PTR) + 10)
  /* &ELEM is the current array element */
ENDDO

Post some code; what did you try? If you can't post any code, post pseudo-code and explain what you want the code to do.

EDIT: The above code snippet implements an array. This is quite similar to a stack. LIFO is as easy as keeping track of the latest array index. In the above snippet, the CHGVAR adds 10 bytes to the pointer. It is 10 bytes because each array element / stack element is 10 bytes (DCL &ELEM). This particular stack holds 5 entries - 5 times 10 = 50 (DCL &ARRAY). Adding 10 bytes is PUSH, subtracting 10 bytes is POP. The current &PTR is the most recent stack entry.

Your next step should be to write one subroutine for each of the major operations in your outline. Start with PUSH. If you aren't familiar with the debuggers on IBM i, use DMPCLPGM to see the results of your code in operation. Try it, then if you still have questions, post your code and ask a specific question about that code. Programming is about writing code, so jump in and try! :-)

OTHER TIPS

As far as I remember, the tutor said that the stack should be only 4 entities in size (10 would be an overkill). Unless we are not talking about the same tutor (pwr?).

Thanks for your answer Buck, I managed to understand how pointers work thanks to you :)

The way I implemented stack for this particular problem is:

PGM                                                                  
DCL        VAR(&STACK) TYPE(*CHAR) LEN(20)                           
DCL        VAR(&STACKPTR) TYPE(*PTR) ADDRESS(&STACK)                 
DCL        VAR(&STACKVAL) TYPE(*CHAR) STG(*BASED) BASPTR(&STACKPTR) LEN(5)                                                 

/* ----------------------------------------------------- */
/* code that uses PUSH and POP subroutines when required */
/* ----------------------------------------------------- */

SUBR       SUBR(PUSH)                                                             
  CHGVAR     VAR(&STACKVAL) VALUE(&WYRAZENIE)                   
  CHGVAR     VAR(%OFFSET(&STACKPTR)) VALUE(%OFFSET(&STACKPTR)+5)
ENDSUBR                                                         
SUBR       SUBR(POP)                                            
  CHGVAR     VAR(%OFFSET(&STACKPTR)) VALUE(%OFFSET(&STACKPTR)-5)               
ENDSUBR                                                         
ENDPGM                                                          

Of course there is no overflow control or whatever, but it gives you the idea :)

Create a *LIFO data queue. Entries sent to the queue can be received in last-in-first-out sequence.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top