From: jhmeyers@miu.edu (John H Meyers)
Newsgroups: comp.sys.hp48
Subject: Several ways to compile a string
Date: 29 Aug 1998 10:21:08 GMT
Organization: MIU Computer Services,  Fairfield, IA 52557.  Not Approved.
Lines: 156
Message-ID: <6s8kmk$aev$1@news.iastate.edu>
Reply-To: jhmeyers@miu.edu
NNTP-Posting-Host: vax1.mum.edu
Xref: republic.btigate.com comp.sys.hp48:15204

From time to time we may need to take some character string,
e.g. "123.45", and produce from it the value it represents;
this is generally necessary after an INPUT command, for example,
the result of which is always just an edited string.

A few different approaches to doing this simple task
are presented below, with analysis.

------------

OBJ\->

This is the most basic way to turn a string into an object,
pretty much the same way the calculator does (for any string
that you type into the command line); however, this can have
some drawbacks; e.g.:

What if the string contains "CLVAR"
(oops, it purges all your variables)

What if the string contains "1 2 3"
(i.e. it unpredictably creates any number of objects).

What if the argument on the stack is a *tagged* string,
or an algebraic expression?  (Try it).

How about an empty string "" ?

------------

STR\->

Slightly better; the only argument type allowed is a string,
and any tag(s) are first removed.

This command is not shown in HP48 menus; it was created
for the HP28S, but fortunately is still available.

------------

"{" SWAP + STR\->

This generally succeeds at producing one result list for
almost any input, although there may be a variable number
of objects in the list, e.g. if "" or "1 2 3" were input.

If you were expecting one single input to be supplied,
and you don't need to be highly rigorous in checking
the input, you might just follow the above with 1 GET;
if you expected two inputs, try 1 2 SUB OBJ\-> DROP, etc.

You can also use DUP SIZE or OBJ\-> to obtain the exact
number of separate objects which were in the resulting list.

To restrict input to numbers only (real or complex),
substitute "[" for "{" -- producing a vector result,
after which the same techniques may be used as for lists.

This provides a simple User-RPL method which is not absolutely
foolproof, but it is pretty safe, and quite easy to program.

Note that a formerly dangerous input string like "CLVAR"
is now rendered somewhat harmless, because enclosing
the objects in a list causes them to be parsed and
included in the list, but not evaluated.

------------

#64037h SYSEVAL STR\->  [note the *hex* address]

This performs STR\->, but with the current directory
temporarily changed to the hidden directory; this causes
the parser to be unable to recognize most commands, e.g.
"CLVAR" will be compiled as if it were a variable name,
and since there is probably no such named variable in
the hidden directory, the result would then be just 'CLVAR'
(whereas parsing "Alarms" would recall the hidden alarms list).

Some normally valid input, e.g. "\<< ... \>>" will produce
an Invalid Syntax error in this environment.

See Goodies Disk #10, Hacker directory, "stopsign.doc"
for a complete explanation of what happens here.

Note that there may still be a variable number of result objects.

------------

IF DUP TYPE 2 == THEN
  #238A4h SYSEVAL #3AC0h SYSEVAL SAME @ SysRPL: palparse FALSE
  { DROP2 STR\-> } IFT @ Generate "Invalid Syntax" error if so
  DUP #25934h SYSEVAL DROP2 DROP @ Unsupported, see below.
END

In most of the previous examples, the string was first parsed,
then the resulting object or program was evaluated;
that's how the command line is processed, for example,
and is why strings containing commands, or an unknown
number of objects, might give us trouble.

In the program immediately above, the string is parsed,
but not evaluated; furthermore, you are *guaranteed*
to get exactly *one* object as a result, even with
input strings like "" or "1 2 3", the reason being
that any results that are not exactly one object
remain enclosed within an "invisible" program object.

E.g. an input string "1 2 3" produces what looks like
three separate numbers 1 2 3 on one stack level;
actually it is a single program equivalent to \<< 1 2 3 \>>,
except that you don't see any program delimiters.

If you subsequently either edit or evaluate this object,
however, then separate values will be obtained
(but any commands like CLVAR might also then be executed).

This happens to be how Kermit safely turns imported ascii
files into single objects.  If single values are expected
as input, this is a very rigorous and "bulletproof"
method of parsing them, but it is not handy for
User-RPL processing of multiple values in one string.

One more thing:

The entry point at *hex* #25934h is *unsupported*,
but I have used it successfully in ROM versions M and R;
if you omit the line containing this system function,
the only different result is for a string like
"\<< ... \>>", representing a user program object,
in which the result of "palparse" is actually an
invisible "outer" program, containing the quoted
user program object which you would normally expect.

When you type "\<< ... \>>" into the command line,
the parsed result is executed, which *then* puts your
actual user program on the stack, whereas when Kermit
imports such a string (or you use the above program to parse it
but not execute it), you want to get your actual user program
extracted *without* executing the parsed object,
which is what the unsupported entry point does.

Curious hackers should look for more information at:

'Re: "palparse" question' 1996/07/03 by Dan Kirkland
<http://www.dejanews.com/getdoc.xp?AN=163446782>

------------

Well, if there are any more ideas, let's hear them!

"These are the only Elements at present known to Harvard;
 if there are any others then they haven't been discovered"
              - Tom Lehrer

-----------------------------------------------------------
With best wishes from:   John H Meyers   <jhmeyers@mum.edu>