-
-
Save masak/ca5a82ae76951cc387cc to your computer and use it in GitHub Desktop.
Simple evaluator in Perl 6
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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