From 992e573ca58e55ae33c420ea0f620b2daf5fa9c0 Mon Sep 17 00:00:00 2001 From: Corey Williamson Date: Wed, 19 Jun 2024 07:06:42 -0500 Subject: [PATCH] Apply new lints --- build/cyan/colorstring.lua | 10 ++++------ build/cyan/command.lua | 8 ++++---- build/cyan/commands/build.lua | 13 +++++++------ build/cyan/commands/check-gen.lua | 7 ++++--- build/cyan/commands/initialize.lua | 14 ++++++++------ build/cyan/commands/run.lua | 5 ++--- build/cyan/config.lua | 14 +++++++------- build/cyan/fs/path.lua | 16 +++++++++------- build/cyan/graph.lua | 6 ++---- build/cyan/interaction.lua | 10 +++++++--- build/cyan/log.lua | 3 +-- build/cyan/sandbox.lua | 4 ++-- build/cyan/tlcommon.lua | 10 +++++----- build/cyan/util.lua | 17 +++++++---------- src/cyan/colorstring.tl | 10 ++++------ src/cyan/command.tl | 6 +++--- src/cyan/commands/build.tl | 11 ++++++----- src/cyan/commands/check-gen.tl | 5 +++-- src/cyan/commands/initialize.tl | 12 +++++++----- src/cyan/commands/run.tl | 5 ++--- src/cyan/config.tl | 12 ++++++------ src/cyan/fs/path.tl | 16 +++++++++------- src/cyan/graph.tl | 6 ++---- src/cyan/interaction.tl | 8 ++++++-- src/cyan/log.tl | 3 +-- src/cyan/sandbox.tl | 4 ++-- src/cyan/tlcommon.tl | 8 ++++---- src/cyan/util.tl | 17 +++++++---------- 28 files changed, 131 insertions(+), 129 deletions(-) diff --git a/build/cyan/colorstring.lua b/build/cyan/colorstring.lua index 7f4f8f0..23ec21e 100644 --- a/build/cyan/colorstring.lua +++ b/build/cyan/colorstring.lua @@ -29,7 +29,7 @@ local ColorString = {} function ColorString:len() local l = 0 - for _, chunk in ipairs(self.content) do + for chunk in ivalues(self.content) do if type(chunk) == "string" then l = l + #chunk end @@ -39,7 +39,7 @@ end local function append(base, other) if type(other) == "table" then - for _, chunk in ipairs(other.content) do + for chunk in ivalues(other.content) do table.insert(base.content, chunk) end else @@ -72,9 +72,8 @@ function ColorString:tostring() return table.concat(map(self.content, function(chunk) if type(chunk) == "string" then return chunk - else - return ansi.CSI .. table.concat(map(chunk, tostring), ";") .. "m" end + return ansi.CSI .. table.concat(map(chunk, tostring), ";") .. "m" end)) end @@ -160,9 +159,8 @@ end function colorstring.copy(str) if type(str) == "string" then return str - else - return str:copy() end + return str:copy() end local cs = colorstring.colors diff --git a/build/cyan/command.lua b/build/cyan/command.lua index c615f04..401f1cc 100644 --- a/build/cyan/command.lua +++ b/build/cyan/command.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local pairs = _tl_compat and _tl_compat.pairs or pairs +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local pairs = _tl_compat and _tl_compat.pairs or pairs @@ -10,8 +10,8 @@ local fs = require("cyan.fs") local log = require("cyan.log") local util = require("cyan.util") -local merge_list, sort, from, keys, contains = -util.tab.merge_list, util.tab.sort_in_place, util.tab.from, util.tab.keys, util.tab.contains +local merge_list, sort, from, keys, contains, ivalues = +util.tab.merge_list, util.tab.sort_in_place, util.tab.from, util.tab.keys, util.tab.contains, util.tab.ivalues local Args = {} @@ -76,7 +76,7 @@ function command.new(cmd) commands[cmd.name] = cmd if cmd.script_hooks then - for _, h in ipairs(cmd.script_hooks) do + for h in ivalues(cmd.script_hooks) do hooks[cmd.name .. ":" .. h] = true end end diff --git a/build/cyan/commands/build.lua b/build/cyan/commands/build.lua index e701b0a..edb9ee0 100644 --- a/build/cyan/commands/build.lua +++ b/build/cyan/commands/build.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local coroutine = _tl_compat and _tl_compat.coroutine or coroutine; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local os = _tl_compat and _tl_compat.os or os; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local coroutine = _tl_compat and _tl_compat.coroutine or coroutine; local io = _tl_compat and _tl_compat.io or io; local os = _tl_compat and _tl_compat.os or os; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack local argparse = require("argparse") local tl = require("tl") @@ -19,7 +19,8 @@ local function exists_and_is_dir(prefix, p) if not p:exists() then log.err(string.format("%s %q does not exist", prefix, p:to_real_path())) return false - elseif not p:is_directory() then + end + if not p:is_directory() then log.err(string.format("%s %q is not a directory", prefix, p:to_real_path())) return false end @@ -283,19 +284,19 @@ local function build(args, loaded_config, starting_dir) log.err("Unable to prune ", kind, " '", disp, "': ", err) end end - for _, p in ipairs(unexpected_files) do + for p in ivalues(unexpected_files) do prune(p, "file") end - for _, p in ipairs(unexpected_directories) do + for p in ivalues(unexpected_directories) do prune(p, "directory") end else local strs = {} - for _, p in ipairs(unexpected_files) do + for p in ivalues(unexpected_files) do table.insert(strs, "\n ") table.insert(strs, display_filename(p)) end - for _, p in ipairs(unexpected_directories) do + for p in ivalues(unexpected_directories) do table.insert(strs, "\n ") table.insert(strs, display_filename(p, true)) end diff --git a/build/cyan/commands/check-gen.lua b/build/cyan/commands/check-gen.lua index 3ec0f75..fc8efc2 100644 --- a/build/cyan/commands/check-gen.lua +++ b/build/cyan/commands/check-gen.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local table = _tl_compat and _tl_compat.table or table +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local table = _tl_compat and _tl_compat.table or table @@ -13,7 +13,8 @@ local log = require("cyan.log") local fs = require("cyan.fs") local util = require("cyan.util") -local map_ipairs = util.tab.map_ipairs +local map_ipairs, ivalues = +util.tab.map_ipairs, util.tab.ivalues local function command_exec(should_compile) return function(args, loaded_config, starting_dir) @@ -120,7 +121,7 @@ local function command_exec(should_compile) if should_compile then if exit ~= 0 then return exit end - for _, data in ipairs(to_write) do + for data in ivalues(to_write) do local fh, err = io.open(data.outfile:to_real_path(), "w") if fh then local generated, gen_err = common.compile_ast(data.output_ast, loaded_config.gen_target) diff --git a/build/cyan/commands/initialize.lua b/build/cyan/commands/initialize.lua index bbcdbef..73e662a 100644 --- a/build/cyan/commands/initialize.lua +++ b/build/cyan/commands/initialize.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table @@ -9,6 +9,9 @@ local config = require("cyan.config") local cs = require("cyan.colorstring") local fs = require("cyan.fs") local log = require("cyan.log") +local util = require("cyan.util") + +local ivalues = util.tab.ivalues local function exec(args, loaded_config, starting_dir) if not args.force and loaded_config.loaded_from then @@ -43,15 +46,14 @@ local function exec(args, loaded_config, starting_dir) if ok then log.info("Created directory ", cs.highlight(cs.colors.file, p:to_real_path())) return true - else - log.err("Unable to create directory ", cs.highlight(cs.colors.file, p:to_real_path()), ":\n ", err) - return false end + log.err("Unable to create directory ", cs.highlight(cs.colors.file, p:to_real_path()), ":\n ", err) + return false end return true end - for _, p in ipairs({ directory, directory .. source, directory .. build }) do + for p in ivalues({ directory, directory .. source, directory .. build }) do if not try_mkdir(p) then return 1 end @@ -71,7 +73,7 @@ local function exec(args, loaded_config, starting_dir) return end ins(1, "%s = {\n", name) - for _, entry in ipairs(arr) do + for entry in ivalues(arr) do ins(2, "%q,\n", entry) end ins(1, "},\n", name) diff --git a/build/cyan/commands/run.lua b/build/cyan/commands/run.lua index 956eb70..ce82874 100644 --- a/build/cyan/commands/run.lua +++ b/build/cyan/commands/run.lua @@ -74,10 +74,9 @@ local function run(args, loaded_config, starting_dir) local ok, err = box:run(1000000000) if ok then return 0 - else - log.err("Error in script:\n", err) - return 1 end + log.err("Error in script:\n", err) + return 1 end command.new({ diff --git a/build/cyan/config.lua b/build/cyan/config.lua index a180b22..c442224 100644 --- a/build/cyan/config.lua +++ b/build/cyan/config.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table @@ -8,8 +8,8 @@ local fs = require("cyan.fs") local sandbox = require("cyan.sandbox") local util = require("cyan.util") -local keys, sort, from, values = -util.tab.keys, util.tab.sort_in_place, util.tab.from, util.tab.values +local keys, sort, from, values, ivalues = +util.tab.keys, util.tab.sort_in_place, util.tab.from, util.tab.values, util.tab.ivalues @@ -53,7 +53,7 @@ local config = { local function get_types_in_array(val, typefn) typefn = typefn or type local set = {} - for _, v in ipairs(val) do + for v in ivalues(val) do set[typefn(v)] = true end return sort(from(keys(set))) @@ -193,7 +193,7 @@ function config.is_config(c) local function verify_warnings(key) local arr = (c)[key] if arr then - for _, warning in ipairs(arr) do + for warning in ivalues(arr) do if not tl.warning_kinds[warning] then table.insert(errs, string.format("Unknown warning in %s: %q", key, warning)) end @@ -205,9 +205,9 @@ function config.is_config(c) if #errs > 0 then return nil, errs, warnings - else - return c, nil, warnings end + + return c, nil, warnings end diff --git a/build/cyan/fs/path.lua b/build/cyan/fs/path.lua index e2e3c2e..66268ab 100644 --- a/build/cyan/fs/path.lua +++ b/build/cyan/fs/path.lua @@ -44,7 +44,8 @@ local function parse_string_path(s, use_os_sep) s = s:gsub(sep .. "+", sep) if s == "" then return {} - elseif s:sub(-1) == sep then + end + if s:sub(-1) == sep then s = s:sub(1, -2) end @@ -84,15 +85,15 @@ end function path.ensure(s, use_os_sep) if type(s) == "string" then return path.new(s, use_os_sep) - else - return s end + return s end local function string_is_absolute_path(p) if path.separator == "/" then return p:sub(1, 1) == "/" - elseif path.separator == "\\" then + end + if path.separator == "\\" then return p:match("^%a:$") end end @@ -143,9 +144,11 @@ function Path:is_absolute() if #self < 1 then return false end if path.separator == "/" then return self[1] == "" - elseif path.separator == "\\" then + end + if path.separator == "\\" then return self[1]:match("^%a:$") end + return false end @@ -287,9 +290,8 @@ function Path:mkdir() local succ, err = self:mk_parent_dirs() if succ then return lfs.mkdir(self:to_real_path()) - else - return false, err end + return false, err end diff --git a/build/cyan/graph.lua b/build/cyan/graph.lua index 51288d8..b4b68c0 100644 --- a/build/cyan/graph.lua +++ b/build/cyan/graph.lua @@ -239,9 +239,8 @@ function Dag:insert_file(fstr, in_dir) local cycles = check_for_cycles(self) if cycles then return false, cycles - else - return true end + return true end @@ -271,9 +270,8 @@ function graph.scan_dir(dir, include, exclude) local cycles = check_for_cycles(d) if cycles then return nil, cycles - else - return d end + return d end return graph diff --git a/build/cyan/interaction.lua b/build/cyan/interaction.lua index a20129b..ef1e92c 100644 --- a/build/cyan/interaction.lua +++ b/build/cyan/interaction.lua @@ -1,9 +1,12 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local string = _tl_compat and _tl_compat.string or string +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local io = _tl_compat and _tl_compat.io or io; local string = _tl_compat and _tl_compat.string or string local log = require("cyan.log") local cs = require("cyan.colorstring") local ansi = require("cyan.ansi") +local util = require("cyan.util") + +local ivalues = util.tab.ivalues local interaction = {} @@ -19,7 +22,7 @@ end local function to_string_set(list) local result = {} - for _, v in ipairs(list) do + for v in ivalues(list) do result[v:lower()] = true end return result @@ -73,7 +76,8 @@ function interaction.yes_no_prompt( if affirm_set[input] then return true - elseif deny_set[input] then + end + if deny_set[input] then return false end end diff --git a/build/cyan/log.lua b/build/cyan/log.lua index 8a716a2..eae1d7f 100644 --- a/build/cyan/log.lua +++ b/build/cyan/log.lua @@ -83,9 +83,8 @@ do inspect = function(x) if type(x) == "string" then return x - else - return actual_inspect(x, inspect_opts) end + return actual_inspect(x, inspect_opts) end else inspect = tostring diff --git a/build/cyan/sandbox.lua b/build/cyan/sandbox.lua index b75a693..72fbc89 100644 --- a/build/cyan/sandbox.lua +++ b/build/cyan/sandbox.lua @@ -39,9 +39,9 @@ function Sandbox:run(max_instructions, ...) table.remove(res, 1) self._result = res return true - else - return false, res[2] .. "\n" .. debug.traceback(t) end + + return false, res[2] .. "\n" .. debug.traceback(t) end function Sandbox:result() diff --git a/build/cyan/tlcommon.lua b/build/cyan/tlcommon.lua index e43607b..a30b8a1 100644 --- a/build/cyan/tlcommon.lua +++ b/build/cyan/tlcommon.lua @@ -1,4 +1,4 @@ -local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local load = _tl_compat and _tl_compat.load or load; local package = _tl_compat and _tl_compat.package or package; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack +local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = true, require('compat53.module'); if p then _tl_compat = m end end; local load = _tl_compat and _tl_compat.load or load; local package = _tl_compat and _tl_compat.package or package; local pairs = _tl_compat and _tl_compat.pairs or pairs; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table; local _tl_table_unpack = unpack or table.unpack @@ -162,7 +162,8 @@ local highlights_by_content = { local function highlight_token(tk) if highlights_by_content[tk.tk] then return cs.highlight(highlights_by_content[tk.tk], tk.tk) - elseif highlights_by_kind[tk.kind] then + end + if highlights_by_kind[tk.kind] then return cs.highlight(highlights_by_kind[tk.kind], tk.tk) end return cs.new(tk.tk == "$EOF$" and "" or tk.tk) @@ -179,7 +180,7 @@ function common.syntax_highlight(s) local tks = tl.lex(s) local highlighted = cs.new() local last_x = 1 - for _, tk in ipairs(tks) do + for tk in ivalues(tks) do local ts = count_tabs(s:sub(last_x, tk.x - 1)) if ts > 0 then @@ -283,9 +284,8 @@ function common.report_result(r, c) if arr and #arr > 0 then common.report_errors(logger, arr, r.filename, category) return false - else - return true end + return true end report(log.warn, warnings, "warning") diff --git a/build/cyan/util.lua b/build/cyan/util.lua index 950b936..87c7552 100644 --- a/build/cyan/util.lua +++ b/build/cyan/util.lua @@ -59,7 +59,7 @@ end function tab.set(lst) local s = {} - for _, v in ipairs(lst) do + for v in tab.ivalues(lst) do s[v] = true end return s @@ -81,11 +81,8 @@ function tab.map_ipairs(t, fn) local i = 0 return function() i = i + 1 - if not t[i] then - return - else - return i, fn(t[i]) - end + if not t[i] then return end + return i, fn(t[i]) end end @@ -109,7 +106,7 @@ end function tab.filter(t, pred) local pass = {} local fail = {} - for _, v in ipairs(t) do + for v in tab.ivalues(t) do table.insert(pred(v) and pass or fail, v) end return pass, fail @@ -121,10 +118,10 @@ function tab.merge_list(a, b) local new_list = {} a = a or {} b = b or {} - for _, v in ipairs(a) do + for v in tab.ivalues(a) do table.insert(new_list, v) end - for _, v in ipairs(b) do + for v in tab.ivalues(b) do table.insert(new_list, v) end return new_list @@ -133,7 +130,7 @@ end function tab.contains(t, val) - for _, v in ipairs(t) do + for v in tab.ivalues(t) do if val == v then return true end diff --git a/src/cyan/colorstring.tl b/src/cyan/colorstring.tl index 3953023..f47bb4a 100644 --- a/src/cyan/colorstring.tl +++ b/src/cyan/colorstring.tl @@ -29,7 +29,7 @@ end --- Note that `ColorString:len() >= ColorString:tostring():len()` function ColorString:len(): integer local l = 0 - for _, chunk in ipairs(self.content) do + for chunk in ivalues(self.content) do if chunk is string then l = l + #chunk end @@ -39,7 +39,7 @@ end local function append(base: ColorString, other: string | ColorString) if other is ColorString then - for _, chunk in ipairs(other.content) do + for chunk in ivalues(other.content) do table.insert(base.content, chunk) end else @@ -72,9 +72,8 @@ function ColorString:tostring(): string return table.concat(map(self.content, function(chunk: {integer} | string): string if chunk is string then return chunk - else - return ansi.CSI .. table.concat(map(chunk, tostring as function(integer): string), ";") .. "m" end + return ansi.CSI .. table.concat(map(chunk, tostring as function(integer): string), ";") .. "m" end)) end @@ -160,9 +159,8 @@ end function colorstring.copy(str: string | ColorString): string | ColorString if str is string then return str - else - return str:copy() end + return str:copy() end local cs = colorstring.colors diff --git a/src/cyan/command.tl b/src/cyan/command.tl index a19f93f..21ad63e 100644 --- a/src/cyan/command.tl +++ b/src/cyan/command.tl @@ -10,8 +10,8 @@ local fs = require("cyan.fs") local log = require("cyan.log") local util = require("cyan.util") -local merge_list , sort , from , keys , contains - = util.tab.merge_list, util.tab.sort_in_place, util.tab.from, util.tab.keys, util.tab.contains +local merge_list , sort , from , keys , contains , ivalues + = util.tab.merge_list, util.tab.sort_in_place, util.tab.from, util.tab.keys, util.tab.contains, util.tab.ivalues local record Args command: string @@ -76,7 +76,7 @@ function command.new(cmd: Command) commands[cmd.name] = cmd if cmd.script_hooks then - for _, h in ipairs(cmd.script_hooks) do + for h in ivalues(cmd.script_hooks) do hooks[cmd.name .. ":" .. h] = true end end diff --git a/src/cyan/commands/build.tl b/src/cyan/commands/build.tl index 1a1bd29..e783fc5 100644 --- a/src/cyan/commands/build.tl +++ b/src/cyan/commands/build.tl @@ -19,7 +19,8 @@ local function exists_and_is_dir(prefix: string, p: fs.Path): boolean if not p:exists() then log.err(string.format("%s %q does not exist", prefix, p:to_real_path())) return false - elseif not p:is_directory() then + end + if not p:is_directory() then log.err(string.format("%s %q is not a directory", prefix, p:to_real_path())) return false end @@ -283,19 +284,19 @@ local function build(args: command.Args, loaded_config: config.Config, starting_ log.err("Unable to prune ", kind, " '", disp, "': ", err) end end - for _, p in ipairs(unexpected_files) do + for p in ivalues(unexpected_files) do prune(p, "file") end - for _, p in ipairs(unexpected_directories) do + for p in ivalues(unexpected_directories) do prune(p, "directory") end else local strs : {string | cs.ColorString} = {} - for _, p in ipairs(unexpected_files) do + for p in ivalues(unexpected_files) do table.insert(strs, "\n ") table.insert(strs, display_filename(p)) end - for _, p in ipairs(unexpected_directories) do + for p in ivalues(unexpected_directories) do table.insert(strs, "\n ") table.insert(strs, display_filename(p, true)) end diff --git a/src/cyan/commands/check-gen.tl b/src/cyan/commands/check-gen.tl index 118b24f..54176d9 100644 --- a/src/cyan/commands/check-gen.tl +++ b/src/cyan/commands/check-gen.tl @@ -13,7 +13,8 @@ local log = require("cyan.log") local fs = require("cyan.fs") local util = require("cyan.util") -local map_ipairs = util.tab.map_ipairs +local map_ipairs , ivalues + = util.tab.map_ipairs, util.tab.ivalues local function command_exec(should_compile: boolean): command.CommandFn return function(args: command.Args, loaded_config: config.Config, starting_dir: fs.Path): integer @@ -120,7 +121,7 @@ local function command_exec(should_compile: boolean): command.CommandFn if should_compile then if exit ~= 0 then return exit end - for _, data in ipairs(to_write) do + for data in ivalues(to_write) do local fh , err = io.open(data.outfile:to_real_path(), "w") if fh then local generated , gen_err = common.compile_ast(data.output_ast, loaded_config.gen_target) diff --git a/src/cyan/commands/initialize.tl b/src/cyan/commands/initialize.tl index c812078..c13c581 100644 --- a/src/cyan/commands/initialize.tl +++ b/src/cyan/commands/initialize.tl @@ -9,6 +9,9 @@ local config = require("cyan.config") local cs = require("cyan.colorstring") local fs = require("cyan.fs") local log = require("cyan.log") +local util = require("cyan.util") + +local ivalues = util.tab.ivalues local function exec(args: command.Args, loaded_config: config.Config, starting_dir: fs.Path): integer if not args.force and loaded_config.loaded_from then @@ -43,15 +46,14 @@ local function exec(args: command.Args, loaded_config: config.Config, starting_d if ok then log.info("Created directory ", cs.highlight(cs.colors.file, p:to_real_path())) return true - else - log.err("Unable to create directory ", cs.highlight(cs.colors.file, p:to_real_path()), ":\n ", err) - return false end + log.err("Unable to create directory ", cs.highlight(cs.colors.file, p:to_real_path()), ":\n ", err) + return false end return true end - for _, p in ipairs{directory, directory .. source, directory .. build} do + for p in ivalues{directory, directory .. source, directory .. build} do if not try_mkdir(p) then return 1 end @@ -71,7 +73,7 @@ local function exec(args: command.Args, loaded_config: config.Config, starting_d return end ins(1, "%s = {\n", name) - for _, entry in ipairs(arr) do + for entry in ivalues(arr) do ins(2, "%q,\n", entry) end ins(1, "},\n", name) diff --git a/src/cyan/commands/run.tl b/src/cyan/commands/run.tl index e2e61c3..d35a5b6 100644 --- a/src/cyan/commands/run.tl +++ b/src/cyan/commands/run.tl @@ -74,10 +74,9 @@ local function run(args: command.Args, loaded_config: config.Config, starting_di local ok , err = box:run(1000000000) if ok then return 0 - else - log.err("Error in script:\n", err) - return 1 end + log.err("Error in script:\n", err) + return 1 end command.new{ diff --git a/src/cyan/config.tl b/src/cyan/config.tl index e41d912..417ac8c 100644 --- a/src/cyan/config.tl +++ b/src/cyan/config.tl @@ -8,8 +8,8 @@ local fs = require("cyan.fs") local sandbox = require("cyan.sandbox") local util = require("cyan.util") -local keys , sort , from , values - = util.tab.keys, util.tab.sort_in_place, util.tab.from, util.tab.values +local keys , sort , from , values , ivalues + = util.tab.keys, util.tab.sort_in_place, util.tab.from, util.tab.values, util.tab.ivalues ---@desc --- The config data @@ -53,7 +53,7 @@ local config = { local function get_types_in_array(val: {any}, typefn: function(any): string): {string} typefn = typefn or type local set = {} - for _, v in ipairs(val) do + for v in ivalues(val) do set[typefn(v)] = true end return sort(from(keys(set))) @@ -193,7 +193,7 @@ function config.is_config(c: any): Config, {string}, {string} local function verify_warnings(key: string) local arr = (c as {string:{string}})[key] if arr then - for _, warning in ipairs(arr) do + for warning in ivalues(arr) do if not tl.warning_kinds[warning as tl.WarningKind] then table.insert(errs, string.format("Unknown warning in %s: %q", key, warning)) end @@ -205,9 +205,9 @@ function config.is_config(c: any): Config, {string}, {string} if #errs > 0 then return nil, errs, warnings - else - return c as Config, nil, warnings end + + return c as Config, nil, warnings end ---@desc diff --git a/src/cyan/fs/path.tl b/src/cyan/fs/path.tl index 7a76f56..ffda156 100644 --- a/src/cyan/fs/path.tl +++ b/src/cyan/fs/path.tl @@ -44,7 +44,8 @@ local function parse_string_path(s: string, use_os_sep: boolean): {string} s = s:gsub(sep .. "+", sep) if s == "" then return {} - elseif s:sub(-1) == sep then + end + if s:sub(-1) == sep then s = s:sub(1, -2) end @@ -84,15 +85,15 @@ end function path.ensure(s: string | Path, use_os_sep: boolean): Path if s is string then return path.new(s, use_os_sep) - else - return s end + return s end local function string_is_absolute_path(p: string): boolean if path.separator == "/" then return p:sub(1, 1) == "/" - elseif path.separator == "\\" then + end + if path.separator == "\\" then return p:match("^%a:$") as boolean end end @@ -143,9 +144,11 @@ function Path:is_absolute(): boolean if #self < 1 then return false end if path.separator == "/" then return self[1] == "" - elseif path.separator == "\\" then + end + if path.separator == "\\" then return self[1]:match("^%a:$") as boolean end + return false end ---@desc @@ -287,9 +290,8 @@ function Path:mkdir(): boolean, string local succ , err = self:mk_parent_dirs() if succ then return lfs.mkdir(self:to_real_path()) - else - return false, err end + return false, err end ---@desc diff --git a/src/cyan/graph.tl b/src/cyan/graph.tl index 6c6f0e7..320f8f7 100644 --- a/src/cyan/graph.tl +++ b/src/cyan/graph.tl @@ -239,9 +239,8 @@ function Dag:insert_file(fstr: string | fs.Path, in_dir: string | fs.Path): bool local cycles = check_for_cycles(self) if cycles then return false, cycles - else - return true end + return true end ---@desc @@ -271,9 +270,8 @@ function graph.scan_dir(dir: string | fs.Path, include: {string}, exclude: {stri local cycles = check_for_cycles(d) if cycles then return nil, cycles - else - return d end + return d end return graph diff --git a/src/cyan/interaction.tl b/src/cyan/interaction.tl index ec9bbd1..41f7773 100644 --- a/src/cyan/interaction.tl +++ b/src/cyan/interaction.tl @@ -4,6 +4,9 @@ local log = require("cyan.log") local cs = require("cyan.colorstring") local ansi = require("cyan.ansi") +local util = require("cyan.util") + +local ivalues = util.tab.ivalues local interaction = {} @@ -19,7 +22,7 @@ end local function to_string_set(list: {string}): {string:boolean} local result = {} - for _, v in ipairs(list) do + for v in ivalues(list) do result[v:lower()] = true end return result @@ -73,7 +76,8 @@ function interaction.yes_no_prompt( if affirm_set[input] then return true - elseif deny_set[input] then + end + if deny_set[input] then return false end end diff --git a/src/cyan/log.tl b/src/cyan/log.tl index 2c8e297..6c1c935 100644 --- a/src/cyan/log.tl +++ b/src/cyan/log.tl @@ -83,9 +83,8 @@ do inspect = function(x: any): string if x is string then return x - else - return actual_inspect(x, inspect_opts) end + return actual_inspect(x, inspect_opts) end else inspect = tostring diff --git a/src/cyan/sandbox.tl b/src/cyan/sandbox.tl index c231d64..fd9e30c 100644 --- a/src/cyan/sandbox.tl +++ b/src/cyan/sandbox.tl @@ -39,9 +39,9 @@ function Sandbox:run(max_instructions: integer, ...: any): boolean, string table.remove(res, 1) self._result = res return true - else - return false, res[2] as string .. "\n" .. debug.traceback(t) end + + return false, res[2] as string .. "\n" .. debug.traceback(t) end function Sandbox:result(): any... diff --git a/src/cyan/tlcommon.tl b/src/cyan/tlcommon.tl index 5563a8c..2b8b971 100644 --- a/src/cyan/tlcommon.tl +++ b/src/cyan/tlcommon.tl @@ -162,7 +162,8 @@ local highlights_by_content : {string:{integer}} = { local function highlight_token(tk: Token): cs.ColorString if highlights_by_content[tk.tk] then return cs.highlight(highlights_by_content[tk.tk], tk.tk) - elseif highlights_by_kind[tk.kind] then + end + if highlights_by_kind[tk.kind] then return cs.highlight(highlights_by_kind[tk.kind], tk.tk) end return cs.new(tk.tk == "$EOF$" and "" or tk.tk) @@ -179,7 +180,7 @@ function common.syntax_highlight(s: string): cs.ColorString local tks = tl.lex(s) as {Token} local highlighted = cs.new() local last_x = 1 - for _, tk in ipairs(tks) do + for tk in ivalues(tks) do -- account for tabs local ts = count_tabs(s:sub(last_x, tk.x - 1)) if ts > 0 then @@ -283,9 +284,8 @@ function common.report_result(r: tl.Result, c: config.Config): boolean if arr and #arr > 0 then common.report_errors(logger, arr, r.filename, category) return false - else - return true end + return true end report(log.warn, warnings, "warning") diff --git a/src/cyan/util.tl b/src/cyan/util.tl index a98fbb7..e6f6e16 100644 --- a/src/cyan/util.tl +++ b/src/cyan/util.tl @@ -59,7 +59,7 @@ end --- Create a Set from a list function tab.set(lst: {Value}): {Value:boolean} local s = {} - for _, v in ipairs(lst) do + for v in tab.ivalues(lst) do s[v] = true end return s @@ -81,11 +81,8 @@ function tab.map_ipairs(t: {Value}, fn: function(Value): Map local i = 0 return function(): integer, MappedValue i = i + 1 - if not t[i] then - return - else - return i, fn(t[i]) - end + if not t[i] then return end + return i, fn(t[i]) end end @@ -109,7 +106,7 @@ end function tab.filter(t: {Value}, pred: function(Value): boolean): {Value}, {Value} local pass : {Value} = {} local fail : {Value} = {} - for _, v in ipairs(t) do + for v in tab.ivalues(t) do table.insert(pred(v) and pass or fail, v) end return pass, fail @@ -121,10 +118,10 @@ function tab.merge_list(a: {Value}, b: {Value}): {Value} local new_list = {} a = a or {} b = b or {} - for _, v in ipairs(a) do + for v in tab.ivalues(a) do table.insert(new_list, v) end - for _, v in ipairs(b) do + for v in tab.ivalues(b) do table.insert(new_list, v) end return new_list @@ -133,7 +130,7 @@ end ---@desc --- Report if an array contains an element (as determined by the `==` operator) function tab.contains(t: {Value}, val: Value): boolean - for _, v in ipairs(t) do + for v in tab.ivalues(t) do if val == v then return true end