[comp.sys.hp] HP-28S program to manipulate programs

postpischil@alien.dec.com (Always mount a scratch monkey.) (02/14/89)

Benoit Menendez asks for a way to create a program object which depends
partially on an argument. 

In the code:

<< -> i
    <<
	<< -> x << x i + >> >>
    >>
>>

realize that << -> x << x i + >> >> is an object, and all that happens
to this object when the program is evaluated is that it is placed on the
stack.

Since we want to create a program object not known at the time of writing, we
cannot simply place a copy of an object on the stack.  We therefore need
a built-in function which returns a program object.  I think the only suitable
function is STR->, which takes a string and can return any object(s).

So one possibility is:

<< "<< -> x << x " SWAP ->STR + " +" + STR-> >>

This will perform the requested function, although it is not necessarily
a good way to solve a problem.  (Actually, there are some objects for which
->STR STR-> will not return the original object, such as strings with embedded
quotes, and the above program will not work with such objects.)


				-- edp

benoitm@hpmwtd.HP.COM (Benoit Menendez) (02/16/89)

I suspected this, but I was under the impression that:

(1) << -> i
        <<
	    << -> x << x i + >>>>
        >>
    >>

was (more or less) equivalent to the algebraic form:

(2) << -> i 'x+i' >>

which expands the i in the quoted algebraic expression, This was troubling me.

But I understand now that (1) does nothing more than placing the object on the
stack while (2) does perform some sort of algebraic evaluation.

It is interresting to think of the STR-> function as an entry point into the
lexical analyzer of the calculator, this powerful function allows programs to
manipulate programs as strings and convert the result back into programs before
evaluation. This can be used to implement a meta-language and the associated
compiler or interpreter.

	Benoit.

Thanks a lot to all of you who responded to my posting.

----
Benoit Menendez

ARPANET  : benoitm%hpmwtd@hplabs.hp.com
UUCP     : ...hplabs!hpmwtd!benoitm