From 9b4a7932b679a6e06608dfafda40079af361e111 Mon Sep 17 00:00:00 2001 From: Sam Roxanne Date: Fri, 25 Jun 2021 09:46:38 -0500 Subject: [PATCH] luaposix time, way faster now --- src/application.lua | 21 ++++++++++++--------- src/ast2.lua | 7 +++---- src/cfg/minifier_providers.lua | 11 +++++------ src/directive_provider.lua | 15 +++++++-------- src/directives/include.lua | 5 ++--- src/generator2.lua | 6 +++--- src/libluacomp.lua | 31 +++++++++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 33 deletions(-) diff --git a/src/application.lua b/src/application.lua index a545b09..96c2594 100644 --- a/src/application.lua +++ b/src/application.lua @@ -8,6 +8,9 @@ local function dprint(...) end end +local stat = require("posix.sys.stat") +local dirent = require("posix.dirent") + --#include "src/shell_var.lua" --#include "src/luacomp_vars.lua" --#include "src/libluacomp.lua" @@ -51,11 +54,9 @@ local file = args.input _sv("LUACOMP_MINIFIER", args.minifier) local f if (file ~= "-") then + local sr, er = stat.stat(file) + if not sr then lc_error("luacomp", er) end f = io.open(file, "r") - if not f then - io.stderr:write("ERROR: File `"..file.."' does not exist!\n") - os.exit(1) - end else f = io.stdin end @@ -64,16 +65,18 @@ local ocode = luacomp.process_file(f, (file == "-") and "stdin" or file, args.ge local minifier = providers[args.minifier] dprint("Minifier: "..args.minifier, minifier) if not minifier then - io.stderr:write("ERROR: Postprocessor `"..args.minifier.."' not found!\n") - os.exit(1) + lc_error("luacomp", "Postprocessor "..args.minifier.." not found!") + --io.stderr:write("ERROR: Postprocessor `"..args.minifier.."' not found!\n") + --os.exit(1) end dprint("Running...") local rcode, err = minifier(ocode) if (not rcode) then - io.stderr:write("ERROR: Error for postprocessor `"..args.minifier.."': \n") - io.stderr:write(err) - os.exit(1) + --io.stderr:write("ERROR: Error for postprocessor `"..args.minifier.."': \n") + --io.stderr:write(err) + --os.exit(1) + lc_error(args.minifier, "Postprocessor error:\n"..err) end local of diff --git a/src/ast2.lua b/src/ast2.lua index 7baeaaa..7e81c03 100644 --- a/src/ast2.lua +++ b/src/ast2.lua @@ -87,8 +87,7 @@ do function ast.parser_error(str, err) local y, x = str:get_yx() --print(y, x) - io.stderr:write(string.format("%s(%d:%d): %s\n", str.file, y or 0, x or 0, err)) - os.exit(1) + lc_error(@[{_GENERATOR.fname}], string.format("%s(%d:%d): %s\n", str.file, y or 0, x or 0, err)) end function ast.unescape(escaped_string) @@ -306,7 +305,7 @@ do local c = str:peek() if c ~= " " and c ~= "\n" and c ~= "" then str:set(apos) - ast.parser_error(str, "malformed string, got "..c) + ast.parser_error(str, "malformed string") end table.insert(args, sval) elseif str:peek() == "\'" then @@ -333,7 +332,7 @@ do local c = str:peek() if c ~= " " and c ~= "\n" and c ~= "" then str:set(apos) - ast.parser_error(str, "malformed code block, got "..c) + ast.parser_error(str, "malformed code block") end table.insert(args, {type="lua_span", val=sval}) elseif str:peek() == "\n" then diff --git a/src/cfg/minifier_providers.lua b/src/cfg/minifier_providers.lua index 4278281..af52236 100644 --- a/src/cfg/minifier_providers.lua +++ b/src/cfg/minifier_providers.lua @@ -108,7 +108,7 @@ end setmetatable(providers, {__index=function(t, i) for i=1, #postproc_paths do - if (os.execute("stat "..postproc_paths[i].."/"..i..".lua 1>/dev/null 2>&1")) then + if stat.stat(postproc_paths[i].."/"..i..".lua") then providers[i] = loadfile(postproc_paths[i].."/"..i..".lua")() return providers[i] end @@ -118,11 +118,10 @@ end}) local function preload_providers() --Do this in the best way possible for i=1, #postproc_paths do - if (os.execute("stat "..postproc_paths[i].."1>/dev/null 2>&1")) then - local fh = io.popen("ls "..postproc_paths[i], "r") - for line in fh:lines() do - if (line:match("%.lua$")) then - providers[line:sub(1, #line-4)] = loadfile(postproc_paths[i].."/"..line)() + if stat.stat(postproc_paths[i]) then + for ent in dirent.files(postproc_paths[i]) do + if ent:match("%.lua$") then + providers[ent:sub(1, #ent-4)] = loadfile(postproc_paths[i].."/"..ent)() end end end diff --git a/src/directive_provider.lua b/src/directive_provider.lua index 08fc285..eb2bc47 100644 --- a/src/directive_provider.lua +++ b/src/directive_provider.lua @@ -24,9 +24,9 @@ setmetatable(directives, {__index=function(t, i) for i=1, #directive_paths do - if (os.execute("stat "..directive_paths[i].."/"..i..".lua 1>/dev/null 2>&1")) then - directives[i] = loadfile(directive_paths[i].."/"..i..".lua")() - return directives[i] + if stat.stat(directive_paths[i].."/"..i..".lua") then + providers[i] = loadfile(directive_paths[i].."/"..i..".lua")() + return providers[i] end end end}) @@ -34,11 +34,10 @@ end}) local function preload_directives() --Do this in the best way possible for i=1, #directive_paths do - if (os.execute("stat "..directive_paths[i].." 1>/dev/null 2>&1")) then - local fh = io.popen("ls "..directive_paths[i], "r") - for line in fh:lines() do - if (line:match("%.lua$")) then - directives[line:sub(1, #line-4)] = loadfile(directive_paths[i].."/"..line)() + if stat.stat(directive_paths[i]) then + for ent in dirent.files(directive_paths[i]) do + if ent:match("%.lua$") then + providers[ent:sub(1, #ent-4)] = loadfile(directive_paths[i].."/"..ent)() end end end diff --git a/src/directives/include.lua b/src/directives/include.lua index a98bc82..d4a5cd8 100644 --- a/src/directives/include.lua +++ b/src/directives/include.lua @@ -1,7 +1,6 @@ function directives.include(env, file) - if (not os.execute("stat "..file..">/dev/null")) then - return false, "File `"..file.."' does not exist!" - end + local sr, err = stat.stat(file) + if not sr then return false, err end --[[local f = io.open(file, "r") local fast = mkast(f, file) fast.file = file diff --git a/src/generator2.lua b/src/generator2.lua index 9481a5c..f324b23 100644 --- a/src/generator2.lua +++ b/src/generator2.lua @@ -38,7 +38,7 @@ do end function generator.run_gcode(fname, gcode) - local env = {code = ""} + local env = {code = "", fname=fname} local fenv = {} for k, v in pairs(_G) do fenv[k] = v @@ -46,9 +46,9 @@ do fenv._G = fenv fenv._GENERATOR = env function fenv.call_directive(dname, ...) - if not directives[dname] then error("invalid directive "..dname) end + if not directives[dname] then lc_error(@[{_GENERATOR.fname}], "invalid directive "..dname) end local r, er = directives[dname](env, ...) - assert(r, er) + if not r then lc_error(@[{_GENERATOR.fname}], er) end end function fenv.write_out(code) diff --git a/src/libluacomp.lua b/src/libluacomp.lua index 7e1f97c..c5d71f8 100644 --- a/src/libluacomp.lua +++ b/src/libluacomp.lua @@ -3,6 +3,37 @@ local luacomp = {} local directives = {} +local unistd = require("posix.unistd") + +local function lc_error(name, msg) + if unistd.isatty(2) then + io.stderr:write(string.format("\27[90;1m(%s) \27[31;22m%s\27[0m\n", name, msg)) + else + io.stderr:write(string.format("(%s) %s\n", name, msg)) + end + os.exit(1) +end + +local function lc_warning(name, msg) + if unistd.isatty(2) then + io.stderr:write(string.format("\27[90;1m(%s) \27[33;22m%s\27[0m\n", name, msg)) + else + io.stderr:write(string.format("(%s) %s\n", name, msg)) + end +end + +function luacomp.error(msg) + local inf = debug.getinfo(1) + local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp" + lc_error(name, msg) +end + +function luacomp.warning(msg) + local inf = debug.getinfo(1) + local name = (inf and inf.short_src:match("[^/]+$"):gsub("^[=@]", "")) or "luacomp" + lc_warning(name, msg) +end + --#include "src/ast2.lua" --#include "src/generator2.lua"