[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Customized interpreter
- From: Daurnimator <quae@...>
- Date: Mon, 21 Dec 2015 19:19:15 +1100
On 21 December 2015 at 19:01, Dirk Laurie <dirk.laurie@gmail.com> wrote:
> I'm trying to write a dual-purpose program that executes via
> the shebang. It should be callable with parameters, in which
> case it does its thing and exits, or without, in which case it
> runs the same initialization code and drops down to the REPL.
>
> In order to achieve the second, the shebang line is
>
> #! /usr/local/bin/lua -i
>
> This part works perfectly.
>
> If there are parameters, one can terminate despite the -i by
> os.exit.
>
> But -i has the side effect of displaying the Lua welcome message,
> which I don't want in the case with parameters. The program should
> look like a system utility.
>
> I can think of two solutions:
>
> 1. Modify lua.c to have a `-q` option that suppresses the welcome
> message even when the REPL is about to be entered,
> 2. Work without `-i` and use debug.debug to provide the REPL.
> This is not very nice since debug.debug does not provide readline
> support, history etc.
>
> Am I missing a trick somewhere?
>
Why not just implement a REPL yourself?
It can be quite simple:
_PROMPT = "> "
while true do
io.write(_PROMPT)
io.flush()
local code = io.read()
local func, err = loadstring(code, "@stdin")
if not func then
func, err = loadstring("return " .. code, "@stdin")
end
if func then
print(select(2, pcall(func)))
else
io.write(err, "\n")
end
end