madlisp/src/Env.php

45 lines
938 B
PHP
Raw Normal View History

2020-05-28 04:55:58 +00:00
<?php
namespace MadLisp;
class Env extends Hash
{
2020-06-06 08:31:09 +00:00
protected string $name;
2020-05-28 04:55:58 +00:00
protected ?Env $parent;
2020-06-06 08:31:09 +00:00
public function __construct(string $name, ?Env $parent = null)
2020-05-28 04:55:58 +00:00
{
2020-06-06 08:31:09 +00:00
$this->name = $name;
2020-05-28 04:55:58 +00:00
$this->parent = $parent;
}
2020-06-06 08:31:09 +00:00
public function getFullName(): string
{
if ($this->parent) {
return $this->parent->getFullName() . '/' . $this->name;
}
return $this->name;
}
2020-05-28 04:55:58 +00:00
public function get(string $key)
{
if (array_key_exists($key, $this->data)) {
2020-05-28 04:55:58 +00:00
return $this->data[$key];
} elseif ($this->parent) {
return $this->parent->get($key);
}
throw new MadLispException("symbol $key not defined in env");
}
2020-06-04 09:06:56 +00:00
2020-10-24 03:02:50 +00:00
public function getParent(): ?Env
{
return $this->parent;
}
public function getRoot(): ?Env
2020-06-04 09:06:56 +00:00
{
return $this->parent ? $this->parent->getRoot() : $this;
2020-06-04 09:06:56 +00:00
}
2020-05-28 04:55:58 +00:00
}