First time here? Check out the FAQ!
x

Can I pass an expression as variable in a Tool?

0 votes
230 views
So here I am now developing my first Tool - very exciting - and here I am the first time scratching my head :)

 

Let's assume the following code:

| x y expression |

x := 1.

expression := expressionFromTool.

"expressionFromTool is a localVariable (Text, Number) where the user should put in an expression like: 2 * x."

y := expression.

 

The problem is I can't pass an expression as variable, I always get that variable x is undeclared - or is there a way? I also tried defining expressionFromTool as an object but that didn't do it. Maybe a string? But then how do I convert the string to an expression?

Thanks!
asked Nov 10, 2015 in Capytalk & Smalltalk by kymaguy (Virtuoso) (10,580 points)

1 Answer

0 votes
The variable you call expressionFromTool should be a String, for example: '2 * x'.  Once you receive the String you can parse it and evaluate the expression.

What is it that you're trying to build in the Tool?  Where is the expression going to be used?

With more background on what your Tool is supposed to do, we might be able to offer more suggestions on how to accomplish it.

Thanks!
answered Nov 11, 2015 by ssc (Savant) (120,590 points)
Thanks!
How do I parse the string?
Well, it's my first Tool so I'm trying to keep things simple.. It's just a functionWriter that let's you plot and save (to wav) functions you put in. So the expression above is basically it, except that I'm iterating over an array of x-values.
| myExpression |
myExpresion := Compiler evaluate: 'aString'.


here some interesting reading about how the Compiler works in Pharo. I guess it is not exactly the same in Kyma, but it can give you an idea.
...