diff --git a/ldo.c b/ldo.c index 1b4768b6..7c59e663 100644 --- a/ldo.c +++ b/ldo.c @@ -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 ** See Copyright Notice in lua.h */ @@ -625,10 +625,10 @@ static void f_parser (lua_State *L, void *ud) { Proto *tf; Closure *cl; 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]) ? 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); incr_top(L); cl = luaF_newLclosure(L, tf); diff --git a/llex.c b/llex.c index 68ccfc82..4e0b7a58 100644 --- a/llex.c +++ b/llex.c @@ -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 ** 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->L = L; + ls->current = firstchar; ls->lookahead.token = TK_EOS; /* no look-ahead token */ ls->z = z; 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 */ luaS_fix(ls->envn); /* never collect this name */ luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */ - next(ls); /* read first char */ } diff --git a/llex.h b/llex.h index 3c45f845..01a40317 100644 --- a/llex.h +++ b/llex.h @@ -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 ** 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_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 void luaX_next (LexState *ls); LUAI_FUNC int luaX_lookahead (LexState *ls); diff --git a/lparser.c b/lparser.c index 56cd417a..41ba802d 100644 --- a/lparser.c +++ b/lparser.c @@ -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 ** 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, - Dyndata *dyd, const char *name) { + Dyndata *dyd, const char *name, int firstchar) { LexState lexstate; FuncState funcstate; BlockCnt bl; @@ -1578,7 +1578,7 @@ Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, lexstate.buff = buff; lexstate.dyd = dyd; 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); luaX_next(&lexstate); /* read first token */ statlist(&lexstate); /* main body */ diff --git a/lparser.h b/lparser.h index 5626d100..de37e125 100644 --- a/lparser.h +++ b/lparser.h @@ -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 ** 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, - Dyndata *dyd, const char *name); + Dyndata *dyd, const char *name, int firstchar); #endif diff --git a/lundump.c b/lundump.c index 161ef1d4..170c9ece 100644 --- a/lundump.c +++ b/lundump.c @@ -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 ** See Copyright Notice in lua.h */ @@ -180,8 +180,8 @@ static void LoadHeader(LoadState* S) char h[LUAC_HEADERSIZE]; char s[LUAC_HEADERSIZE]; luaU_header(h); - LoadBlock(S,s,LUAC_HEADERSIZE); - if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible"); + LoadBlock(S,s,LUAC_HEADERSIZE-1); /* 1st char already read */ + if (memcmp(h+1,s,LUAC_HEADERSIZE-1)!=0) error(S,"incompatible"); } /* diff --git a/lzio.c b/lzio.c index 24826f68..a2720333 100644 --- a/lzio.c +++ b/lzio.c @@ -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 ** See Copyright Notice in lua.h */ @@ -22,40 +22,23 @@ int luaZ_fill (ZIO *z) { size_t size; lua_State *L = z->L; const char *buff; - if (z->eoz) return EOZ; lua_unlock(L); buff = z->reader(L, z->data, &size); lua_lock(L); - if (buff == NULL || size == 0) { - z->eoz = 1; /* avoid calling reader function next time */ + if (buff == NULL || size == 0) return EOZ; - } - z->n = size - 1; + z->n = size - 1; /* discount char being returned */ z->p = buff; 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) { z->L = L; z->reader = reader; z->data = data; z->n = 0; 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) { while (n) { size_t m; - if (luaZ_lookahead(z) == EOZ) - return n; /* return number of missing bytes */ + if (z->n == 0) { /* no bytes in buffer? */ + 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 */ memcpy(b, z->p, m); z->n -= m; diff --git a/lzio.h b/lzio.h index 36a00900..2c666ab3 100644 --- a/lzio.h +++ b/lzio.h @@ -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 ** 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, void *data); 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 */ void* data; /* additional data */ lua_State *L; /* Lua state (for reader) */ - int eoz; /* true if reader has no more data */ };