看流星社区

 找回密码
 注册账号
查看: 2102|回复: 0

让LUA5.21支持中文函数名

[复制链接]

该用户从未签到

发表于 2013-1-27 20:25:04 | 显示全部楼层 |阅读模式
在使用最新版的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;
        }
      }
点击按钮快速添加回复内容: 支持 高兴 激动 给力 加油 苦寻 生气 回帖 路过 感恩
您需要登录后才可以回帖 登录 | 注册账号

本版积分规则

小黑屋|手机版|Archiver|看流星社区 |网站地图

GMT+8, 2024-5-4 18:11

Powered by Kanliuxing X3.4

© 2010-2019 kanliuxing.com

快速回复 返回顶部 返回列表