´ë¹®    
FindPage  |  TitleIndex  |  UserPreferences  |  [http]me2day  |  redpixel
RecentChanges
 


LUA Tutorial

ÀÌ ¹®¼­ÀÇ ¸ñÀû #

ÀÌ ¹®¼­´Â Lua ÇÁ·Î±×·¡¹Ö ¾ð¾î¿¡ ´ëÇÑ ¾È³»¸¦ ¸ñÀûÀ¸·Î ÇÑ´Ù. À̰ÍÀº ¾ð¾î ·¹ÆÛ·±½ºº¸´Ù ¿¹Á¦¸¦ ÅëÇØ¼­ ¹è¿ì´Â °ÍÀÌ ´õ ºü¸£´Ù°í »ý°¢ÇÏ´Â À̵éÀ» À§ÇÑ °ÍÀÌ´Ù.

Lua? #

Lua´Â È£½ºÆ® ¾îÇø®ÄÉÀ̼ǿ¡ ³»ÀåµÉ ¸ñÀûÀ¸·Î ÀÛ¼ºµÈ ½ºÅ©¸³Æ® ¾ð¾îÀÌ´Ù. LuaÀÇ °ø½ÄÀûÀÎ À¥»çÀÌÆ®´Â [http]http://www.lua.orgÀÌ´Ù. È®½ÇÇÑ LuaÀÇ ÀÕÁ¡Àº ´Ü¼ø¼º°ú Å©±âÀÌ´Ù.

¿¹Á¦¸¦ ½ÇÇàÇϱâ #

LuaÀÇ ¼³Ä¡¿¡ µû¶ó, ¿©·¯ºÐÀº ¿¹Á¦¸¦ ½ÇÇàÇÒ ¼öµµ, ¸øÇÒ ¼öµµ ÀÖ´Ù. ¾Õ¿¡¼­ ¾ð±ÞÇßµíÀÌ, LuaÀÇ ÁÖµÈ Á¦ÀÛ¸ñÀûÀº È£½ºÆ® ¾îÇø®ÄÉÀ̼ǿ¡ ³»ÀåµÇ´Â °ÍÀÌ´Ù. ¾î·°Å³ª ¿¹Á¦¸¦ µ¹·Áº¸±â À§Çؼ­ "lua"¶ó°í ºÒ¸®´Â µ¶¸³ ¾îÇø®ÄÉÀ̼ÇÀ» ÄÄÆÄÀÏÇÏÀÚ. "lua.exe"´Â Ä¿¸Çµå ¶óÀο¡ Á¤ÀÇµÈ È­ÀÏÀ» Àаųª (°¡´ÉÇÏ´Ù¸é) stdin¿¡¼­ µ¥ÀÌŸ¸¦ ¾ò¾î¼­ Lua ÄÚµåµéÀ» ½ÇÇàÇØÁØ´Ù.

Hello, world #

¸ðµç ¾ð¾îµéÀÇ ¼­µÎ°¡ ±×·¸µíÀÌ ¿©±â¼­µµ "Hello, world!"·Î ½ÃÀÛÇØº¸ÀÚ.
-- Hello, world!
print("Hello, world!")
 Hello, world!
"--"Àº ±× ÁÙ³¡±îÁö´Â ÁÖ¼®¹®À̶ó´Â °ÍÀ» ³ªÅ¸³½´Ù.

Lua ŸÀÔ #

Lua´Â 6°³ÀÇ ±âº» ŸÀÔÀÌ ÀÖ´Ù : nil, number, string, function, userdata, table. ÁÖ¾îÁø º¯¼öÀÇ Å¸ÀÔÀ» ¾Ë¾Æ³»·Á¸é ´ÙÀ½ ¿¹Á¦¸¦ ÂüÁ¶Çضó:
-- ŸÀÔÀ» ¾Ë¾Æ³»±â À§ÇØ Åױ׸¦ »ç¿ëÇÑ´Ù.
function a_function()
	-- À̰ÍÀº ´ÜÁö ÅÖºó ÇÔ¼öÀÌ´Ù.
end

print(tag(undefined))
print(tag(1))
print(tag("Hello, world"))
print(tag({1,2,3}))
print(tag(a_function))
1
2
3
4
5

¼±ÅÃÀûÀ¸·Î, "type"Ű¿öµå°¡ »ç¿ëµÉ ¼ö ÀÖ´Ù:

-- ŸÀÔ¸íÀ» Ãâ·ÂÇÑ´Ù.

function a_function()
	-- ´ÜÁö ÅÖºó ÇÔ¼ö
end

print (type(undefined))
print (type(1))
print (type("Hello, there!"))
print (type({1,2,3}))
print (type(a_function))
nil
number
string
table
function

Å×À̺í #


Lua´Â "Å×À̺í"À̶ó´Â µ¥ÀÌŸŸÀÔÀ¸·Î È®ÀåµÈ ¿ëµµ¸¦ Á¦°øÇÑ´Ù. Å×À̺íÀº ´ÙÀ½°ú °°ÀÌ Á¤ÀǵȴÙ:

-- Å×À̺íÀ» Á¤ÀÇÇϱâ
table = {1, 2, 3, 4}  -- 4°³ÀÇ ¿ä¼Ò¸¦ °¡Áø Å×À̺í

-- À̰ÍÀ» Ãâ·ÂÇØº¸ÀÚ
i = 1    -- Å×À̺íÀº 1ºÎÅÍ ¼¾´Ù. 0ÀÌ ¾Æ´Ï´Ù.

-- index³»¿¡ °ªÀÌ µé¾îÀÖÀ¸¸é table[index]´Â trueÀÌ´Ù. 
while table[i] do
	print (table[i])
	i = i + 1
end
1
2
3
4

Lua ÀÇ Å×À̺íÀº ¼ýÀÚ(number)°¡ ¾Æ´Ñ ´Ù¸¥Å¸ÀÔÀ¸·Îµµ À妽ºÈ­µÉ ¼ö ÀÖ´Ù. table.name °ú table[ "name" ]Àº µ¿µîÇϴٴ°Ϳ¡ ÁÖ¸ñÇ϶ó. ¶ÇÇÑ table.number °°Àº Ç¥ÇöÀº ºÒ°¡´ÉÇϴٴ°Ϳ¡µµ À¯ÀÇÇ϶ó.

-- Table example

function stupid_function()
	print "I do nothing! Nothing!"
end

table = {}

table.name = "Just a table"
table["passcode"] = "Agent 007"

table[table] = "Penguin"

table[stupid_function] = "This is a stupid function"
table["do_stuff"] = stupid_function

print ( table["name"], table.passcode, table[table] )
print ( table[table["do_stuff"]] )

table["do_stuff"]()
table.do_stuff()       -- ÀÌ µÎ ¹®ÀåÀº °°Àº ÀǹÌÀÌ´Ù.
Just a table Agent 007 Penguin
This is a stupid function
I do nothing! Nothing!
I do nothing! Nothing!

LuaÀÇ È帧Á¦¾î #

Lua´Â Á¶°Ç ºÐ±â¸¦ À§ÇØ "if" ¶ó´Â Ű¿öµå¸¦ »ç¿ëÇÑ´Ù.
-- If example

variable = "This is a string"
variable_2 = 10

if 0 then    -- Note: Zero IS true(!)
	print ("Zero is true")
else
	print ("Zero is false")
end

if type(variable) == "string" then
	print (variable, "is a string")
else
	print (variable, "is not a string")
end

if type(variable_2) == "string" then
	print (variable_2, "is a string")
elseif pigs_can_fly then
	print ("Pigs really CAN fly.")
else
	print (variable_2, "is not a string")
end
Zero is true
This is a string is a string
10 is not a string

Lua´Â ÁÖ¾îÁø Á¶°Ç½ÄÀÌ °ÅÁþÀ¸·Î Æò°¡µÉ ¶§ ±îÁö ¹Ýº¹Çϰųª, ÁÖ¾îÁø ¼öÄ¡ÀÇ ¹üÀ§¸¸Å­ ¹Ýº¹ÇÒ ¼ö ÀÖ´Â ÀϹÝÀûÀÎ Á¦¾î±¸Á¶¸¦ °¡Áö°í ÀÖ´Ù:
-- Small examples of Lua loops

-- Something to iterate
list = {"One", "Two", "Three", "Four", "Five", "Six"}

-- For-loops
print ("Counting from one to three:")
for element = 1, 3 do
	print (element, list[element])
end

print ("Counting from one to four,")
print ("in steps of two:")
for element = 1, 4, 2 do
	print (element, list[element])
end

-- While-loops

print ("Count elements in list")
print ("on numeric index")
element = 1
while list[element] do
	print (element, list[element])
	element = element + 1
end

-- Repeat-loop

print ("Count elements in list")
print ("using repeat")

element = 1
repeat
	print (element, list[element])
	element = element + 1
until not list[element]
Counting from one to three:
1 One
2 Two
3 Three
Counting from one to four
in steps of two:
1 One
3 Three
Counting elements in list
on numeric index
1 One
2 Two
3 Three
4 Four
5 Five
6 Six
Counting elements in list
using repeat
1 One
2 Two
3 Three
4 Four
5 Five
6 Six

ÇÔ¼ö #

Lua´Â »ç¿ëÀÚ°¡ ÇÔ¼ö¸¦ Á¤ÀÇÇÒ ¼ö ÀÖµµ·Ï ÇÑ´Ù. ÇÔ¼ö´Â Á¤ÇØÁø °¹¼öÀÇ ÆÄ¶ó¹ÌÅ͸¦ ¹ÞÀ» ¼ö ÀÖ´Ù:
-- ÇÔ¼ö¸¦ Á¤ÀÇÇÏ°í ±×°ÍÀ» È£ÃâÇØº¸ÀÚ

function add_two (parameter1, parameter2)
	return parameter1 + parameter2
end

print ( add_two(3, 4) )
7

Lua´Â ¶ÇÇÑ °¡º¯ÀûÀÎ °¹¼öÀÇ ÆÄ¶ó¹ÌÅ͸¦ Àü´Þ ÇÒ ¼öµµ ÀÖ´Ù. ´ÙÀ½ ¿¹Á¦¸¦ Âü°íÇ϶ó:

-- °¡º¯Àμö¸¦ ¹Þ´Â ÇÔ¼ö

function table_parameters(a, b, ...)
	print ("a is:", a, " b is:", b)
	local i
	i = 1
	while arg[i] do
		print ("More arguments:", arg[i])
		i = i + 1
	end
end

table_parameters("Hello", "there", 1, 2, "Hey")
a is: Hello b is: there
More arguments: 1
More arguments: 2
More arguments: Hey

°¡º¯Àμö Áö½ÃÀÚ(varargs-indicator) (...) À» »ç¿ëÇØ Àü´ÞµÈ À̸§¾ø´Â ÆÄ¶ó¹ÌÅ͵éÀº "arg" ¶ó´Â Å×À̺íÀ» ÅëÇØ Áö¿ªÀûÀ¸·Î ¿¢¼¼½º µÉ ¼ö ÀÖ´Ù.

º¯¼ö¿Í º¯¼öÀÇ Åë¿ë¹üÀ§(scope) #

Lua´Â ÇÔ¼ö ¾È¿¡¼­ Àü¿ª º¯¼ö¸¦ ¿¢¼¼½ºÇÒ ¼ö ÀÖ´Ù:
-- º¯¼ö¿Í ¹üÀ§(scope) ¸¦ Å×½ºÆ®ÇÏ´Â ¿¹Á¦

function access_global()
	i = i + 1
end

function create_a()
	a = 2
end

i = 0
print (i)
access_global()
print (i)

create_a()
print (a)
0
1
2

´ÙÀ½ ¿¹Á¦¿¡¼­ "local" Ű¿öµå¸¦ »ç¿ëÇѰͰú ºñ±³ÇØ º¸¶ó:

-- º¯¼ö¿Í ¹üÀ§(scope) ¸¦ Å×½ºÆ®ÇÏ´Â ¿¹Á¦

function access_global()
	local i
	i = 0       -- i ´Â ÇöÀç Á¤ÀǵÇÁö ¾Ê¾Ò´Ù (nil)
	i = i + 1
end

function create_a()
	local a
	a = 2
end

i = 0
print (i)
access_global()
print (i)

create_a()
print (a)           -- À̰ÍÀº nil À» Ãâ·ÂÇÒ°ÍÀÌ´Ù.
                    -- Àü¿ª¹üÀ§¿¡¼­ a°¡ Á¤ÀǵÇÁö ¾Ê¾Ò±â ¶§¹®ÀÌ´Ù.
0
0
nil

"°´Ã¼ ÁöÇâ" ÇÁ·Î±×·¡¹Ö #

Lua´Â °´Ã¼ÁöÇâ ºñ½º¹«¸®ÇÑ ¹«¾ð°¡¸¦ ÇÒ ´É·ÂÀÌ ÀÖ´Ù. ÀÌ ¿¹Á¦´Â »ó¼ÓÀ» Æ÷ÇÔÇÏÁö ¾Ê´Â´Ù. ±âº»ÀûÀ¸·Î, ÀÌ¿¹Á¦ÀÇ ÇÔ¼ö´Â Å×À̺í°ú ±×°Í¿¡ ¿¬°üµÈ ¸Þ¼­µå(method)µéÀ» »ý¼ºÇÑ´Ù. ±×¸®°í ±×°ÍÀ» À§ÇÑ µÎ°¡Áö ´Ù¸¥ Ç¥±â¹ýÀ» º¸¿©ÁØ´Ù. ¶ÇÇÑ ÀνºÅϽº¿Í ¿¬°üµÈ µ¥ÀÌÅ͸¦ ¾î¶»°Ô ¿¢¼¼½º ÇÏ´ÂÁöµµ º¸¿©ÁØ´Ù. ¸Þ¼­µå¸¦ È£ÃâÇÒ¶§ . À» »ç¿ëÇÏ´Â ´ë½Å¿¡ : À» »ç¿ëÇϸé ÀÚµ¿ÀûÀ¸·Î Å×À̺íÀÇ ÀνºÅϽº°¡ ¸Þ¼­µåÀÇ Ã¹¹øÂ° ÀÎÀÚ·Î Àü´ÞµÈ´Ù.
-- °´Ã¼ÁöÇâ ÇÁ·Î±×·¡¹Ö Å×½ºÆ®

function car(brand)
	-- ÀÌ ÇÔ¼ö´Â »ý¼ºÀÚ(constructor)ó·³ µ¿ÀÛÇÑ´Ù.
	local data
	data = {}

	-- ºê·£µå°¡ ¾øÀ¸¸é ±âº» ºê·£µå¸¦ °ñ¶óÁÖÀÚ.
	if brand == nil then
		data.Brand = "Volvo"
	else
		data.Brand = brand
	end

	data.report = function(self)
		print ("I'm a", self.Brand)
	end

	-- ÄÝ·Ð( : ) À» ½á¼­ Ç¥±âÇϸé ÀÚµ¿ÀûÀ¸·Î "self" ÆÄ¶ó¹ÌÅ͸¦ Ãß°¡ÇØ ÁØ´Ù.
	function data:say_something_smart()
		print ("I'm STILL a", self.Brand, "! Wroom!")
	end

	return data
end

my_car = car()
my_other_car = car("Ferrari")

print (my_car.Brand)
print (my_other_car.Brand)

my_car:report()       -- ¸Þ¼­µå¸¦ È£ÃâÇÒ¶§ . ´ë½Å : À» »ç¿ëÇÑ´Ù.
my_other_car:report()

my_car:say_something_smart()
my_other_car:say_something_smart()
Volvo
Ferrari
I'm a Volvo
I'm a Ferrari
I'm STILL a Volvo ! Wroom!
I'm STILL a Ferrari ! Wroom!


EditText | FindPage | DeletePage | LikePages

Powered by MoniWiki
xhtml1 | css2 | any browser | rss
Last modified 2004-02-26 12:56:42
Loading 0.3263 sec