the magic behind "do it"
Andrew C. Greenberg
werdna at mucow.com
Thu Dec 27 16:22:58 UTC 2001
On Thursday, December 27, 2001, at 10:26 AM, Brent Vukmer wrote:
> What do I need to do to get Compiler-love?
>
>
> | exprStream result failBlock |
>
> exprStream _ ReadWriteStream on: ''.
> exprStream nextPutAll: '1 + 2'.
> failBlock _ [ Transcript show: 'ArithmeticExpression#eval FAILED: ',
> exprStream contents. ].
> result _ Compiler new evaluate: exprStream in: nil to: nil notifying:
> nil ifFail: failBlock.
> ^ result
The problem is not compiler-related (it has to do with use of
ReadWriteStream). I'll begin with a somewhat more elementary "teach how
to fish" introduction for our newbies, and get back to your question in
a moment:
How to fish, when you are messing with strange code:
To understand what code is doing or how it works, Try browsing the
selector (just select the expression with the selector and type cmd-B or
cmd-M) and see its cousins, or browsing for methods that use it
(cmd-N). This will give you a sense how things work -- or at least how
they are used in practice, which is much the same thing.
Doing this, you would find a simpler class method that does most of the
work for you:
Compiler evaluate: '1 + 2'
Compiler evaluate: (ReadStream on: '1 + 2')
These all work as you expect, but
s := ReadWriteStream on: ''.
s nextPutAll: '1 + 2'.
Compiler evaluate: s
yields nil. (Why?) Simpify, always simpllfy. Before assuming that the
problem is Compiler love, consider whether it may be stream loving.
Compare the results of:
(ReadStream on: '1 + 2') nextLine '123'
and
s := ReadWriteStream on: ''.
s nextPutAll: '1 + 2'.
s nextLine nil
This is the problem! To confirm, note that:
Compiler evaluate: nil nil
Why did ReadWriteStream do that? It has to do with the nuances of
ReadWriteStream, whose "position" is set after writing to the END of the
stream for more writing. If you want to read what you have written to
such a stream, you need to reset it. thus:
s := ReadWriteStream on: ''.
s nextPutAll: '1 + 2'.
s reset.
s nextLine '1 + 2'
and hence:
s := ReadWriteStream on: ''.
s nextPutAll: '1 + 2'.
s reset.
Compiler evaluate: s 3
More information about the Squeak-dev
mailing list
|