From 80b3d28f4a9e518bf40b35f786199919180bfcd4 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 9 Dec 1997 11:50:08 -0200 Subject: [PATCH] details (mainly error messages) --- lapi.c | 6 +++--- lauxlib.c | 17 ++++++++++++++++- lauxlib.h | 4 +++- lbuiltin.c | 39 ++++++++++++--------------------------- ldo.c | 5 +++-- lfunc.c | 10 +++++----- lgc.c | 3 +-- liolib.c | 8 ++++---- llex.c | 8 ++++---- lmathlib.c | 25 ++++++------------------- lstring.c | 6 +++--- lstrlib.c | 8 ++++---- ltable.c | 4 ++-- lua.h | 8 +++++--- lua.stx | 24 +++++++++++++++--------- lvm.c | 6 +++--- 16 files changed, 89 insertions(+), 92 deletions(-) diff --git a/lapi.c b/lapi.c index b813d73d..63409950 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.10 1997/11/27 18:25:14 roberto Exp roberto $ +** $Id: lapi.c,v 1.11 1997/11/28 16:56:05 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -142,7 +142,7 @@ lua_Object lua_rawgettable (void) { checkCparams(2); if (ttype(L->stack.top-2) != LUA_T_ARRAY) - lua_error("indexed expression not a table in raw gettable"); + lua_error("indexed expression not a table in rawgettable"); else { TObject *h = luaH_get(avalue(L->stack.top-2), L->stack.top-1); --L->stack.top; @@ -490,7 +490,7 @@ char *lua_getobjname (lua_Object o, char **name) void lua_beginblock (void) { if (L->numCblocks >= MAX_C_BLOCKS) - lua_error("`lua_beginblock': too many nested blocks"); + lua_error("too many nested blocks"); L->Cblocks[L->numCblocks] = L->Cstack; L->numCblocks++; } diff --git a/lauxlib.c b/lauxlib.c index 85bb04ac..ed893f70 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.3 1997/11/04 15:27:53 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.4 1997/11/21 19:00:46 roberto Exp roberto $ ** Auxiliar functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -55,6 +55,21 @@ double luaL_opt_number (int numArg, double def) { return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : luaL_check_number(numArg); +} + + +lua_Object luaL_tablearg (int arg) +{ + lua_Object o = lua_getparam(arg); + luaL_arg_check(lua_istable(o), arg, "table expected"); + return o; +} + +lua_Object luaL_functionarg (int arg) +{ + lua_Object o = lua_getparam(arg); + luaL_arg_check(lua_isfunction(o), arg, "function expected"); + return o; } lua_Object luaL_nonnullarg (int numArg) diff --git a/lauxlib.h b/lauxlib.h index f1b59a3a..712c484c 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.2 1997/09/26 15:02:26 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.3 1997/11/21 19:00:46 roberto Exp roberto $ ** Auxiliar functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -23,6 +23,8 @@ char *luaL_check_string (int numArg); char *luaL_opt_string (int numArg, char *def); double luaL_check_number (int numArg); double luaL_opt_number (int numArg, double def); +lua_Object luaL_functionarg (int arg); +lua_Object luaL_tablearg (int arg); lua_Object luaL_nonnullarg (int numArg); void luaL_verror (char *fmt, ...); diff --git a/lbuiltin.c b/lbuiltin.c index 5d6274c3..e48e1d5d 100644 --- a/lbuiltin.c +++ b/lbuiltin.c @@ -1,5 +1,5 @@ /* -** $Id: lbuiltin.c,v 1.13 1997/11/28 12:39:45 roberto Exp roberto $ +** $Id: lbuiltin.c,v 1.14 1997/12/01 20:30:44 roberto Exp roberto $ ** Built-in functions ** See Copyright Notice in lua.h */ @@ -23,21 +23,6 @@ -static lua_Object tablearg (int arg) -{ - lua_Object o = lua_getparam(arg); - luaL_arg_check(lua_istable(o), arg, "table expected"); - return o; -} - -static lua_Object functionarg (int arg) -{ - lua_Object o = lua_getparam(arg); - luaL_arg_check(lua_isfunction(o), arg, "function expected"); - return o; -} - - static void pushstring (TaggedString *s) { TObject o; @@ -71,7 +56,7 @@ static void nextvar (void) static void foreachvar (void) { - TObject f = *luaA_Address(functionarg(1)); + TObject f = *luaA_Address(luaL_functionarg(1)); GCnode *g; StkId name = L->Cstack.base++; /* place to keep var name (to avoid GC) */ ttype(L->stack.stack+name) = LUA_T_NIL; @@ -95,7 +80,7 @@ static void foreachvar (void) static void next (void) { - lua_Object o = tablearg(1); + lua_Object o = luaL_tablearg(1); lua_Object r = luaL_nonnullarg(2); Node *n = luaH_next(luaA_Address(o), luaA_Address(r)); if (n) { @@ -107,8 +92,8 @@ static void next (void) static void foreach (void) { - TObject t = *luaA_Address(tablearg(1)); - TObject f = *luaA_Address(functionarg(2)); + TObject t = *luaA_Address(luaL_tablearg(1)); + TObject f = *luaA_Address(luaL_functionarg(2)); int i; for (i=0; inhash; i++) { Node *nd = &(avalue(&t)->node[i]); @@ -165,7 +150,9 @@ static char *to_string (lua_Object obj) } case LUA_T_NIL: return "nil"; - default: return ""; + default: + lua_error("internal error"); + return NULL; /* to avoid warnings */ } } @@ -203,9 +190,7 @@ static void lua_obj2number (void) static void luaI_error (void) { - char *s = lua_getstring(lua_getparam(1)); - if (s == NULL) s = "(no message)"; - lua_error(s); + lua_error(lua_getstring(lua_getparam(1))); } @@ -262,7 +247,7 @@ static int getnarg (lua_Object table) static void luaI_call (void) { lua_Object f = luaL_nonnullarg(1); - lua_Object arg = tablearg(2); + lua_Object arg = luaL_tablearg(2); char *options = luaL_opt_string(3, ""); lua_Object err = lua_getparam(4); int narg = getnarg(arg); @@ -302,7 +287,7 @@ static void luaI_call (void) static void settag (void) { - lua_Object o = tablearg(1); + lua_Object o = luaL_tablearg(1); lua_pushobject(o); lua_settag(luaL_check_number(2)); } @@ -354,7 +339,7 @@ static void gettagmethod (void) static void seterrormethod (void) { - lua_Object nf = functionarg(1); + lua_Object nf = luaL_functionarg(1); lua_pushobject(nf); lua_pushobject(lua_seterrormethod()); } diff --git a/ldo.c b/ldo.c index 4a37fe5d..f813922e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 1.12 1997/11/26 20:44:52 roberto Exp roberto $ +** $Id: ldo.c,v 1.13 1997/11/27 18:25:14 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -381,6 +381,7 @@ int lua_dofile (char *filename) #define SIZE_PREF 20 /* size of string prefix to appear in error messages */ +#define SSIZE_PREF "20" int lua_dostring (char *str) @@ -390,7 +391,7 @@ int lua_dostring (char *str) char *temp; ZIO z; if (str == NULL) return 1; - sprintf(buff, "(dostring) >> %.20s", str); + sprintf(buff, "(dostring) >> %." SSIZE_PREF "s", str); temp = strchr(buff, '\n'); if (temp) *temp = 0; /* end string after first line */ luaZ_sopen(&z, str); diff --git a/lfunc.c b/lfunc.c index 2ab76335..82f3407b 100644 --- a/lfunc.c +++ b/lfunc.c @@ -1,6 +1,6 @@ /* -** $Id: lfunc.c,v 1.5 1997/10/24 17:17:24 roberto Exp roberto $ -** Lua Funcion auxiliar +** $Id: lfunc.c,v 1.6 1997/11/19 17:29:23 roberto Exp roberto $ +** Auxiliar functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -11,8 +11,8 @@ #include "lmem.h" #include "lstate.h" -#define gcsizeproto(p) 5 -#define gcsizeclosure(c) 1 +#define gcsizeproto(p) 5 /* approximate "weight" for a prototype */ +#define gcsizeclosure(c) 1 /* approximate "weight" for a closure */ @@ -83,7 +83,7 @@ void luaF_freeclosure (Closure *l) /* -** Look for n-esim local variable at line "line" in function "func". +** Look for n-th local variable at line "line" in function "func". ** Returns NULL if not found. */ char *luaF_getlocalname (TProtoFunc *func, int local_number, int line) diff --git a/lgc.c b/lgc.c index 203e9d93..dac3b632 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.9 1997/11/27 15:59:25 roberto Exp roberto $ +** $Id: lgc.c,v 1.10 1997/12/01 20:31:25 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -268,7 +268,6 @@ long lua_collectgarbage (long limit) luaF_freeclosure(freeclos); luaM_clearbuffer(); recovered = recovered-L->nblocks; -/*printf("==total %ld coletados %ld\n", L->nblocks+recovered, recovered);*/ L->GCthreshold = (limit == 0) ? 2*L->nblocks : L->nblocks+limit; return recovered; } diff --git a/liolib.c b/liolib.c index 22668404..4befcfa6 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.7 1997/11/27 15:59:44 roberto Exp roberto $ +** $Id: liolib.c,v 1.8 1997/11/28 12:40:37 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -28,7 +28,7 @@ #define LC_MONETARY 0 #define LC_NUMERIC 0 #define LC_TIME 0 -#define strerror(e) "O.S. is unable to define the error" +#define strerror(e) "(no error message provided by operating system)" #endif @@ -72,7 +72,7 @@ static int ishandler (lua_Object f) { if (lua_isuserdata(f)) { if (lua_tag(f) == gettag(CLOSEDTAG)) - lua_error("trying to access a closed file"); + lua_error("cannot access a closed file"); return lua_tag(f) == gettag(IOTAG); } else return 0; @@ -82,7 +82,7 @@ static FILE *getfile (char *name) { lua_Object f = lua_getglobal(name); if (!ishandler(f)) - luaL_verror("global variable %.50s is not a file handle", name); + luaL_verror("global variable `%.50s' is not a file handle", name); return lua_getuserdata(f); } diff --git a/llex.c b/llex.c index 8f4553f9..9c8bcb3d 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 1.8 1997/11/21 19:00:46 roberto Exp roberto $ +** $Id: llex.c,v 1.9 1997/12/02 12:43:54 roberto Exp roberto $ ** Lexical Analizer ** See Copyright Notice in lua.h */ @@ -166,7 +166,7 @@ static void inclinenumber (LexState *LS) /* go through */ case 5: /* if */ if (LS->iflevel == MAX_IFS-1) - luaY_syntaxerror("too many nested `$ifs'", "$if"); + luaY_syntaxerror("too many nested $ifs", "$if"); readname(LS, buff); LS->iflevel++; LS->ifstate[LS->iflevel].elsepart = 0; @@ -181,7 +181,7 @@ static void inclinenumber (LexState *LS) LS->ifstate[LS->iflevel].condition; break; default: - luaY_syntaxerror("invalid pragma", buff); + luaY_syntaxerror("unknown pragma", buff); } skipspace(LS); if (LS->current == '\n') /* pragma must end with a '\n' ... */ @@ -414,7 +414,7 @@ int luaY_lex (YYSTYPE *l) case EOZ: save(LS, 0); if (LS->iflevel > 0) - luaY_error("missing $endif"); + luaY_syntaxerror("input ends inside a $if", ""); return 0; default: diff --git a/lmathlib.c b/lmathlib.c index cd28b1a1..13c74a0a 100644 --- a/lmathlib.c +++ b/lmathlib.c @@ -1,5 +1,5 @@ /* -** $Id: lmathlib.c,v 1.5 1997/11/19 18:16:33 roberto Exp roberto $ +** $Id: lmathlib.c,v 1.6 1997/11/28 12:39:22 roberto Exp roberto $ ** Lua standard mathematical library ** See Copyright Notice in lua.h */ @@ -12,28 +12,15 @@ #include "lua.h" #include "lualib.h" -#ifndef PI +#ifdef M_PI +#define PI M_PI +#else #define PI ((double)3.14159265358979323846) #endif - -#define FROMRAD(a) ((a)/torad()) -#define TORAD(a) ((a)*torad()) - - -static double torad (void) -{ - char *s = luaL_opt_string(2, "d"); - switch (*s) { - case 'd' : return PI/180.0; - case 'r' : return (double)1.0; - case 'g' : return PI/50.0; - default: - luaL_arg_check(0, 2, "invalid mode"); - return 0; /* to avoid warnings */ - } -} +#define FROMRAD(a) ((a)*(180.0/PI)) +#define TORAD(a) ((a)*(PI/180.0)) static void math_abs (void) diff --git a/lstring.c b/lstring.c index 6f3ba519..b750acfd 100644 --- a/lstring.c +++ b/lstring.c @@ -1,6 +1,6 @@ /* -** $Id: lstring.c,v 1.6 1997/11/21 19:00:46 roberto Exp roberto $ -** String table (keep all strings handled by Lua) +** $Id: lstring.c,v 1.7 1997/12/01 20:31:25 roberto Exp roberto $ +** String table (keeps all strings handled by Lua) ** See Copyright Notice in lua.h */ @@ -17,7 +17,7 @@ #define NUM_HASHS 61 -#define gcsizestring(l) (1+(l/64)) +#define gcsizestring(l) (1+(l/64)) /* "weight" for a string with length 'l' */ diff --git a/lstrlib.c b/lstrlib.c index cdd2ffe5..e5b87598 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.2 1997/11/26 18:53:45 roberto Exp roberto $ ** Standard library for strings and pattern-matching ** See Copyright Notice in lua.h */ @@ -283,7 +283,7 @@ static char *matchitem (char *s, char *p, int level, char **ep) else if (*p == 'b') { /* balanced string */ p++; if (*p == 0 || *(p+1) == 0) - lua_error("bad balanced pattern specification"); + lua_error("unbalanced pattern"); *ep = p+2; return matchbalance(s, *p, *(p+1)); } @@ -484,7 +484,7 @@ static void str_format (void) arg++; strncpy(form+1, initf, strfrmt-initf+1); /* +1 to include convertion */ form[strfrmt-initf+2] = 0; - buff = openspace(1000); /* to store the formated value */ + buff = openspace(1000); /* to store the formatted value */ switch (*strfrmt++) { case 'q': luaI_addquoted(luaL_check_string(arg)); @@ -503,7 +503,7 @@ static void str_format (void) sprintf(buff, form, luaL_check_number(arg)); break; default: /* also treat cases 'pnLlh' */ - lua_error("invalid format option in function `format'"); + lua_error("invalid option in `format'"); } lbuffer.size += strlen(buff); } diff --git a/ltable.c b/ltable.c index c5c1f714..270d1fa3 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.6 1997/11/19 17:29:23 roberto Exp roberto $ +** $Id: ltable.c,v 1.7 1997/11/21 19:00:46 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -44,7 +44,7 @@ static long int hashindex (TObject *ref) break; default: lua_error("unexpected type to index table"); - h = 0; /* UNREACHEABLE */ + h = 0; /* to avoid warnings */ } return (h >= 0 ? h : -(h+1)); } diff --git a/lua.h b/lua.h index 3cc871dc..a83826c9 100644 --- a/lua.h +++ b/lua.h @@ -1,13 +1,15 @@ /* -** $Id: lua.h,v 1.7 1997/11/27 18:25:14 roberto Exp roberto $ +** $Id: lua.h,v 1.8 1997/12/01 20:31:25 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 +** www: http://www.tecgraf.puc-rio.br/lua/ */ /********************************************************************* -* Copyright © 1994-1996 TeCGraf, PUC-Rio. Written by Waldemar Ce­ -* les Filho, Roberto Ierusalimschy and Luiz Henrique de Figueiredo. +* Copyright © 1994-1996 TeCGraf, PUC-Rio. +* Written by Waldemar Celes Filho, Roberto Ierusalimschy and +* Luiz Henrique de Figueiredo. * All rights reserved. * * Permission is hereby granted, without written agreement and with­ diff --git a/lua.stx b/lua.stx index b1afe6b4..0b794fee 100644 --- a/lua.stx +++ b/lua.stx @@ -1,6 +1,6 @@ %{ /* -** $Id: lua.stx,v 1.19 1997/11/21 19:00:46 roberto Exp roberto $ +** $Id: lua.stx,v 1.20 1997/12/02 12:43:54 roberto Exp roberto $ ** Syntax analizer and code generator ** See Copyright Notice in lua.h */ @@ -26,6 +26,10 @@ int luaY_parse (void); +#define AMES_LIM(x) #x +#define MES_LIM(x) "(limit=" AMES_LIM(x) ")" + + /* size of a "normal" jump instruction: OpCode + 1 byte */ #define JMPSIZE 2 @@ -43,6 +47,8 @@ int luaY_parse (void); /* maximum number of upvalues */ #define MAXUPVALUES 16 + + /* ** Variable descriptor: ** if 0currState->stacksize += delta; if (L->currState->stacksize > L->currState->maxstacksize) { if (L->currState->stacksize > 255) - luaY_error("function/expression too complex (limit 256)"); + luaY_error("function/expression too complex"); L->currState->maxstacksize = L->currState->stacksize; } } @@ -156,7 +162,7 @@ static int code_oparg_at (int pc, OpCode op, int builtin, int arg, int delta) L->currState->f->code[pc+2] = arg>>8; return 3; } - else luaY_error("code too long (limit 64K)"); + else luaY_error("code too long " MES_LIM(64K)); return 0; /* to avoid warnings */ } @@ -314,7 +320,7 @@ static void store_localvar (TaggedString *name, int n) if (L->currState->nlocalvar+n < MAXLOCALS) L->currState->localvar[L->currState->nlocalvar+n] = name; else - luaY_error("too many local variables (limit 32)"); + luaY_error("too many local variables " MES_LIM(MAXLOCALS)); luaI_registerlocalvar(name, L->lexstate->linenumber); } @@ -341,7 +347,7 @@ static vardesc var2store (vardesc var) static void add_varbuffer (vardesc var, int n) { if (n >= MAXVAR) - luaY_error("variable buffer overflow (limit 32)"); + luaY_error("variable buffer overflow " MES_LIM(MAXVAR)); L->currState->varbuffer[n] = var2store(var); } @@ -379,7 +385,7 @@ static int indexupvalue (TaggedString *n) } /* new one */ if (++(L->currState->nupvalues) > MAXUPVALUES) - luaY_error("too many upvalues in a single function (limit 16)"); + luaY_error("too many upvalues in a single function " MES_LIM(MAXUPVALUES)); L->currState->upvalues[i] = v; /* i = L->currState->nupvalues - 1 */ return i; } @@ -493,7 +499,7 @@ static int lua_codestore (int i, int left) } else { /* indexed var with values in between*/ code_oparg(SETTABLE, 0, left+i, -1); - return left+2; /* table/index are not poped, since they are not on top */ + return left+2; /* table/index are not popped, since they are not on top */ } } @@ -580,7 +586,7 @@ static void init_state (TaggedString *filename) static void init_func (void) { if (L->currState-L->mainState >= MAXSTATES-1) - luaY_error("too many nested functions (limit 6)"); + luaY_error("too many nested functions " MES_LIM(MAXSTATES)); L->currState++; init_state(L->mainState->f->fileName); luaY_codedebugline(L->lexstate->linenumber); @@ -604,7 +610,7 @@ static TProtoFunc *close_func (void) /* -** Parse LUA code. +** Parse Lua code. */ TProtoFunc *luaY_parser (ZIO *z, char *chunkname) { diff --git a/lvm.c b/lvm.c index 0b6f68fa..bb3d9d8c 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.14 1997/11/19 17:29:23 roberto Exp roberto $ +** $Id: lvm.c,v 1.15 1997/11/21 19:00:46 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -213,7 +213,7 @@ static void call_binTM (IMS event, char *msg) static void call_arith (IMS event) { - call_binTM(event, "unexpected type at arithmetic operation"); + call_binTM(event, "unexpected type in arithmetic operation"); } @@ -229,7 +229,7 @@ static void comparison (lua_Type ttype_less, lua_Type ttype_equal, else if (ttype(l) == LUA_T_STRING && ttype(r) == LUA_T_STRING) result = strcoll(svalue(l), svalue(r)); else { - call_binTM(op, "unexpected type at comparison"); + call_binTM(op, "unexpected type in comparison"); return; } S->top--;