
I have a couple of routines that a friend finds quite helpful.  The
problem is that he borrows my '28 to use a couple of routines that I've
written for him and doesn't really understand the '28s, all he wants to
do is put in his numbers and have his answers come out. 

Sometimes he passes a routine a number when it wants a list, or some
such. I started modifying each routine to check that it had the correct
number and type of arguments, but that started to get to be a bit much.

After thinking about it I came up with CHKARG.  It takes two arguments:
an algebraic (the name of the calling program) and a list describing the
type of each argument that the calling routine wants.  CHKARG checks the
arguments and it returns a TRUE/FALSE indicating whether the supplied
arguments match the required ones in quantity and type. 

If there is a mismatch it displays a nice text message telling the user
what argument was wrong, indicating what was supplied and what was
required. 

Initially the argument description list had to consist of numbers, like
those returned by the TYPE command, but I could never remember what they
were so I modified it so the list consists of dummy arguments of the
right type. 

If you had a routine, called 'TST' that took three arguments (a list, a
complex and a real number) you would call CHKARG using
something like:

	'TST' { {} (0.0) 0 } CHKARG

CHKARG returns a TRUE/FALSE indicating whether there were enough
arguments of the correct type remaining on the stack after CHKARG
removed it's two arguments.

The code is reasonably straight forward.  There are only two 'tricky'
parts and they're more paying attention to details than tricky. 

The first is where CHKARG gets the current depth of the stack to
determine that there will be at least one argument left after CHKARG
removes it's two. CHKARG them subtracts two from the depth and uses this
number as the number of supplied arguments instead of the actual depth.

The second part is where CHKARG checks that the type of each element in
the argument list is the same as the matching one passed on the stack.

There is a test routine (TST) at the end of the listing to show the
how CHKARG is called.

To make looking at the CHKARG program a little easier here's a sort of
index for some of the variables:

    arglist - template for the count and type of arguments the caller wants.
    args    - number of items that are on the stack.
    name    - name of the caller, converted from '' to ""
    arg     - the index of the current argument being checked

Here's the two routines. The checksums were done using the standard HP29
checksumming routine.

CHKARG : [A0A0] (No isn't that an interesting checksum!)
<<
  IF DEPTH 2 > THEN
    DEPTH 2 - ROT ->STR 2 OVER SIZE 1 - SUB 
    -> arglist args name
    <<
      IF arglist SIZE args > THEN
        name ": Wrong arg. count." + 1 DISP
        "Got: " args ->STR + 
        "Need: " + arglist SIZE ->STR + 2 DISP
        0
      ELSE
        1 arglist SIZE 0
        -> arg args done
        <<
          DO
            IF args arg 1 - - PICK TYPE arglist arg GET TYPE != THEN
              { "Real" "Comp" "Str" "Vec" "CVec" "List" "Name" "Prgm" 
                "Algb" "Bin" }
              -> argnames
              <<
                name ": Arg " + arg ->STR + " wrong type!" + 1 DISP
                "Want: " argnames arglist arg GET TYPE 1 + GET + 
                " Got: " + argnames args arg 1 - - 2 + PICK TYPE 1 + GET +
                2 DISP
                1 'done' STO
              >>
            END
            arg 1 + 'arg' STO
          UNTIL arg args > done OR END
          done NOT
        >>
      END
    >>
  ELSE
    -> name arglist
    <<
      name ->STR 2 OVER SIZE 1 - SUB 'name' STO 
      name ": Argument error!" + 1 DISP 
      "Need " arglist SIZE ->STR + " arg" +
      IF arglist 1 != THEN
        "s" +
      END
      "." + 2 DISP 
      0
    >>
  END
>>

TEST : [AF62]
<<
  IF 'TST' { {} (0, 0) 0 } CHKARG THEN
    "Args are OK." 1 DISP
    -> a1 a2 a3
    <<
      "a1: list    ( " a1 ->STR + ")" + 2 DISP
      "a2: complex ( " a2 ->STR + ")" + 3 DISP
      "a3: real    ( " a3 ->STR + ")" + 4 DISP
    >>
  END
>>
john.Latala@Waterloo.NCR.COM
