-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathenv.lua
56 lines (50 loc) · 1.29 KB
/
env.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local table = require('table')
local types = require('types')
local printer = require('printer')
local Env = {}
function Env:new(outer, binds, exprs)
-- binds is a MAL sequence of MAL symbols
-- exprs is an LUA table of MAL forms
local data = {}
local newObj = {outer = outer, data = data}
self.__index = self
if binds then
for i, b in ipairs(binds) do
if binds[i].val == '&' then
data[binds[i+1].val] = types.List.slice(exprs, i)
break
end
data[binds[i].val] = exprs[i]
end
end
return setmetatable(newObj, self)
end
function Env:get(sym)
-- sym is an LUA string
-- returns nil if the key is not found
local env = self
local result
while true do
result = env.data[sym]
if result ~= nil then return result end
env = env.outer
if env == nil then return nil end
end
end
function Env:set(sym,val)
-- sym is an LUA string
self.data[sym] = val
return val
end
function Env:debug()
local env = self
while env.outer ~=nil do
line = ' ENV:'
for k, v in pairs(env.data) do
line = line .. ' ' .. k .. '=' .. printer._pr_str(v)
end
print(line)
env = env.outer
end
end
return Env