怎么讓 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 ')'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM