- 注册时间
- 2011-3-6
- 最后登录
- 1970-1-1
该用户从未签到
|
在使用最新版的LUA5.21的过程中,我发现它可以支持中文变量名,这很好。 但是就算是在脚本中注册了中文的函数名,然后在脚本中使用,会无反应。例如:lua_register(bat_MyLuaPlus.g_pLua,"函数A",YaoYao);//注册函数 YaoYao() 到中文 "函数A",
然后在脚本 " a.lua" 中:函数A()
调用:luaL_dofile(bat_MyLuaPlus.g_pLua, " a.lua");
///////////////////////////////////////////////////////////////////但是如果这样:lua_register(bat_MyLuaPlus.g_pLua,"AA",YaoYao);//注册函数 YaoYao() 到 "AA",然后在脚本 " a.lua" 中:A(A)
调用:luaL_dofile(bat_MyLuaPlus.g_pLua, " a.lua");这样,可以执行成功。
========================================我百度了,所有对LUA中文函数名支持的修改,都是基于5.1版本LUA的,所以搞了一个下午,把5.20版本的中文函数名支持给改了出来:修改方法如下:第一步:先找到5.20版本src文件夹中的llex.c文件打开,找到static int llex (LexState *ls, SemInfo *seminfo) {这个函数,在它的前面添加如下代码:
//////////////////////////////////////////////////////////////////////////
//
//这是中文函数名添加的函数
//////////////////////////////////////////////////////////////////////////
/*开始添加支持中英文变量名*/
/*因为LS->current是int类型!*/
#define USE_CHINESE_NAME
#ifdef USE_CHINESE_NAME
#define isChineseCode(charint) (charint > 0x80)
#define readxxname(ls) readChinesename(ls)
void readChinesename(LexState *ls)
{
do
{
if (isChineseCode(ls->current))
{
save_and_next(ls);
save_and_next(ls); //处理了一个中文字符
}
else
{
save_and_next(ls); //处理英文字符或者下划线
}
} while (isChineseCode(ls->current) || ls->current == '_' || isalnum(ls->current));
}
#else
#define isChineseCode(charint) 0
#define readxxname(ls) readname(ls)
void readname(LexState *ls)
{
do
{
save_and_next(ls); //处理英文字符或者下划线
} while (isChineseCode(ls->current) || ls->current == '_' || isalnum(ls->current));
}
#endif
//////////////////////////////////////////////////////////////////////////
//
//以上是中文变量名定义函数
//
//////////////////////////////////////////////////////////////////////////
=======================第二步:进入刚才的static int llex (LexState *ls, SemInfo *seminfo) {函数的内部,找到default: {部分代码,反它替换为以下代码:(注意括号的对称性)
default: {
if (lislalpha(ls->current)|| isChineseCode(ls->current)) { /* identifier or reserved word? */
TString *ts;
readxxname(ls);
ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
luaZ_bufflen(ls->buff));
//////////////////////////////////////////////////////////////////////////
seminfo->ts = ts;
if (isreserved(ts)) /* reserved word? */
return ts->tsv.extra - 1 + FIRST_RESERVED;
else {
return TK_NAME;
}
}
else { /* single-char tokens (+ - / ...) */
int c = ls->current;
next(ls);
return c;
}
} |
|