diff options
| author | Joel Martin <github@martintribe.org> | 2015-01-08 23:25:40 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-01-08 23:25:40 -0600 |
| commit | 9d42904e47c50c5ff2306da04993b2a32bc9cd16 (patch) | |
| tree | e1b2d46a232e6573dc2c185967ebe988be3db973 /lua/env.lua | |
| parent | fd888612ca589d7e1a46c36fc3fe12aed126f6a8 (diff) | |
| download | mal-9d42904e47c50c5ff2306da04993b2a32bc9cd16.tar.gz mal-9d42904e47c50c5ff2306da04993b2a32bc9cd16.zip | |
Lua: all steps and self-hosting.
Also some misc docs/TODO updates.
Diffstat (limited to 'lua/env.lua')
| -rw-r--r-- | lua/env.lua | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lua/env.lua b/lua/env.lua new file mode 100644 index 0000000..ee19c90 --- /dev/null +++ b/lua/env.lua @@ -0,0 +1,53 @@ +local rex = require('rex_pcre') +local string = require('string') +local table = require('table') +local utils = require('utils') +local types = require('types') + +local Env = {} + +function Env:new(outer, binds, exprs) + 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 + local new_exprs = types.List:new() + for j = i, #exprs do + table.insert(new_exprs, exprs[j]) + end + table.remove(exprs, 1) + data[binds[i+1].val] = new_exprs + break + end + data[binds[i].val] = exprs[i] + end + end + return setmetatable(newObj, self) +end +function Env:find(sym) + if self.data[sym.val] ~= nil then + return self + else + if self.outer ~= nil then + return self.outer:find(sym) + else + return nil + end + end +end +function Env:set(sym,val) + self.data[sym.val] = val + return val +end +function Env:get(sym) + local env = self:find(sym) + if env then + return env.data[sym.val] + else + types.throw("'"..sym.val.."' not found") + end +end + +return Env |
