[GOODIE] StringCompiler (was: Blocks from strings)
David T. Lewis
lewis at mail.msen.com
Wed Dec 31 01:54:43 UTC 2003
I'm quite sure that this is a Really Bad Idea for some reason or other, but I
can't exactly explain why so I guess I'll go ahead and post it. This is the
example that Ned posted earlier today, wrapped in a utility class. It provides
a convenient way to create blocks from source strings.
Change Set: CompileString-dtl
Date: 30 December 2003
Author: David T. Lewis
A utility class for creating a BlockContext from a String, from an example provided by Ned Konz.
To obtain a new BlockContext instance, evaluate:
StringCompiler compileString: '3 + 4'
On Mon, Dec 29, 2003 at 08:49:41PM -0800, Ned Konz wrote:
> On Monday 29 December 2003 8:28 pm, Nevin Pratt wrote:
> > How do I programmatically create a block from a string?
> >
> > The block's context would be the method that is creating the block.
> >
> > Nevin
> >
> >
> > --
> > Nevin Pratt
> > Bountiful Baby
> > http://www.bountifulbaby.com
> > (801) 992-3137
> >
> >
>
> This is kind of an excerpt from some of my code; you can try it in a
> Workspace.
>
> model := nil. "or set to Browser or Workspace, etc."
> stream := ReadWriteStream
> on: (String new: 100).
> stream nextPutAll: '[ 3 + 4 ]'. "your code here in brackets"
> stream reset.
> (model respondsTo: #doItReceiver)
> ifTrue: [FakeClassPool adopt: model selectedClass.
> "Include model pool vars if any"
> rcvr := model doItReceiver.
> ctxt := model doItContext]
> ifFalse: [rcvr := ctxt := nil].
> result := [rcvr class evaluatorClass new
> evaluate: stream
> in: ctxt
> to: rcvr
> notifying: self
> ifFail: [FakeClassPool adopt: nil.
> ^ #failedDoit]]
> on: OutOfScopeNotification
> do: [:ex | ex resume: true].
> FakeClassPool adopt: nil.
>
> result value
-------------- next part --------------
A non-text attachment was scrubbed...
Name: CompileString-dtl.1.cs.gz
Type: application/x-gunzip
Size: 1136 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20031230/e1780b00/CompileString-dtl.1.cs.bin
More information about the Squeak-dev
mailing list
|