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

2022-09-22 ยท (updated 2023-08-11) ยท 422 words