From ef1287e5437b236fbd0aacb987d403f31f1214c5 Mon Sep 17 00:00:00 2001 From: dvs1 Date: Thu, 24 Oct 2024 12:32:42 +1000 Subject: Introducing PolygLua, writing Lua in any language. Basically include a script written in any script language, as a string in your Lua code, then call it from your Lua code. --- _.lua | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/_.lua b/_.lua index 12d7461..e3e6a82 100755 --- a/_.lua +++ b/_.lua @@ -1,5 +1,10 @@ #!/usr/bin/env luajit +--[[ + +TODO - I should name this project PolygLua. Gluing things onto Lua, making it a polyglot language. + +]] -- Most of this _ stuff was copied from apt-panopticon. local _ = {} @@ -79,7 +84,7 @@ _.readCmd = function(cmd) end __ = function(c) - local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c} + local exe = {status = 0, lines = {}, logging = false, showing = false, cmd = '', command = c, isScript = false, script = ''} local n = 0 exe.cmd = '{ ' @@ -89,10 +94,16 @@ __ = function(c) exe.cmd = exe.cmd .. l .. ' ; ' end elseif 'string' == type(c) then + exe.isScript = ('#!' == c:sub(1,2)) and (n == 0) for l in string.gmatch(c, "\n*([^\n]+)\n*") do if '' ~= l then - if ('#!' == l:sub(1,2)) and (n == 0) then - -- TODO - should do something with this, not just run it as shell stuff. + if exe.isScript then + if '' == exe.script then + exe.scriptFile = os.tmpname() + D('Creating temporary script file at ' .. exe.scriptFile) + exe.cmd = exe.cmd .. l:sub(3) .. ' ' .. exe.scriptFile .. ' ; ' + end + exe.script = exe.script .. l .. '\n' else n = n + 1 exe.cmd = exe.cmd .. l .. ' ; ' @@ -100,6 +111,14 @@ __ = function(c) end end end + if exe.isScript then + local a, e = io.open(exe.scriptFile, "w") + if nil == a then E("Could not open " .. exe.scriptFile) else + a:write(exe.script) + a:close() + end + exe.cmd = exe.cmd .. 'rm ' .. exe.scriptFile .. ' ; ' + end exe.cmd = exe.cmd .. ' } ' if 1 == n then exe.cmd = c end -- cgit v1.1