diff --git a/code_obj.lua b/code_obj.lua index 8a51832..11f78b2 100644 --- a/code_obj.lua +++ b/code_obj.lua @@ -80,6 +80,37 @@ do local _class_0 local _base_0 = { is_code = true, + __tostring = function(self) + if self.__str == nil then + local buff, indent = { }, 0 + local match, gsub, rep + do + local _obj_0 = string + match, gsub, rep = _obj_0.match, _obj_0.gsub, _obj_0.rep + end + for i, b in ipairs(self.bits) do + if type(b) == 'string' then + do + local spaces = match(b, "\n([ ]*)[^\n]*$") + if spaces then + indent = #spaces + end + end + else + b = tostring(b) + if indent > 0 then + b = gsub(b, "\n", "\n" .. rep(" ", indent)) + end + end + buff[#buff + 1] = b + end + self.__str = concat(buff, "") + end + return self.__str + end, + __len = function(self) + return #tostring(self) + end, dirty = function(self) self.__str = nil self._trailing_line_len = nil @@ -185,6 +216,10 @@ do end end return self:dirty() + end, + parenthesize = function(self) + self:prepend("(") + return self:append(")") end } _base_0.__index = _base_0 @@ -215,6 +250,8 @@ do local _class_0 local _parent_0 = Code local _base_0 = { + __tostring = Code.__tostring, + __len = Code.__len, add_free_vars = function(self, vars) if not (#vars > 0) then return @@ -327,37 +364,6 @@ do end return statements end, - __tostring = function(self) - if self.__str == nil then - local buff, indent = { }, 0 - local match, gsub, rep - do - local _obj_0 = string - match, gsub, rep = _obj_0.match, _obj_0.gsub, _obj_0.rep - end - for i, b in ipairs(self.bits) do - if type(b) == 'string' then - do - local spaces = match(b, "\n([ ]*)[^\n]*$") - if spaces then - indent = #spaces - end - end - else - b = tostring(b) - if indent > 0 then - b = gsub(b, "\n", "\n" .. rep(" ", indent)) - end - end - buff[#buff + 1] = b - end - self.__str = concat(buff, "") - end - return self.__str - end, - __len = function(self) - return #tostring(self) - end, make_offset_table = function(self) local lua_to_nomsu, nomsu_to_lua = { }, { } local walk @@ -386,12 +392,9 @@ do } end, parenthesize = function(self) - if self.is_value then - self:prepend("(") - return self:append(")") - else - return error("Cannot parenthesize lua statements") - end + assert(self.is_value, "Cannot parenthesize lua statements") + self:prepend("(") + return self:append(")") end } _base_0.__index = _base_0 @@ -439,41 +442,8 @@ do local _class_0 local _parent_0 = Code local _base_0 = { - __tostring = function(self) - if self.__str == nil then - local buff, indent = { }, 0 - local match, gsub, rep - do - local _obj_0 = string - match, gsub, rep = _obj_0.match, _obj_0.gsub, _obj_0.rep - end - for i, b in ipairs(self.bits) do - if type(b) == 'string' then - do - local spaces = match(b, "\n([ ]*)[^\n]*$") - if spaces then - indent = #spaces - end - end - else - b = tostring(b) - if indent > 0 then - b = gsub(b, "\n", "\n" .. rep(" ", indent)) - end - end - buff[#buff + 1] = b - end - self.__str = concat(buff, "") - end - return self.__str - end, - __len = function(self) - return #tostring(self) - end, - parenthesize = function(self) - self:prepend("(") - return self:append(")") - end + __tostring = Code.__tostring, + __len = Code.__len } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) diff --git a/error_handling.lua b/error_handling.lua index d88cfeb..88ce1a9 100644 --- a/error_handling.lua +++ b/error_handling.lua @@ -109,8 +109,6 @@ print_error = function(error_message, start_fn, stop_fn) local filename, start, stop = calling_fn.source:match('@([^[]*)%[([0-9]+):([0-9]+)]') assert(filename) local file = files.read(filename) - local err_line = files.get_line(file, calling_fn.currentline) - local offending_statement = colored.bright(colored.red(err_line:match("^[ ]*(.*)"))) if calling_fn.name then do local tmp = calling_fn.name:match("^A_([a-zA-Z0-9_]*)$") @@ -125,7 +123,15 @@ print_error = function(error_message, start_fn, stop_fn) else name = "main chunk" end - line = colored.yellow(tostring(filename) .. ":" .. tostring(calling_fn.currentline) .. " in " .. tostring(name) .. "\n " .. tostring(offending_statement)) + do + local err_line = files.get_line(file, calling_fn.currentline) + if err_line then + local offending_statement = colored.bright(colored.red(err_line:match("^[ ]*(.*)"))) + line = colored.yellow(tostring(filename) .. ":" .. tostring(calling_fn.currentline) .. " in " .. tostring(name) .. "\n " .. tostring(offending_statement)) + else + line = colored.yellow(tostring(filename) .. ":" .. tostring(calling_fn.currentline) .. " in " .. tostring(name)) + end + end else local file ok, file = pcall(function() @@ -188,9 +194,13 @@ print_error = function(error_message, start_fn, stop_fn) end end if file then - local err_line = files.get_line(file, line_num) - local offending_statement = colored.bright(colored.red(err_line:match("^[ ]*(.*)$"))) - line = line .. ("\n " .. offending_statement) + do + local err_line = files.get_line(file, line_num) + if err_line then + local offending_statement = colored.bright(colored.red(err_line:match("^[ ]*(.*)$"))) + line = line .. ("\n " .. offending_statement) + end + end end end end diff --git a/nomsu.lua b/nomsu.lua index 8918b11..8d591e3 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -268,7 +268,6 @@ run = function() end if #file_queue == 0 then nomsu:run([[#!/usr/bin/env nomsu -V2 -use "core" use "lib/consolecolor.nom" action [quit, exit]: lua> "os.exit(0)" action [help]: