Lua
print("Hello, World!")
local function factorial(n)
if n == 0 or n == 1 then
return 1
end
return n * factorial(n - 1)
end
local function is_integer(n)
return type(n) == "number" and math.floor(n) == n
end
io.write("enter a number:\n")
local n = assert(io.read("*number"), "your input is not a valid number")
if n < 0 or not is_integer(n) then
error("your input is not a non-negative number");
end
io.write(factorial(n))
공식 문서
루아를 쓰는 곳
Redis
Defold : 오픈소스 게임 엔진
NeoVim : 에디터
OpenResty: 클라우드 플레어의 Nginx 기반 인프라
Hammerspoon : MacOS 자동화
설치하기
패키지 매니저로 설치하기
Exercism은 다양한 언어의 개발환경을 구축하는 법을 소개해주고 있습니다. 루아 설치 가이드를 참고합니다.
직접 빌드하기
루아는 C 소스 코드를 직접 빌드해서 설치할 수도 있습니다. Mac OS X와 Linux, WSL에서는 다음 명령어를 실행하면 됩니다.
curl -R -O http://www.lua.org/ftp/lua-5.1.5.tar.gz
tar zxf lua-5.1.5.tar.gz
cd lua-5.1.5
make all test
윈도우에서는 좀 더 번거롭습니다.
LuaJIT
동적 언어로 C나 Rust 급의 성능을 내는 것으로 유명한 Just-In-Time 컴파일러로, Lua 5.1 버전을 지원합니다. LuaJIT 은 맥이나 리눅스에서 다음과 같이 간단하게 설치할 수 있습니다.
Lua의 최신 버전은 5.4.6이지만, NeoVim과 Defold 게임엔진을 비롯해 많은 곳에서 Lua 5.1 버전을 사용하고 있습니다. 저희도 LuaJIT과 호환되는 5.1 버전을 사용하려 합니다.
git clone https://luajit.org/git/luajit.git
cd luajit
make && sudo make install
윈도우에서는 공식 문서를 참고합니다.
튜토리얼
에디터, LSP
편리한 자동완성과 정적 분석 외에도 더 많은 걸 제공합니다!
---@param n number
---@return number
local function square_of_sum(n)
return square(f.sum(f.range(1, n)))
end
VS Code
NeoVim
패키지 매니저 LuaRock
LUA_PATH 환경 변수는 다음과 같이 생성해서, .bashrc나 .zshrc에 환경변수로 추가합니다.
If you installed both Lua and LuaRocks in their default directories (/usr/local on Linux and Mac OSX), then the “system” tree is /usr/local and it will work by default. However, the “user” tree (for installing rocks without admin privileges) is not detected by Lua by default. For that we’ll need to configure these environment variables.
LuaRocks offers a semi-automated way to do this. If you type the following command:
luarocks path --bin
# .zshrc 예시
export LUA_PATH='/home/taehee/.asdf/downloads/lua/5.1.5/lua-5.1.5/luarocks-3.9.2/src/?.lua;./?.lua;/usr/local/share/lua/5.1/?.lua;/usr/local/share/lua/5.1/?/init.lua;/usr/local/lib/lua/5.1/?.lua;/usr/local/lib/lua/5.1/?/init.lua;;/home/taehee/.luarocks/share/lua/5.1/?.lua;/home/taehee/.luarocks/share/lua/5.1/?/init.lua;/home/taehee/.asdf/installs/lua/5.1.5/luarocks/share/lua/5.1/?.lua;/home/taehee/.asdf/installs/lua/5.1.5/luarocks/share/lua/5.1/?/init.lua'
export LUA_CPATH='./?.so;/usr/local/lib/lua/5.1/?.so;/usr/local/lib/lua/5.1/loadall.so;/home/taehee/.luarocks/lib/lua/5.1/?.so;/home/taehee/.asdf/installs/lua/5.1.5/luarocks/lib/lua/5.1/?.so'
export PATH="/home/taehee/.luarocks/bin:/home/taehee/.asdf/installs/lua/5.1.5/bin:/home/taehee/.asdf/installs/lua/5.1.5/luarocks/bin:$PATH"
test
동적 언어에는 test가 필요하죠.
busted
describe('Busted unit testing framework', function()
describe('should be awesome', function()
it('should be easy to use', function()
assert.truthy('Yup.')
end)
it('should have lots of features', function()
-- deep check comparisons!
assert.same({ table = 'great'}, { table = 'great' })
-- or check by reference!
assert.is_not.equals({ table = 'great'}, { table = 'great'})
assert.falsy(nil)
assert.error(function() error('Wat') end)
end)
it('should provide some shortcuts to common functions', function()
assert.unique({{ thing = 1 }, { thing = 2 }, { thing = 3 }})
end)
end)
end)
repl
croissant

xeus lua notebook

inspect
table을 인간이 읽을 수 있는 형태로 toString 해주는 utility
inspect = require('inspect')
print({1,2,3,4}) --> table: 0x56209c00a690
print(inspect({1,2,3,4})) --> "{ 1, 2, 3, 4 }"
print({a=1,b=2}) --> table: 0x55912b975690
print(inspect({a=1,b=2})) --> "{ a = 1, b = 2 }"
포매터 LuaFormatter
c++ 로 만든 루아 포매터로 vscode, vim 지원