看到网上各种转载修改源码使lua支持中文的,结果试了下发现测试出来有问题,如:
>function 为而的() end
stdin:1: '(' expected near ')'
这里就会报错,只好改下,就没问题了,如下改后的源码(改动之处已标明,lua5.1/lua5.2/lua5.3均支持)。
</pre><pre name="code" class="javascript">
#pragma once
#include <ctype.h>
//
//
//这是中文函数名添加的函数
//
/*开始添加支持中英文变量名*/
/*因为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);
//if(ls->current!='(')
if(isChineseCode(ls->current)) //<<<<<<<<<<<<<<<<<<< 第二处
{
save_and_next(ls); //处理了一个中文字符
}
}
else
{
//if(ls->current!='(')
{
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
//
//
//以上是中文变量名定义函数
//
//