diff --git a/luash b/luash index 77f6b3d..0dfe261 100755 --- a/luash +++ b/luash @@ -223,12 +223,32 @@ function start_repl() end -- 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] +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 print("Usage: luash ") - print(" luash -i # Interactive mode") - print(" luash -h # Show this help") + print(" luash -i # Interactive mode") + print(" luash -c # COMPILE MODE: print Lua and exit (no colors)") + print(" luash -C # COMPILE MODE: print Lua with highlighting and exit") + print(" luash -d # Print generated Lua (highlighted) then run") + print(" luash -h # Show this help") return elseif not filename or filename == "-i" or filename == "--interactive" then start_repl() @@ -236,5 +256,5 @@ elseif not filename or filename == "-i" or filename == "--interactive" then end -- Delegate to runner for file execution local runner = require('runner') -runner.run_file(filename) +runner.run_file(filename, { debug = debug_mode, compile = compile_mode, highlight = highlight_mode }) diff --git a/src/main.lua b/src/main.lua index 64d92d0..49313d1 100644 --- a/src/main.lua +++ b/src/main.lua @@ -3,11 +3,31 @@ local runner = require('runner') local M = {} 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] + 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 print("Usage: luash ") - print(" luash -i # Interactive mode") - print(" luash -h # Show this help") + print(" luash -i # Interactive mode") + print(" luash -c # COMPILE MODE: print Lua and exit (no colors)") + print(" luash -C # COMPILE MODE: print Lua with highlighting and exit") + print(" luash -d # Print generated Lua (highlighted) then run") + print(" luash -h # Show this help") return elseif not filename or filename == "-i" or filename == "--interactive" then local entry = require('repl') @@ -26,7 +46,7 @@ function M.run(argv) entry.start(preprocess_luash_repl, load_injected_lib) return else - runner.run_file(filename) + runner.run_file(filename, { debug = debug_mode, compile = compile_mode, highlight = highlight_mode }) end end diff --git a/src/runner.lua b/src/runner.lua index 7c5bfa9..6e0dad2 100644 --- a/src/runner.lua +++ b/src/runner.lua @@ -1,9 +1,10 @@ local preprocess = require('preprocess') local injected = require('injected') +local util = require('util') local M = {} -function M.run_file(filename) +function M.run_file(filename, opts) local file = io.open(filename, "r") if not file then 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) 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 local func, err = load(source_code, "@" .. filename) if not func then diff --git a/src/util.lua b/src/util.lua index 8edceb4..d9859f2 100644 --- a/src/util.lua +++ b/src/util.lua @@ -10,6 +10,12 @@ function M.is_tty() return r == true 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) if enable then os.execute("stty -echo -icanon -isig min 1 time 0") @@ -18,6 +24,28 @@ function M.set_raw_mode(enable) 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() return (debug.getinfo(2, "S").source:match("@(.*/)") or "./") end