From: jhmeyers@miu.edu (John H Meyers)
Subject: OBJN: a very safe OBJ-> for strings (NEW)
Date: 21 Nov 1996 00:00:00 GMT
Message-ID: <571s7f$ggi@news.iastate.edu>
organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
reply-to: jhmeyers@miu.edu
newsgroups: comp.sys.hp48


Borrowing an idea suggested by Joe Horn in Goodies Disk #10 (STOPSIGN.DOC),
the following tiny program may be used to safely perform OBJ-> on a string
which a user may have typed in response to, say, an INPUT command.

Not only is this safe (e.g. you can even input "CLVAR" and
nothing serious will happen), but you can even verify that the user
typed exactly the number of objects you were expecting (often one object,
but we allow for circumstances where you may prompt for several values
at once by pre-inserting several "tags" into the command line,
or prompt in any other manner for more than one input value);
finally, this vast array of features will cost you only 89 bytes!

%%HP: T(3); @ \-> is a right-arrow character, etc.
@ OBJN - safely compile a string into exactly N objects  (S/SX/G/GX)
@ Stack diagram:   "string"  N   -->   object(s)
\<< DEPTH \-> n d
  \<< #64037h SYSEVAL STR\-> DEPTH 2 + d - n - { 296 DOERR } IFT \>> \>>
@ 89 bytes (on stack), #EE35h checksum

*CAUTION*   Please be very careful when typing the SYSEVAL address for
entry point "WithHidden", and notice the "h" following it, meaning HEX,
not DECimal; also verify the program's checksum with the BYTES command,
since mistakes with SYSEVAL can otherwise wipe out memory pretty readily!

The SYSEVAL and STR\-> commands are not found in any menu, and must be
manually typed in (or you can download the above program text using Kermit).

How do you use it?  After, say, an INPUT command (the result of which is
a string that is left for you to parse or compile yourself), just put the
number of expected input objects on the stack, and then call the above
program; the result will either be the desired number of objects,
or else an error message.

How does it work?  It temporarily changes to the "hidden directory"
(an invisible last object in HOME, in which your Alarms and User Key
assignments are normally kept) before doing STR-> (a form of OBJ->
which accepts only a string as its argument); the internal "WithHidden"
command also sets the "Stopsign" (see STOPSIGN.DOC in GD#10/Hacker),
so that no commands of any type are "visible" to the compiler while
it is compiling your argument string (if present, they will simply
be compiled as meaningless Global Names, e.g. 'CLVAR', 'SIN', etc.)

When the string is finished being compiled and executed (assuming it
had no Invalid Syntax), the number of objects it produced is finally
compared to the number you expected (by comparing the stack depth
before and after evaluation of the string), and a "Wrong Argument Count"
error is generated if unequal.  The internal "WithHidden" command
always returns to your original directory and re-sets the "stopsign,"
even when an error occurs.

If you also want to check the type of each object, a program such as CHKARGS
(found in Donnelly's HP48 Handbook) is useful when there are several objects;
for only one object, it may be easier to just use IF DUP TYPE ...

For universal use, the best place to store this program is in your
HOME directory ("Don't leave HOME without it!"), and may all your
user interfacing now be perfectly safe.

-----------------------------------------------------------
With best wishes from:  John H Meyers  ( jhmeyers@mum.edu )
