1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 01:57:09 +03:00
mal/impls/r/env.r
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

43 lines
938 B
R

..env.. <- TRUE
if(!exists("..types..")) source("types.r")
new.Env <- function(outer=emptyenv(), binds=list(), exprs=list()) {
e <- structure(new.env(parent=outer), class="Env")
if (length(binds) > 0) {
for(i in seq(length(binds))) {
b <- binds[[i]]
if (b == "&") {
e[[binds[[i+1]]]] <-
slice(exprs, i, length(exprs))
break
} else {
e[[b]] <- exprs[[i]]
}
}
}
e
}
Env.find <- function(e, key) {
if (exists(key, envir=e, inherits=FALSE)) {
e
} else if (!identical(parent.env(e), emptyenv())) {
Env.find(parent.env(e), key)
} else {
nil
}
}
Env.set <- function(e, key, val) {
e[[key]] <- val
invisible(val)
}
Env.get <- function(e, key) {
e <- Env.find(e, key)
if (.nil_q(e)) throw(concat("'", key, "' not found"))
e[[key]]
}