secret
Last active

Simple evaluator in Perl 6

  • Download Gist
evaluate.p6
Perl6
1 2 3 4 5 6 7 8 9 10
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);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.