aboutsummaryrefslogtreecommitdiff
path: root/php/env.php
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2014-04-02 22:23:37 -0500
committerJoel Martin <github@martintribe.org>2014-04-02 22:23:37 -0500
commitea81a8087bcd7953b083a2be9db447f75e7ebf56 (patch)
tree6cf47a2dbd55d42efc4a901eaabdec952f40ce89 /php/env.php
parent1617910ad342a55762f3ddabb975849d843cff85 (diff)
downloadmal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.tar.gz
mal-ea81a8087bcd7953b083a2be9db447f75e7ebf56.zip
All: split types into types, env, printer, core.
- types: low-level mapping to the implementation language. - core: functions on types that are exposed directly to mal. - printer: implementation called by pr-str, str, prn, println. - env: the environment implementation - Also, unindent all TCO while loops so that the diff of step4 and step5 are minimized.
Diffstat (limited to 'php/env.php')
-rw-r--r--php/env.php56
1 files changed, 56 insertions, 0 deletions
diff --git a/php/env.php b/php/env.php
new file mode 100644
index 0000000..61bedaf
--- /dev/null
+++ b/php/env.php
@@ -0,0 +1,56 @@
+<?php
+
+require_once 'types.php';
+
+class Env {
+ public $data = array();
+ public $outer = NULL;
+ public function __construct($outer, $binds=NULL, $exprs=NULL) {
+ $this->outer = $outer;
+ if ($binds) {
+ if (_sequential_Q($exprs)) {
+ $exprs = $exprs->getArrayCopy();
+ }
+ for ($i=0; $i<count($binds); $i++) {
+ if ($binds[$i]->value === "&") {
+ if ($exprs !== NULL && $i < count($exprs)) {
+ $lst = call_user_func_array('_list', array_slice($exprs, $i));
+ } else {
+ $lst = _list();
+ }
+ $this->data[$binds[$i+1]->value] = $lst;
+ break;
+ } else {
+ if ($exprs !== NULL && $i < count($exprs)) {
+ $this->data[$binds[$i]->value] = $exprs[$i];
+ } else {
+ $this->data[$binds[$i]->value] = NULL;
+ }
+ }
+ }
+ }
+ }
+ public function find($key) {
+ if (array_key_exists($key, $this->data)) {
+ return $this;
+ } elseif ($this->outer) {
+ return $this->outer->find($key);
+ } else {
+ return NULL;
+ }
+ }
+ public function set($key, $value) {
+ $this->data[$key] = $value;
+ return $value;
+ }
+ public function get($key) {
+ $env = $this->find($key);
+ if (!$env) {
+ throw new Exception("'" . $key . "' not found");
+ } else {
+ return $env->data[$key];
+ }
+ }
+}
+
+?>