1
0
Fork 0
mirror of https://github.com/Reuh/candran.git synced 2025-10-27 09:59:29 +00:00
candran/bin/can
2019-08-24 15:19:59 +02:00

53 lines
1.3 KiB
Lua

#!/bin/lua
local candran = require("candran")
local cmdline = require("lib.cmdline")
local args = cmdline(arg)
if args.help or args.h then
print("Candran "..candran.VERSION.." interpreter by Reuh")
print("Usage: "..arg[0].." [options] filename")
print("Specify no options to start the REPL.")
print("Use - instead of a filename to read from the standard input.")
print("Interpreter options:")
print(" -help or -h print this text")
print("Default options:")
for opt, val in pairs(candran.default) do
if type(val) == "string" then val = val:gsub("\n", "\\n") end
print((" %s=%q"):format(opt, val))
end
return
end
if arg[#arg] == "-" then
local f, err = candran.load(io.read("*a"), "stdin", nil, args)
if not f then
io.stderr:write("can: "..err.."\n")
os.exit(1)
end
f()
elseif #args >= 1 then
candran.dofile(args[1], args)
else -- REPL
print("Candran " .. candran.VERSION)
candran.setup()
while true do
io.write("> ")
local line = io.read()
if line:match("^=") then
line = line:gsub("^=", "return tostring(") .. ")"
end
local t = { pcall(candran.load, line, "stdin") }
if t[1] == false then
print(t[2])
else
t = { pcall(t[2]) }
if t[1] == false then
print(t[2])
elseif #t > 1 then
print(unpack(t, 2))
end
end
end
end