Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
metaprogramming in Smalltalk
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  11 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Carter  
View profile  
 More options Apr 8 2008, 11:42 am
Newsgroups: comp.lang.smalltalk
From: Carter <carterch...@gmail.com>
Date: Tue, 8 Apr 2008 08:42:45 -0700 (PDT)
Local: Tues, Apr 8 2008 11:42 am
Subject: metaprogramming in Smalltalk
I apologize if this is a bit of a newbie question but I was curious
about the capabilities of the Smalltalk language and how close it
might be to something like LISP/Scheme. Is it possible within the
Smalltalk language to write code which generates code. Is it possible
for example to generate a completely arbitrary object at runtime?

Thanks in advance,


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Randal L. Schwartz  
View profile  
 More options Apr 8 2008, 12:41 pm
Newsgroups: comp.lang.smalltalk
From: mer...@stonehenge.com (Randal L. Schwartz)
Date: Tue, 08 Apr 2008 09:41:46 -0700
Local: Tues, Apr 8 2008 12:41 pm
Subject: Re: metaprogramming in Smalltalk

>>>>> "Carter" == Carter  <carterch...@gmail.com> writes:

Carter> I apologize if this is a bit of a newbie question but I was curious
Carter> about the capabilities of the Smalltalk language and how close it
Carter> might be to something like LISP/Scheme. Is it possible within the
Carter> Smalltalk language to write code which generates code. Is it possible
Carter> for example to generate a completely arbitrary object at runtime?

Yes, probably easier than most languages, in fact.  The classic ST80 image,
which is still living inside Squeak (www.squeak.org) has the entire compiler
written in Smalltalk.

You can even create anonymous classes and give them behavior, then instantiate
them.  For example:

       | myClass myInstance |
       myClass := Behavior new. "create anon behavior"
       myClass compile: 'theAnswer ^42'. "add a method for instances"
       myInstance := myClass new. "create an instance"
       Transcript show: myInstance theAnswer; cr. "shows 42"

So, yeah.  metaprogramming is definitely Smalltalk's playground.

Just another Smalltalk hacker,
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans-Martin Mosner  
View profile  
 More options Apr 8 2008, 3:11 pm
Newsgroups: comp.lang.smalltalk
From: Hans-Martin Mosner <h...@heeg.de>
Date: Tue, 8 Apr 2008 19:11:52 +0000 (UTC)
Local: Tues, Apr 8 2008 3:11 pm
Subject: Re: metaprogramming in Smalltalk
mer...@stonehenge.com (Randal L. Schwartz) wrote:

> So, yeah.  metaprogramming is definitely Smalltalk's playground.

Although one must admit that in Lisp, you would generate code by
manipulating list structures, while in Smalltalk, you manipulate
strings to generate source code. So Lisp has a very natural and direct
way of dealing with program structures.
If more involved symbolic manipulation of Smalltalk methods is needed,
the Refactoring Browser with its parse nodes and transformations is the
tool of choice.

Cheers,
Hans-Martin
--


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tim M  
View profile  
 More options Apr 8 2008, 8:03 pm
Newsgroups: comp.lang.smalltalk
From: Tim M <365n...@gmail.com>
Date: Wed, 9 Apr 2008 00:03:05 +0000 (UTC)
Local: Tues, Apr 8 2008 8:03 pm
Subject: Re: metaprogramming in Smalltalk
Hi Hans-Martin Mosner,

> If more involved symbolic manipulation of Smalltalk methods is needed,
> the Refactoring Browser with its parse nodes and transformations is
> the tool of choice.

Its a good point - and the parse node support is all there as well - however
as programmers I think its possibly easier to thing of lines of code. But
if you want to get grungy I have examples like the following:

patch: aClass selector: aSelectorSymbol prepending: aStringNewStatement identifiedBy:
aSymbol
        "Append a statement to the end of a method, or just before the last return
(^) if one exists"

        | postNode methodParseTree patched |
        methodParseTree := aClass parseTreeFor: aSelectorSymbol.

        postNode := SmalltalkParser parseExpression: aStringNewStatement.

        self addPatchedNode: postNode firstIn: methodParseTree body identifiedBy:
aSymbol.

        patched  := self compileChangesFor: aSelectorSymbol in: aClass with: methodParseTree.

where #addPatchedNode:... ultimately calls a method like this:

addCommentNodeFor: postNode in: node identifiedBy: aSymbol
        aSymbol notNil ifTrue: [node addNode: (StLiteralNode value: aSymbol) before:
postNode]


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bernd Elkemann  
View profile  
 More options Apr 9 2008, 9:18 am
Newsgroups: comp.lang.smalltalk
From: Bernd Elkemann <elkem...@web.de>
Date: Wed, 09 Apr 2008 15:18:40 +0200
Local: Wed, Apr 9 2008 9:18 am
Subject: Re: metaprogramming in Smalltalk
Carter schrieb:
 > I apologize if this is a bit of a newbie question but I was curious
 > about the capabilities of the Smalltalk language and how close it
 > might be to something like LISP/Scheme. Is it possible within the
 > Smalltalk language to write code which generates code. Is it possible
 > for example to generate a completely arbitrary object at runtime?

You already received some very technical info about how code is
generated at runtime but since you are new to smalltalk here the info
that is most interesting for you:
First get Squeak from squeak.org to try the following examples.

In Smalltalk everything is an object, even classes.
Everything can be looked at and manipulated at runtime.
For example in a workspace write the following in a fresh line:
    Morph new explore.
and then do-it by pressing alt-d.
you have an explorer on the new Morph object.
you can manipulate it at runtime: write in the textpane of the explorer:
    self color: Color red.       then do-it by pressing alt-d.
Adding dynamic behavior:  again in the text-pane of the explorer
self on: #mouseUp send: #value to: [self color: Color random].
you have added behavior to your already-in-existence-object.

"generating code at runtime" is very easy.
The Funny thing is e.g. in squeak you are always(!) doing exactly that,
when writing code (a method or a block(lambda)) you are in fact inside a
running smalltalk program, the squeak-image.
All Behavior you want an object to have is defined in its class (and
superclasses), so if you want to add behavior(code) you can use a
class-browser BUT if you e.g. really want to as you put it "write code
which generates code" you can do the following: write a class-name,
write compile:, then write a string like so: (in any text-panel, then do-it)
Object compile: 'sayHi Speaker man say: ''hi'' '.
You have just created a method sayHi that makes all objects able to
respond to a message sayHi. e.g. try:
1 sayHi
The number one just said hi through your speakers.
This was the specific example of how you translate (at runtime) a string
into code for dynamic behavior of objects.
You can change the String into whatever behavior you want.

Greetings! Have Fun!


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
daredevil  
View profile  
 More options Apr 9 2008, 10:15 am
Newsgroups: comp.lang.smalltalk
From: daredevil <hithac...@gmail.com>
Date: Wed, 9 Apr 2008 07:15:38 -0700 (PDT)
Local: Wed, Apr 9 2008 10:15 am
Subject: Re: metaprogramming in Smalltalk
On Apr 9, 6:18 pm, Bernd Elkemann <elkem...@web.de> wrote:

Is there any performance diffrence between 'generating code at runtime
using #compile:' and using Macros in Lisp ?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
florin.mat...@gmail.com  
View profile  
 More options Apr 9 2008, 12:36 pm
Newsgroups: comp.lang.smalltalk
Followup-To: comp.lang.smalltalk
From: florin.mat...@gmail.com
Date: Wed, 9 Apr 2008 09:36:48 -0700 (PDT)
Local: Wed, Apr 9 2008 12:36 pm
Subject: Re: metaprogramming in Smalltalk
On Apr 8, 11:42 am, Carter <carterch...@gmail.com> wrote:

> I apologize if this is a bit of a newbie question but I was curious
> about the capabilities of the Smalltalk language and how close it
> might be to something like LISP/Scheme. Is it possible within the
> Smalltalk language to write code which generates code. Is it possible
> for example to generate a completely arbitrary object at runtime?

> Thanks in advance,

One other possibility that has not been mentioned in the previous
posts, relies not on the compiler and string or parse nodes
manipulation, but on the reflection capabilities.

Starting from Randal's example, let us assume that now you want
another anonymous class with the same behavior. Instead of repeating
the code that invokes the compiler for it, you could do:

       | myClass myInstance anotherClass |
       myClass := Behavior new. "create anon behavior"
       myClass compile: 'theAnswer ^42'. "add a method for instances"
       myInstance := myClass new. "create an instance"
       Transcript show: myInstance theAnswer; cr. "shows 42"

       anotherClass := Behavior new.
       anotherClass addSelectorSilently: #theAnswer withMethod:
          ((CompiledMethod newFrom: (myClass compiledMethodAt:
#theAnswer))
                                                        methodClass:
anotherClass).
       Transcript show: anotherClass new theAnswer; cr.

If you want, you can change the selector for the method in the new
class to #theOtherAnswer.You can even be fancier and change what the
method #theOtherAnswer returns (e.g. to 43).

       | myClass myInstance anotherClass |
       myClass := Behavior new. "create anon behavior"
       myClass compile: 'theAnswer ^42'. "add a method for instances"
       myInstance := myClass new. "create an instance"
       Transcript show: myInstance theAnswer; cr. "shows 42"

        anotherClass := Behavior new.
       anotherClass addSelectorSilently: #theOtherAnswer withMethod:
         ((CompiledMethod newFrom: (myClass compiledMethodAt:
#theAnswer))
                                                       methodClass:
anotherClass;
                                                       selector:
#theOtherAnswer;
                                                       objectAt: 2
put: 43;
                                                       yourself).
       Transcript show: anotherClass new theOtherAnswer; cr.

Although the above example still uses the compiler to to provide the
prototype method, I hope that by now it is obvious that we can get rid
of it and create the compiled method directly from a sequence of
bytes, instead of copying it from an existing one. This would also
take care of any performance issue caused by invoking the compiler. Of
course, this just moves the problem from source manipulation to
bytecode manipulation, which is not necessarily preferable, but it
shows another way of doing it

Florin


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
scgb...@gmail.com  
View profile  
 More options Apr 9 2008, 7:25 pm
Newsgroups: comp.lang.smalltalk
From: scgb...@gmail.com
Date: Wed, 9 Apr 2008 16:25:33 -0700 (PDT)
Local: Wed, Apr 9 2008 7:25 pm
Subject: Re: metaprogramming in Smalltalk
On Apr 8, 5:42 pm, Carter <carterch...@gmail.com> wrote:

> I apologize if this is a bit of a newbie question but I was curious
> about the capabilities of the Smalltalk language and how close it
> might be to something like LISP/Scheme. Is it possible within the
> Smalltalk language to write code which generates code. Is it possible
> for example to generate a completely arbitrary object at runtime?

> Thanks in advance,

It is even possible to set an object to be its own class, and hence
get prototype-oriented instances running under Squeak.

oroboros := Class new.
oroboros superclass: Class.
oroboros methodDictionary: MethodDictionery new.
oroboros setFormat: Class format.
oroboros primitiveChangeClassTo: oroboros basicNew
oroboros class = oroboros --> true

To learn more goto http://smallwiki.unibe.ch/adriankuhn/protalk/

cheers,
AA


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Carter  
View profile  
 More options Apr 10 2008, 9:07 am
Newsgroups: comp.lang.smalltalk
From: Carter <carterch...@gmail.com>
Date: Thu, 10 Apr 2008 06:07:24 -0700 (PDT)
Local: Thurs, Apr 10 2008 9:07 am
Subject: Re: metaprogramming in Smalltalk
Thanks everyone for the replies. It more than answers my initial
question.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Hans-Martin Mosner  
View profile  
 More options Apr 12 2008, 3:19 pm
Newsgroups: comp.lang.smalltalk
From: Hans-Martin Mosner <h...@heeg.de>
Date: Sat, 12 Apr 2008 19:19:33 +0000 (UTC)
Local: Sat, Apr 12 2008 3:19 pm
Subject: Re: metaprogramming in Smalltalk

daredevil <hithac...@gmail.com> wrote:
> Is there any performance diffrence between 'generating code at runtime
> using #compile:' and using Macros in Lisp ?

These are different things.
Macros in Lisp are evaluated/expanded at compile time, so they're not
comparable to generating code at runtime. The #compile: mechanism is
probably a bit more complex since the whole compiler machinery is
involved, but the resultant code is executed with the same performance
as any other code in the system (in fact, all other code has been
compiled using exactly that mechanism...)

Cheers,
Hans-Martin
--


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
daredevil  
View profile  
 More options Apr 14 2008, 2:04 am
Newsgroups: comp.lang.smalltalk
From: daredevil <hithac...@gmail.com>
Date: Sun, 13 Apr 2008 23:04:07 -0700 (PDT)
Local: Mon, Apr 14 2008 2:04 am
Subject: Re: metaprogramming in Smalltalk
On Apr 13, 12:19 am, Hans-Martin Mosner <h...@heeg.de> wrote:

> daredevil <hithac...@gmail.com> wrote:
> > Is there any performance diffrence between 'generating code at runtime
> > using #compile:' and using Macros in Lisp ?

> These are different things.
> Macros in Lisp are evaluated/expanded at compile time, so they're not
> comparable to generating code at runtime. The #compile: mechanism is
> probably a bit more complex since the whole compiler machinery is
> involved, but the resultant code is executed with the same performance
> as any other code in the system (in fact, all other code has been
> compiled using exactly that mechanism...)

> Cheers,
> Hans-Martin
> --

Thanks, that is what i wanted to know.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »