Can you give some basic examples about how to define global variables, assign values to them and then get the values from them?

Since I’m new to Spoofax, these basic instructions will help me a lot.
Thank you!

Asked by Hilal Cora on 24 January 2013 at 22:13

As far as I know you must use a dynamic rule to do global variables. You could define a rule like this:

set-global = ?(name,value) ; rules(get-global: name -> value)

Then you can set/get a global like:

<set-global> ("foo", "bar")
bar := <get-global> "foo"

The docs for dynamic rules are all encompassed in the paper about dynamic rules in spoofax. There an introduction to it in this paper:

http://swerl.tudelft.nl/twiki/pub/Main/TechnicalReports/TUD-SERG-2010-014.pdf

A paper specifically about this feature is here:

http://strategoxt.org/Stratego/ProgramTransformationWithScopedDynamicRewriteRules

Answered by Dobes Vandermeer on 24 January 2013 at 22:34

There is a dynamic rule variant that defines a global variable, using :=

rules(Foo := "bar");
bar := <Foo>

Note that these rules are evaluated when defined instead of when called:

rules( Number := <<debug> "evaluated"; !1> );
<debug> "number:";
<debug> <Number>

outputs

evaluated
number:
1

Origin of this dynamic rule variant: http://eelcovisser.org/post/56/global-variables

Answered by Danny Groenewegen on 24 January 2013 at 23:38

Thanks for your answer.

Answered by Hilal Cora on 25 January 2013 at 08:12

Hi Hilal, maybe better to hit the mailing list with this question.

Answered by Dobes Vandermeer on 25 January 2013 at 21:05