- ¸ðµâ ÆÐ½º µî·ÏÇϱâ
- ºñÆ® ¿¬»ê
- CSV ÆÄ¼
- ¾Ï¹¬Àû(implicit)ÀÎ Àü¿ª º¯¼ö ¼±¾ð ¸·±â
- ù¹øÂ° ¹æ¹ý
- µÎ¹øÂ° ¹æ¹ý
1 ¸ðµâ ÆÐ½º µî·ÏÇϱâ
5.0 ¹öÀü±îÁö´Â LUA_PATH ¶ó´Â À̸§ÀÇ Àü¿ª º¯¼ö¿¡´Ù ¼³Á¤ÇÏ¸é µÆ´Âµ¥, 5.1 ¹öÀüºÎÅÍ package Å×ÀÌºí ¾ÈÀÇ path º¯¼ö(package.path)¸¦ ÀÌ¿ëÇØ¾ßÇÑ´Ù.
/// \brief ·ç¾Æ ¸ðµâ ÆÐ½º¸¦ µî·ÏÇÑ´Ù.
/// \param L ·ç¾Æ »óÅ °´Ã¼
/// \param path ÆÐ½º ¹®ÀÚ¿. "D:\data\;D:\data\skill\"°ú °°Àº Çü½Ä, Áï °¢°¢ÀÇ
/// µð·ºÅ丮¸¦ ';'·Î ±¸ºÐÁöÀº ¹®ÀÚ¿·Î µé¾î°¡ ÀÖ¾î¾ß ÇÑ´Ù.
/// \param bAppend ±âÁ¸ °æ·Î¿¡ Ãß°¡Çϴ°¡ÀÇ ¿©ºÎ
void SetModulePath(lua_State* L, const char* path, bool bAppend)
{
size_t before = 0;
size_t current = 0;
std::string original(path);
std::string modified;
// 012345678901234567890123456789
// D:\data\;D:\data\skill\
// b c
while (current < original.size())
{
current = original.find_first_of(';', before);
if (current == std::string::npos) current = original.size();
std::string token = original.substr(before, current - before);
if (!token.empty())
{
if (token[token.size()-1] != '\\')
token += '\\';
#ifdef _DEBUG
assert(::PathFileExists(token.c_str()) && "path not found");
#endif
modified += token + "?.lua;";
modified += token + "?.dll;";
}
before = current + 1;
}
if (!modified.empty())
{
if (bAppend)
{
lua_getfield(L, LUA_GLOBALSINDEX, "package");
assert(lua_istable(L, -1));
lua_getfield(L, -1, "path");
std::string old_path = lua_tostring(L, -1);
if (old_path[old_path.size()-1] != ';')
old_path += ';';
modified = old_path + modified;
lua_pop(L, 2);
}
lua_getfield(L, LUA_GLOBALSINDEX, "package");
assert(lua_istable(L, -1));
lua_pushstring(L, modified.c_str());
lua_setfield(L, -2, "path");
lua_pop(L, 1);
}
}
2 ºñÆ® ¿¬»ê
function extract(value, index)
local v = value
local m = 0
for i=1,index do
m = math.mod(v, 2)
v = v / 2
end
if m >= 1 then return 1 end
return 0
end
function bitwise_and(n1, n2)
local rvalue = 0
for i=1,32 do
if extract(n1, i) == 1 and extract(n2, i) == 1 then
rvalue = rvalue + math.pow(2, i-1)
end
end
return rvalue
end
function bitwise_or(n1, n2)
local rvalue = 0
for i=1,32 do
if extract(n1, i) == 1 or extract(n2, i) == 1 then
rvalue = rvalue + math.pow(2, i-1)
end
end
return rvalue
end
assert(bitwise_or(4, 1) == 5)
assert(bitwise_or(4, 2) == 6)
assert(bitwise_or(4, 4) == 4)
assert(bitwise_and(4, 1) == 0)
assert(bitwise_and(4, 2) == 0)
assert(bitwise_and(4, 4) == 4) Å©¾Æ¾Ç... ³Ê¹« ±æ´Ù. ·ç¾Æ¸¦ Àß ¾ËÁö ¸øÇؼ ±×·± °ÍÀϼöµµ ÀÖ°ÚÀ¸³ª, °³ÀÎÀûÀ¸·Î´Â mathlib.c ÆÄÀÏÀ» ¼öÁ¤Çؼ C ÇÔ¼ö Çϳª Ãß°¡ÇÏ´Â ÆíÀÌ ³ªÀ» °Å¶ó°í »ý°¢ÇÑ´Ù.
...
static int math_bwand(lua_State* L) {
int lhs = (int)luaL_checknumber(L, 1);
int rhs = (int)luaL_checknumber(L, 2);
lua_pushnumber(L, lhs & rhs);
return 1;
}
static int math_bwor(lua_State* L) {
int lhs = (int)luaL_checknumber(L, 1);
int rhs = (int)luaL_checknumber(L, 2);
lua_pushnumber(L, lhs | rhs);
return 1;
}
...
static const luaL_reg mathlib[] = {
{"abs", math_abs},
...»ý·«...
{"randomseed", math_randomseed},
+ {"bwand", math_bwand},
+ {"bwor", math_bwor},
{NULL, NULL}
};
3 CSV ÆÄ¼
"Programming in Lua"¶õ Ã¥¿¡ ³ª¿À´Â CSV ÆÄ¼¸¦ ¾à°£ ¼öÁ¤ÇÑ ÆÄ¼
CSVRow =
{
Columns = {}, -- Ä÷³ ¹®ÀÚ¿µé
new = function (self)
o = {}
setmetatable(o, self)
self.__index = self
return o
end,
parse = function(self, line)
self.Columns = {}
s = line .. ',' -- ending comma
local fieldstart = 1
repeat
-- next field is quoted? (start with `"'?)
if string.find(s, '^"', fieldstart) then
local a, c
local i = fieldstart
repeat
-- find closing quote
a, i, c = string.find(s, '"("?)', i+1)
until c ~= '"' -- quote not followed by quote?
if not i then
error('unmatched "')
end
local f = string.sub(s, fieldstart+1, i-1)
table.insert(self.Columns, (string.gsub(f, '""', '"')))
fieldstart = string.find(s, ',', i) + 1
else -- unquoted; find next comma
local nexti = string.find(s, ',', fieldstart)
table.insert(self.Columns, string.sub(s, fieldstart, nexti-1))
fieldstart = nexti + 1
end
until fieldstart > string.len(s)
end,
to_string = function(self)
local line = ""
for _, token in ipairs(self.Columns) do
line = line .. ","
if string.find(token, '[,"]') then
line = line .. '"' .. string.gsub(token, '"', '""') .. '"'
else
line = line .. token
end
end
return string.sub(line, 2)
end
}
CSVFile =
{
Rows = {},
new = function (self)
o = {}
setmetatable(o, self)
self.__index = self
return o
end,
load = function(self, filename)
local file = assert(io.open(filename, "r"))
while true do
local line = file:read("*line")
if line == nil then break end
local row = CSVRow:new()
row:parse(line)
table.insert(self.Rows, row)
end
file:close()
end,
save = function(self, filename)
local file = assert(io.open(filename, "w"))
for _, row in pairs(self.Rows) do
file:write(row:to_string() .. "\n")
end
end
}
-- ¿©±â¼ºÎÅÍ´Â »ùÇà ÄÚµå
f = CSVFile:new()
f:load("test.csv")
print(f.Rows[1].Columns[1])
f:save("output.csv")
4 ¾Ï¹¬Àû(implicit)ÀÎ Àü¿ª º¯¼ö ¼±¾ð ¸·±â
·ç¾Æ¿¡¼´Â local ±¸¹®À» »ç¿ëÇÏÁö ¾Ê°í º¯¼ö¸¦ ¼±¾ðÇÏ¸é ±âº»ÀûÀ¸·Î Àü¿ª º¯¼ö°¡ µÇ¾î¹ö¸°´Ù. LuaGarbageCollection Ç׸ñ¿¡¼µµ ¾ð±ÞÇßÁö¸¸, Àü¿ª Å×ÀÌºí¿¡ ÀÖ´Â ¿ÀºêÁ§Æ®µéÀº ±âº»ÀûÀ¸·Î °¡ºñÁö Ä÷º¼ÇÀÇ ´ë»óÀÌ µÇÁö ¾Ê´Â´Ù. Á¶±×¸¸ ÇÁ·Î±×·¥À̶ó¸é ¸ð¸£µÇ, ¿À·£ ½Ã°£ µ¿¾È ½ÇÇàÇØ¾ßÇÏ´Â ÇÁ·Î±×·¥ °°Àº °æ¿ì¿¡´Â ¹«½ÃÇÒ ¼ö ¾ø´Â ÀÏÀ̶ó°í ÇÒ ¼ö ÀÖ´Ù.
Àü¿ª ¿ÀºêÁ§Æ®µéÀÌ µé¾îÀÖ´Â Àü¿ª Å×ÀÌºíµµ ÀϹÝÀûÀÎ Å×À̺í°ú ¶È°°Àº Å×À̺íÀ̱⠶§¹®¿¡, ¸ÞŸÅ×À̺í°ú ¸ÞŸ¸Þ¼µå¸¦ Àß ¼³Á¤ÇØÁÖ¸é, ¾Ï¹¬ÀûÀÎ Àü¿ª º¯¼ö Àбâ/¾²±â¸¦ ¸·À» ¼ö ÀÖ´Ù.
4.1 ù¹øÂ° ¹æ¹ý
function declare(name, initval)
rawset(_G, name, initval or false)
end
setmetatable(_G, {
__newindex =
function(_, n)
error("attempt to write to undeclared variable " .. n, 2)
end,
__index =
function(_, n)
error("attempt to read undeclared variable " .. n, 2)
end,
}) À§ Äڵ带 ½ÇÇàÇÏ°í ³ª¼´Â, º¯¼ö ¼±¾ðÀº declare ÇÔ¼ö¸¦ ÀÌ¿ëÇØ¾ß¸¸ ÇÑ´Ù. ¾Æ´Ï¸é ¿¡·¯°¡ ¹ß»ýÇÑ´Ù.
> a = 1
stdin:1: attempt to write to undeclared variable a
> declare("a")
> a = 1
ÀÌ ¹æ¹ý¿¡´Â ¾à°£ ±ÍÂúÀº ¹®Á¦°¡ Çϳª Àִµ¥, ¾î¶² º¯¼ö°¡ nil °ªÀ» °¡Áö°í ÀÖ´ÂÁö °Ë»çÇÏ´Â °æ¿ì¿¡µµ ¿¡·¯°¡ ¹ß»ýÇÑ´Ù´Â Á¡ÀÌ´Ù. ÀÌ ¹®Á¦´Â rawset ÇÔ¼ö¸¦ ÀÌ¿ëÇÏ¸é µÈ´Ù.
-- ÀÌ·± Äڵ带...
if var == nil then
...
end
-- ÀÌ·¸°Ô º¯°æÇÑ´Ù...
if rawget(var) == nil then
...
end
4.2 µÎ¹øÂ° ¹æ¹ý
local declared_names = {}
function declare(name, initval)
rawset(_G, name, initval)
declared_names[name] = true
end
setmetatable(_G, {
__newindex =
function(t, n, v)
if not declared_names[n] then
error("attempt to write to undeclared variable "..n, 2)
else
rawset(t, n, v)
end
end,
__index =
function(_, n)
if not declared_names[n] then
error("attempt to read undeclared variable "..n, 2)
else
return nil
end
end
}) ù¹øÂ° ¹æ¹ý¿¡¼ rawget ¹®Á¦¸¦ ÇØ°áÇÑ ¹æ¹ýÀÌ´Ù. »ç¿ëÇÏ±â ÆíÇÑ ´ë½Å, ¸ÞŸ¸Þ¼µå¸¦ È£ÃâÇÒ °æ¿ì°¡ ÀÖÀ¸¹Ç·Î ¾à°£ ´À¸®´Ù. ÇÏÁö¸¸ ¾î¶² º¯¼öÀÇ °ªÀÌ nilÀÎÁö °Ë»çÇÒ ¶§¸¸À̹ǷΠ¹«½ÃÇÒ¸¸ ÇÏ´Ù.
SeriousMoin v1 (koMoinMoin 1.0a4 Modified)