/*
** 作者:苏晓 时间:2006年5月24日
** 若有转载请注明出处,谢谢!
*/
Lua脚本的执行效率是相当高的,源文件用纯C写成相当小巧,其可扩展性相当强,前途一片光明。
其源文件写得不复杂,相当值得一读。
作为中国人,在编程语言的使用上如果能够使用中文作为变量名是非常好的一件事。但是标准的Lua并不支持中文变量名。虽然可以在字符串中处理中文。
其实只要修改少量代码就可以让Lua支持中文变量名。
打开lua的源文件文件夹(包)可以从文件名知道,llex.h,llex.c是用来做文件语法分析的。
备份llex.c为llex_old.c
打开llex.c进行修改如下:
/*
** $Id: llex.c,v 1.119 2003/03/24 12:39:34 roberto Exp $
** Lexical Analyzer
** See Copyright Notice in lua.h
*/
#include <ctype.h>
#include <string.h>
#define llex_c
#include "lua.h"
#include "ldo.h"
#include "llex.h"
#include "lobject.h"
#include "lparser.h"
#include "lstate.h"
#include "lstring.h"
#include "lzio.h"
#define next(LS) (LS->current = zgetc(LS->z))
/* ORDER RESERVED */
static const char *const token2string[] =
{
"and", "break", "do", "else", "elseif", "end", "false", "for", "function",
"if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true",
"until", "while", "*name", "..", "...", "==", ">=", "<=", "~=", "*number",
"*string", "<eof>"
};
void luaX_init(lua_State *L)
{
int i;
for (i = 0; i < NUM_RESERVED; i++)
{
TString *ts = luaS_new(L, token2string[i]);
luaS_fix(ts); /* reserved words are never collected */
lua_assert(strlen(token2string[i]) + 1 <= TOKEN_LEN);
ts->tsv.reserved = cast(lu_byte, i + 1); /* reserved word */
}
}
#define MAXSRC 80
void luaX_checklimit(LexState *ls, int val, int limit, const char *msg)
{
if (val > limit)
{
msg = luaO_pushfstring(ls->L, "too many %s (limit=%d)", msg, limit);
luaX_syntaxerror(ls, msg);
}
}
void luaX_errorline(LexState *ls, const char *s, const char *token, int line)
{
lua_State *L = ls->L;
char buff[MAXSRC];
luaO_chunkid(buff, getstr(ls->source), MAXSRC);
luaO_pushfstring(L, "%s:%d: %s near `%s'", buff, line, s, token);
luaD_throw(L, LUA_ERRSYNTAX);
}
static void luaX_error(LexState *ls, const char *s, const char *token)
{
luaX_errorline(ls, s, token, ls->linenumber);
}
void luaX_syntaxerror(LexState *ls, const char *msg)
{
const char *lasttoken;
switch (ls->t.token)
{
case TK_NAME:
lasttoken = getstr(ls->t.seminfo.ts);
break;
case TK_STRING:
case TK_NUMBER:
lasttoken = luaZ_buffer(ls->buff);
break;
default:
lasttoken = luaX_token2str(ls, ls->t.token);
break;
}
luaX_error(ls, msg, lasttoken);
}
const char *luaX_token2str(LexState *ls, int token)
{
if (token < FIRST_RESERVED)
{
lua_assert(token == (unsigned char)token);
return luaO_pushfstring(ls->L, "%c", token);
}
else
return token2string[token - FIRST_RESERVED];
}
static void luaX_lexerror(LexState *ls, const char *s, int token)
{
if (token == TK_EOS)
luaX_error(ls, s, luaX_token2str(ls, token));
else
luaX_error(ls, s, luaZ_buffer(ls->buff));
}
static void inclinenumber(LexState *LS)
{
next(LS); /* skip `/n' */
++LS->linenumber;
luaX_checklimit(LS, LS->linenumber, MAX_INT, "lines in a chunk");
}
void luaX_setinput(lua_State *L, LexState *LS, ZIO *z, TString *source)
{
LS->L = L;
LS->lookahead.token