1
0
mirror of https://github.com/Adorable-Catgirl/LuaComp.git synced 2024-12-23 08:48:05 +11:00

luacomp3 maybe?

This commit is contained in:
Sam 2023-08-02 12:55:41 -04:00
parent 798d60dadd
commit 81fdcb064b
4 changed files with 84 additions and 0 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "libpreproc"]
path = libpreproc
url = git@github.com:lunaboards-dev/libpreproc.git

1
libpreproc Submodule

@ -0,0 +1 @@
Subproject commit af74760d6113fee5e509cc983163f140a674ae07

12
luacomp3/init.lua Normal file
View File

@ -0,0 +1,12 @@
--@[[if false then]]
local parser = require("luacomp3.parser")
--@[[else]]
--#include "parser.lua" "parser"
--@[[end]]
local p = setmetatable({}, {__index=parser})
p:init()
local f = io.open(arg[1], "rb")
local d = f:read("a")
f:close()
p:parse(d)

68
luacomp3/parser.lua Normal file
View File

@ -0,0 +1,68 @@
local lpp = require("libpreproc")
local p = function(a)return lpp.pattern(a, true)end
local parser = {}
function parser:single_pass()
end
local span = lpp.block(p"[{", p"}]")
local block = lpp.block(p"[[", p"]]")
local parens = lpp.block(p"(", p")")
local luacode = lpp.pattern("@(%d*)")
local shellcode = p"$"
local directive = lpp.pattern("--#!*")
function parser:init()
local parse = lpp.instance()
--Lua code span
parse:add_token(lpp.prefix(luacode, span), function(stream, instance, result)
if #result.matches == 2 and result.matches[1] and result.matches[1] ~= "" then
result.matches.pass = tonumber(result.matches[1], 10)
if (result.matches.pass > 0) then
instance:emit(string.format("@%d[{%s}]", result.matches.pass, result.matches[2]))
return
end
end
instance:write(string.format("write(%s)", result.matches[#result.matches]))
end)
-- Lua code block
parse:add_token(lpp.prefix(luacode, block), function(stream, instance, result)
--print("<m>", table.unpack(result.matches))
if #result.matches == 2 and result.matches[1] and result.matches[1] ~= "" then
result.matches.pass = tonumber(result.matches[1], 10)
if (result.matches.pass > 0) then
instance:emit(string.format("@%d[[%s]]", result.matches.pass, result.matches[2]))
return
end
end
instance:write(result.matches[#result.matches])
end)
-- Shell variable
parse:add_token(lpp.prefix(shellcode, span), function(stream, instance, result)
instance:write(string.format("write(os.getenv(%q))", result.matches[1]))
end)
-- Shell variable, wrapped in quotes
parse:add_token(lpp.prefix(shellcode, parens), function(stream, instance, result)
instance:write(string.format("write('\"'..os.getenv(%q)..'\"')", result.matches[1]))
end)
-- Shell block
parse:add_token(lpp.prefix(shellcode, block), function(stream, instance, matches)
end)
self.parser = parse
end
function parser:parse(code)
self.parser:compile(code)
print(self.parser.code)
local res = self.parser:generate()
print(res)
end
return parser