lua - deptno/deptno.github.io GitHub Wiki

lua

lua 5.1 -> luajit -> 5.x

neovim 은 lua 5.1 을 슀크립트 μ—”μ§„μœΌλ‘œ λ‚΄μž₯ν•˜κ³  μžˆλ‹€. 좔후에 neovim μ—μ„œ λ‚΄μž₯ 슀크립트 버전이 μ—…λ°μ΄νŠΈλ˜λŠ” 경우 5.1 μ™€μ˜ ν˜Έν™˜μ„±μ€ 보μž₯ν•œλ‹€.

cli

lua # 인터프리터 μ‹œμž‘
lua [FILENAME] # νŒŒμΌμ„ μ‹€ν–‰
lua -i [FILENAME] # 인터프리터 μ‹œμž‘κ³Ό ν•¨κ²Œ ν•΄λ‹Ή νŒŒμΌμ„ λΆˆλŸ¬μ™€μ„œ μ‹€ν–‰ `dofile` κ³Ό κ°™μŒ
lua -e "LUA_CODE" # μ½”λ“œ μ‹€ν–‰
lua -llib -e "LUA_CODE" # lib을 μž„ν¬νŠΈν•œ ν›„ `LUA_CODE` μ‹€ν–‰
lua -llib -e -i "LUA_CODE" # lib을 μž„ν¬νŠΈν•œ ν›„ `LUA_CODE` μ‹€ν–‰ 후에 인터프리터 λͺ¨λ“œλ‘œ μ§„μž…

arg

arg λŠ” μ „μ—­ λ°°μ—΄λ‘œ cli λ₯Ό ν†΅ν•΄μ„œ μ‹€ν–‰ν• λ•Œ 슀크립트 파일λͺ…을 κΈ°μ€€μœΌλ‘œ 0 μΈλ±μŠ€κ°€ μ •ν•΄μ§€κ³  κ·Έ 이전은 - μΈλ±μŠ€κ°€ λœλ‹€

lua -e "print('hello')" script a b
  • arg[-3] "lua"
  • arg[-2] "-e"
  • arg[-1] "print('hello')"
  • arg[ 0] "script"
  • arg[ 1] "a"
  • arg[ 2] "b"

ν™˜κ²½λ³€μˆ˜

INIT_LUA

ν™˜κ²½ λ³€μˆ˜μ˜ λ‚΄μš©μ— λ”°λ₯Έ μ‹€ν–‰ λΆ„λ₯˜

  • @ 둜 μ‹œμž‘ν•˜λŠ” κ²½μš°μ—λŠ” 파일λͺ…μœΌλ‘œ μΈμ§€ν•˜κ³  ν•΄λ‹Ή νŒŒμΌμ„ μž„ν¬νŠΈ
  • @ κ°€ μ•„λ‹Œ λ‹€λ₯Έ κ²ƒμœΌλ‘œ μ‹œμž‘ν•˜λŠ” κ²½μš°μ—λŠ” 루아 μ½”λ“œλ₯Ό μ‹€ν–‰, dofile κ³Ό 같은 역할이라고 생각됨
  • INIT_LUA_5_2
    • 버전λͺ…μ—λ”°λΌμ„œ suffix κ°€ λ‹¬λΌμ§€λŠ”λ° 5.1 μ΄λ‚˜ luajit μ—μ„œλ„ μ‚¬μš©μ΄ κ°€λŠ₯ν•œμ§€λŠ” 확인이 ν•„μš”
  • INIT_LUA
    • INIT_LUA_5_2 κ°€ 읽힌 후에도 μ½μ–΄μ§€λŠ” 확인 ν•„μš”

문법

import

dofile("FILENAME") -- eval
require("FILENAME") -- import

μ „μ—­

_CAPITAL _ λ°”λ‘œ μ‹œμž‘λ˜λŠ” λŒ€λ¬Έμž μ „μ—­λ³€μˆ˜λŠ” μ˜ˆμ•½μ–΄λ‘œ μ‚¬μš©

μ •κ·œν‘œν˜„μ‹

lua λŠ” μ •κ·œν‘œν˜„μ‹μ„ fully κ΅¬ν˜„ν•˜μ§€ μ•ŠλŠ”λ‹€ escape

  • νŒ¨ν„΄ λ§€μΉ­ λ‚΄μ—μ„œλ„ 이전 λ§€μΉ­ κ²°κ³Όλ₯Ό μ‚¬μš©ν•  수 있음
    • (['"])hello%1 ' ν˜Ήμ€ " 이 λ§€μΉ­λ˜μ—ˆμ„λ•Œ %1 은 이전에 맀칭된 결과와 같은 λ¬Έμžμ—΄λ‘œ ν”½μŠ€λ¨
      • 성곡, "hello", 'hello'
      • μ‹€νŒ¨, "hello', 'hello"
  • string pattern λ§€μΉ˜μ—λŠ” μ •κ·œν‘œν˜„μ‹μ˜ {7, 40} 에 ν•΄λ‹Ή ν•˜λŠ” ꡬ문이 μ—†λ‹€ diary:2023-10-12
    • git sha1 을 λ§€μΉ­ν•˜κΈ° μœ„ν•΄ μ‚¬μš©ν–ˆλ‹€.
local is_git_hash = function(hash)
  local char = '^[0-9a-fA-F]$'
  local matched = string.match(hash, string.format('%s*', char:rep(8)))

  return matched ~= nil and #matched <= 40 and matched
end

ν˜•νƒœλ‘œ κ΅¬ν˜„ν–ˆλ‹€. 7κΈ€μžμ΄μƒ 40κΈ€μž μ΄ν•˜μΌλ•Œ λ§€μΉ­λœλ‹€, 링크된 μ½”λ“œλŠ” line matching 을 κ°€μ •ν•΄μ„œ ^$ κ°€ 제거된 버전이닀

vs javascript

νƒ€μž…ν™•μΈ

  • lua: type(λ³€μˆ˜)
  • js: typeof [λ³€μˆ˜]

true/false

  • lua: false, nil λ§Œμ„ false 둜 κ°„μ£Όν•œλ‹€
  • js: undefined, null, "", 0, NaN λ“± 더 λ§Žμ€ 상황을 false 둜 κ°„μ£Όν•œλ‹€

regexp|μ •κ·œν‘œν˜„μ‹

  • lua: % λ₯Ό escape 문자둜 μ‚¬μš©
  • js: \ λ₯Ό escape 문자둜 μ‚¬μš©

lexical scoping

function a() {
  v = 2
}
let v = 1

console.log(v) // 1
a()
console.log(v) // 2
function a()
  v = 2
end
local v = 1

print(v) // 1
a()
print(v) // 1

μžλ°”μŠ€ν¬λ¦½νŠΈμ™€λŠ” λ‹€λ₯΄κ²Œ ν΄λ‘œμ € μŠ€μ½”ν•‘μ΄ 쑰금 λ‹€λ₯΄λ‹€

  • function a 의 μ„ μ–Έ μ‹œμ μ΄ local v 보닀 이전이라 local v 에 μ ‘κ·Όν•  수 μ—†μŒ
  • ν•¨μˆ˜λ‚΄μ—μ„œ μ ‘κ·Όν•˜λ €λŠ” λ³€μˆ˜λŠ” μ „μ—­λ³€μˆ˜μ΄κ±°λ‚˜ ν˜„μž¬ ν•¨μˆ˜μ˜ μ •μ˜ 이전에 μ„ μ–Έλ˜μ–΄μ•Όν•œλ‹€.
    • local a 만 μ„ μ–Έν•˜κ³  a = function {body} end λ“±μ˜ μ •μ˜λŠ” 늦게 μ™€λ„λœλ‹€. λŒ€μΆ© μŠ€μ½”ν•‘μ΄ ν•¨μˆ˜ 호좜 μ‹œμ μ΄ μ•„λ‹ˆλΌ μ •μ˜ μ‹œμ μ— λ§Œλ“€μ–΄μ§„λ‹€κ³  μƒκ°ν•˜λ©΄ 될 λ“―ν•˜λ‹€

garbage collection

  • local 은 λ©”λͺ¨λ¦¬ 할당을 μƒμ„±ν•˜μ§€ μ•Šμ§€λ§Œ string 은 μƒμ„±ν•œλ‹€. λ™μΌν•œ string 을 μ—°μ†μœΌλ‘œμ‚¬μš©ν•˜λŠ” 것은 λ©”λͺ¨λ¦¬λ₯Ό λ‚­λΉ„ν•˜μ§€μ•ŠλŠ”λ‹€.
  • κ°€λ³€μΈμž ν•¨μˆ˜λŠ” ν•¨μˆ˜κ°€ ν˜ΈμΆœλ λ•Œ 인자 ν…Œμ΄λΈ”μ„ μƒμ„±ν•œλ‹€
  • collectgarbage("count") * 1024 둜 확인

coroutines

  • coroutine.create 둜 coroutine.yeild λ°˜ν™˜μ‹œμ— 쀑단점을 μ €μž₯ν•˜κ³  μΆ”ν›„ coroutine.resume 을 톡해 계속 μ‹€ν–‰ν•  수 μžˆλŠ” ν•¨μˆ˜λ₯Ό λ°˜ν™˜ν•œλ‹€.
  • coroutine.yeild λ₯Ό 톡해 λ°˜ν™˜ ν•˜λŠ” μ‹œμ μ΄ λ‹€μŒ coroutine.resume 을 톡해 인자λ₯Ό μ „λ‹¬ν•˜λŠ” μ‹œμ μœΌλ‘œ 이어진닀

μ±…

doc

link