Raku Books / Creating a Compiler in Raku / Working on Grammar

Get rid of globals

For storing variable values, we are using a global hash %var. Let’s make it more elegant and move it to the actions class as a data member.

class LinguaActions {
    has %!var;

    . . .
}

Of course, you should replace all occurrences of %var to %!var now, for example, in the assignment action (there are three more such places in the LinguaActions class):

method assignment($/) {
    %!var{~$<variable-name>} = $<expression>.made;
}

And finally, as we need a place for the hash, you need to instantiate the actions class before calling the parse method:

my $result = Lingua.parse($code,
                          :actions(LinguaActions.new));

Course navigation

Using multi-rules   |   Better variable names