修改源代码,让Lua支持中文,中英混合变量名收藏

   修改源代码,让Lua支持中文,中英混合变量名收藏

新一篇: Doris一款为Lua制作的小巧OpenGL封装 | 旧一篇: 自己写的DllCall类方便dll动态链接库函数调用

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

/*
** 作者:苏晓  时间: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 = TK_EOS; /* no look-ahead token */
  LS->z = z;
  LS->fs = NULL;
  LS->linenumber = 1;
  LS->lastline = 1;
  LS->source = source;
  next(LS); /* read first char */
  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值