Quick actions

cmd+k|ctrl+k

Navigation

Languages

Untitled

Snippet info

Language

Raku

Visibility

unlisted

Author

legacy-anonymous

Created

2025-11-14T11:50:02.40917Z

Updated

2025-11-14T11:50:02.40917Z

#!/usr/bin/env raku

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

grammar Simple {
    token TOP { <statement>* %% \n || <comment> || <code> }
    token statement { <comment> || <loop> || <assignement> || <call> }
    token comment { '#' \N* }
    regex loop { 'for' \s+ $<cond>=<code> \n+ $<body> = <statement>* \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>} // 0 } }
}

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

my $code = Simple.parse: q:to/LANG/;
n = 5
z = 0
for z
end
LANG
say $code;
say %VARS;
INFO