Stratego documentation provides a call() instruction to call a strategy.
I could not use it in Spoofax. Does someone could use it successfully?

My problem: I’ve got a string with the name of a strategy, and I want to call it on some terms.

Asked by Emmanuel Castro on 22 January 2013 at 01:24

You might have to do it by hand, basically something like:

CallNamedRule: ("foo",arg) -> <foo>
CallNamedRule: ("bar",arg) -> <bar>

With one of those for any rule you want to have dynamically callable.

Note that I don’t know whether the rule you mention is supposed to work or not - I just offer this alternative in case it’s not directly supported by the runtime.

Answered by Dobes Vandermeer on 23 January 2013 at 01:24

I don’t think there is any library/API for doing this. You would have to create one yourself.

When using the Stratego interpreter (ctree target), It’s probably easy since the interpreter has a map of all the strategies somewhere. When using generated Java code (jar target), it is harder since strategies are compiled to Java methods. You could try reflection to call these Java methods. There is actually a primitive for calling Java methods, but I don’t know how this works:

Answered by Gabriël Konat on 23 January 2013 at 08:33

There used to be a call primitive in StrategoXT, however this does not seem to be implemented in the java backend:

translate-PrimT :
  CallDynamic(f, s*, t*) -> <fail>
    // They could be supported through the interopcontext,
    // but they're never used anyway
    fatal-err(|"Think again. Dynamic calls are not supported")
Answered by Rob Vermaas on 23 January 2013 at 09:56

test83 of the Stratego Compiler test set shows how the dynamic calls feature of Stratego is used:

module test83
imports libstratego-lib

main =
!“inc” => f ; <call(f||)>1 => 2
; !“apply-it-now” ; call(|inc|2) => 3

DYNAMIC-CALLS = inc ; apply-it-now(id|"")

apply-it-now(s|t) = t

That is, a strategy that can be used for dynamic calls should be ‘declared’ using a dummy call in the definition of the DYNAMIC-CALLS strategy. Then such a strategy can be called using call(name|s1,..,sn|t1,...,tn) where name is a string, the si are strategy arguments, and the ti are term arguments.

According to the Makefile, this test should be run as part of the continuous build of Stratego/XT, which is included in Spoofax. Furthermore, the Stratego editor in Spoofax recognizes the call construct.

Answered by Eelco Visser on 24 January 2013 at 07:55