adding a method via Parser/Compiler

Peter van Rooijen peter at vanrooijen.com
Sat May 8 13:14:07 UTC 2004


Adrian Lienhard wrote:

> Hi Jakob,
> 
> The problem that the newly compiled method does not show up in the class 
> browser is that it is not classified.

This is not entirely true.

The problem with Jakob's script is twofold:
1) the method is not actually compiled, only parsed (methodNode holds a 
parse tree, not a compiled method)
2) no compiled method is installed in the class

In other words, Jakob's comment:

"create a new method node and add it to TestClass's MethodDictionary"

is incorrect.

The compiler can transform source into a parse tree, and a parse tree 
into a compiled method (with byte codes for the instructions in the 
method). Jakob's scripts only creates the parse tree.

The compiled method then has to be installed in the class (specifically, 
in its method dictionary). Only then will the method be invokable by 
sending a message to an instance of the class in whose method dictionary 
the compiled method has been installed.

Adrian's script performs all the steps outlined above as required to 
completely install the method (and more, like logging to the change 
log). To see exactly what it does, select the script, pop up the context 
menu, and choose 'debug'. You will then get a debugger and be able to 
step through the instructions, using the 'into' and 'over' buttons.

If you lose your way while stepping through the code, just close the 
debugger and try again. Stepping through code is one of the best 
exercises for Smalltalk programmers of any level.

Good luck,

Peter

> Try this to add a new method, which is even simpler than directly 
> calling the compiler:
> 
> TestClass
>     compile:  'showOnTranscript: anObject
>                  Transcript show: anObject.'
>     classified: 'mycategory'
> 
> Cheers,
> Adrian
> 
> 
> On May 7, 2004, at 10:47 AM, Jakob Praher wrote:
> 
>> hi squeakers,
>>
>> finally I got some time to play with the squeak environment - kudos to 
>> you all.
>> Please apologize, but I am a smalltalk beginnner.
>>
>> I wanted to know how to define a new Method on the fly and, thanks to 
>> the terrific inspectors and browsers I found out that the Compiler / 
>> Parser can be used to compile text to parse trees and optionally hang 
>> in methods in classes.
>>
>> "create a new class"
>> Object subclass: #TestClass  .....
>>
>> "create a new method node and add it to TestClass's MethodDictionary"
>> methodNode := Compiler new
>>    compile: (ReadStream on:
>>       'showOnTranscript anObject
>>          Transcript show: anObject.'
>>    in: TestClass
>>    notifying: nil
>>    ifFail: [Smalltalk beep]
>> .
>>
>> This works very well, if I say for instance:
>>
>> TestClass new showOnTranscript: 'hello from showOnTranscript in 
>> TestClass'.
>>
>> I get the results printed on the Transcript.
>> The funny thing is though, that the method is not shown in the 
>> ObjectBrowser/ClassBrowser.
>> But the #subclass method creates a new Entry in the Browser.
>>
>> I have also tried to create a CompiledMethod with the generate method 
>> and hang it in the TestClass manually via Behavior>>addSelector, but 
>> had no luck with that too.
>>
>> so I am just curious, and would like to know, how to do it right :-)
>> -- Jakob
>>
>>
> ___________________
> Adrian Lienhard
> www.adrian-lienhard.ch
> www.netstyle.ch
> 
> 
> 
> 




More information about the Squeak-dev mailing list