-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv.lua
37 lines (30 loc) · 811 Bytes
/
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
local interop = require("interop")
local Env = {}
function Env:new(parent, name)
local obj = {parent = parent,
name = name,
vars = {}}
setmetatable(obj, {__index = self})
return obj
end
function Env:def_var_unsafe(forth_name, lua_name)
table.insert(self.vars, {forth_name = forth_name,
lua_name = lua_name})
end
function Env:def_var(name)
local lua_name = interop.sanitize(name)
self:def_var_unsafe(name, lua_name)
return lua_name
end
function Env:has_var(forth_name)
return self:find_var(forth_name) ~= nil
end
function Env:find_var(forth_name)
for i, each in ipairs(self.vars) do
if each.forth_name == forth_name then
return each
end
end
return self.parent and self.parent:find_var(forth_name)
end
return Env