groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arnaud Estève <arnaud.est...@gmail.com>
Subject Re: AST transformation : return a CurriedClosure
Date Thu, 17 Dec 2015 21:24:55 GMT
Sorry for the noise, I'm answering myself here but I had something working
: (assume originalMethod is the MethodNode you want to curry)

Parameter[] params = originalMethod.parameters
params = params.take(params.size() - 1)
List<ASTNode> nodes = new AstBuilder().buildFromSpec {
   method(originalMethod.name,
org.codehaus.groovy.ast.MethodNode.ACC_PUBLIC, String.class) {
      parameters {
         params.each { Parameter param ->
            parameter "${param.name}": param.type.typeClass
         }
      }
      exceptions {}
      block {
         returnStatement {
            methodCall {
               methodPointer {
                  variable 'this'
                  constant originalMethod.name
               }
               constant 'curry'
               argumentList {
                  params.each {
                     constant it.name
                  }
               }
            }
         }
      }
   }
}


nodes[0] seems indeed to be the original method curried with every
parameter but the last

Pretty hard to understand how the ASTBuilder works but... wow... That's
impressive

Hope this can help someone someday.

On Thu, Dec 17, 2015 at 9:34 PM, Arnaud Estève <arnaud.esteve@gmail.com>
wrote:

> Hi everyone !
>
> I'm currently playing with AST transformations, and the field of
> opportunities is... Well... Huge !
>
> There's just something I just can't figure out how to express properly.
>
>
> Let's imagine the following class :
>
>
> class Greeter {
>
>   void greet(String firstname, String lastname) {
>     "Hello $firstname $lastname"
>   }
>
> }
>
> I'd like to write an AST transformation so that the class becomes :
>
> class Greeter {
>
>   void greet(String firstname, String lastname) {
>     "Hello $firstname $lastname"
>   }
>
>   CurriedClosure greet2(String firstname) { // even better if it's named
> "greet" but... ?
>     this.&greet.curry(firstname)
>   }
>
> }
>
> And I'm not really sure if it's even possible with AST transformations,
> and if I should go with ASTbuilder.buildFromCode or buildFromSpec, or
> anything else ...
>
>
> I'm able to get the methodNode i want to add a method for, no problem, I'm
> able to add a method to a class, no problem.
>
> The problem is just creating the definition of the AST method.
>
> If someone has any pointer I can look at to give me an idea, don't
> hesitate.
>
>
> Thanks a lot, and keep up the good work !
>
> Arnaud.
>

Mime
View raw message