LuaInterface
public static int fread(CharPtr ptr, int size, int num, Stream stream){
int num_bytes = num * size;
byte[] bytes = new byte[num_bytes];
try{
int result = stream.Read(bytes, 0, num_bytes);
bytes = Encoding.Convert(Encoding.GetEncoding("GB2312"), Encoding.Unicode, bytes, 0, result);
var chars = Encoding.Unicode.GetChars(bytes);
for (int i = 0; i < chars.Length; i++){
ptr[i] = chars[i];
}
return chars.Length / size;
} catch{
return 0;
}
}
public static int luaL_loadbuffer(lua_State L, CharPtr buff, uint size,
CharPtr name){
LoadS ls = new LoadS();
ls.s = new CharPtr(buff);
ls.size = (uint) strlen(ls.s);
return lua_load(L, getS, ls, name);
}
private static int llex(LexState ls, SemInfo seminfo){
luaZ_resetbuffer(ls.buff);
for (;;){
switch (ls.current){
case '\n':
case '\r':{
inclinenumber(ls);
continue;
}
case '-':{
next(ls);
if (ls.current != '-') return '-';
/* else is a comment */
next(ls);
if (ls.current == '['){
int sep = skip_sep(ls);
luaZ_resetbuffer(ls.buff); /* `skip_sep' may dirty the buffer */
if (sep >= 0){
read_long_string(ls, null, sep); /* long comment */
luaZ_resetbuffer(ls.buff);
continue;
}
}
/* else short comment */
while (!currIsNewline(ls) && ls.current != EOZ)
next(ls);
continue;
}
case '[':{
int sep = skip_sep(ls);
if (sep >= 0){
read_long_string(ls, seminfo, sep);
return (int) RESERVED.TK_STRING;
} else if (sep == -1) return '[';
else luaX_lexerror(ls, "invalid long string delimiter", (int) RESERVED.TK_STRING);
}
break;
case '=':{
next(ls);
if (ls.current != '=') return '=';
else{
next(ls);
return (int) RESERVED.TK_EQ;
}
}
case '<':{
next(ls);
if (ls.current != '=') return '<';
else{
next(ls);
return (int) RESERVED.TK_LE;
}
}
case '>':{
next(ls);
if (ls.current != '=') return '>';
else{
next(ls);
return (int) RESERVED.TK_GE;
}
}
case '~':{
next(ls);
if (ls.current != '=') return '~';
else{
next(ls);
return (int) RESERVED.TK_NE;
}
}
case '"':
case '\'':{
read_string(ls, ls.current, seminfo);
return (int) RESERVED.TK_STRING;
}
case '.':{
save_and_next(ls);
if (check_next(ls, ".") != 0){
if (check_next(ls, ".") != 0)
return (int) RESERVED.TK_DOTS; /* ... */
else return (int) RESERVED.TK_CONCAT; /* .. */
} else if (!isdigit(ls.current)) return '.';
else{
read_numeral(ls, seminfo);
return (int) RESERVED.TK_NUMBER;
}
}
case EOZ:{
return (int) RESERVED.TK_EOS;
}
default:{
if (isspace(ls.current)){
lua_assert(!currIsNewline(ls));
next(ls);
continue;
} else if (isdigit(ls.current)){
read_numeral(ls, seminfo);
return (int) RESERVED.TK_NUMBER;
} 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 (int) RESERVED.TK_NAME;
}
} else{
int c = ls.current;
next(ls);
return c; /* single-char tokens (+ - / ...) */
}
}
}
}
}