mirror of
https://github.com/peklaiho/madlisp.git
synced 2024-11-22 21:35:03 +00:00
34 lines
765 B
PHP
34 lines
765 B
PHP
<?php
|
|
namespace MadLisp;
|
|
|
|
class Env extends Hash
|
|
{
|
|
protected ?Env $parent;
|
|
|
|
public function __construct(?Env $parent = null)
|
|
{
|
|
$this->parent = $parent;
|
|
}
|
|
|
|
public function get(string $key)
|
|
{
|
|
if ($this->has($key)) {
|
|
return $this->data[$key];
|
|
} elseif ($this->parent) {
|
|
return $this->parent->get($key);
|
|
}
|
|
|
|
throw new MadLispException("symbol $key not defined in env");
|
|
}
|
|
|
|
public function set(string $key, $value)
|
|
{
|
|
// Do not allow overwriting values in root env
|
|
if ($this->has($key) && $this->parent == null) {
|
|
throw new MadLispException("attempt to overwrite $key in root env");
|
|
}
|
|
|
|
return parent::set($key, $value);
|
|
}
|
|
}
|