1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-11 08:56:41 +03:00
mal/nim/env.nim

26 lines
774 B
Nim
Raw Normal View History

2015-02-28 18:08:36 +03:00
import tables, types
2015-02-28 21:52:01 +03:00
proc initEnv*(outer: Env = nil, binds, exprs: MalType = nilObj): Env =
result = Env(data: initTable[string, MalType](), outer: outer)
if binds.kind in {List, Vector}:
for i, e in binds.list:
2015-03-01 06:38:43 +03:00
if e.str == "&":
2015-03-01 23:46:19 +03:00
result.data[binds.list[i+1].str] = list(exprs.list[i .. -1])
2015-02-28 21:52:01 +03:00
break
else:
2015-03-01 06:38:43 +03:00
result.data[e.str] = exprs.list[i]
2015-02-28 18:08:36 +03:00
proc set*(e: var Env, key: string, value: MalType): MalType {.discardable.} =
e.data[key] = value
value
2015-03-01 08:08:18 +03:00
proc find*(e: Env, key: string): Env =
2015-02-28 18:08:36 +03:00
if e.data.hasKey(key): return e
if e.outer != nil: return e.outer.find(key)
proc get*(e: Env, key: string): MalType =
let env = e.find(key)
if env == nil: raise newException(ValueError, "'" & key & "' not found")
env.data[key]