#String->Expression?
12 messages in this thread
Is there any quick and easy way to turn a string (eg/ 'sin(x)') into an
expression, for which you can substitue a value of x? I know that BBC Basic has
an expression like this, and it was *very* useful.
Simon,
>> Is there any quick and easy way to turn a string (eg/ 'sin(x)') into an
>> expression, for which you can substitue a value of x?
Not in any Mac language I know of…
Simon,
4th Dimension has this capability with the EXECUTE and Get Pointer commands,
but you may not be looking for a specialized database language.
Craig Miller
Simon,
Why do you want to do this? Wouldn't it be good enough to use sin(x)
intrinsically in a function? What does the BBC BASIC functionality do exactly?
I may have a solution for you but hesitate to offer it without knowing more re
the intent of the operation. Thanks
Tom Nalevanko Mainstay
I'm writing a program that integrates a function. The user has to be able to
enter a complex expression, (cos(sin(x^(2^log(x)))) or something like that, and
I'm too lazy to figure out an algorithm to analyze it. In BBC BASIC, you could
write,
S$="(cos(sin(x^(2^log(x))))"
z=Eval(S$,x,3)
And z would equal (cos(sin(3^(2^log(3))))
Simon,
We have a higher level function in both our VIP-C and VIP-BASIC products called
_evaluate_expression. It works similarly to what you have described. Here is a
description of the function.
_evaluate_expression
Use _evaluate_expression to evaluate an expression given as a character string.
SYNTAX void _evaluate_expression (char expression[], char nb_param, char
param_names[][256], extended param_values[], char *status_code, extended
*expr_value);
expression Address of a character string that contains the expression to be
evaluated.
nb_param Number of parameters.
param_names Address of a character string that contains the parameter's names.
param_value Array of extended variables that contains the values of the
parameters.
status_code Address of a char variable that, upon return, contains a zero value
if the expression can be correctly parsed and computed. Otherwise, status_code
contains a nonzero value and expr_value is undefined. The following values may
be returned:
Value Indicates
0 No error.
1 Undefined parameter in expression.
2 Invalid numeric constant in expression.
3 Illegal operand in expression.
4 Intrinsic function has invalid argument expression.
5 Intrinsic function has wrong number of arguments.
6 Invalid intrinsic function.
7 Syntax error in expression.
expr_value Address of an extended variable that, upon return, contains the
result of the expression.
NOTE Even though an expression may be evaluated as an integer, its value will
be converted to a real number in expr_value.
EXAMPLE See "Eval" example in the "Math Library" folder.
VIP-C will be released later this month. VIP-BASIC will be released in 6 to 8
weeks. For some product literature, pls. provide your address.
To see both products in action, visit us at Macworld Boston, Aug. 2-6, Booth
5328 WTC.
Thanks for your interest in VIP-C and VIP-BASIC.
Tom Nalevanko Mainstay
Simon,
"Is there any quick and easy way to turn a string (eg/ 'sin(x)') into an
expression, for which you can substitue a value of x?"
If you're willing to use AppleEvents, and pass the string to either Userland
Frontier or HyperCard, either will return a result that you can stuff into a
function call.
It's not exactly what you have in mind, I don't think, but it's a way of
dealing with string expressions that might work for you. Also, it just occurs
to me that Microsoft Excel also supports Apple Events and could handle that
chore.
-rich-
Thanks, Rich, but I'm looking for a stand alone application. It looks like I'll
have to do it the hard way…
Simon,
"I'm looking for a stand alone application. It looks like I'll have to do it
the hard way…"
Building an interpreter for such expressions shouldn't be very difficult. You
might give the spreadsheet example in my book a look. It includes code for an
expression parser that handles simple formulas. It could be easily extended to
handle function calls as well as arithmetic.
-rich-
Simon,
In the July, 1992 issue of MacTutor (now MacTech) magazine is an article by
Kevin Raner called "A Compiler for Math Equations". At run time this C program
allows the input of a mathematical function in string form. The function is
then compiled into machine code in the heap (while the program is running!).
The function can be used in the program like any other one. It runs at full
speed because it doesn't have to be interpreted each time. I am sure you can
adapt Kevin's Equation Compiler for your purposes.
Bob
Thanks alot, Bob, I'll check it out!
Simon,
I always took it for granted that expressions such as that (eg one's that may
appear in a spreadsheet cell) were parsed and referred to an internal
dictionary of functions.
Dave.