tolua++その2(コマンド動作)

http://www.codenix.com/~tolua/

toluaの動作について。
コンパイルせずにlua本家http://www.lua.org/download.htmlインタプリタでtoluaの埋め込みスクリプトが動くものを書いてみた。src/bin/tolua.cをluaに移しただけ。

#!/usr/bin/lua

TOLUA_VERSION="tolua++-1.0.93(lua)"

local function help()
    print([[
usage: tolua++ [options] input_file

Command line options are:
  -v       : print version information.
  -o  file : set output file; default is stdout.
  -H  file : create include file.
  -n  name : set package name; default is input file root name.
  -p       : parse only.
  -P       : parse and print structure information (for debug).
  -S       : disable support for c++ strings.
  -1       : substract 1 to operator[] index (for compatibility with tolua5).
  -L  file : run lua file (with dofile()) before doing anything.
  -D       : disable automatic exporting of destructors for classes that have
             constructors (for compatibility with tolua5)
  -W       : disable warnings for unsupported features (for compatibility
             with tolua5)
  -C       : disable cleanup of included lua code (for easier debugging)
  -E  value[=value] : add extra values to the luastate
  -t       : export a list of types asociates with the C++ typeid name
  -q       : don't print warnings to the console
  -h       : print this message.
Should the input file be omitted, stdin is assumed;
in that case, the package name must be explicitly set.\n
]])
    os.exit(0)
end

local function version()
    print(TOLUA_VERSION.." (written by W. Celes, A. Manzur)")
    os.exit(0)
end

local function pdofile(path)
    local success, errmsg=pcall(dofile, path)
    if success then
        return true
    end
    print(errmsg)
    return false
end

local function new_iter(t)
    local i=0
    return function()
        i=i+1
        if i>#t then
            return
        end
        return t[i]
    end
end

------------------------------------------------------------------------------
-- main
------------------------------------------------------------------------------
if #arg==0 then
    help()
end

_extra_parameters={}
flags={}
local argMap={
    ['v']=function(iter) version() end,
    ['h']=function(iter) help() end,
    ['p']=function(iter) flags["p"]=true end,
    ['P']=function(iter) flags["P"]=true end,
    ['o']=function(iter) flags["o"]=iter() end,
    ['n']=function(iter) flags["n"]=iter() end,
    ['H']=function(iter) flags["H"]=iter() end,
    ['S']=function(iter) flags["S"]=true end,
    ['1']=function(iter) flags["1"]=true end,
    ['L']=function(iter) flags["L"]=iter() end,
    ['D']=function(iter) flags["D"]=true end,
    ['W']=function(iter) flags["W"]=true end,
    ['C']=function(iter) flags["C"]=true end,
    ['E']=function(iter) table.insert(_extra_parameters, iter()) end,
    ['t']=function(iter) flags["t"]=true end,
    ['q']=function(iter) flags["q"]=true end,
}
local iter=new_iter(arg)
while true do
    local v=iter()
    if not v then
        break
    end
    if v:sub(1, 1)=='-' then
        -- options
        key=v:sub(2, 2)
        if argMap[key] then
            argMap[key](iter)
        else
            print("tolua: unknown option '"..key.."'")
            os.exit(1)
        end
    else
        -- pkg
        --argMap.f=v 間違い
        flags.f=v
    end
end

--pdofile("src/bin/lua/compat-5.1.lua")
pdofile("src/bin/lua/compat.lua")
pdofile("src/bin/lua/basic.lua")
pdofile("src/bin/lua/feature.lua")
pdofile("src/bin/lua/verbatim.lua")
pdofile("src/bin/lua/code.lua")
pdofile("src/bin/lua/typedef.lua")
pdofile("src/bin/lua/container.lua")
pdofile("src/bin/lua/package.lua")
pdofile("src/bin/lua/module.lua")
pdofile("src/bin/lua/namespace.lua")
pdofile("src/bin/lua/define.lua")
pdofile("src/bin/lua/enumerate.lua")
pdofile("src/bin/lua/declaration.lua")
pdofile("src/bin/lua/variable.lua")
pdofile("src/bin/lua/array.lua")
pdofile("src/bin/lua/function.lua")
pdofile("src/bin/lua/operator.lua")
pdofile("src/bin/lua/template_class.lua")
pdofile("src/bin/lua/class.lua")
pdofile("src/bin/lua/clean.lua")
pdofile("src/bin/lua/doit.lua")

local err,msg = pcall(doit)
if not err then
    local _,_,label,msg = strfind(msg,"(.-:.-:%s*)(.*)")
    tolua_error(msg,label)
    print(debug.traceback())
end

単なるluaスクリプトになったので、中身の解読に入って行く(予定)。