让LUA5.20支持中文函数名

hby95703 hby95703 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
让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 ;
...全文
评论 3
收藏 已收藏
分享
返回顶部
hby95703
脚本语言 37,738 成员 34,208 内容