diff --git a/code_obj.lua b/code_obj.lua index 0fcd34d..37ac55a 100644 --- a/code_obj.lua +++ b/code_obj.lua @@ -82,8 +82,8 @@ do is_code = true, append = function(self, ...) local n = select("#", ...) - local bits, indents = self.bits, self.indents local match = string.match + local bits = self.bits for i = 1, n do local _continue_0 = false repeat @@ -98,27 +98,6 @@ do if type(b) ~= 'string' and not (type(b) == 'table' and b.is_code) then b = repr(b) end - if type(b) == 'string' then - local trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$") - if trailing_text then - self.current_indent = #spaces - self.trailing_line_len = #trailing_text - else - self.trailing_line_len = self.trailing_line_len + #b - end - else - do - local trailing_text = match(tostring(b), "\n([^\n]*)$") - if trailing_text then - self.trailing_line_len = #trailing_text + self.current_indent - else - self.trailing_line_len = self.trailing_line_len + #tostring(b) - end - end - if self.current_indent ~= 0 then - indents[#bits] = self.current_indent - end - end _continue_0 = true until true if not _continue_0 then @@ -126,11 +105,35 @@ do end end self.__str = nil + self._trailing_line_len = nil + end, + trailing_line_len = function(self) + if self._trailing_line_len == nil then + local bits, match = self.bits, string.match + local len = 0 + for i = #bits, 1, -1 do + local b = bits[i] + if type(b) == 'string' then + do + local line = match(b, "\n([^\n]*)$") + if line then + return len + #line + else + len = len + #b + end + end + else + len = len + b:trailing_line_len() + end + end + self._trailing_line_len = len + end + return self._trailing_line_len end, concat_append = function(self, values, joiner, wrapping_joiner) wrapping_joiner = wrapping_joiner or joiner - local bits, indents = self.bits, self.indents local match = string.match + local bits = self.bits local line_len = 0 for i = 1, #values do local b = values[i] @@ -143,47 +146,28 @@ do end end bits[#bits + 1] = b - if type(b) ~= 'string' and self.current_indent ~= 0 then - indents[#bits] = self.current_indent - end local b_str = tostring(b) - local line, spaces = match(b_str, "\n(([ ]*)[^\n]*)$") - if spaces then - if type(b) == 'string' then - self.current_indent = #spaces - end + local line = match(b_str, "\n([^\n]*)$") + if line then line_len = #line else line_len = line_len + #b end end self.__str = nil + self._trailing_line_len = nil end, prepend = function(self, ...) local n = select("#", ...) - local bits, indents = self.bits, self.indents + local bits = self.bits for i = #bits + n, n + 1, -1 do bits[i] = bits[i - n] end for i = 1, n do bits[i] = select(i, ...) end - self.current_indent = 0 - for i, b in ipairs(bits) do - if type(b) == 'string' then - do - local spaces = b:match("\n([ ]*)[^\n]*$") - if spaces then - self.current_indent = #spaces - end - end - elseif self.current_indent ~= 0 then - indents[i] = self.current_indent - else - indents[i] = nil - end - end self.__str = nil + self._trailing_line_len = nil end } _base_0.__index = _base_0 @@ -191,8 +175,6 @@ do __init = function(self, source, ...) self.source = source self.bits = { } - self.indents, self.current_indent = { }, 0 - self.trailing_line_len = 0 if type(self.source) == 'string' then self.source = Source:from_string(self.source) end @@ -242,6 +224,7 @@ do end end self.__str = nil + self._trailing_line_len = nil end, remove_free_vars = function(self, vars) if not (#vars > 0) then @@ -274,6 +257,7 @@ do end end self.__str = nil + self._trailing_line_len = nil end, declare_locals = function(self, to_declare) if to_declare == nil then @@ -336,11 +320,25 @@ do end, __tostring = function(self) if self.__str == nil then - local buff, indents = { }, self.indents + 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 - b = tostring(b) - if indents[i] then - b = b:gsub("\n", "\n" .. ((" "):rep(indents[i]))) + 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 @@ -394,7 +392,6 @@ do _class_0.__parent.__init(self, ...) self.free_vars = { } self.is_value = false - self.__str = nil end, __base = _base_0, __name = "LuaCode", @@ -424,11 +421,6 @@ do lua.is_value = true return lua end - self.Comment = function(...) - local lua = LuaCode(...) - lua.is_comment = true - return lua - end if _parent_0.__inherited then _parent_0.__inherited(_parent_0, _class_0) end @@ -440,11 +432,25 @@ do local _base_0 = { __tostring = function(self) if self.__str == nil then - local buff, indents = { }, self.indents + 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 - b = tostring(b) - if indents[i] then - b = b:gsub("\n", "\n" .. ((" "):rep(indents[i]))) + 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 diff --git a/code_obj.moon b/code_obj.moon index 5397cf6..e1cf226 100644 --- a/code_obj.moon +++ b/code_obj.moon @@ -48,8 +48,6 @@ class Code is_code: true new: (@source, ...)=> @bits = {} - @indents, @current_indent = {}, 0 - @trailing_line_len = 0 if type(@source) == 'string' @source = Source\from_string(@source) assert(@source and Source\is_instance(@source), "Source has the wrong type") @@ -57,8 +55,8 @@ class Code append: (...)=> n = select("#",...) - bits, indents = @bits, @indents match = string.match + bits = @bits for i=1,n b = select(i, ...) assert(b, "code bit is nil") @@ -67,24 +65,28 @@ class Code bits[#bits+1] = b if type(b) != 'string' and not (type(b) == 'table' and b.is_code) b = repr(b) - if type(b) == 'string' - trailing_text, spaces = match(b, "\n(([ ]*)[^\n]*)$") - if trailing_text - @current_indent = #spaces - @trailing_line_len = #trailing_text - else @trailing_line_len += #b - else - if trailing_text = match(tostring(b), "\n([^\n]*)$") - @trailing_line_len = #trailing_text + @current_indent - else @trailing_line_len += #tostring(b) - if @current_indent != 0 - indents[#bits] = @current_indent @__str = nil + @_trailing_line_len = nil + + trailing_line_len: => + if @_trailing_line_len == nil + bits, match = @bits, string.match + len = 0 + for i=#bits,1,-1 + b = bits[i] + if type(b) == 'string' + if line = match(b, "\n([^\n]*)$") + return len + #line + else len += #b + else + len += b\trailing_line_len! + @_trailing_line_len = len + return @_trailing_line_len concat_append: (values, joiner, wrapping_joiner)=> wrapping_joiner or= joiner - bits, indents = @bits, @indents match = string.match + bits = @bits line_len = 0 for i=1,#values b = values[i] @@ -95,51 +97,35 @@ class Code else bits[#bits+1] = joiner bits[#bits+1] = b - if type(b) != 'string' and @current_indent != 0 - indents[#bits] = @current_indent b_str = tostring(b) - line, spaces = match(b_str, "\n(([ ]*)[^\n]*)$") - if spaces - if type(b) == 'string' - @current_indent = #spaces + line = match(b_str, "\n([^\n]*)$") + if line line_len = #line else line_len += #b @__str = nil + @_trailing_line_len = nil prepend: (...)=> n = select("#",...) - bits, indents = @bits, @indents + bits = @bits for i=#bits+n,n+1,-1 bits[i] = bits[i-n] for i=1,n bits[i] = select(i, ...) - @current_indent = 0 - for i,b in ipairs(bits) - if type(b) == 'string' - if spaces = b\match("\n([ ]*)[^\n]*$") - @current_indent = #spaces - elseif @current_indent != 0 - indents[i] = @current_indent - else indents[i] = nil @__str = nil + @_trailing_line_len = nil class LuaCode extends Code new: (...)=> super ... @free_vars = {} @is_value = false - @__str = nil @Value = (...)-> lua = LuaCode(...) lua.is_value = true return lua - - @Comment = (...)-> - lua = LuaCode(...) - lua.is_comment = true - return lua add_free_vars: (vars)=> return unless #vars > 0 @@ -150,6 +136,7 @@ class LuaCode extends Code @free_vars[#@free_vars+1] = var seen[var] = true @__str = nil + @_trailing_line_len = nil remove_free_vars: (vars)=> return unless #vars > 0 @@ -169,6 +156,7 @@ class LuaCode extends Code if type(b) != 'string' stack[#stack+1] = b @__str = nil + @_trailing_line_len = nil declare_locals: (to_declare=nil)=> if to_declare == nil @@ -205,11 +193,16 @@ class LuaCode extends Code __tostring: => if @__str == nil - buff, indents = {}, @indents + buff, indent = {}, 0 + {:match, :gsub, :rep} = string for i,b in ipairs @bits - b = tostring(b) - if indents[i] - b = b\gsub("\n", "\n"..((" ")\rep(indents[i]))) + if type(b) == 'string' + if spaces = match(b, "\n([ ]*)[^\n]*$") + indent = #spaces + else + b = tostring(b) + if indent > 0 + b = gsub(b, "\n", "\n"..rep(" ", indent)) buff[#buff+1] = b @__str = concat(buff, "") return @__str @@ -246,11 +239,16 @@ class LuaCode extends Code class NomsuCode extends Code __tostring: => if @__str == nil - buff, indents = {}, @indents + buff, indent = {}, 0 + {:match, :gsub, :rep} = string for i,b in ipairs @bits - b = tostring(b) - if indents[i] - b = b\gsub("\n", "\n"..((" ")\rep(indents[i]))) + if type(b) == 'string' + if spaces = match(b, "\n([ ]*)[^\n]*$") + indent = #spaces + else + b = tostring(b) + if indent > 0 + b = gsub(b, "\n", "\n"..rep(" ", indent)) buff[#buff+1] = b @__str = concat(buff, "") return @__str diff --git a/files.lua b/files.lua index fe1263e..65a8e1b 100644 --- a/files.lua +++ b/files.lua @@ -34,25 +34,52 @@ files.read = function(filename) _FILE_CACHE[filename] = contents return contents end -local iterate_single -iterate_single = function(item, prev) - if item == prev then - return nil - else - return item - end -end local match, gsub do local _obj_0 = string match, gsub = _obj_0.match, _obj_0.gsub end -iterate_single = function(item, prev) - if item == prev then - return nil - else - return item +local sanitize +sanitize = function(path) + path = gsub(path, "\\", "\\\\") + path = gsub(path, "`", "") + path = gsub(path, '"', '\\"') + path = gsub(path, "$", "") + return path +end +files.exists = function(path) + if not (io.popen("ls " .. tostring(sanitize(path))):close()) then + return true end + if package.nomsupath then + for nomsupath in package.nomsupath:gmatch("[^;]+") do + if not (io.popen("ls " .. tostring(nomsupath) .. "/" .. tostring(sanitize(path))):close()) then + return true + end + end + end + return false +end +local _browse_cache = { } +local browse +browse = function(path) + if not (_browse_cache[path]) then + local f = io.popen('find -L "' .. package.nomsupath .. '/' .. path .. '" -not -path "*/\\.*" -type f -name "*.nom"') + local _files + do + local _tbl_0 = { } + for line in f:lines() do + local _key_0, _val_0 = line + _tbl_0[_key_0] = _val_0 + end + _files = _tbl_0 + end + if not (f:close()) then + _files = false + end + _browse_cache[path] = _files + end + return _browse_cache[path] end local ok, lfs = pcall(require, "lfs") if ok then @@ -78,109 +105,76 @@ if ok then end return false end - local browse browse = function(filename) - local file_type, err = lfs.attributes(filename, 'mode') - if file_type == 'file' then - if match(filename, "%.nom$") or match(filename, "%.lua$") then - coroutine.yield(filename) - return true - end - elseif file_type == 'directory' or file_type == 'link' then - for subfile in lfs.dir(filename) do - if not (subfile == "." or subfile == "..") then - browse(filename .. "/" .. subfile) + if not (_browse_cache[filename]) then + _browse_cache[filename] = false + local file_type, err = lfs.attributes(filename, 'mode') + if file_type == 'file' then + if match(filename, "%.nom$") or match(filename, "%.lua$") then + _browse_cache[filename] = { + filename + } end - end - return true - elseif file_type == 'char device' then - coroutine.yield(filename) - return true - end - return false - end - files.walk = function(path) - if match(path, "%.nom$") or match(path, "%.lua$") or path == 'stdin' then - return iterate_single, path - end - return coroutine.wrap(function() - if not browse(path) and package.nomsupath then - for nomsupath in package.nomsupath:gmatch("[^;]+") do - if browse(nomsupath .. "/" .. path) then - break + elseif file_type == 'char device' then + _browse_cache[filename] = { + filename + } + elseif file_type == 'directory' or file_type == 'link' then + local _files = { } + for subfile in lfs.dir(filename) do + if not (subfile == "." or subfile == "..") then + local _list_0 = (browse(filename .. "/" .. subfile) or { }) + for _index_0 = 1, #_list_0 do + local f = _list_0[_index_0] + _files[#_files + 1] = f + end end end + _browse_cache[filename] = _files end - return nil - end) + end + return _browse_cache[filename] end else if io.popen('find . -maxdepth 0'):close() then error("Could not find 'luafilesystem' module and couldn't run system command `find` (this might happen on Windows). Please install `luafilesystem` (which can be found at: http://keplerproject.github.io/luafilesystem/ or `luarocks install luafilesystem`)", 0) end - local sanitize - sanitize = function(path) - path = gsub(path, "\\", "\\\\") - path = gsub(path, "`", "") - path = gsub(path, '"', '\\"') - path = gsub(path, "$", "") - return path +end +files.walk = function(path, flush_cache) + if flush_cache == nil then + flush_cache = false end - files.exists = function(path) - if not (io.popen("ls " .. tostring(sanitize(path))):close()) then - return true - end - if package.nomsupath then - for nomsupath in package.nomsupath:gmatch("[^;]+") do - if not (io.popen("ls " .. tostring(nomsupath) .. "/" .. tostring(sanitize(path))):close()) then - return true + if flush_cache then + _browse_cache = { } + end + local _files = browse(path) + if package.nomsupath and not _files then + for nomsupath in package.nomsupath:gmatch("[^;]+") do + do + _files = browse(nomsupath .. "/" .. path) + if _files then + break end end end - return false end - files.walk = function(path) - if match(path, "%.nom$") or match(path, "%.lua$") or path == 'stdin' then - return iterate_single, path + local iter + iter = function(_files, i) + i = i + 1 + do + local f = _files[i] + if f then + return i, f + end end - path = sanitize(path) - return coroutine.wrap(function() - local f = io.popen('find -L "' .. path .. '" -not -path "*/\\.*" -type f -name "*.nom"') - local found = false - for line in f:lines() do - found = true - coroutine.yield(line) - end - if not found and package.nomsupath then - f:close() - for nomsupath in package.nomsupath:gmatch("[^;]+") do - f = io.popen('find -L "' .. package.nomsupath .. '/' .. path .. '" -not -path "*/\\.*" -type f -name "*.nom"') - for line in f:lines() do - found = true - coroutine.yield(line) - end - f:close() - if found then - break - end - end - end - if not (found) then - return error("Invalid file path: " .. tostring(path)) - end - end) end + return iter, _files, 0 end local line_counter = re.compile([[ lines <- {| line (%nl line)* |} line <- {} (!%nl .)* ]], { nl = lpeg.P("\r") ^ -1 * lpeg.P("\n") }) -local get_lines = re.compile([[ lines <- {| line (%nl line)* |} - line <- {[^%nl]*} -]], { - nl = lpeg.P("\r") ^ -1 * lpeg.P("\n") -}) local _LINE_STARTS = { } files.get_line_starts = function(str) if type(str) ~= 'string' then @@ -196,6 +190,7 @@ files.get_line_starts = function(str) _LINE_STARTS[str] = line_starts return line_starts end +local log = { } files.get_line_number = function(str, pos) local line_starts = files.get_line_starts(str) local lo, hi = 1, #line_starts @@ -213,6 +208,11 @@ files.get_line = function(str, line_no) local line_starts = files.get_line_starts(str) return str:sub(line_starts[line_no] or 1, (line_starts[line_no + 1] or 1) - 2) end +local get_lines = re.compile([[ lines <- {| line (%nl line)* |} + line <- {[^%nl]*} +]], { + nl = lpeg.P("\r") ^ -1 * lpeg.P("\n") +}) files.get_lines = function(str) return get_lines:match(str) end diff --git a/files.moon b/files.moon index 9ac8ca7..6f06c8c 100644 --- a/files.moon +++ b/files.moon @@ -28,11 +28,33 @@ files.read = (filename)-> _FILE_CACHE[filename] = contents return contents -iterate_single = (item, prev) -> if item == prev then nil else item - -- `walk` returns an iterator over all files matching a path. {:match, :gsub} = string -iterate_single = (item, prev) -> if item == prev then nil else item + +-- TODO: improve sanitization +sanitize = (path)-> + path = gsub(path,"\\","\\\\") + path = gsub(path,"`","") + path = gsub(path,'"','\\"') + path = gsub(path,"$","") + return path + +files.exists = (path)-> + return true unless io.popen("ls #{sanitize(path)}")\close! + if package.nomsupath + for nomsupath in package.nomsupath\gmatch("[^;]+") + return true unless io.popen("ls #{nomsupath}/#{sanitize(path)}")\close! + return false + +_browse_cache = {} +browse = (path)-> + unless _browse_cache[path] + f = io.popen('find -L "'..package.nomsupath..'/'..path..'" -not -path "*/\\.*" -type f -name "*.nom"') + _files = {line for line in f\lines!} + _files = false unless f\close! + _browse_cache[path] = _files + return _browse_cache[path] + ok, lfs = pcall(require, "lfs") if ok raw_file_exists = (filename)-> @@ -44,82 +66,48 @@ if ok for nomsupath in package.nomsupath\gmatch("[^;]+") return true if raw_file_exists(nomsupath.."/"..path) return false - -- Return 'true' if any files or directories are found, otherwise 'false', and yield every filename - browse = (filename)-> - file_type, err = lfs.attributes(filename, 'mode') - if file_type == 'file' - if match(filename, "%.nom$") or match(filename, "%.lua$") - coroutine.yield filename - return true - elseif file_type == 'directory' or file_type == 'link' - for subfile in lfs.dir(filename) - unless subfile == "." or subfile == ".." - browse(filename.."/"..subfile) - return true - elseif file_type == 'char device' - coroutine.yield(filename) - return true - return false - files.walk = (path)-> - if match(path, "%.nom$") or match(path, "%.lua$") or path == 'stdin' - return iterate_single, path - return coroutine.wrap -> - if not browse(path) and package.nomsupath - for nomsupath in package.nomsupath\gmatch("[^;]+") - break if browse(nomsupath.."/"..path) - return nil + export browse + browse = (filename)-> + unless _browse_cache[filename] + _browse_cache[filename] = false + file_type, err = lfs.attributes(filename, 'mode') + if file_type == 'file' + if match(filename, "%.nom$") or match(filename, "%.lua$") + _browse_cache[filename] = {filename} + elseif file_type == 'char device' + _browse_cache[filename] = {filename} + elseif file_type == 'directory' or file_type == 'link' + _files = {} + for subfile in lfs.dir(filename) + unless subfile == "." or subfile == ".." + for f in *(browse(filename.."/"..subfile) or {}) + _files[#_files+1] = f + _browse_cache[filename] = _files + return _browse_cache[filename] else if io.popen('find . -maxdepth 0')\close! error "Could not find 'luafilesystem' module and couldn't run system command `find` (this might happen on Windows). Please install `luafilesystem` (which can be found at: http://keplerproject.github.io/luafilesystem/ or `luarocks install luafilesystem`)", 0 - - -- TODO: improve sanitization - sanitize = (path)-> - path = gsub(path,"\\","\\\\") - path = gsub(path,"`","") - path = gsub(path,'"','\\"') - path = gsub(path,"$","") - return path - files.exists = (path)-> - return true unless io.popen("ls #{sanitize(path)}")\close! - if package.nomsupath - for nomsupath in package.nomsupath\gmatch("[^;]+") - return true unless io.popen("ls #{nomsupath}/#{sanitize(path)}")\close! - return false - - files.walk = (path)-> - if match(path, "%.nom$") or match(path, "%.lua$") or path == 'stdin' - return iterate_single, path - path = sanitize(path) - return coroutine.wrap -> - f = io.popen('find -L "'..path..'" -not -path "*/\\.*" -type f -name "*.nom"') - found = false - for line in f\lines! - found = true - coroutine.yield(line) - if not found and package.nomsupath - f\close! - for nomsupath in package.nomsupath\gmatch("[^;]+") - f = io.popen('find -L "'..package.nomsupath..'/'..path..'" -not -path "*/\\.*" -type f -name "*.nom"') - for line in f\lines! - found = true - coroutine.yield(line) - f\close! - break if found - unless found - error("Invalid file path: "..tostring(path)) +files.walk = (path, flush_cache=false)-> + if flush_cache + export _browse_cache + _browse_cache = {} + _files = browse(path) + if package.nomsupath and not _files + for nomsupath in package.nomsupath\gmatch("[^;]+") + if _files = browse(nomsupath.."/"..path) then break + iter = (_files, i)-> + i += 1 + if f = _files[i] + return i, f + return iter, _files, 0 line_counter = re.compile([[ lines <- {| line (%nl line)* |} line <- {} (!%nl .)* ]], nl:lpeg.P("\r")^-1 * lpeg.P("\n")) -get_lines = re.compile([[ - lines <- {| line (%nl line)* |} - line <- {[^%nl]*} -]], nl:lpeg.P("\r")^-1 * lpeg.P("\n")) - -- LINE_STARTS is a mapping from strings to a table that maps line number to character positions _LINE_STARTS = {} files.get_line_starts = (str)-> @@ -131,6 +119,7 @@ files.get_line_starts = (str)-> _LINE_STARTS[str] = line_starts return line_starts +log = {} files.get_line_number = (str, pos)-> line_starts = files.get_line_starts(str) -- Binary search for line number of position @@ -146,6 +135,11 @@ files.get_line = (str, line_no)-> line_starts = files.get_line_starts(str) return str\sub(line_starts[line_no] or 1, (line_starts[line_no+1] or 1) - 2) +get_lines = re.compile([[ + lines <- {| line (%nl line)* |} + line <- {[^%nl]*} +]], nl:lpeg.P("\r")^-1 * lpeg.P("\n")) + files.get_lines = (str)-> get_lines\match(str) return files diff --git a/lib/os.nom b/lib/os.nom index 329a77f..1be7a7e 100644 --- a/lib/os.nom +++ b/lib/os.nom @@ -6,7 +6,7 @@ use "core" action [path of Nomsu file %filename] lua> ".." - for f in files.walk(\%filename) do return f end + for i,f in files.walk(\%filename) do return f end barf "Could not find file: \%filename" action [sh> %cmd] @@ -21,7 +21,7 @@ action [read file %filename] compile [for file %f in %path %body] to Lua ".." - for \(%f as lua expr) in files.walk(\(%path as lua expr)) do + for i,\(%f as lua expr) in files.walk(\(%path as lua expr)) do \(%body as lua statements) \(compile as: === next %f ===) end @@ -31,7 +31,7 @@ compile [%expr for file %f in %path] to Lua value ".." (function() local ret = list{} - for \(%f as lua expr) in files.walk(\(%path as lua expr)) do + for i,\(%f as lua expr) in files.walk(\(%path as lua expr)) do ret[#ret+1] = \(%expr as lua statements) end return ret diff --git a/nomsu.lua b/nomsu.lua index 0f22eff..3ca85b5 100644 --- a/nomsu.lua +++ b/nomsu.lua @@ -144,7 +144,7 @@ run = function() if not (files.exists(f)) then error("Could not find: " .. tostring(f)) end - for filename in files.walk(f) do + for _, filename in files.walk(f) do input_files[filename] = true end end @@ -228,7 +228,7 @@ run = function() local parse_errs = { } for _index_0 = 1, #file_queue do local f = file_queue[_index_0] - for filename in files.walk(f) do + for _, filename in files.walk(f) do local _continue_0 = false repeat if not (filename == "stdin" or filename:match("%.nom$")) then @@ -323,18 +323,20 @@ say ".." end end end -local debugger -if args.debugger == "nil" then - debugger = { } -else - debugger = require(args.debugger or 'error_handling') +do + local debugger + if args.debugger == "nil" then + debugger = { } + else + debugger = require(args.debugger or 'error_handling') + end + local guard + if type(debugger) == 'function' then + guard = debugger + else + guard = debugger.guard or debugger.call or debugger.wrap or debugger.run or (function(fn) + return fn() + end) + end + return guard(run) end -local guard -if type(debugger) == 'function' then - guard = debugger -else - guard = debugger.guard or debugger.call or debugger.wrap or debugger.run or (function(fn) - return fn() - end) -end -return guard(run) diff --git a/nomsu.moon b/nomsu.moon index c2e5e05..82cdada 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -95,7 +95,7 @@ run = -> for f in *file_queue unless files.exists(f) error("Could not find: #{f}") - for filename in files.walk(f) + for _,filename in files.walk(f) input_files[filename] = true nomsu.can_optimize = (f)-> @@ -149,7 +149,7 @@ run = -> parse_errs = {} for f in *file_queue - for filename in files.walk(f) + for _,filename in files.walk(f) continue unless filename == "stdin" or filename\match("%.nom$") if args.check_syntax -- Check syntax @@ -218,8 +218,10 @@ say ".." elseif not ok Errhand.print_error ret -debugger = if args.debugger == "nil" then {} -else require(args.debugger or 'error_handling') -guard = if type(debugger) == 'function' then debugger -else debugger.guard or debugger.call or debugger.wrap or debugger.run or ((fn)->fn()) -guard(run) + +do + debugger = if args.debugger == "nil" then {} + else require(args.debugger or 'error_handling') + guard = if type(debugger) == 'function' then debugger + else debugger.guard or debugger.call or debugger.wrap or debugger.run or ((fn)->fn()) + guard(run) diff --git a/nomsu_compiler.lua b/nomsu_compiler.lua index 2d82613..ac3e741 100644 --- a/nomsu_compiler.lua +++ b/nomsu_compiler.lua @@ -266,7 +266,7 @@ do local add_bit_lua add_bit_lua = function(lua, bit_lua) local bit_leading_len = #(tostring(bit_lua):match("^[^\n]*")) - lua:append(lua.trailing_line_len + bit_leading_len > MAX_LINE and ",\n " or ", ") + lua:append(lua:trailing_line_len() + bit_leading_len > MAX_LINE and ",\n " or ", ") return lua:append(bit_lua) end local operate_on_text @@ -334,11 +334,11 @@ do ["use %"] = function(self, tree, _path) if _path.type == 'Text' and #_path == 1 and type(_path[1]) == 'string' then local path = _path[1] - for f in files.walk(path) do + for _, f in files.walk(path) do self:run_file(f) end end - return LuaCode(tree.source, "for f in files.walk(", self:compile(_path), ") do nomsu:run_file(f) end") + return LuaCode(tree.source, "for i,f in files.walk(", self:compile(_path), ") do nomsu:run_file(f) end") end, ["test %"] = function(self, tree, _body) return LuaCode("") @@ -845,7 +845,7 @@ do next_space = match(next_space, "[^ ]*") end nomsu:append(next_space) - if arg_nomsu and nomsu.trailing_line_len + #tostring(arg_nomsu) < MAX_LINE then + if arg_nomsu and nomsu:trailing_line_len() + #tostring(arg_nomsu) < MAX_LINE then if bit.type == "Block" then nomsu:append(arg_nomsu) next_space = "\n.." @@ -867,7 +867,7 @@ do end pos = bit.source.stop end - if next_space == " " and nomsu.trailing_line_len > MAX_LINE then + if next_space == " " and nomsu:trailing_line_len() > MAX_LINE then next_space = "\n.." end end @@ -1035,8 +1035,8 @@ do if item.type == "Block" then item_nomsu:parenthesize() end - if nomsu.trailing_line_len + #tostring(item_nomsu) <= MAX_LINE then - if nomsu.trailing_line_len > 0 then + if nomsu:trailing_line_len() + #tostring(item_nomsu) <= MAX_LINE then + if nomsu:trailing_line_len() > 0 then nomsu:append(", ") end nomsu:append(item_nomsu) @@ -1053,7 +1053,7 @@ do nomsu:append("\n") end else - if nomsu.trailing_line_len > 0 then + if nomsu:trailing_line_len() > 0 then nomsu:append('\n') end nomsu:append(pop_comments(item.source.start), item_nomsu) @@ -1092,8 +1092,8 @@ do if item.type == "Block" then item_nomsu:parenthesize() end - if nomsu.trailing_line_len + #tostring(item_nomsu) <= MAX_LINE then - if nomsu.trailing_line_len > 0 then + if nomsu:trailing_line_len() + #tostring(item_nomsu) <= MAX_LINE then + if nomsu:trailing_line_len() > 0 then nomsu:append(", ") end nomsu:append(item_nomsu) @@ -1110,7 +1110,7 @@ do nomsu:append("\n") end else - if nomsu.trailing_line_len > 0 then + if nomsu:trailing_line_len() > 0 then nomsu:append('\n') end nomsu:append(pop_comments(item.source.start), item_nomsu) diff --git a/nomsu_compiler.moon b/nomsu_compiler.moon index 9ba014a..877de6b 100644 --- a/nomsu_compiler.moon +++ b/nomsu_compiler.moon @@ -162,7 +162,7 @@ with NomsuCompiler return LuaCode.Value(code.source, cls_str, repr(tostring(code.source)), ", ", @compile(code), ")") add_bit_lua = (lua, bit_lua)-> bit_leading_len = #(tostring(bit_lua)\match("^[^\n]*")) - lua\append(lua.trailing_line_len + bit_leading_len > MAX_LINE and ",\n " or ", ") + lua\append(lua\trailing_line_len! + bit_leading_len > MAX_LINE and ",\n " or ", ") lua\append(bit_lua) operate_on_text = (text)-> lua = LuaCode.Value(text.source, cls_str, repr(tostring(text.source))) @@ -217,9 +217,9 @@ with NomsuCompiler ["use %"]: (tree, _path)=> if _path.type == 'Text' and #_path == 1 and type(_path[1]) == 'string' path = _path[1] - for f in files.walk(path) + for _,f in files.walk(path) @run_file(f) - return LuaCode(tree.source, "for f in files.walk(", @compile(_path), ") do nomsu:run_file(f) end") + return LuaCode(tree.source, "for i,f in files.walk(", @compile(_path), ") do nomsu:run_file(f) end") ["test %"]: (tree, _body)=> return LuaCode "" @@ -549,7 +549,7 @@ with NomsuCompiler else assert recurse(bit,inline:true) next_space = match(next_space, "[^ ]*") if bit.type == "Block" nomsu\append next_space - if arg_nomsu and nomsu.trailing_line_len + #tostring(arg_nomsu) < MAX_LINE + if arg_nomsu and nomsu\trailing_line_len! + #tostring(arg_nomsu) < MAX_LINE if bit.type == "Block" nomsu\append arg_nomsu next_space = "\n.." @@ -567,7 +567,7 @@ with NomsuCompiler next_space = "\n.." pos = bit.source.stop - if next_space == " " and nomsu.trailing_line_len > MAX_LINE + if next_space == " " and nomsu\trailing_line_len! > MAX_LINE next_space = "\n.." nomsu\append pop_comments(pos, '\n') return nomsu @@ -681,8 +681,8 @@ with NomsuCompiler for i, item in ipairs tree item_nomsu = assert recurse(item, inline:true) item_nomsu\parenthesize! if item.type == "Block" - if nomsu.trailing_line_len + #tostring(item_nomsu) <= MAX_LINE - nomsu\append ", " if nomsu.trailing_line_len > 0 + if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE + nomsu\append ", " if nomsu\trailing_line_len! > 0 nomsu\append item_nomsu else if #tostring(item_nomsu) > MAX_LINE @@ -694,7 +694,7 @@ with NomsuCompiler nomsu\append "(..)\n ", item_nomsu nomsu\append "\n" if i < #tree else - nomsu\append '\n' if nomsu.trailing_line_len > 0 + nomsu\append '\n' if nomsu\trailing_line_len! > 0 nomsu\append pop_comments(item.source.start), item_nomsu nomsu\append pop_comments(tree.source.stop, '\n') return NomsuCode(tree.source, "[..]\n ", nomsu) @@ -716,8 +716,8 @@ with NomsuCompiler for i, item in ipairs tree item_nomsu = assert recurse(item, inline:true) item_nomsu\parenthesize! if item.type == "Block" - if nomsu.trailing_line_len + #tostring(item_nomsu) <= MAX_LINE - nomsu\append ", " if nomsu.trailing_line_len > 0 + if nomsu\trailing_line_len! + #tostring(item_nomsu) <= MAX_LINE + nomsu\append ", " if nomsu\trailing_line_len! > 0 nomsu\append item_nomsu else if #tostring(item_nomsu) > MAX_LINE @@ -729,7 +729,7 @@ with NomsuCompiler nomsu\append "(..)\n ", item_nomsu nomsu\append "\n" if i < #tree else - nomsu\append '\n' if nomsu.trailing_line_len > 0 + nomsu\append '\n' if nomsu\trailing_line_len! > 0 nomsu\append pop_comments(item.source.start), item_nomsu nomsu\append pop_comments(tree.source.stop, '\n') return NomsuCode(tree.source, "{..}\n ", nomsu)