怎么让 Lua 5.3.4 支持中文变量名和中文函数名
1. 在官网下载最新版Lua源码
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 ')'
┆凉┆暖┆降┆等┆幸┆我┆我┆里┆将┆ ┆可┆有┆谦┆戮┆那┆ ┆大┆始┆ ┆然┆
┆薄┆一┆临┆你┆的┆还┆没┆ ┆来┆ ┆是┆来┆逊┆没┆些┆ ┆雁┆终┆ ┆而┆
┆ ┆暖┆ ┆如┆地┆站┆有┆ ┆也┆ ┆我┆ ┆的┆有┆精┆ ┆也┆没┆ ┆你┆
┆ ┆这┆ ┆试┆方┆在┆逃┆ ┆会┆ ┆在┆ ┆清┆来┆准┆ ┆没┆有┆ ┆没┆
┆ ┆生┆ ┆探┆ ┆最┆避┆ ┆在┆ ┆这┆ ┆晨┆ ┆的┆ ┆有┆来┆ ┆有┆
┆ ┆之┆ ┆般┆ ┆不┆ ┆ ┆这┆ ┆里┆ ┆没┆ ┆杀┆ ┆来┆ ┆ ┆来┆