Post Reply 
RPL beginner
11-11-2021, 02:33 AM
Post: #15
RE: RPL beginner
(11-08-2021 05:40 PM)dwesti Wrote:  Probably the size of the program can be reduced by using arrays

Suggestion:

Instead of an array, consider using NULL-named LAMs. They are fast to access, can be of any type, and are flexible to use with built-in commands. They are much like an array but are in some ways more convenient.

The following sample implementation introduces the following SysRPL concepts:
  • NULLLAMs instead of named LAMs
  • definite loops
  • case statements (when checking for boundary conditions)
  • use of DEFINEs to improve the readability of the code
Note that when defining NULLLAMs, the objects need to be placed on the stack in the reverse order of their index. This is due to the natural consequence of stack level numbering starting at 1 with the most recently added object. Since the LAM indices match the stack level numbers when the LAMs were created, the last item added to the stack is the one indexed as NULLLAM 1.

Code:
DEFINE   X1                1GETLAM
DEFINE   X2                2GETLAM
DEFINE   X3                3GETLAM
DEFINE   X4                4GETLAM
DEFINE   Y1                5GETLAM
DEFINE   Y2                6GETLAM
DEFINE   Y3                7GETLAM
DEFINE   Y4                8GETLAM
DEFINE   TotalLAMs         SEVENTEEN
DEFINE   DrawOrClearLine   17GETLAM EVAL
::
   ( no arguments expected )
   CK0NOLASTWD

   ( setup/clear display )
   ClrDA1IsStat
   RECLAIMDISP
   TURNMENUOFF

   ( subroutine for line drawing/clearing )
   ' ::
      (
         DrwLn: Given line endpoints and a boolean, draws or clears the line
                designated by the endpoints in the text/alpha display.

         Input
            SL5: X1
            SL4: Y1
            SL3: X2
            SL2: Y2
            SL1: TRUE [draw] or FALSE [clear]

         Output
            Draws or clears a line in alpha/text display
      )
      5UNROLL                    ( move boolean to top of args )
      4PICK 3PICK #> IT 2SWAP    ( order endpoints as needed )
      5ROLL ITE LINEON LINEOFF   ( draw or clear line )
   ;

   ( initial values for NULLLAMs )
   ( note: reverse order [stack index aligned] )

   ( DY values [DY4 - DY1] )
   FIVE FIVE FIVE -5

   ( DX values [DX4 - DX1] )
   FIVE FIVE -5 FIVE

   ( Y values [Y4 - Y1] )
   FORTY TEN ZERO THIRTY

   ( X values [X4 - X1] )
   120 TEN SIXTY 90

   ( bind locals )
   ' NULLLAM TotalLAMs NDUPN DOBIND

   ( clear key buffer )
   FLUSHKEYS ATTNFLGCLR

   ( loop until a key is pressed )
   BEGIN
      KEYINBUFFER? ATTN? OR NOT
   WHILE
      ( draw current line )
      X1 Y1
      X2 Y2
      TRUE
      DrawOrClearLine

      ( brief pause for emulator )
      % 0.02 dowait

      ( clear previous line )
      X3 Y3
      X4 Y4
      FALSE
      DrawOrClearLine

      ( step values )
      NINE ONE_DO (DO)
         INDEX@               ( X/Y index )
         DUP #8+              ( DX/DY index )
         OVER GETLAM          ( X/Y value )
         OVER GETLAM #+DUP    ( add DX/DY value, DUP )
         4PICK PUTLAM         ( PUT new X/Y value )

         ( check for step delta change )
         ::
            ZERO OVER#=case ::            ( current X/Y value is 0 )
               FIVE 3PICK PUTLAM          ( DX/DY should change to 5 )
            ;
            3PICK FIVE #< ITE 130 SIXTY   ( determine upper value change point )
            OVER#=case ::                 ( current X/Y value is max point )
               -5 3PICK PUTLAM            ( DX/DY should change to -5 )
            ;
         ;

         ( drop 3 BINTs on stack [X/Y index, DX/DY index, new X/Y value] )
         3DROP
      LOOP

   REPEAT

   ( clear pressed key )
   FLUSHKEYS ATTNFLGCLR

   ( abandon locals )
   ABND

   ( reset display )
   ClrDAsOK
;
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RPL beginner - dwesti - 10-30-2021, 07:23 PM
RE: RPL beginner - David Hayden - 11-01-2021, 02:11 PM
RE: RPL beginner - dwesti - 11-01-2021, 04:40 PM
RE: RPL beginner - DavidM - 11-02-2021, 10:51 AM
RE: RPL beginner - dwesti - 11-02-2021, 02:18 PM
RE: RPL beginner - dwesti - 11-02-2021, 05:20 PM
RE: RPL beginner - DavidM - 11-04-2021, 01:07 AM
RE: RPL beginner - dwesti - 11-04-2021, 09:21 AM
RE: RPL beginner - DavidM - 11-04-2021, 09:49 AM
RE: RPL beginner - dwesti - 11-04-2021, 04:04 PM
RE: RPL beginner - DavidM - 11-05-2021, 10:12 AM
RE: RPL beginner - dwesti - 11-06-2021, 06:41 PM
RE: RPL beginner - DavidM - 11-07-2021, 01:38 PM
RE: RPL beginner - dwesti - 11-08-2021, 05:40 PM
RE: RPL beginner - DavidM - 11-11-2021 02:33 AM
RE: RPL beginner - dlidstrom - 11-13-2021, 08:04 PM
RE: RPL beginner - BINUBALL - 11-14-2021, 02:06 AM
RE: RPL beginner - DavidM - 11-14-2021, 12:49 PM



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