luaposix time, way faster now

This commit is contained in:
Sam Roxanne 2021-06-25 09:46:38 -05:00
parent e4741cd571
commit 9b4a7932b6
7 changed files with 63 additions and 33 deletions

View File

@ -8,6 +8,9 @@ local function dprint(...)
end end
end end
local stat = require("posix.sys.stat")
local dirent = require("posix.dirent")
--#include "src/shell_var.lua" --#include "src/shell_var.lua"
--#include "src/luacomp_vars.lua" --#include "src/luacomp_vars.lua"
--#include "src/libluacomp.lua" --#include "src/libluacomp.lua"
@ -51,11 +54,9 @@ local file = args.input
_sv("LUACOMP_MINIFIER", args.minifier) _sv("LUACOMP_MINIFIER", args.minifier)
local f local f
if (file ~= "-") then if (file ~= "-") then
local sr, er = stat.stat(file)
if not sr then lc_error("luacomp", er) end
f = io.open(file, "r") f = io.open(file, "r")
if not f then
io.stderr:write("ERROR: File `"..file.."' does not exist!\n")
os.exit(1)
end
else else
f = io.stdin f = io.stdin
end end
@ -64,16 +65,18 @@ local ocode = luacomp.process_file(f, (file == "-") and "stdin" or file, args.ge
local minifier = providers[args.minifier] local minifier = providers[args.minifier]
dprint("Minifier: "..args.minifier, minifier) dprint("Minifier: "..args.minifier, minifier)
if not minifier then if not minifier then
io.stderr:write("ERROR: Postprocessor `"..args.minifier.."' not found!\n") lc_error("luacomp", "Postprocessor "..args.minifier.." not found!")
os.exit(1) --io.stderr:write("ERROR: Postprocessor `"..args.minifier.."' not found!\n")
--os.exit(1)
end end
dprint("Running...") dprint("Running...")
local rcode, err = minifier(ocode) local rcode, err = minifier(ocode)
if (not rcode) then if (not rcode) then
io.stderr:write("ERROR: Error for postprocessor `"..args.minifier.."': \n") --io.stderr:write("ERROR: Error for postprocessor `"..args.minifier.."': \n")
io.stderr:write(err) --io.stderr:write(err)
os.exit(1) --os.exit(1)
lc_error(args.minifier, "Postprocessor error:\n"..err)
end end
local of local of

View File

@ -87,8 +87,7 @@ do
function ast.parser_error(str, err) function ast.parser_error(str, err)
local y, x = str:get_yx() local y, x = str:get_yx()
--print(y, x) --print(y, x)
io.stderr:write(string.format("%s(%d:%d): %s\n", str.file, y or 0, x or 0, err)) lc_error(@[{_GENERATOR.fname}], string.format("%s(%d:%d): %s\n", str.file, y or 0, x or 0, err))
os.exit(1)
end end
function ast.unescape(escaped_string) function ast.unescape(escaped_string)
@ -306,7 +305,7 @@ do
local c = str:peek() local c = str:peek()
if c ~= " " and c ~= "\n" and c ~= "" then if c ~= " " and c ~= "\n" and c ~= "" then
str:set(apos) str:set(apos)
ast.parser_error(str, "malformed string, got "..c) ast.parser_error(str, "malformed string")
end end
table.insert(args, sval) table.insert(args, sval)
elseif str:peek() == "\'" then elseif str:peek() == "\'" then
@ -333,7 +332,7 @@ do
local c = str:peek() local c = str:peek()
if c ~= " " and c ~= "\n" and c ~= "" then if c ~= " " and c ~= "\n" and c ~= "" then
str:set(apos) str:set(apos)
ast.parser_error(str, "malformed code block, got "..c) ast.parser_error(str, "malformed code block")
end end
table.insert(args, {type="lua_span", val=sval}) table.insert(args, {type="lua_span", val=sval})
elseif str:peek() == "\n" then elseif str:peek() == "\n" then

View File

@ -108,7 +108,7 @@ end
setmetatable(providers, {__index=function(t, i) setmetatable(providers, {__index=function(t, i)
for i=1, #postproc_paths do 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")() providers[i] = loadfile(postproc_paths[i].."/"..i..".lua")()
return providers[i] return providers[i]
end end
@ -118,11 +118,10 @@ end})
local function preload_providers() local function preload_providers()
--Do this in the best way possible --Do this in the best way possible
for i=1, #postproc_paths do for i=1, #postproc_paths do
if (os.execute("stat "..postproc_paths[i].."1>/dev/null 2>&1")) then if stat.stat(postproc_paths[i]) then
local fh = io.popen("ls "..postproc_paths[i], "r") for ent in dirent.files(postproc_paths[i]) do
for line in fh:lines() do if ent:match("%.lua$") then
if (line:match("%.lua$")) then providers[ent:sub(1, #ent-4)] = loadfile(postproc_paths[i].."/"..ent)()
providers[line:sub(1, #line-4)] = loadfile(postproc_paths[i].."/"..line)()
end end
end end
end end

View File

@ -24,9 +24,9 @@
setmetatable(directives, {__index=function(t, i) setmetatable(directives, {__index=function(t, i)
for i=1, #directive_paths do for i=1, #directive_paths do
if (os.execute("stat "..directive_paths[i].."/"..i..".lua 1>/dev/null 2>&1")) then if stat.stat(directive_paths[i].."/"..i..".lua") then
directives[i] = loadfile(directive_paths[i].."/"..i..".lua")() providers[i] = loadfile(directive_paths[i].."/"..i..".lua")()
return directives[i] return providers[i]
end end
end end
end}) end})
@ -34,11 +34,10 @@ end})
local function preload_directives() local function preload_directives()
--Do this in the best way possible --Do this in the best way possible
for i=1, #directive_paths do for i=1, #directive_paths do
if (os.execute("stat "..directive_paths[i].." 1>/dev/null 2>&1")) then if stat.stat(directive_paths[i]) then
local fh = io.popen("ls "..directive_paths[i], "r") for ent in dirent.files(directive_paths[i]) do
for line in fh:lines() do if ent:match("%.lua$") then
if (line:match("%.lua$")) then providers[ent:sub(1, #ent-4)] = loadfile(directive_paths[i].."/"..ent)()
directives[line:sub(1, #line-4)] = loadfile(directive_paths[i].."/"..line)()
end end
end end
end end

View File

@ -1,7 +1,6 @@
function directives.include(env, file) function directives.include(env, file)
if (not os.execute("stat "..file..">/dev/null")) then local sr, err = stat.stat(file)
return false, "File `"..file.."' does not exist!" if not sr then return false, err end
end
--[[local f = io.open(file, "r") --[[local f = io.open(file, "r")
local fast = mkast(f, file) local fast = mkast(f, file)
fast.file = file fast.file = file

View File

@ -38,7 +38,7 @@ do
end end
function generator.run_gcode(fname, gcode) function generator.run_gcode(fname, gcode)
local env = {code = ""} local env = {code = "", fname=fname}
local fenv = {} local fenv = {}
for k, v in pairs(_G) do for k, v in pairs(_G) do
fenv[k] = v fenv[k] = v
@ -46,9 +46,9 @@ do
fenv._G = fenv fenv._G = fenv
fenv._GENERATOR = env fenv._GENERATOR = env
function fenv.call_directive(dname, ...) 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, ...) local r, er = directives[dname](env, ...)
assert(r, er) if not r then lc_error(@[{_GENERATOR.fname}], er) end
end end
function fenv.write_out(code) function fenv.write_out(code)

View File

@ -3,6 +3,37 @@ local luacomp = {}
local directives = {} 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/ast2.lua"
--#include "src/generator2.lua" --#include "src/generator2.lua"