Re: Evaluate a string
- To: mathgroup at smc.vnet.net
- Subject: [mg25174] Re: Evaluate a string
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Tue, 12 Sep 2000 21:24:20 -0400 (EDT)
- Organization: Universitaet Leipzig
- References: <8pfe7f$d1b@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
>
> The inner problem. In Perl, for example, you can evaluate a string back
> in thru the Perl parser:
?ToExpression
"ToExpression[input] gives the expression obtained by interpreting
strings or \
boxes as Mathematica input. ToExpression[input, form] uses
interpretation \
rules corresponding to the specified form. ToExpression[input, form, h]
wraps \
the head h around the expression produced before evaluating it."
In[]:= x = 0;
ToExpression["x=3"];
x
Out[]= 3
> Taking the code out of the string is not an option. (But I could
> conceivably write the string to a file, and then Load or Run this. But
> I can't find a Mathematica command to load a file and execute it.)
>
Ok lets use a file:
In[]:=Put[OutputForm[tstring], "/tmp/test.m"];
In[]:=Get["/tmp/test.m"]
Out[]= y^2
MLEvaluateString() is not a real ML function ?
A simple look into the C-code show, there is the infamous
ToExpression call again, together with a EvaluatePacket so
that the expression gets evaluated by the kernel.
int MLEvaluate( mlp, s)
MLINK mlp;
charp_ct s;
{
if( MLAbort) return 0;
return MLPutFunction( mlp, "EvaluatePacket", 1L)
&& MLPutFunction( mlp, "ToExpression", 1L)
&& MLPutString( mlp, s)
&& MLEndPacket( mlp);
} /* MLEvaluate */
Evaluate string make nothing than to wait
for the answer.
int MLEvaluateString( MLINK mlp, charp_ct s)
{
int pkt;
if( MLAbort) return 0;
if( MLEvaluate( mlp, s)){
while( (pkt = MLAnswer( mlp), pkt) && pkt != RETURNPKT)
MLNewPacket( mlp);
MLNewPacket( mlp);
}
return MLError( mlp) == MLEOK;
} /* MLEvaluateString */
There is no additional dependence and it seems to be easy to copy both
functions.
You should somewehere have a MLAbort variable in your program.
It shold be simple to rewrite the functions in perl if you have access
to MLPutString() and MLPutFunction().
>
> Where is the optimal path thru all this to some simple environmental
> features that all other languages have???
The optimal path should be reading "The Mathematica Book" by S. Wolfram
that came with your Mathematica copy.
Regards
Jens