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

Love - ๋ฃจ์•„ 2D ๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ

Intro love๋Š” lua์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” 2D ๊ฒŒ์ž„ ํ”„๋ ˆ์ž„์›Œํฌ์ด๋‹ค. ๊ฒŒ์ž„์„ ๋งŒ๋“ค๋ ค๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๊ณ  2D ๊ทธ๋ž˜ํ”ฝ ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ์„ ๋ชจ์ƒ‰ํ•˜๊ธฐ ์œ„ํ•จ. ์ž‘์€ ๋ชจ์˜ ์‹คํ—˜์ด๋‚˜ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์šฉ์œผ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ์—ญ์‹œ ์ž‘๊ณ  ๊ฐ€๋ณ๊ณ  ๋น ๋ฅด๋‹ค. Install debian/ubuntu package install $ sudo apt install love # ํŒจํ‚ค์ง€ ์„ค์น˜ $ love --version # check version ubuntu ppa install: ์ตœ์‹ ๋ฒ„์ „์œผ๋กœ ์„ค์น˜ $ sudo add-apt-repository ppa:bartbes/love-stable $ sudo apt update $ sudo apt install love $ love --version Hello World $ mkdir ~/love-test # make dir for love application $ cd ~/love-test # goto dir $ cat <<EOF > main.lua # edit main.lua source file function love.draw() love.graphics.print("Hello World", 400, 300) end EOF $ love . # love <path> : ์‹คํ–‰ OR $ love ~/love-test # ํ”„๋กœ์ ํŠธ ํด๋”๋ช… vim nvim ์ง€์› ํ”Œ๋Ÿฌ๊ทธ์ธ sumneko lua-language-server ๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์„ค์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค. ...

2022-09-15 ยท (updated 2023-01-26) ยท 694 words

Raspberry-Pi์—์„œ lua ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ค์น˜

Install luarocks ๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ํ•„์š”ํ•œ ๋ชจ๋“ˆ๋งŒ ์„ค์น˜ํ•ด์„œ ์‚ฌ์šฉํ•˜๊ณ ์ž ํ•  ๋•Œ $ sudo apt update && sudo apt upgrade $ apt install git build-essensial #git, gcc, make ํ™˜๊ฒฝ ํ•„์š” $ apt install lua5.1.0 liblua5.1.0 #lua module ์ปดํŒŒ์ผ์‹œ liblua ํ•„์š” $ git clone https://github.com/lunarmodules/luafilesystem.git #luafilesystem ์†Œ์Šค ๋ณต์‚ฌ $ cd luafilesystem $ make # gcc, make, liblua5.1.x ํ•„์š” ์กฐ๊ฑด $ cp src/lfs.so your_devel_directory/ $ cd your_devel_directory/ $ vim myprogram.lua require'lfs' ... LuaStatic ์„ค์น˜ ๋ฐฉ๋ฒ•1: luastatic.lua ์ž์‹ ์„ ์ปดํŒŒ์ผํ•ด์„œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ์‚ฌ์šฉ $ git clone https://github.com/ers35/luastatic.git $ cd luastatic/ $ uastatic.lua luastatic.lua /usr/lib/arm-linux-gnueabihf/liblua5.1.a -I/usr/include/lua5.1/ $ ./luastatic # ๋ฐ”์ด๋„ˆ๋ฆฌ ์‹คํ–‰ ๋ฐ ๋ฒ„์ „ ํ™•์ธ ๋ฐฉ๋ฒ•2: luastatic.lua๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ $ git clone https://github.com/ers35/luastatic.git $ cd luastatic/ $ ./luastatic.lua # ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰ ๋ฐ ๋ฒ„์ „ ํ™•์ธ ๋ชจ๋“ˆ๊ณผ ํ•ฉ์ณ์„œ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ์ปดํŒŒ์ผ ํ•˜๊ธฐ luastatic.lua ์ž์‹ ์€ ์˜์กด์„ฑ์ด ์—†์œผ๋ฏ€๋กœ ~/bin ๋“ฑ์— ์˜ฎ๊ธด ํ›„ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค. myprogram.lua๊ฐ€ ๊ฐ™์€ ํด๋”์— ์žˆ๋Š” ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ์ž๋™์œผ๋กœ ํฌํ•จํ•ด์„œ ์ปดํŒŒ์ผ๋œ๋‹ค. $ cp luastatic.lua ~/bin $ luastatic.lua myprogram.lua /usr/lib/arm-linux-gnueabihf/liblua5.1.a -I/usr/include/lua5.1/ ๊ณ ๋ ค ์‚ฌํ•ญ ์œ„์˜ ์˜ˆ์ œ์ฒ˜๋Ÿผ ์ปดํŒŒ์ผ ํ™˜๊ฒฝ์„ ๋งŒ๋“ค๊ณ  ๋ชจ๋“ˆ์„ ๋งŒ๋“ค์–ด requireํ•ด์„œ ์‚ฌ์šฉ. ์—ฌ๋Ÿฌ ๋ชจ๋“ˆ๊ณผ main.lua๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ด๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ ๋ฐ”์ด๋„ˆ๋ฆฌ๋กœ ๋งŒ๋“ ๋‹ค. ๋˜๋Š” ํŒŒ์ผ์„ ์„ค์น˜ํ•˜๊ณ  ์‹คํ–‰ํ™˜๊ฒฝ์„ ๋งŒ๋“ค์–ด ์ค€๋‹ค. ์ด์— ๋Œ€ํ•ด์„œ๋Š” ๋‚˜์ค‘์— ์ถ”๊ฐ€ํ•  ์˜ˆ์ •.

2022-09-13 ยท 181 words

lua-dev.nvim - nvim์—์„œ lua ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ๊ตฌ์„ฑ ํ”Œ๋Ÿฌ๊ทธ์ธ

Intro lua๋กœ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ํ•  ๋•Œ ํ•จ์ˆ˜๋‚˜ ์ž๋™์™„์„ฑ์„ ๋„์™€์ฃผ๋Š” ํ”Œ๋Ÿฌ๊ทธ์ธ์ด๋‹ค. nvim์˜ lua ๋‚ด์žฅํ•จ์ˆ˜ ๋ฐ api๋„ ์ง€์›ํ•œ๋‹ค. ๋‹ค๋ฅธ lsp ์„œ๋ฒ„์˜ ์„ค์น˜์™€ ์„ค์ •๋ณด๋‹ค ์–ด๋ ค์›Œ์„œ ๋งŽ์ด ๊ณ ์ƒํ–ˆ๋‹ค. ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๊ฒƒ์€ lua-language-server๋ฅผ ์ œ๋Œ€๋กœ ์„ค์น˜ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ์„œ๋ฒ„๋ฅผ ์ปดํŒŒ์ผ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š” ๋ฐ”์ด๋„ˆ๋ฆฌ๋ฅผ ๋‹ค์šด ๋ฐ›์•„ ์“ฐ๋Š” ๊ฒƒ์„ ๊ถŒ์žฅํ•œ๋‹ค. Requirements nvim-cmp :์ž๋™์™„์„ฑ ํ”Œ๋Ÿฌ๊ทธ์ธ nvim-lsp :Language Server Protocol ํ”Œ๋Ÿฌ๊ทธ์ธ cmp-nvim-lsp : lsp ์ž๋™์™„์„ฑ ์ง€์› ํ”Œ๋Ÿฌ๊ทธ์ธ sumneko lua-language-server : Lua lsp server ํ”„๋กœ๊ทธ๋žจ (lua 5.1 ~ 5.4, luaJit ๋ชจ๋‘ ์ง€์›) lua-language-server Install ์•„๋ž˜์—์„œ ์ปดํŒŒ์ผ๋œ ๋ฐ”์ด๋„ˆ๋ฆฌ ๋‹ค์šด๋กœ๋“œ (๋ฆฌ๋ˆ…์Šค 64bit๊ธฐ์ค€) https://github.com/sumneko/lua-language-server/releases ์ ๋‹นํ•œ ์œ„์น˜์— ์„ค์น˜ $ mkdir ~/data/lua-language-server && cd lua-language-server $ tar xvfz ~/Download/lua-language-server-3.5.5.tar.gz . $ cat <<EOF > ~/bin/lua-language-server # ์‹คํ–‰์Šคํฌ๋ฆฝํŠธ ์ž‘์„ฑ #!/usr/bin/bash exec "~/data/lua-language-server/bin/lua-language-server" "$@" EOF $ chmod u+x ~/bin/lua-language-server # ~/bin ์ด PATH์— ์„ค์ •๋˜์–ด ์žˆ์–ด์•ผ $ lua-language-server --version # ์—๋Ÿฌ ์—†์ด ๋ฒ„์ „์ด ์ถœ๋ ฅ๋˜๋ฉด ์„ฑ๊ณต lua-dev Install ๋จผ์ € nvim-cmp nvim-lsp, cmp-nvim-lsp๊ฐ€ ์„ค์น˜ ๋ฐ ์„ค์ •๋˜์—ˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•œ๋‹ค. ...

2022-09-12 ยท (updated 2022-10-31) ยท 245 words

Lua Quick Guide - tutorialspoint

Basic Tokens in Lua io.write("Hello world, from ",_VERSION,"!\n") Comments -- simple comment --[[ block comments --]] identifiers - ๋ณ€์ˆ˜๋ช…, ํ•จ์ˆ˜๋ช… ๋“ฑ ๊ทœ์น™ mohd zera abc move_name a_123 myname50 _temp a23b9 retVal Keywords - ์˜ˆ์•ฝ์–ด and break do else elseif end false for function if in local nil not or repeat return then true until while Lua - Variables Global variables, Local bariables, Table fields Variable Example -- Variable definition: local a, b --local g,l = 20,30 --global -- Initialization a = 10 b = 30 print("value of a:", a) print("value of b:", b) -- Swapping of variables b, a = a, b print("value of a:", a) print("value of b:", b) f = 70.0/3.0 print("value of f", f) Data Types nil - no data boolean - ture, false number - read numbers string - array of characters function - method that is written in C or Lua userdata - C data thread - independent threads of execution table - arrays, symbol tables, sets, records, graphs, trees, etc ...

2022-09-09 ยท 2184 words