2015-05-19 05:54:18 +03:00
|
|
|
import types.MalException
|
|
|
|
import types.MalSymbol
|
|
|
|
|
|
|
|
class env {
|
|
|
|
static class Env {
|
|
|
|
def data
|
|
|
|
def outer
|
|
|
|
|
|
|
|
Env() {
|
|
|
|
outer = null
|
|
|
|
data = [:]
|
|
|
|
}
|
|
|
|
Env(Env outer_env) {
|
|
|
|
outer = outer_env
|
|
|
|
data = [:]
|
|
|
|
}
|
|
|
|
Env(Env outer_env, binds, exprs) {
|
|
|
|
outer = outer_env
|
|
|
|
data = [:]
|
|
|
|
for (int i=0; i<binds.size; i++) {
|
|
|
|
if (binds[i].value == "&") {
|
|
|
|
data[binds[i+1].value] = (exprs.size() > i) ? exprs[i..-1] : []
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
data[binds[i].value] = exprs[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def set(MalSymbol key, def val) {
|
|
|
|
data[key.value] = val
|
|
|
|
}
|
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
def find(String key) {
|
|
|
|
if (data.containsKey(key)) {
|
2015-05-19 05:54:18 +03:00
|
|
|
this
|
|
|
|
} else if (outer != null) {
|
|
|
|
outer.find(key)
|
|
|
|
} else {
|
|
|
|
null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-10 02:15:40 +03:00
|
|
|
def get(String key) {
|
2015-05-19 05:54:18 +03:00
|
|
|
def e = find(key)
|
|
|
|
if (e == null) {
|
2022-01-10 02:15:40 +03:00
|
|
|
throw new MalException("'${key}' not found")
|
2015-05-19 05:54:18 +03:00
|
|
|
} else {
|
2022-01-10 02:15:40 +03:00
|
|
|
e.data.get(key)
|
2015-05-19 05:54:18 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|