1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-17 17:50:24 +03:00
mal/impls/scala/env.scala
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
1000 B
Scala

import types._list
import scala.collection.mutable
object env {
class Env(outer: Env = null,
binds: Iterator[Any] = null,
exprs: Iterator[Any] = null) {
val data: mutable.Map[Symbol, Any] = mutable.Map()
if (binds != null && exprs != null) {
binds.foreach(b => {
val k = b.asInstanceOf[Symbol]
if (k == '&) {
data(binds.next().asInstanceOf[Symbol]) = _list(exprs.toSeq:_*)
} else {
data(k) = exprs.next()
}
})
}
def find(key: Symbol): Env = {
if (data.contains(key)) {
this
} else if (outer != null) {
outer.find(key)
} else {
null
}
}
def set(key: Symbol, value: Any): Any = {
data(key) = value
value
}
def get(key: Symbol): Any = {
val env = find(key)
if (env == null) throw new Exception("'" + key.name + "' not found")
env.data(key)
}
}
}
// vim:ts=2:sw=2