Adding a statement programmatically

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
11 messages Options
Reply | Threaded
Open this post in threaded view
|

Adding a statement programmatically

Sean P. DeNigris
Administrator
What's the easiest way to add a statement to an existing method?

In the case I'm thinking about, it's going at the end, so I can cheat and just recompile with the string appended, but I'd like to know how to insert into an arbitrary place in the AST...

Thanks.
Sean
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

Noury Bouraqadi-2
Hi Sean,

Did you thought about:
-using method wrappers?
-object as methods. An object o put in a method dictionary instead of some method with selector #s. When #s is looked up, o  receives the message
        run: selector with: args in: originalReceiver
-proxies (look at Ghost) so you introduce an indirection to perform what ever action you want before performing the actual code

Noury
On 7 mars 2012, at 21:12, Sean P. DeNigris wrote:

> What's the easiest way to add a statement to an existing method?
>
> In the case I'm thinking about, it's going at the end, so I can cheat and
> just recompile with the string appended, but I'd like to know how to insert
> into an arbitrary place in the AST...
>
> Thanks.
> Sean
>
> --
> View this message in context: http://forum.world.st/Adding-a-statement-programmatically-tp4454447p4454447.html
> Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
>

Noury
--
http://twitter.com/#!/NouryBouraqadi
http://www.kroobe.com/profile/noury


Afin de contribuer au respect de l'environnement,
merci de n'imprimer ce courriel qu'en cas de necessite

Please consider the environment before you print




Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

Philippe Marschall-2
In reply to this post by Sean P. DeNigris
On 03/07/2012 09:12 PM, Sean P. DeNigris wrote:
> What's the easiest way to add a statement to an existing method?
>
> In the case I'm thinking about, it's going at the end, so I can cheat and
> just recompile with the string appended, but I'd like to know how to insert
> into an arbitrary place in the AST...

Reflectivity [1]?

  [1] http://scg.unibe.ch/research/reflectivity

Cheers
Philippe


Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

Marcus Denker-4
In reply to this post by Sean P. DeNigris

On Mar 8, 2012, at 12:21 PM, Noury Bouraqadi wrote:

> Hi Sean,
>
> Did you thought about:
> -using method wrappers?
> -object as methods. An object o put in a method dictionary instead of some method with selector #s. When #s is looked up, o  receives the message
> run: selector with: args in: originalReceiver
> -proxies (look at Ghost) so you introduce an indirection to perform what ever action you want before performing the actual code
>
-> Compile the code to an RB AST, this then has all the interfaces needed to replace nodes or add nodes.
You can then pretty print it back to source.

        Marcus


--
Marcus Denker -- http://marcusdenker.de


Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

Sean P. DeNigris
Administrator
Marcus Denker-4 wrote
-> Compile the code to an RB AST, this then has all the interfaces needed to replace nodes or add nodes.
You can then pretty print it back to source.
Thanks Marcus. That's what I was trying to do. I got the AST and created the node:
    body := (AbstractSound>>#initialize) parseTree body.
    body addNode: (RBParser parseExpression: 'envelopes := nil').

But then I couldn't figure out how to get the source back...

"These both returned the statements without the header"
body formattedCode.
body newSource.

"This returned the full method, but without the added node"
body source
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

NorbertHartl

Am 08.03.2012 um 17:08 schrieb Sean P. DeNigris:

>
> Marcus Denker-4 wrote
>>
>> -> Compile the code to an RB AST, this then has all the interfaces needed
>> to replace nodes or add nodes.
>> You can then pretty print it back to source.
>>
>
> Thanks Marcus. That's what I was trying to do. I got the AST and created the
> node:
>    body := (AbstractSound>>#initialize) parseTree body.
>    body addNode: (RBParser parseExpression: 'envelopes := nil').
>
> But then I couldn't figure out how to get the source back...
>
> "These both returned the statements without the header"
> body formattedCode.
> body newSource.
>
> "This returned the full method, but without the added node"
> body source
>
Sean,

you extracted the body first and operate on it. So you only have the body. Try

method := (AbstractSound>>#initialize) parseTree.
method body addNode: (RBParser parseExpression: 'envelopes := nil').
method formattedCode

Norbert
Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

Sean P. DeNigris
Administrator
Norbert Hartl wrote
Try...
Thanks Norbert. Same result.
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

NorbertHartl


Am 08.03.2012 um 20:05 schrieb "Sean P. DeNigris" <[hidden email]>:

>
> Norbert Hartl wrote
>>
>> Try...
>
> Thanks Norbert. Same result.
>
Does not work? What do you mean by saying "same result"?

Norbert
> --
> View this message in context: http://forum.world.st/Adding-a-statement-programmatically-tp4454447p4457352.html
> Sent from the Pharo Smalltalk mailing list archive at Nabble.com.
>

Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

hernanmd
In reply to this post by Sean P. DeNigris
| parseTree resultNode searchTree |

parseTree := RBParser parseMethod: 'renderContentOn: aRenderer

    aRenderer div id: ''id1'';
        with: [
            aRenderer div
                    id: ''idInner1'';
                    render: ''text1'' ].'.
               
searchTree := RBParser parseExpression: 'aRenderer div
                    id: ''idInner1'';
                    render: ''text1''.'.
       
resultNode := ( RBParseTreeSearcher new
    matchesTree: searchTree
    do: [ :aNode :answer | aNode parent ] ) executeTree: parseTree.

resultNode addNode: (RBParser parseExpression: 'envelopes := nil').
parseTree formattedCode.


2012/3/7 Sean P. DeNigris <[hidden email]>
What's the easiest way to add a statement to an existing method?

In the case I'm thinking about, it's going at the end, so I can cheat and
just recompile with the string appended, but I'd like to know how to insert
into an arbitrary place in the AST...

Thanks.
Sean

--
View this message in context: http://forum.world.st/Adding-a-statement-programmatically-tp4454447p4454447.html
Sent from the Pharo Smalltalk mailing list archive at Nabble.com.




--
Hernán Morales
Information Technology Manager,
Institute of Veterinary Genetics.
National Scientific and Technical Research Council (CONICET).
La Plata (1900), Buenos Aires, Argentina.
Telephone: +54 (0221) 421-1799.
Internal: 422
Fax: 425-7980 or 421-1799.

Reply | Threaded
Open this post in threaded view
|

Re: Adding a statement programmatically

Sean P. DeNigris
Administrator
In reply to this post by NorbertHartl
Norbert Hartl wrote
method := (AbstractSound>>#initialize) parseTree.
method body addNode: (RBParser parseExpression: 'envelopes := nil').
method formattedCode
...
Does not work? What do you mean by saying "same result"?
It did work (one year later, lol)! My last statement was mistakenly "body formattedCode" instead of "method formattedCode"
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: [Pharo-dev] Adding a statement programmatically

NorbertHartl


Am 06.06.2013 um 22:18 schrieb "Sean P. DeNigris" <[hidden email]>:

> Norbert Hartl wrote
>> method := (AbstractSound>>#initialize) parseTree.
>> method body addNode: (RBParser parseExpression: 'envelopes := nil').
>> method formattedCode
>> ...
>> Does not work? What do you mean by saying "same result"?
>
> It did work (one year later, lol)! My last statement was mistakenly "body
> formattedCode" instead of "method formattedCode"

:)

Norbert
>
> -----
> Cheers,
> Sean
> --
> View this message in context: http://forum.world.st/Adding-a-statement-programmatically-tp4454447p4692030.html
> Sent from the Pharo Smalltalk Developers mailing list archive at Nabble.com.
>