lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


> >(Roberto has something similar because CGILua uses this.)

For anyone interested, here is my scipt. It is very primitive, but it
does the job. It reads a source file (.c or .h) in stdin and writes a
modified version in stdout; moreover, you have to set the "file" variable
with the name of the file being handled. Something like

> for xx in `ls l*.[ch]`; do
  lua file=$xx change.lua  <$xx >changed/$xx
  done


The modified version has an extra parameter in all functions, "lua_State 
*L", and an extra argument in all function calls, "L". Moreover, the type 
"lua_CFunction" now has signature 

  typedef void (*lua_CFunction) (lua_State *L);

-- Roberto



-------------------------change.lua-----------------------------------
t = read('.*')

if strfind(t, '@<') then
  error('file has @<!!!')
end

-- list of reserved words, macros and other "functions" that should not
-- be modified
p1 = {'if', 'while', 'for', 'void', 'LUA_ANYTAG', 'int', 'return', 'else',
'switch', 'MAX_WORD', 'MAX_INT', 'EOZ', 'MINGLOBAL', 'NEED_OTHER',
'MAXMESSAGE', 'PI', 'TORAD', 'FROMRAD', 'MAX_ARG', 'MAX_BYTE', 'NUM_HASHS',
'RADIANS_PER_DEGREE', 'expected',
}

-- continuation of p1 (it should be only one table...)
p2 = {'ttype', 'nvalue', 'svalue', 'tsvalue', 'clvalue', 'avalue', 'fvalue',
'tfvalue', 'protovalue', 'strcmp', 'memcpy', 'strcpy', 'printf', 'sprintf',
'fprintf', 'skip_word', 'get_word' , 'next_word', 'sscanf', 'isatty',
'fgets', 'setlocale', 'strchr', 'perror', 'sizeof', 'va_start', 'va_end',
'vsprintf', 'setjmp', 'longjmp', 'freopen', 'fclose', 'fgetc', 'ungetc',
'exit', 'fopen', 'isalpha', 'isalnum', 'isdigit', 'toupper', 'tolower',
'zgetc', 'zungetc', 'free', 'malloc', 'realloc', 'strlen', 'strcoll', 
'read', 'fread', 'getc', 'strftime', 'popen', 'pclose', 'strerror', 
'localtime', 'time', 'getenv', 'system', 'remove', 'rename', 'tmpnam',
'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2', 'ceil', 'floor',
'fmod', 'fputs', 'sqrt', 'pow', 'log', 'log10', 'exp' , 'rand', 'srand', 
'iscntrl', 'ispunct', 'isspace', 'islower', 'isupper', 'strncmp', 'strpbrk',
'strncpy', 'strstr', 'skip_word', 'get_word', 'next_word', 'getchar', 'signal',
'laction', 'strtol', 'memmove', 'luaZ_Fopen', 'luaZ_mopen', 'zname', 'memcmp',
'zread', 'fscanf', 'fwrite', 'fseek', 'ftell', 'fflush', 'clock', 'strcat',
'frexp', 'ldexp', 'main', 'lua_open', 'isxdigit', 'Copyright', 'memset',
'fabs',
}

-- some pieces of code need special treatment
patch = {
  ["lua.c"] =
    {["int %(%*f%)%(char %*%)"] = "int (*f)(lua_State *, char *)",
     ["int i;"] = "int i;\n  lua_State *L = lua_open();",
     ["laction %(int i%) {.-}"] = "laction (int i) {}",
    },
  ["lstate.h"] =
    {["#define L	lua_state"] = ""},
  ["ltm.c"] =
    {["%(%*fn%)%(TObject %*%)"] = "(*fn)(lua_State *, TObject *)"},
  ["ltm.h"] =
    {["%(%*fn%)%(TObject %*%)"] = "(*fn)(lua_State *, TObject *)"},
  ["lstring.c"] =
    {["%(%*fn%)%(TObject %*%)"] = "(*fn)(lua_State *, TObject *)"},
  ["lstring.h"] =
    {["%(%*fn%)%(TObject %*%)"] = "(*fn)(lua_State *, TObject *)"},
  ["lapi.c"] =
    {["int %(lua_call.*ag%(L, t%); }"] = "\n\n",
     ["lua_State %*lua_setstate %(.-}"] = ""
    },
  ["lua.h"] =
    {["typedef void %(%*lua_CFunction%) %(void%);"] =
      "typedef void (*lua_CFunction) (lua_State *L);",
     ["void%s*lua_open%s*%(void%);"] = "lua_State            *lua_open (void);",
     ["extern lua_State %*lua_state;"] = "",
    },
  ["luadebug.h"] =
   {["typedef void %(%*lua_LHFunction%) %("] =
     "typedef void (*lua_LHFunction) (lua_State *L, ",
    ["typedef void %(%*lua_CHFunction%) %("] =
     "typedef void (*lua_CHFunction) (lua_State *L, ",
   },
  ["ldo.c"] = {
    ["%(%*f%)%(%);"] = "(*f)(L);",
    ["%(%*fn%)%(TObject %*%)"] = "(*fn)(lua_State *, TObject *)",
    ["hook%)%("] = "hook)(L, "},
  ["ldo.h"] =
    {["%(%*fn%)%(TObject %*%)"] = "(*fn)(lua_State *, TObject *)"},
  ["lstate.c"] =
    {["(if %(lua_state%) return;)"] = "",
     ["void lua_open %(void%)"] = "lua_State *lua_open (void)",
     ["lua_State %*lua_state = NULL;"] = "",
     ["lua_state = luaM_new"] = "lua_State *L = luaM_new",
     ["luaB_predefine%(L%);"] = "luaB_predefine(L);\n  return L;",
    },
  ["lauxlib.c"] =
    {["lua_open%(%);  /%* make sure lua is already open %*/"] = "",},

}

x = {}
foreach(p1, function (_, v) x[v]=1 end)
foreach(p2, function (_, v) x[v]=1 end)

t = gsub(t, '(%a[_%w]*)%(%)', function (n)
      if x[n] then return n.."@<)"
      else return n..'@<L)' end
    end)

t = gsub(t, '(%a[_%w]*)%(', function (n)
      if x[n] then return n.."@<"
      else return n..'@<L, ' end
    end)

t = gsub(t, '(%a[_%w]*)%s%s*%(void%)', function (n)
     if x[n] then return n.." @<void)"
     else return n..' @<lua_State *L)' end
    end)

t = gsub(t, '(%a[_%w]*)%s%s*%(', function (n)
      if x[n] then return n.." @<"
      else return n..' @<lua_State *L, ' end
    end)

t = gsub(t, '@<', '(')

if patch[file] then
  foreach(patch[file], function (a, b) t = gsub(t, a, b) end)
end

if file ~= "lstate.h" then write('#include "lstate.h"\n\n') end

write(t)