[squeakland] How can I evaluate an Expression in Text
Bert Freudenberg
bert at freudenbergs.de
Thu Mar 31 08:03:26 EDT 2011
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 -
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakland.org/pipermail/squeakland/attachments/20110331/ff3f842f/attachment-0002.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Eval.001.pr
Type: application/x-squeak-project
Size: 27804 bytes
Desc: not available
URL: <http://lists.squeakland.org/pipermail/squeakland/attachments/20110331/ff3f842f/attachment-0001.bin>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakland.org/pipermail/squeakland/attachments/20110331/ff3f842f/attachment-0003.html>
More information about the squeakland
mailing list