Quick actions

cmd+k|ctrl+k

Navigation

Languages

Untitled

Snippet info

Language

Raku

Visibility

unlisted

Author

legacy-anonymous

Created

2025-11-14T11:21:55.549717Z

Updated

2025-11-14T11:21:55.549717Z

#!/usr/bin/env raku

my %VARS;
my %FNS = '+' => { $^a + $^b },
          '-' => { $^a - $^b },
          '*' => { $^a * $^b },
          '/' => { $^a / $^b };

grammar Simple {
    rule TOP { <comment> | <code> | <statement>* %% \n }
    token statement { \s* [ <comment> | <loop> | <assignement> ] }
    token comment { '#' \N* }
    token loop { 'for' \s+ $<cond>=<code> \n+ $<body> = <statement>* \n* \s* 'end'
        { my $cond = $<cond>;
          my @statements = $<statement>;
          while eval($cond) { 
              eval($_) for @statements;
          }
        }
    }
    token assignement { $<identifier> = \w+ \s* '=' \s* <code> {%VARS{$<identifier>} = $<code>.made} }
    token call { $<args> = (\w+ %% \s+) $<fn> = \S+ {say "[call] $<fn> ($<args>)"} }
    token code { $<value> = [\d+|'"' <(<-["]>*)> '"'] {make $<value>} | $<identifier> = \w+ {make %VARS{$<identifier>} } }
}

sub eval($code) {
    say "[code] $code";
    Simple.parse($code)<code>.made;
}

my $code = Simple.parse: q:to/LANG/;
for z
end
a = 2
# foo
b = 3
for z
end
str = "foobar"
n = 5
y = n
z = 0
LANG
say $code;
say %VARS;
INFO