打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
【小技巧】如何修改 Lua 5.1 的引擎,让Lua支持中文变量名、混合变量名
2008-09-29 21:20 2275人阅读 评论(0) 收藏 举报
最近在倒腾 Lua 脚本引擎。Lua 现在广泛用于各种游戏、PSP中,速度极快,与 C++ 宿主代码“相处”能力强,还有第三方增强工程如 LuaPlus, LuaBind 等的支持,笔者正是在使用 LuaPlus 这个不错的 Lua C++ 封装,内置 Lua 5.1 引擎。

Lua 本身不能支持中文变量名或函数名,作为想尽善尽美的支持脚本的软件来说,当然不爽啦。从网上搜到了一些代码,能够修改老版本的 Lua 核心使得 Lua 支持中文变量名,如:价格=45 之类,但 Lua 5.1 的核心代码已经略有不同了。当然只要稍微作些改动,就能让 Lua 5.1 乖乖的也认识起中文变量名、函数名来。

以 LuaPlus 为例(Visual Studio 2005 IDE环境下):

1. 找到 LuaPlus 工程下的 Lua Source Files 下的 llex.c;

2. 在该文件中找到下面所列函数;
  1. static int llex (LexState *ls, SemInfo *seminfo)
3. 这是 Lua 的语法分析模块的关键函数,它由一长段 switch 构成,在一串 case 后找到 default 分支

4. 在该分支下找到如下代码段,这是一段完整的 if 分支,该代码段负责将符合条件的字符组合识别为 identifier (函数名或变量名)或 keyword (关键字)
  1. else if (isalpha(ls->current) || ls->current == '_') {
  2.           /* identifier or reserved word */
  3.           TString *ts;
  4.           if (ls->current == 'L') {
  5.             next(ls);
  6.             if (ls->current == '"' || ls->current == '/'') {
  7.               read_wstring(ls, ls->current, seminfo);
  8.               return TK_WSTRING;
  9.             }
  10.             save(ls, 'L');
  11.           }
  12.           /* identifier or reserved word */
  13.           do {
  14.             save_and_next(ls);
  15.           } while (isalnum(ls->current) || ls->current == '_');
  16.           ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  17.                                   luaZ_bufflen(ls->buff));
  18.           if (ts->tsv.reserved > 0)  /* reserved word? */
  19.             return ts->tsv.reserved - 1 + FIRST_RESERVED;
  20.           else {
  21.             seminfo->ts = ts;
  22.             return TK_NAME;
  23.           }
  24.         }


5. 将该段替换为下面这一段即可,其作用在于加入了对中文ASCII字符的识别

  1. else if (isalpha(ls->current) || ls->current == '_' || ls->current > 0x80) {
  2.           /* identifier or reserved word */
  3.           TString *ts;
  4.           if (ls->current == 'L') {
  5.             next(ls);
  6.             if (ls->current == '"' || ls->current == '/'') {
  7.               read_wstring(ls, ls->current, seminfo);
  8.               return TK_WSTRING;
  9.             }
  10.             save(ls, 'L');
  11.           }
  12.           /* identifier or reserved word */
  13.           do {
  14.               if(ls->current > 0x80)
  15.               {
  16.                  save_and_next(ls);
  17.                  save_and_next(ls);
  18.               }
  19.               else
  20.                  save_and_next(ls);
  21.           } while (isalnum(ls->current) || ls->current == '_' || ls->current > 0x80);
  22.           ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
  23.                                   luaZ_bufflen(ls->buff));
  24.           if (ts->tsv.reserved > 0)  /* reserved word? */
  25.             return ts->tsv.reserved - 1 + FIRST_RESERVED;
  26.           else {
  27.             seminfo->ts = ts;
  28.             return TK_NAME;
  29.           }
  30.         }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
怎么让 Lua 5.3.4 支持中文变量名和中文函数名
交易中的复盘,你是这样做的吗?
用C/C++为 Lua 脚本设置全局表变量(转)
lua和其他语言
lua
Lua脚本语言入门
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服