1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 10:07:45 +03:00
mal/perl6/env.pm
Hinrik Örn Sigurðsson a708140106 Add Perl 6 implementation
All tests pass, but readline support (via Linenoise module) is commented
out in step0_repl.pl as it is not a core module. Should maybe change it
when docker support is added.
2016-06-11 15:02:06 +00:00

37 lines
713 B
Raku

unit class MalEnv;
use types;
has $.outer;
has %.data;
has @.binds;
has @.exprs;
method new ($outer?, @binds?, @exprs?) {
self.bless(:$outer, :@binds, :@exprs);
}
submethod BUILD (:@!binds, :@!exprs, :$!outer, :%!data) {
for @!binds.kv -> $idx, $key {
if $key eq '&' {
my $value = MalList([@!exprs[$idx..*]]);
self.set(@!binds[$idx+1], $value);
last;
}
my $value = @!exprs[$idx];
self.set($key, $value);
}
}
method set ($key, $value) {
%.data{$key} = $value;
}
method find ($key) {
return %.data{$key} ?? self !! $.outer && $.outer.find($key);
}
method get ($key) {
my $env = self.find($key) or die X::MalNotFound.new(name => $key);
return $env.data{$key};
}