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-12-06 01:26:27 +00:00
|
|
|
public function get(string $key, bool $throw = true)
|
2020-05-28 04:55:58 +00:00
|
|
|
{
|
2020-12-03 02:58:58 +00:00
|
|
|
if (array_key_exists($key, $this->data)) {
|
2020-05-28 04:55:58 +00:00
|
|
|
return $this->data[$key];
|
|
|
|
} elseif ($this->parent) {
|
2020-12-06 01:26:27 +00:00
|
|
|
return $this->parent->get($key, $throw);
|
2020-05-28 04:55:58 +00:00
|
|
|
}
|
|
|
|
|
2020-12-06 01:26:27 +00:00
|
|
|
if ($throw) {
|
|
|
|
throw new MadLispException("symbol $key not defined in env");
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2020-05-28 04:55:58 +00:00
|
|
|
}
|
2020-06-04 09:06:56 +00:00
|
|
|
|
2020-10-24 03:02:50 +00:00
|
|
|
public function getParent(): ?Env
|
|
|
|
{
|
|
|
|
return $this->parent;
|
|
|
|
}
|
|
|
|
|
2020-10-24 05:27:28 +00:00
|
|
|
public function getRoot(): ?Env
|
2020-06-04 09:06:56 +00:00
|
|
|
{
|
2020-10-24 05:27:28 +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
|
|
|
}
|