From 52ee91dd73199e068d31d3ac138d933ddd4fb9b1 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 22 Feb 2001 14:15:18 -0300 Subject: [PATCH] better encapsulation of some types --- lbaselib.c | 4 +-- ldo.c | 7 ++---- liolib.c | 6 ++--- llex.c | 6 ++--- llimits.h | 12 ++++++++- lobject.c | 4 +-- lstring.c | 4 +-- lstrlib.c | 72 +++++++++++++++++++++++++++--------------------------- lua.h | 19 +++++++------- lualib.h | 13 +++++++++- 10 files changed, 83 insertions(+), 64 deletions(-) diff --git a/lbaselib.c b/lbaselib.c index cbaf3b4e..44dc38e5 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.23 2001/02/09 19:52:24 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.24 2001/02/20 18:29:54 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -100,7 +100,7 @@ static int luaB_tonumber (lua_State *L) { luaL_arg_check(L, 2 <= base && base <= 36, 2, "base out of range"); n = strtoul(s1, &s2, base); if (s1 != s2) { /* at least one valid digit? */ - while (isspace((unsigned char)*s2)) s2++; /* skip trailing spaces */ + while (isspace(uchar(*s2))) s2++; /* skip trailing spaces */ if (*s2 == '\0') { /* no invalid trailing characters? */ lua_pushnumber(L, n); return 1; diff --git a/ldo.c b/ldo.c index 36304fb7..7ab130af 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 1.123 2001/02/07 18:13:49 roberto Exp roberto $ +** $Id: ldo.c,v 1.124 2001/02/20 18:15:33 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -265,12 +265,9 @@ static int parse_file (lua_State *L, const char *filename) { ZIO z; int status; int bin; /* flag for file mode */ - int c; /* look ahead char */ FILE *f = (filename == NULL) ? stdin : fopen(filename, "r"); if (f == NULL) return LUA_ERRFILE; /* unable to open file */ - c = fgetc(f); - ungetc(c, f); - bin = (c == ID_CHUNK); + bin = (ungetc(fgetc(f), f) == ID_CHUNK); if (bin && f != stdin) { fclose(f); f = fopen(filename, "rb"); /* reopen in binary mode */ diff --git a/liolib.c b/liolib.c index 68b0abec..f9aa2716 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.105 2001/02/09 16:25:50 roberto Exp roberto $ +** $Id: liolib.c,v 1.106 2001/02/09 19:52:54 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -214,7 +214,7 @@ static int read_number (lua_State *L, FILE *f) { static int read_word (lua_State *L, FILE *f) { - int c; + l_charint c; luaL_Buffer b; luaL_buffinit(L, &b); do { c = fgetc(f); } while (isspace(c)); /* skip spaces */ @@ -273,7 +273,7 @@ static void read_file (lua_State *L, FILE *f) { static int read_chars (lua_State *L, FILE *f, size_t n) { if (n == 0) { /* test eof? */ - int c = fgetc(f); + l_charint c = fgetc(f); ungetc(c, f); lua_pushlstring(L, NULL, 0); return (c != EOF); diff --git a/llex.c b/llex.c index 8d72cb5e..5ed43493 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 1.76 2001/01/19 13:20:30 roberto Exp roberto $ +** $Id: llex.c,v 1.77 2001/02/09 20:22:29 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -36,7 +36,7 @@ void luaX_init (lua_State *L) { for (i=0; imarked = (unsigned char)(RESERVEDMARK+i); /* reserved word */ + ts->marked = RESERVEDMARK+i; /* reserved word */ } } @@ -255,7 +255,7 @@ static void read_string (LexState *LS, int del, SemInfo *seminfo) { c = 10*c + (LS->current-'0'); next(LS); } while (++i<3 && isdigit(LS->current)); - if (c != (unsigned char)c) { + if (c > UCHAR_MAX) { save(L, '\0', l); luaX_error(LS, "escape sequence too large", TK_STRING); } diff --git a/llimits.h b/llimits.h index 1fbfd1d9..2b0556a1 100644 --- a/llimits.h +++ b/llimits.h @@ -1,5 +1,5 @@ /* -** $Id: llimits.h,v 1.22 2001/02/09 16:24:44 roberto Exp roberto $ +** $Id: llimits.h,v 1.23 2001/02/20 18:15:33 roberto Exp roberto $ ** Limits, basic types, and some other "installation-dependent" definitions ** See Copyright Notice in lua.h */ @@ -32,11 +32,17 @@ /* function to convert a lua_Number to a string */ +#ifndef NUMBER_FMT #define NUMBER_FMT "%.16g" /* LUA_NUMBER */ +#endif +#ifndef lua_number2str #define lua_number2str(s,n) sprintf((s), NUMBER_FMT, (n)) +#endif /* function to convert a string to a lua_Number */ +#ifndef lua_str2number #define lua_str2number(s,p) strtod((s), (p)) +#endif @@ -63,6 +69,10 @@ typedef long ls_nstr; typedef unsigned char lu_byte; +/* macro to `unsign' a character */ +#define uchar(c) ((unsigned char)(c)) + + #define MAX_SIZET ((size_t)(~(size_t)0)-2) diff --git a/lobject.c b/lobject.c index bd1fa56a..9292f053 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 1.64 2001/02/02 15:13:05 roberto Exp roberto $ +** $Id: lobject.c,v 1.65 2001/02/20 18:15:33 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -48,7 +48,7 @@ int luaO_str2d (const char *s, lua_Number *result) { /* LUA_NUMBER */ char *endptr; lua_Number res = lua_str2number(s, &endptr); if (endptr == s) return 0; /* no conversion */ - while (isspace((unsigned char)*endptr)) endptr++; + while (isspace(uchar(*endptr))) endptr++; if (*endptr != '\0') return 0; /* invalid trailing characters? */ *result = res; return 1; diff --git a/lstring.c b/lstring.c index f7b38376..660e9db5 100644 --- a/lstring.c +++ b/lstring.c @@ -1,5 +1,5 @@ /* -** $Id: lstring.c,v 1.58 2001/02/09 20:29:33 roberto Exp roberto $ +** $Id: lstring.c,v 1.59 2001/02/20 18:15:33 roberto Exp roberto $ ** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -69,7 +69,7 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { size_t step = (l>>5)+1; /* if string is too long, don't hash all its chars */ size_t l1; for (l1=l; l1>=step; l1-=step) /* compute hash */ - h = h ^ ((h<<5)+(h>>2)+(unsigned char)str[l1-1]); + h = h ^ ((h<<5)+(h>>2)+uchar(str[l1-1])); for (ts = G(L)->strt.hash[lmod(h, G(L)->strt.size)]; ts; ts = ts->nexthash) { if (ts->len == l && (memcmp(str, getstr(ts), l) == 0)) return ts; diff --git a/lstrlib.c b/lstrlib.c index 42e4e9d3..07ec43ad 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.61 2001/01/10 16:58:11 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.62 2001/02/02 19:02:40 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -55,7 +55,7 @@ static int str_lower (lua_State *L) { const char *s = luaL_check_lstr(L, 1, &l); luaL_buffinit(L, &b); for (i=0; isrc_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) + while ((s+i)src_end && luaI_singlematch(*(s+i), p, ep)) i++; /* keeps trying to match with the maximum repetitions */ while (i>=0) { @@ -276,7 +276,7 @@ static const char *min_expand (MatchState *ms, const char *s, const char *p, const char *res = match(ms, s, ep+1); if (res != NULL) return res; - else if (ssrc_end && luaI_singlematch((unsigned char)*s, p, ep)) + else if (ssrc_end && luaI_singlematch(*s, p, ep)) s++; /* try with one more repetition */ else return NULL; } @@ -328,7 +328,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { case ')': /* end capture */ return end_capture(ms, s, p+1); case ESC: /* may be %[0-9] or %b */ - if (isdigit((unsigned char)(*(p+1)))) { /* capture? */ + if (isdigit(uchar(*(p+1)))) { /* capture? */ s = match_capture(ms, s, *(p+1)); if (s == NULL) return NULL; p+=2; goto init; /* else return match(ms, s, p+2) */ @@ -347,7 +347,7 @@ static const char *match (MatchState *ms, const char *s, const char *p) { else goto dflt; default: dflt: { /* it is a pattern item */ const char *ep = luaI_classend(ms, p); /* points to what is next */ - int m = ssrc_end && luaI_singlematch((unsigned char)*s, p, ep); + int m = ssrc_end && luaI_singlematch(*s, p, ep); switch (*ep) { case '?': { /* optional */ const char *res; @@ -460,7 +460,7 @@ static void add_s (MatchState *ms, luaL_Buffer *b) { luaL_putchar(b, news[i]); else { i++; /* skip ESC */ - if (!isdigit((unsigned char)news[i])) + if (!isdigit(uchar(news[i]))) luaL_putchar(b, news[i]); else { int level = check_capture(ms, news[i]); @@ -552,15 +552,15 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form, int *hasprecision) { const char *p = strfrmt; while (strchr("-+ #0", *p)) p++; /* skip flags */ - if (isdigit((unsigned char)*p)) p++; /* skip width */ - if (isdigit((unsigned char)*p)) p++; /* (2 digits at most) */ + if (isdigit(uchar(*p))) p++; /* skip width */ + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ if (*p == '.') { p++; *hasprecision = 1; - if (isdigit((unsigned char)*p)) p++; /* skip precision */ - if (isdigit((unsigned char)*p)) p++; /* (2 digits at most) */ + if (isdigit(uchar(*p))) p++; /* skip precision */ + if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ } - if (isdigit((unsigned char)*p)) + if (isdigit(uchar(*p))) lua_error(L, "invalid format (width or precision too long)"); if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */ lua_error(L, "invalid format (too long)"); @@ -585,7 +585,7 @@ static int str_format (lua_State *L) { char form[MAX_FORMAT]; /* to store the format (`%...') */ char buff[MAX_ITEM]; /* to store the formatted item */ int hasprecision = 0; - if (isdigit((unsigned char)*strfrmt) && *(strfrmt+1) == '$') + if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$') lua_error(L, "obsolete `format' option (d$)"); arg++; strfrmt = scanformat(L, strfrmt, form, &hasprecision); diff --git a/lua.h b/lua.h index 08e1a3e5..09221693 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.86 2001/02/09 19:53:16 roberto Exp roberto $ +** $Id: lua.h,v 1.87 2001/02/20 18:15:33 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: lua@tecgraf.puc-rio.br @@ -39,10 +39,6 @@ #define LUA_MULTRET (-1) -/* minimum stack available for a C function */ -#define LUA_MINSTACK 20 - - /* error codes for lua_do* */ #define LUA_ERRRUN 1 #define LUA_ERRFILE 2 @@ -50,10 +46,6 @@ #define LUA_ERRMEM 4 #define LUA_ERRERR 5 - -/* Lua numerical type */ -typedef double lua_Number; - typedef struct lua_State lua_State; typedef int (*lua_CFunction) (lua_State *L); @@ -71,6 +63,7 @@ typedef int (*lua_CFunction) (lua_State *L); #define LUA_TFUNCTION 5 + /* ** generic extra include file */ @@ -79,6 +72,14 @@ typedef int (*lua_CFunction) (lua_State *L); #endif +/* minimum stack available for a C function */ +#define LUA_MINSTACK 20 + + +/* Lua numerical type */ +typedef double lua_Number; + + /* mark for all API functions */ #ifndef LUA_API diff --git a/lualib.h b/lualib.h index 9dfcee5a..aea9ee08 100644 --- a/lualib.h +++ b/lualib.h @@ -1,5 +1,5 @@ /* -** $Id: lualib.h,v 1.14 2000/10/27 16:15:53 roberto Exp roberto $ +** $Id: lualib.h,v 1.15 2000/11/23 13:49:35 roberto Exp roberto $ ** Lua standard libraries ** See Copyright Notice in lua.h */ @@ -25,4 +25,15 @@ LUALIB_API void lua_mathlibopen (lua_State *L); LUALIB_API void lua_dblibopen (lua_State *L); + +/* +** `private' part +*/ + +/* macro to `unsign' a character */ +#define uchar(c) ((unsigned char)(c)) + +/* integer type to hold the result of fgetc */ +typedef int l_charint; + #endif