From b6d3cbd61cd08e39d20a569b7c5ece6bb25897dd Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Tue, 1 Jan 2019 15:05:58 -0800 Subject: Misc changes, including text indented interpolations are now indented relative to the text, not the opening '("', code objects can now remove all free vars, the REPL uses global vars. Error API is changing a bit. --- code_obj.lua | 51 ++++++++++++++++++++++++++++----------------------- 1 file changed, 28 insertions(+), 23 deletions(-) (limited to 'code_obj.lua') diff --git a/code_obj.lua b/code_obj.lua index 5afca1d..a09608f 100644 --- a/code_obj.lua +++ b/code_obj.lua @@ -320,6 +320,10 @@ do return self:dirty() end, remove_free_vars = function(self, vars) + if vars == nil then + vars = nil + end + vars = vars or self:get_free_vars() if not (#vars > 0) then return end @@ -351,33 +355,34 @@ do end return self:dirty() end, - declare_locals = function(self, to_declare) - if to_declare == nil then - to_declare = nil - end - if to_declare == nil then - local seen - to_declare, seen = { }, { } - local gather_from - gather_from = function(self) - local _list_0 = self.free_vars - for _index_0 = 1, #_list_0 do - local var = _list_0[_index_0] - if not (seen[var]) then - seen[var] = true - to_declare[#to_declare + 1] = var - end + get_free_vars = function(self) + local vars, seen = { }, { } + local gather_from + gather_from = function(self) + local _list_0 = self.free_vars + for _index_0 = 1, #_list_0 do + local var = _list_0[_index_0] + if not (seen[var]) then + seen[var] = true + vars[#vars + 1] = var end - local _list_1 = self.bits - for _index_0 = 1, #_list_1 do - local bit = _list_1[_index_0] - if not (type(bit) == 'string') then - gather_from(bit) - end + end + local _list_1 = self.bits + for _index_0 = 1, #_list_1 do + local bit = _list_1[_index_0] + if not (type(bit) == 'string') then + gather_from(bit) end end - gather_from(self) end + gather_from(self) + return vars + end, + declare_locals = function(self, to_declare) + if to_declare == nil then + to_declare = nil + end + to_declare = to_declare or self:get_free_vars() if #to_declare > 0 then self:remove_free_vars(to_declare) self:prepend("local " .. tostring(concat(to_declare, ", ")) .. ";\n") -- cgit v1.2.3