no need of lookahead in Zio

This commit is contained in:
Roberto Ierusalimschy 2011-02-23 10:13:10 -03:00
parent 03b769053a
commit 7482e8f914
8 changed files with 29 additions and 41 deletions

6
ldo.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 2.91 2011/02/04 17:34:43 roberto Exp roberto $ ** $Id: ldo.c,v 2.92 2011/02/07 17:14:50 roberto Exp roberto $
** Stack and Call structure of Lua ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -625,10 +625,10 @@ static void f_parser (lua_State *L, void *ud) {
Proto *tf; Proto *tf;
Closure *cl; Closure *cl;
struct SParser *p = cast(struct SParser *, ud); struct SParser *p = cast(struct SParser *, ud);
int c = luaZ_lookahead(p->z); int c = zgetc(p->z); /* read first character */
tf = (c == LUA_SIGNATURE[0]) tf = (c == LUA_SIGNATURE[0])
? luaU_undump(L, p->z, &p->buff, p->name) ? luaU_undump(L, p->z, &p->buff, p->name)
: luaY_parser(L, p->z, &p->buff, &p->dyd, p->name); : luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
setptvalue2s(L, L->top, tf); setptvalue2s(L, L->top, tf);
incr_top(L); incr_top(L);
cl = luaF_newLclosure(L, tf); cl = luaF_newLclosure(L, tf);

7
llex.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.c,v 2.44 2011/01/26 16:30:02 roberto Exp roberto $ ** $Id: llex.c,v 2.45 2011/02/02 14:55:17 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -152,9 +152,11 @@ static void inclinenumber (LexState *ls) {
} }
void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
int firstchar) {
ls->decpoint = '.'; ls->decpoint = '.';
ls->L = L; ls->L = L;
ls->current = firstchar;
ls->lookahead.token = TK_EOS; /* no look-ahead token */ ls->lookahead.token = TK_EOS; /* no look-ahead token */
ls->z = z; ls->z = z;
ls->fs = NULL; ls->fs = NULL;
@ -164,7 +166,6 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
ls->envn = luaS_new(L, LUA_ENV); /* create env name */ ls->envn = luaS_new(L, LUA_ENV); /* create env name */
luaS_fix(ls->envn); /* never collect this name */ luaS_fix(ls->envn); /* never collect this name */
luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
next(ls); /* read first char */
} }

4
llex.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: llex.h,v 1.67 2011/02/04 17:34:43 roberto Exp roberto $ ** $Id: llex.h,v 1.68 2011/02/07 17:14:50 roberto Exp roberto $
** Lexical Analyzer ** Lexical Analyzer
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -69,7 +69,7 @@ typedef struct LexState {
LUAI_FUNC void luaX_init (lua_State *L); LUAI_FUNC void luaX_init (lua_State *L);
LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
TString *source); TString *source, int firstchar);
LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l); LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
LUAI_FUNC void luaX_next (LexState *ls); LUAI_FUNC void luaX_next (LexState *ls);
LUAI_FUNC int luaX_lookahead (LexState *ls); LUAI_FUNC int luaX_lookahead (LexState *ls);

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.c,v 2.105 2011/02/14 14:59:28 roberto Exp roberto $ ** $Id: lparser.c,v 2.106 2011/02/14 16:36:34 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -1568,7 +1568,7 @@ static void statement (LexState *ls) {
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name) { Dyndata *dyd, const char *name, int firstchar) {
LexState lexstate; LexState lexstate;
FuncState funcstate; FuncState funcstate;
BlockCnt bl; BlockCnt bl;
@ -1578,7 +1578,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
lexstate.buff = buff; lexstate.buff = buff;
lexstate.dyd = dyd; lexstate.dyd = dyd;
dyd->actvar.n = dyd->gt.n = dyd->label.n = 0; dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
luaX_setinput(L, &lexstate, z, tname); luaX_setinput(L, &lexstate, z, tname, firstchar);
open_mainfunc(&lexstate, &funcstate, &bl); open_mainfunc(&lexstate, &funcstate, &bl);
luaX_next(&lexstate); /* read first token */ luaX_next(&lexstate); /* read first token */
statlist(&lexstate); /* main body */ statlist(&lexstate); /* main body */

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lparser.h,v 1.66 2011/02/04 17:34:43 roberto Exp roberto $ ** $Id: lparser.h,v 1.67 2011/02/07 17:14:50 roberto Exp roberto $
** Lua Parser ** Lua Parser
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -114,7 +114,7 @@ typedef struct FuncState {
LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
Dyndata *dyd, const char *name); Dyndata *dyd, const char *name, int firstchar);
#endif #endif

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 2.14 2010/10/25 14:33:38 roberto Exp roberto $ ** $Id: lundump.c,v 2.15 2011/02/07 19:15:24 roberto Exp roberto $
** load precompiled Lua chunks ** load precompiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -180,8 +180,8 @@ static void LoadHeader(LoadState* S)
char h[LUAC_HEADERSIZE]; char h[LUAC_HEADERSIZE];
char s[LUAC_HEADERSIZE]; char s[LUAC_HEADERSIZE];
luaU_header(h); luaU_header(h);
LoadBlock(S,s,LUAC_HEADERSIZE); LoadBlock(S,s,LUAC_HEADERSIZE-1); /* 1st char already read */
if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible"); if (memcmp(h+1,s,LUAC_HEADERSIZE-1)!=0) error(S,"incompatible");
} }
/* /*

33
lzio.c
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.c,v 1.31 2005/06/03 20:15:29 roberto Exp roberto $ ** $Id: lzio.c,v 1.32 2011/02/17 17:34:16 roberto Exp roberto $
** a generic input stream interface ** a generic input stream interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -22,40 +22,23 @@ int luaZ_fill (ZIO *z) {
size_t size; size_t size;
lua_State *L = z->L; lua_State *L = z->L;
const char *buff; const char *buff;
if (z->eoz) return EOZ;
lua_unlock(L); lua_unlock(L);
buff = z->reader(L, z->data, &size); buff = z->reader(L, z->data, &size);
lua_lock(L); lua_lock(L);
if (buff == NULL || size == 0) { if (buff == NULL || size == 0)
z->eoz = 1; /* avoid calling reader function next time */
return EOZ; return EOZ;
} z->n = size - 1; /* discount char being returned */
z->n = size - 1;
z->p = buff; z->p = buff;
return char2int(*(z->p++)); return char2int(*(z->p++));
} }
int luaZ_lookahead (ZIO *z) {
if (z->n == 0) {
if (luaZ_fill(z) == EOZ)
return EOZ;
else {
z->n++; /* luaZ_fill removed first byte; put back it */
z->p--;
}
}
return char2int(*z->p);
}
void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
z->L = L; z->L = L;
z->reader = reader; z->reader = reader;
z->data = data; z->data = data;
z->n = 0; z->n = 0;
z->p = NULL; z->p = NULL;
z->eoz = 0;
} }
@ -63,8 +46,14 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
size_t luaZ_read (ZIO *z, void *b, size_t n) { size_t luaZ_read (ZIO *z, void *b, size_t n) {
while (n) { while (n) {
size_t m; size_t m;
if (luaZ_lookahead(z) == EOZ) if (z->n == 0) { /* no bytes in buffer? */
return n; /* return number of missing bytes */ if (luaZ_fill(z) == EOZ) /* try to read more */
return n; /* no more input; return number of missing bytes */
else {
z->n++; /* luaZ_fill consumed first byte; put it back */
z->p--;
}
}
m = (n <= z->n) ? n : z->n; /* min. between n and z->n */ m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
memcpy(b, z->p, m); memcpy(b, z->p, m);
z->n -= m; z->n -= m;

4
lzio.h
View File

@ -1,5 +1,5 @@
/* /*
** $Id: lzio.h,v 1.22 2009/05/18 17:26:25 roberto Exp roberto $ ** $Id: lzio.h,v 1.23 2011/02/17 17:34:16 roberto Exp roberto $
** Buffered streams ** Buffered streams
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -50,7 +50,6 @@ LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n);
LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
void *data); void *data);
LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */ LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
LUAI_FUNC int luaZ_lookahead (ZIO *z);
@ -62,7 +61,6 @@ struct Zio {
lua_Reader reader; /* reader function */ lua_Reader reader; /* reader function */
void* data; /* additional data */ void* data; /* additional data */
lua_State *L; /* Lua state (for reader) */ lua_State *L; /* Lua state (for reader) */
int eoz; /* true if reader has no more data */
}; };