1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 22:58:00 +03:00
mal/impls/perl/env.pm
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

64 lines
1.4 KiB
Perl

use strict;
use warnings;
use Exporter 'import';
{
package Mal::Env;
use Data::Dumper;
sub new {
my ($class,$outer,$binds,$exprs) = @_;
my $data = { __outer__ => $outer };
if ($binds) {
my @expr = @$exprs;
foreach my $bind (@$binds) {
if ($$bind eq "&") {
# variable length arguments
@expr = (Mal::List->new([@expr]));
next;
}
$data->{$$bind} = shift @expr;
}
}
bless $data => $class
}
sub find {
my ($self, $key) = @_;
if (exists $self->{$$key}) { return $self; }
elsif ($self->{__outer__}) { return $self->{__outer__}->find($key); }
else { return undef; }
}
sub set {
my ($self, $key, $value) = @_;
$self->{$$key} = $value;
return $value
}
sub get {
my ($self, $key) = @_;
my $env = $self->find($key);
die "'$$key' not found\n" unless $env;
return $env->{$$key};
}
}
#my $e1 = Mal::Env->new();
#print Dumper($e1);
#
#my $e2 = Mal::Env->new();
#$e2->set('abc', 123);
#$e2->set('def', 456);
#print Dumper($e2);
#
#my $e3 = Mal::Env->new($e2);
#$e3->set('abc', 789);
#$e3->set('ghi', 1024);
#print Dumper($e3);
#
#print Dumper($e3->find('abc'));
#print Dumper($e3->get('abc'));
#print Dumper($e3->find('def'));
#print Dumper($e3->get('def'));
1;