让LUA5.20支持中文函数名

hby95703 2013-01-14 05:01:10
让LUA5.20支持中文函数名
  在使用最新版的LUA5.20的过程中,我发现它可以支持中文变量名,这很好。
  但是就算是在脚本中注册了中文的函数名,然后在脚本中使用,会无反应。
例如:
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;
}
}
=======================================
在这再附上改好的源码,在VC2010中直接编译为lib静态库。
http://www.kuaipan.cn/file/id_1191260719236405.htm ;
...全文
290 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
tkminigame 2013-01-15
  • 打赏
  • 举报
回复
lua不清楚,python里面可以这么做,骗你是小狗
甲 = '中文变量'
打印 = print
求长度 = len
打印(甲,求长度(甲))
bugs2k 2013-01-14
  • 打赏
  • 举报
回复
ChongQingJin28 2013-01-14
  • 打赏
  • 举报
回复
不错。挺好。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧