Skip to content

Instantly share code, notes, and snippets.

@masak
Last active October 10, 2015 02:47
Show Gist options
  • Save masak/ca5a82ae76951cc387cc to your computer and use it in GitHub Desktop.
Save masak/ca5a82ae76951cc387cc to your computer and use it in GitHub Desktop.
Simple evaluator in Perl 6
multi ev(Pair ( :$key where 'number', :value($n) )) { $n }
multi ev(Pair ( :$key where 'add', :$value [$l, $r] )) { ev($l) + ev($r) }
multi ev(Pair ( :$key where 'multiply', :$value [$l, $r] )) { ev($l) * ev($r) }
multi ev(Pair ( :$key where 'variable', :value($name) )) { %*env{$name} }
my %*env = a => 3, b => 4, c => 5;
my $tree = :add[:variable<a>, :multiply[:number(2), :variable<b>]];
say ev($tree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment