1
1
mirror of https://github.com/kanaka/mal.git synced 2024-09-20 10:07:45 +03:00
mal/groovy/env.groovy
Joel Martin a9cd654347 Add groovy implementation.
I'm away from my main workstation for a week and unfortunately, I only
copied the code but not the branch with full history so this is just
the implementation. However, the history isn't all that interesting
(mostly just the steps one at a time) and I wanted to get this out
there.
2015-05-18 19:54:18 -07:00

56 lines
1.3 KiB
Groovy

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
}
def find(MalSymbol key) {
if (data.containsKey(key.value)) {
this
} else if (outer != null) {
outer.find(key)
} else {
null
}
}
def get(MalSymbol key) {
def e = find(key)
if (e == null) {
throw new MalException("'${key.value}' not found")
} else {
e.data.get(key.value)
}
}
}
}