Lua - Interactive Mode
2023-08-11: rlwarp + readline์ ํ์ฉํ ํค์๋ ์์ฑ ๊ธฐ๋ฅ ์ถ๊ฐ Intro ์ฝ๋ฉ์ ํ๊ธฐ ์ ๋๋ ๋น ๋ฅธ ํ์ธ์ ์ํด ์ธํฐ์ํฐ๋ธ ๋ชจ๋๋ฅผ ์ฌ์ฉ ๊ฐ๋จํ ํ ์คํธ๋ ๋ช๊ฐ์ง ์คํ์ ํด๋ณด๋ ์ฉ๋๋ก์ ์ข๋ค. ilua๋ฅผ ์ฌ์ฉํ๋ฉด jupyter console, notebook ๋ฑ์ผ๋ก ํ์ฅํ ์ ์๋ค. Execute String $ lua -e 'print("hello world")' hello world Interactive Mode lua -i ๋๋ luajit -i ๋ก interactive mode๋ก ์ง์ -i ์ต์ ์ด ์์ด๋ ์ง์ ๊ฐ๋ฅ local ๋ณ์๊ฐ ๋ค์ ๋ผ์ธ์์ ์ธ์๋์ง ์๋๋ค. ์ฌ์ฉํ ๋ ์ฃผ์ $ lua -i -- ๋๋ luajit -i > local a = 'hello world' -- local ๋ณ์ ์ค์ > print(a) -- print nil -- ๊ฒฐ๊ณผ: nil > a = 'hello world' -- Global ๋ณ์ ์ค์ > print(a) -- print hello world -- ๊ฒฐ๊ณผ: hello world > os.exit() -- ์ข ๋ฃ: ๋๋ Ctrl+d, Ctrl+c Opening Lua File > f = assert(loadfile('foo.lua') -- luaํ์ผ ๋ก๋ > f() -- ์คํ > dofile('foo.lua') -- ๋ก๋ ๋ฐ ์คํ > m = require('mymodule') -- mymodule.lua ๋ชจ๋ ๋ก๋ > m.parse() -- ๋ชจ๋๋ด ํจ์ ์คํ Load Libary Example -- file 'lib1.lua' function norm(x,y) local n2 = x^2 + y^2 return math.sqrt(n2) end function twice(x) return 2*x end > dofile('lib1.lua') -- load your library > n = norm(3.4, 1.0) -- using lib1.lua functions > print(twice(n)) -- using twice functions Command Line Arguments (-- /dev/null ์ต์ ์ฃผ๋ชฉ) $ lua -i -- /dev/null one two three > print(arg[1]) -- one > print(arg[2]) -- two > print(arg[3]) -- three > print(arg[0]) -- /dev/null Lua, LuaJit line completion like in bash bash์์ line completion๊ณผ ์ ์ฌํ ๊ธฐ๋ฅ์ ๊ตฌํ rlwrap ํ์ฉ - -a:always-readline,-c:complete-filenames,-e:extra-char-after-completion $ sudo apt install rlwrap $ alias luajit='rlwrap -ac -e "" -pRed luajit' # add this line to ~/.bashrc $ luajit > = 2 + 2 4 > = 2 + 2 --> ํ์ดํค๋ฅผ ์ด์ฉํ์ฌ ์์ ๋ช ๋ น๋ผ์ธ์ ๋ถ๋ฌ ์ฌ ์ ์๋ค. rlwarp์ readline์ ํ์ฉํ ๋ช ๋ น์ด ์์ฑ: Dictionary ํ์ผ์ ๋ง๋ค๊ณ ์ฌ์ฉ. $ echo "print local assert string ..." > ~/.luadict $ alias luajit='rlwrap -b "" -f $HOME/.luadic -pRed -ca -e "" luajit' $ luajit > ass(-> Press Tab Key anc Check) iLua : with Jupyter Jupyter์ ๋ง๊ฐํ ์ง์์๋ ์ฝ์ํ ์ธํฐ์ํฐ๋ธ๋ฅผ ์ฌ์ฉํ ์ ์๋ค. ์ฝ๋์์ฑ ๋ฐ vi edit mode ๊ทธ๋ฆฌ๊ณ ๋ณ์๋ฅผ ์ง์ ์ถ๋ ฅ๊ฐ๋ฅํ๋ค. ๋ฌผ๋ก ํน์ฑ์ local ๋ณ์๋ ์ญ์ ์ง์๋์ง ์๋๋ค. ํ์ฌ๋ก์๋ ๊ทธ๋ ๋ค. ~/.local ์ดํ์ jupyter ๋ฐ ilua๊ฐ ์ค์น๋๋ค. ์๋์ฒ๋ผ vi edit mode๋ฅผ ์ฌ์ฉํ๋ฉด ๋์ฑ ํธ๋ฆฌํ๋ค. $ pip install ilua $ echo "c.ZMQTerminalInteractiveShell.editing_mode='vi'" > ~/.jupyter/jupyter_console_config.py $ jupyter console -h # help $ jupyter kernelspec list # ๋ด์ฅ ์ปค๋ ์คํ ํ์ธ $ ~/.local/bin/ilua -i lua[jit] # ์คํ $ echo "alias ilua='ilua -i luajit'" >> ~/.bashrc # luajit ์ ์ฉ alias $ source ~/.bashrc $ ilua Links https://www.lua.org/pil/1.1.html https://www.tutorialspoint.com/command-line-arguments-in-lua https://lua-users.org/wiki/InteractiveLua https://github.com/guysv/ilua https://jupyter-console.readthedocs.io/en/latest/config_options.html