lua
lua: Lua interpreter.
Synopsis
lua [options] [script [args…]]
Description
lua is Lua’s standalone interpreter, as lua.c is. It runs Lua given as text: statements from -e, a script file, standard input, or lines typed at its interactive prompt (the REPL).
Every mcos program is already Lua: the kernel starts each process through the loader, /lib/ld.lua, which opens the standard library and runs the program, so programs do not need lua to run. lua is a program like any other.
Options are handled in order: -e and -l run as they are met, then the script runs with args as its … and in the global table arg: arg[0] is the script, arg[1] on its arguments, and the interpreter and its options at negative indices. What the script returns is ignored.
With no script and no -e or -v, lua starts the REPL if standard input is a terminal, and otherwise runs standard input as a script.
An error is reported on standard error as lua: message followed by a stack traceback (the interpreter’s own frames left out).
Options
- -e stat
- Run the statement stat.
- -l mod
- Require module mod into the global mod.
- -l g=mod
- Require module mod into the global g.
- -i
- Enter the REPL after running script (and print the version first).
- -v
- Print the version.
- -E -W
- Accepted, and do nothing.
- –
- Stop handling options.
- -
- Stop handling options and run standard input.
The argument of -e and -l may also be joined to it (-lfs).
The repl
The prompt is > ; a line that is an incomplete statement goes on at >> . A line is first tried as an expression; its values are printed, separated by tabs, a table as its contents (readably, through /lib/serialization.lua), other values as print shows them.
Lines are read with /lib/readline.lua and its keys:
- Left Right Home End ^A ^E
- Move the cursor.
- Ctrl+Left Ctrl+Right
- Move a word back or on, as bash does (Alt+Left and Alt+Right too).
- Backspace Delete ^D
- Erase the character before or under the cursor.
- ^U ^K ^W
- Cut the line before the cursor, after it, or the word before it.
- Up Down
- Go through the history (100 lines).
- ^L
- Clear the screen.
- Tab
- Complete a Lua name: a global, or a field of the table named before the last . or : (methods through __index too). A table’s name gets a ., a function’s a (. A second Tab lists the choices.
- ^C
- Drop the line being typed; while code runs, end it with interrupted! at its next system call (code that makes none runs on).
- ^D
- On an empty line, leave the REPL.
Exit status
0 on success; 1 on a bad option, or if a chunk given by -e, -l, the script or standard input fails to load or raises an error.
Examples
lua -e "print(2^10)"
lua -l fs -e 'print(fs.isdir("/tmp"))'
lua script.lua one two
echo "print(1+1)" | lua
Notes
-E and -W have no effect: there are no LUA_INIT variables and warnings go nowhere.
See also
sh(1), edit(1)