Implement command-line options for debug, compile, and highlight modes in luash. Update usage instructions and modify runner to handle new execution options, enhancing flexibility for script execution.
This commit is contained in:
26
luash
26
luash
@@ -223,12 +223,32 @@ function start_repl()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- 1. Check for an input file or REPL mode
|
-- 1. Check for an input file or REPL mode
|
||||||
|
local debug_mode = false -- legacy alias for highlight compile
|
||||||
|
local compile_mode = false
|
||||||
|
local highlight_mode = false
|
||||||
local filename = arg[1]
|
local filename = arg[1]
|
||||||
|
if filename == "-d" or filename == "--debug" then
|
||||||
|
debug_mode = true
|
||||||
|
compile_mode = false
|
||||||
|
highlight_mode = true
|
||||||
|
filename = arg[2]
|
||||||
|
elseif filename == "-c" or filename == "--compile" then
|
||||||
|
compile_mode = true
|
||||||
|
highlight_mode = false
|
||||||
|
filename = arg[2]
|
||||||
|
elseif filename == "-C" or filename == "--compile-highlight" then
|
||||||
|
compile_mode = true
|
||||||
|
highlight_mode = true
|
||||||
|
filename = arg[2]
|
||||||
|
end
|
||||||
|
|
||||||
if filename == "-h" or filename == "--help" then
|
if filename == "-h" or filename == "--help" then
|
||||||
print("Usage: luash <script.luash>")
|
print("Usage: luash <script.luash>")
|
||||||
print(" luash -i # Interactive mode")
|
print(" luash -i # Interactive mode")
|
||||||
print(" luash -h # Show this help")
|
print(" luash -c <file> # COMPILE MODE: print Lua and exit (no colors)")
|
||||||
|
print(" luash -C <file> # COMPILE MODE: print Lua with highlighting and exit")
|
||||||
|
print(" luash -d <file> # Print generated Lua (highlighted) then run")
|
||||||
|
print(" luash -h # Show this help")
|
||||||
return
|
return
|
||||||
elseif not filename or filename == "-i" or filename == "--interactive" then
|
elseif not filename or filename == "-i" or filename == "--interactive" then
|
||||||
start_repl()
|
start_repl()
|
||||||
@@ -236,5 +256,5 @@ elseif not filename or filename == "-i" or filename == "--interactive" then
|
|||||||
end
|
end
|
||||||
-- Delegate to runner for file execution
|
-- Delegate to runner for file execution
|
||||||
local runner = require('runner')
|
local runner = require('runner')
|
||||||
runner.run_file(filename)
|
runner.run_file(filename, { debug = debug_mode, compile = compile_mode, highlight = highlight_mode })
|
||||||
|
|
||||||
|
|||||||
26
src/main.lua
26
src/main.lua
@@ -3,11 +3,31 @@ local runner = require('runner')
|
|||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.run(argv)
|
function M.run(argv)
|
||||||
|
local debug_mode = false -- legacy: maps to compile-highlight
|
||||||
|
local compile_mode = false
|
||||||
|
local highlight_mode = false
|
||||||
local filename = argv[1]
|
local filename = argv[1]
|
||||||
|
if filename == "-d" or filename == "--debug" then
|
||||||
|
debug_mode = true
|
||||||
|
highlight_mode = true
|
||||||
|
compile_mode = false
|
||||||
|
filename = argv[2]
|
||||||
|
elseif filename == "-c" or filename == "--compile" then
|
||||||
|
compile_mode = true
|
||||||
|
highlight_mode = false
|
||||||
|
filename = argv[2]
|
||||||
|
elseif filename == "-C" or filename == "--compile-highlight" then
|
||||||
|
compile_mode = true
|
||||||
|
highlight_mode = true
|
||||||
|
filename = argv[2]
|
||||||
|
end
|
||||||
if filename == "-h" or filename == "--help" then
|
if filename == "-h" or filename == "--help" then
|
||||||
print("Usage: luash <script.luash>")
|
print("Usage: luash <script.luash>")
|
||||||
print(" luash -i # Interactive mode")
|
print(" luash -i # Interactive mode")
|
||||||
print(" luash -h # Show this help")
|
print(" luash -c <file> # COMPILE MODE: print Lua and exit (no colors)")
|
||||||
|
print(" luash -C <file> # COMPILE MODE: print Lua with highlighting and exit")
|
||||||
|
print(" luash -d <file> # Print generated Lua (highlighted) then run")
|
||||||
|
print(" luash -h # Show this help")
|
||||||
return
|
return
|
||||||
elseif not filename or filename == "-i" or filename == "--interactive" then
|
elseif not filename or filename == "-i" or filename == "--interactive" then
|
||||||
local entry = require('repl')
|
local entry = require('repl')
|
||||||
@@ -26,7 +46,7 @@ function M.run(argv)
|
|||||||
entry.start(preprocess_luash_repl, load_injected_lib)
|
entry.start(preprocess_luash_repl, load_injected_lib)
|
||||||
return
|
return
|
||||||
else
|
else
|
||||||
runner.run_file(filename)
|
runner.run_file(filename, { debug = debug_mode, compile = compile_mode, highlight = highlight_mode })
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
local preprocess = require('preprocess')
|
local preprocess = require('preprocess')
|
||||||
local injected = require('injected')
|
local injected = require('injected')
|
||||||
|
local util = require('util')
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.run_file(filename)
|
function M.run_file(filename, opts)
|
||||||
local file = io.open(filename, "r")
|
local file = io.open(filename, "r")
|
||||||
if not file then
|
if not file then
|
||||||
print("Error: Cannot open file '" .. filename .. "'")
|
print("Error: Cannot open file '" .. filename .. "'")
|
||||||
@@ -20,6 +21,62 @@ function M.run_file(filename)
|
|||||||
-- Load injected helpers into global scope (not prepended to source)
|
-- Load injected helpers into global scope (not prepended to source)
|
||||||
injected.load()
|
injected.load()
|
||||||
|
|
||||||
|
-- Optional compile output of generated Lua
|
||||||
|
local dbg_env = os.getenv("LUASH_DEBUG")
|
||||||
|
local dbg_flag = opts and opts.debug == true
|
||||||
|
local compile_flag = opts and opts.compile == true
|
||||||
|
local highlight_flag = opts and opts.highlight == true
|
||||||
|
if dbg_flag or compile_flag or (dbg_env == "1" or dbg_env == "true" or dbg_env == "yes") then
|
||||||
|
-- If env var triggered, prefer highlighting
|
||||||
|
if not highlight_flag and (dbg_flag or (dbg_env == "1" or dbg_env == "true" or dbg_env == "yes")) then
|
||||||
|
highlight_flag = true
|
||||||
|
end
|
||||||
|
-- Try to read injected library source for holistic view
|
||||||
|
local injected_src = nil
|
||||||
|
do
|
||||||
|
local base = debug.getinfo(1, "S").source:match("@(.*/)") or "./"
|
||||||
|
base = base:gsub("/src/?$", "/")
|
||||||
|
local lib_path = base .. "src/lib/injected_lib.lua"
|
||||||
|
local f = io.open(lib_path, "r")
|
||||||
|
if f then
|
||||||
|
injected_src = f:read("*a")
|
||||||
|
f:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if injected_src and injected_src ~= "" then
|
||||||
|
print("--- Luash COMPILE MODE: injected library ---")
|
||||||
|
if highlight_flag then
|
||||||
|
local highlighted_inj = util.highlight_lua_ansi(injected_src)
|
||||||
|
if highlighted_inj then
|
||||||
|
io.write(highlighted_inj)
|
||||||
|
if not highlighted_inj:match("\n$") then io.write("\n") end
|
||||||
|
else
|
||||||
|
print(injected_src)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print(injected_src)
|
||||||
|
end
|
||||||
|
print("--- end injected library ---")
|
||||||
|
end
|
||||||
|
|
||||||
|
print("--- Luash COMPILE MODE: generated Lua (" .. filename .. ") ---")
|
||||||
|
if highlight_flag then
|
||||||
|
local highlighted = util.highlight_lua_ansi(source_code)
|
||||||
|
if highlighted then
|
||||||
|
io.write(highlighted)
|
||||||
|
if not highlighted:match("\n$") then io.write("\n") end
|
||||||
|
else
|
||||||
|
print(source_code)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print(source_code)
|
||||||
|
end
|
||||||
|
print("--- end generated Lua ---")
|
||||||
|
|
||||||
|
-- Only skip execution in explicit compile mode
|
||||||
|
if compile_flag then return end
|
||||||
|
end
|
||||||
|
|
||||||
-- Execute
|
-- Execute
|
||||||
local func, err = load(source_code, "@" .. filename)
|
local func, err = load(source_code, "@" .. filename)
|
||||||
if not func then
|
if not func then
|
||||||
|
|||||||
28
src/util.lua
28
src/util.lua
@@ -10,6 +10,12 @@ function M.is_tty()
|
|||||||
return r == true
|
return r == true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.is_stdout_tty()
|
||||||
|
local r = os.execute("test -t 1 >/dev/null 2>&1")
|
||||||
|
if type(r) == "number" then return r == 0 end
|
||||||
|
return r == true
|
||||||
|
end
|
||||||
|
|
||||||
function M.set_raw_mode(enable)
|
function M.set_raw_mode(enable)
|
||||||
if enable then
|
if enable then
|
||||||
os.execute("stty -echo -icanon -isig min 1 time 0")
|
os.execute("stty -echo -icanon -isig min 1 time 0")
|
||||||
@@ -18,6 +24,28 @@ function M.set_raw_mode(enable)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.has_command(cmd)
|
||||||
|
local r = os.execute("command -v " .. cmd .. " >/dev/null 2>&1")
|
||||||
|
if type(r) == "number" then return r == 0 end
|
||||||
|
return r == true
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.highlight_lua_ansi(code)
|
||||||
|
if not M.has_command("highlight") then return nil end
|
||||||
|
local tmpin = os.tmpname()
|
||||||
|
local f = io.open(tmpin, "w")
|
||||||
|
if not f then return nil end
|
||||||
|
f:write(code)
|
||||||
|
f:close()
|
||||||
|
local handle = io.popen("highlight -O ansi -S lua " .. tmpin .. " 2>/dev/null")
|
||||||
|
if not handle then os.remove(tmpin); return nil end
|
||||||
|
local out = handle:read("*a") or ""
|
||||||
|
handle:close()
|
||||||
|
os.remove(tmpin)
|
||||||
|
if out == "" then return nil end
|
||||||
|
return out
|
||||||
|
end
|
||||||
|
|
||||||
function M.script_dir()
|
function M.script_dir()
|
||||||
return (debug.getinfo(2, "S").source:match("@(.*/)") or "./")
|
return (debug.getinfo(2, "S").source:match("@(.*/)") or "./")
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user