让石器的LUA支持中文变量名的第二方法

大部分石器时代的代码里都采用的LUA5.1,并不支持中文的变量,我们通过修改LUA代码可以实现中文变量名,在我的老端里早已修改成中文变量也把接口追加了中文名,中文是为了方便记忆(毕竟英文单词太长又是大小写就很难记的嘛,哈哈)

很早之前已经讲过让LUA支持中文变量名的方法,这里把第一种重新说明下,下面会教大家第二种方法。

修改代码如下:

方法一:

找到 llex.c 文件,打开后,大概在420-432行进行修改。

原来的代码如下:

else if (isalpha(ls->current) || ls->current == '_') { 
  /* identifier or reserved word */                    
  TString *ts;                                         
  do {                                                 
    save_and_next(ls);                                 
  } while (isalnum(ls->current) || ls->current == '_');
  ts = luaX_newstring(ls, luaZ_buffer(ls->buff),       
                          luaZ_bufflen(ls->buff));     
  if (ts->tsv.reserved > 0)  /* reserved word? */      
    return ts->tsv.reserved - 1 + FIRST_RESERVED;      
  else {                                               
    seminfo->ts = ts;                                  
    return TK_NAME;                                    
  }                                                    
}

修改后的代码如下:

else if (isalpha(ls->current) || ls->current == '_' || ls->current > 0x80) { 
  /* identifier or reserved word */                                          
  TString *ts;                                                               
  do {                                                                       
      if(ls->current > 0x80)                                                 
      {                                                                      
         save_and_next(ls);                                                  
         save_and_next(ls);                                                  
      }                                                                      
      else                                                                   
         save_and_next(ls);                                                  
  } while (isalnum(ls->current) || ls->current == '_' || ls->current > 0x80);
  ts = luaX_newstring(ls, luaZ_buffer(ls->buff),                             
                          luaZ_bufflen(ls->buff));                           
  if (ts->tsv.reserved > 0)  /* reserved word? */                            
    return ts->tsv.reserved - 1 + FIRST_RESERVED;                            
  else {                                                                     
    seminfo->ts = ts;                                                        
    return TK_NAME;                                                          
  }                                                                          
}

通过对比我们可发现,只有两处被修改了,都是增加了|| ls->current > 0x80


方法二:

直接修改 lctype.c 文件,这样是把对字符编码属性进行定义,把第8行到第b行, 以及e行的全部从 0x00 修改为 0x01 或 0x05, 若修改为 0x05 表示可打印

修改后的代码如下:

LUAI_DDEF const lu_byte luai_ctype_[UCHAR_MAX + 2] = {
    0x00,  /* EOZ */
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,    /* 0. */
    0x00,  0x08,  0x08,  0x08,  0x08,  0x08,  0x00,  0x00,
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,    /* 1. */
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
    0x0c,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,    /* 2. */
    0x04,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,
    0x16,  0x16,  0x16,  0x16,  0x16,  0x16,  0x16,  0x16,    /* 3. */
    0x16,  0x16,  0x04,  0x04,  0x04,  0x04,  0x04,  0x04,
    0x04,  0x15,  0x15,  0x15,  0x15,  0x15,  0x15,  0x05,    /* 4. */
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,    /* 5. */
    0x05,  0x05,  0x05,  0x04,  0x04,  0x04,  0x04,  0x05,
    0x04,  0x15,  0x15,  0x15,  0x15,  0x15,  0x15,  0x05,    /* 6. */
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,    /* 7. */
    0x05,  0x05,  0x05,  0x04,  0x04,  0x04,  0x04,  0x00,
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,    /* 8. */
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,    /* 9. */
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,    /* a. */
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,    /* b. */
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,    /* c. */
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,    /* d. */
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,    /* e. */
    0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,  0x05,
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,    /* f. */
    0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,  0x00,
  };

用任意一种方法修改完毕后,重新编译LUA即可。接口的话就请自行去myluaconst.c里进行汉化吧


发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。