madlisp/src/Env.php

55 lines
1.1 KiB
PHP
Raw Normal View History

2020-05-28 04:55:58 +00:00
<?php
2020-12-14 01:49:07 +00:00
/**
* MadLisp language
* @link http://madlisp.com/
* @copyright Copyright (c) 2020 Pekka Laiho
*/
2020-05-28 04:55:58 +00:00
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
{
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-12-17 02:45:37 +00:00
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
}