lua

A copy of the Lua development repository
Log | Files | Refs | README

commit 52ee91dd73199e068d31d3ac138d933ddd4fb9b1
parent 191fd35f0a6cd47ea03417a663395acf5d0e9bf5
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date:   Thu, 22 Feb 2001 14:14:56 -0300

better encapsulation of some types

Diffstat:
Mlbaselib.c | 4++--
Mldo.c | 7++-----
Mliolib.c | 6+++---
Mllex.c | 6+++---
Mllimits.h | 12+++++++++++-
Mlobject.c | 4++--
Mlstring.c | 4++--
Mlstrlib.c | 72++++++++++++++++++++++++++++++++++++------------------------------------
Mlua.h | 19++++++++++---------
Mlualib.h | 13++++++++++++-
10 files changed, 83 insertions(+), 64 deletions(-)

diff --git 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 @@ -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 @@ -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 @@ -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; i<NUM_RESERVED; i++) { TString *ts = luaS_new(L, token2string[i]); lua_assert(strlen(token2string[i])+1 <= TOKEN_LEN); - ts->marked = (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 @@ -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 @@ -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 @@ -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 @@ -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; i<l; i++) - luaL_putchar(&b, tolower((unsigned char)(s[i]))); + luaL_putchar(&b, tolower(uchar(s[i]))); luaL_pushresult(&b); return 1; } @@ -68,7 +68,7 @@ static int str_upper (lua_State *L) { const char *s = luaL_check_lstr(L, 1, &l); luaL_buffinit(L, &b); for (i=0; i<l; i++) - luaL_putchar(&b, toupper((unsigned char)(s[i]))); + luaL_putchar(&b, toupper(uchar(s[i]))); luaL_pushresult(&b); return 1; } @@ -91,7 +91,7 @@ static int str_byte (lua_State *L) { const char *s = luaL_check_lstr(L, 1, &l); sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l); luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2, "out of range"); - lua_pushnumber(L, (unsigned char)s[pos-1]); + lua_pushnumber(L, uchar(s[pos-1])); return 1; } @@ -103,8 +103,8 @@ static int str_char (lua_State *L) { luaL_buffinit(L, &b); for (i=1; i<=n; i++) { int c = luaL_check_int(L, i); - luaL_arg_check(L, (unsigned char)c == c, i, "invalid value"); - luaL_putchar(&b, (unsigned char)c); + luaL_arg_check(L, uchar(c) == c, i, "invalid value"); + luaL_putchar(&b, uchar(c)); } luaL_pushresult(&b); return 1; @@ -177,26 +177,26 @@ static const char *luaI_classend (MatchState *ms, const char *p) { } -static int match_class (int c, int cl) { +static int match_class (char c, char cl) { int res; - switch (tolower(cl)) { - case 'a' : res = isalpha(c); break; - case 'c' : res = iscntrl(c); break; - case 'd' : res = isdigit(c); break; - case 'l' : res = islower(c); break; - case 'p' : res = ispunct(c); break; - case 's' : res = isspace(c); break; - case 'u' : res = isupper(c); break; - case 'w' : res = isalnum(c); break; - case 'x' : res = isxdigit(c); break; + switch (tolower(uchar(cl))) { + case 'a' : res = isalpha(uchar(c)); break; + case 'c' : res = iscntrl(uchar(c)); break; + case 'd' : res = isdigit(uchar(c)); break; + case 'l' : res = islower(uchar(c)); break; + case 'p' : res = ispunct(uchar(c)); break; + case 's' : res = isspace(uchar(c)); break; + case 'u' : res = isupper(uchar(c)); break; + case 'w' : res = isalnum(uchar(c)); break; + case 'x' : res = isxdigit(uchar(c)); break; case 'z' : res = (c == '\0'); break; default: return (cl == c); } - return (islower(cl) ? res : !res); + return (islower(uchar(cl)) ? res : !res); } -static int matchbracketclass (int c, const char *p, const char *endclass) { +static int matchbracketclass (char c, const char *p, const char *endclass) { int sig = 1; if (*(p+1) == '^') { sig = 0; @@ -205,30 +205,30 @@ static int matchbracketclass (int c, const char *p, const char *endclass) { while (++p < endclass) { if (*p == ESC) { p++; - if (match_class(c, (unsigned char)*p)) + if (match_class(c, *p)) return sig; } else if ((*(p+1) == '-') && (p+2 < endclass)) { p+=2; - if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p) + if (uchar(*(p-2)) <= uchar(c) && uchar(c) <= uchar(*p)) return sig; } - else if ((int)(unsigned char)*p == c) return sig; + else if (*p == c) return sig; } return !sig; } -static int luaI_singlematch (int c, const char *p, const char *ep) { +static int luaI_singlematch (char c, const char *p, const char *ep) { switch (*p) { case '.': /* matches any char */ return 1; case ESC: - return match_class(c, (unsigned char)*(p+1)); + return match_class(c, *(p+1)); case '[': return matchbracketclass(c, p, ep-1); default: - return ((unsigned char)*p == c); + return (*p == c); } } @@ -258,7 +258,7 @@ static const char *matchbalance (MatchState *ms, const char *s, const char *p) { static const char *max_expand (MatchState *ms, const char *s, const char *p, const char *ep) { sint32 i = 0; /* counts maximum expand for item */ - while ((s+i)<ms->src_end && luaI_singlematch((unsigned char)*(s+i), p, ep)) + while ((s+i)<ms->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 (s<ms->src_end && luaI_singlematch((unsigned char)*s, p, ep)) + else if (s<ms->src_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 = s<ms->src_end && luaI_singlematch((unsigned char)*s, p, ep); + int m = s<ms->src_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 @@ -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 @@ -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