怎么让 Lua 5.3.4 支持中文变量名和中文函数名

1. 在官网下载最新版Lua源码

Lua :Download

2. 解压后进入目录,找到/src/llex.c,打开修改

找到如下内容

修改为下面代码,并保存。

default: {
  if (lislalpha(ls->current)|| ls->current >= 0x80) {  /* identifier or reserved word? *///修改
    TString *ts;
    do {
      if (ls->current >= 0x80) {  //修改
        save_and_next(ls);  //修改
        if(ls->current !='('&&ls->current >=0x80)//修改
          save_and_next(ls);  //修改
      }
      else if(ls->current !='('){  //修改
        save_and_next(ls);  //修改
      } 
    } while (lislalnum(ls->current)|| ls->current >= 0x80);//修改
    ts = luaX_newstring(ls, luaZ_buffer(ls->buff),
                        luaZ_bufflen(ls->buff));
    seminfo->ts = ts;
    if (isreserved(ts))  /* reserved word? */
      return ts->extra - 1 + FIRST_RESERVED;
    else {
      return TK_NAME;
    }
  }
  else {  /* single-char tokens (+ - / ...) */
    int c = ls->current;
    next(ls);
    return c;
  }
}

3. 编译

  • Mac OS X 系统
  cd lua-5.3.4 #进入lua目录
  make macosx test
  make install
  • Linux 系统
  cd lua-5.3.4
  make linux test
  make install
  • Win 系统可以用vs编译。

ps.这样修改也支持中文函数名,网上修改的方法 function 跑()运行会报错如下:

lua: 任务.lua:18: '(' expected near ')'

原文地址:https://www.cnblogs.com/flipped/p/7466228.html