Re: How can I evaluate an Expression in Text

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Re: How can I evaluate an Expression in Text

Bert Freudenberg

On 30.03.2011, at 23:28, Ricardo Moran wrote:

Hi Steve, 

From a script of the text object you can write:

Compiler evaluate: self getCharacters.

and if the text contains a valid smalltalk expression it will work, otherwise it will throw an error. I don't remember exactly how to catch syntax errors, though. I did it for the spreadsheet object last year, but I wasn't sure if that's the right way.

Here's a textual script to do this with error handling. It needs a "result" variable of type Number, and a "valid" variable of type Boolean. 

eval
[self setResult: (Compiler evaluate: self getCharacters) asNumber.
self setValid: true]
on: SyntaxErrorNotification , Error
do: [self setValid: false]

See attached project.

As for the format, I have no idea. Sorry.

Use asInteger, asFloat, or asFraction. But note that Etoys always displays numbers as floats. If you want to display a number as fraction, you would need to use something like

self setCharacters: 0.75 asFraction asString

E.g.

Float pi = 3.141592653589793
Float pi asInteger = 3
Float pi asFraction = (884279719003555/281474976710656)

- Bert -



_______________________________________________
squeakland mailing list
[hidden email]
http://lists.squeakland.org/mailman/listinfo/squeakland

Eval.001.pr (37K) Download Attachment