Pattern matching on ATerm strategy parameters
I would like to be able to pattern match on ATerm parameters.
eval(|"methpar-1") = is-methodreference eval(|"methpar-2") = methodreference-get-method eval(|"methpar-3") = methodreference-get-params;map(param-get-name) eval(|"methpar-4") = methodreference-get-method
Should be sugar forSubmitted by Daco Harkes on 16 January 2015 at 13:50
eval(|f) = where("methpar-1":=f); is-methodreference eval(|f) = where("methpar-2":=f); methodreference-get-method eval(|f) = where("methpar-3":=f); methodreference-get-params;map(param-get-name) eval(|f) = where("methpar-4":=f); methodreference-get-method
You mean you want to be able to pattern match on term parameters! Strategy parameters are the parameters before the |
Yes, I’ve wanted to do that myself.
This requires an extension of the syntax + a desugaring. Shouldn’t be too hard. And it should work for rules as well, of course.
Yes, sorry for the confusion of calling it both term and strategy parameters. (Note I called it ATerm parameters because the stratego xt api calls it ATerm parameters.)
By the looks of it it is indeed just a syntax extension (it doesnt parse now) + syntactic desugaring.
This feature has now been added to Stratego. See metaborg/strategoxt/pull#14. The syntax is as follows:
You can specify any term as a term parameter to a strategy or rule, not just variables. You can mix-and-match term variables with term patterns:
mystrategy(|"exact", v) = !v
Use a wildcard if you don’t care about the value:
mystrategy(|_) = !()
The syntax works for both rules and strategies:
myrule(|"exact"): x -> x
Strategy application is supported:
mystrategy(|<newname> "a") = !()
…also without a term:
myrule(|<id>): _ -> ()
…which allows negative matching:
mystrategy(|<not(?"x")>) = id
As an example, a switch-case using strategies:
to-english(|1) = !"one" to-english(|2) = !"two" to-english(|_) = !"many"
Thanks Daniël! Can you put this documentation on the website?
Somewhere in the stratego directory; we should get started somehow.
Log in to post comments