1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 01:57:09 +03:00
mal/impls/yorick/env.i
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

45 lines
887 B
OpenEdge ABL

require, "hash.i"
require, "types.i"
struct Env {
pointer outer
Hash data
}
func env_new(outer_ptr, binds=, exprs=)
{
env = Env(outer=outer_ptr, data=hash_new())
for (i = 1; i <= numberof(binds); ++i) {
if (binds(i)->val == "&") {
rest_args = numberof(exprs) >= i ? exprs(i:) : []
env_set, env, binds(i + 1)->val, MalList(val=&rest_args)
break
} else {
env_set, env, binds(i)->val, *exprs(i)
}
}
return env
}
func env_find(env, key)
{
if (hash_has_key(env.data, key)) return env
if (is_void(*env.outer)) return nil
return env_find(*env.outer, key)
}
func env_get(env, key)
{
found_env = env_find(env, key)
if (is_void(found_env)) return MalError(message=("'" + key + "' not found"))
return hash_get(found_env.data, key)
}
func env_set(&env, key, val)
{
d = env.data
hash_set, d, key, val
env.data = d
return val
}