about summary refs log tree commit diff
path: root/lua_src/main.lua
blob: 776f2a43100f1741ede238583099d0395635a2ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
local config = require("sild.config")
local parse = require("sild.parse")
local session = require("sild.session")
local debugger = require("sild.debugger")
local commands = require("sild.commands")

local cli = config.parse_cli(arg)

if not cli.script_path then
  config.print_help(SILD_VERSION); os.exit(1)
end

local home = os.getenv("HOME") or os.getenv("USERPROFILE") or ""
local xdg_config_home = os.getenv("XDG_CONFIG_HOME") or (home .. "/.config")
config.parse_file(cli.config_file or (xdg_config_home .. "/sild/sild.cfg"))

local chunk, load_err = loadfile(cli.script_path)
if not chunk then
  local ansi = require("sild.ansi")
  io.stderr:write(ansi.color_error(
    "ERROR: cannot load '" .. cli.script_path .. "': " .. tostring(load_err)
  ) .. "\n")
  os.exit(1)
end

local script_src_key = "@" .. cli.script_path
-- TODO: Look for annotations in require'd files
local breakpoints_list, actions_list = parse.annotations(cli.script_path)
session.breakpoints[script_src_key] = breakpoints_list
for _, ln in ipairs(cli.breaks) do
  session.breakpoints[script_src_key][ln] = true
end
session.actions[script_src_key] = actions_list

local real_arg = {}
for i, v in ipairs(cli.script_args) do real_arg[i] = v end
real_arg[0] = cli.script_path
arg = real_arg

local ansi = require("sild.ansi")
print(ansi.color_prompt("sild " .. SILD_VERSION) .. "  " .. ansi.color_bold(cli.script_path))
print(ansi.color_dim("n=step  c=continue  ?=help  q=quit") .. "  " ..
  -- TODO: use XDG_CONFIG_HOME here
  ansi.color_dim("config: " .. (cli.config_file or (os.getenv("HOME") or "~") .. "/.config/sild/sild.cfg")) .. "\n")

local has_immediate_bp = next(session.breakpoints[script_src_key]) ~= nil
debugger.install_hook(has_immediate_bp, commands.run)
chunk()