error functions are shared by all libraries

This commit is contained in:
Roberto Ierusalimschy 1995-11-10 15:54:31 -02:00
parent 87fe07c0d4
commit 5f664a4516
3 changed files with 52 additions and 62 deletions

View File

@ -2,7 +2,7 @@
** Libraries to be used in LUA programs
** Grupo de Tecnologia em Computacao Grafica
** TeCGraf - PUC-Rio
** $Id: lualib.h,v 1.2 1994/08/24 15:29:02 celes Stab roberto $
** $Id: lualib.h,v 1.3 1994/12/13 15:59:16 roberto Exp roberto $
*/
#ifndef lualib_h
@ -12,5 +12,11 @@ void iolib_open (void);
void strlib_open (void);
void mathlib_open (void);
/* auxiliar functions (private) */
void lua_arg_error(char *funcname);
char *lua_check_string (int numArg, char *funcname);
float lua_check_number (int numArg, char *funcname);
#endif

View File

@ -3,7 +3,7 @@
** Mathematics library to LUA
*/
char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $";
char *rcs_mathlib="$Id: mathlib.c,v 1.12 1995/10/09 12:48:38 roberto Exp roberto $";
#include <stdio.h> /* NULL */
#include <math.h>
@ -17,25 +17,9 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $";
#define TODEGREE(a) ((a)*180.0/PI)
#define TORAD(a) ((a)*PI/180.0)
static void str_error(char *funcname)
{
char buff[250];
sprintf(buff, "incorrect arguments to function `%s'", funcname);
lua_error(buff);
}
static float get_and_check_float (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
if (!lua_isnumber(o))
str_error(funcname);
return lua_getnumber(o);
}
static void math_abs (void)
{
double d = get_and_check_float(1, "abs");
double d = lua_check_number(1, "abs");
if (d < 0) d = -d;
lua_pushnumber (d);
}
@ -43,7 +27,7 @@ static void math_abs (void)
static void math_sin (void)
{
double d = get_and_check_float(1, "sin");
double d = lua_check_number(1, "sin");
lua_pushnumber (sin(TORAD(d)));
}
@ -51,7 +35,7 @@ static void math_sin (void)
static void math_cos (void)
{
double d = get_and_check_float(1, "cos");
double d = lua_check_number(1, "cos");
lua_pushnumber (cos(TORAD(d)));
}
@ -59,64 +43,64 @@ static void math_cos (void)
static void math_tan (void)
{
double d = get_and_check_float(1, "tan");
double d = lua_check_number(1, "tan");
lua_pushnumber (tan(TORAD(d)));
}
static void math_asin (void)
{
double d = get_and_check_float(1, "asin");
double d = lua_check_number(1, "asin");
lua_pushnumber (TODEGREE(asin(d)));
}
static void math_acos (void)
{
double d = get_and_check_float(1, "acos");
double d = lua_check_number(1, "acos");
lua_pushnumber (TODEGREE(acos(d)));
}
static void math_atan (void)
{
double d = get_and_check_float(1, "atan");
double d = lua_check_number(1, "atan");
lua_pushnumber (TODEGREE(atan(d)));
}
static void math_atan2 (void)
{
double d1 = get_and_check_float(1, "atan2");
double d2 = get_and_check_float(2, "atan2");
double d1 = lua_check_number(1, "atan2");
double d2 = lua_check_number(2, "atan2");
lua_pushnumber (TODEGREE(atan2(d1, d2)));
}
static void math_ceil (void)
{
double d = get_and_check_float(1, "ceil");
double d = lua_check_number(1, "ceil");
lua_pushnumber (ceil(d));
}
static void math_floor (void)
{
double d = get_and_check_float(1, "floor");
double d = lua_check_number(1, "floor");
lua_pushnumber (floor(d));
}
static void math_mod (void)
{
int d1 = (int)get_and_check_float(1, "mod");
int d2 = (int)get_and_check_float(2, "mod");
int d1 = (int)lua_check_number(1, "mod");
int d2 = (int)lua_check_number(2, "mod");
lua_pushnumber (d1%d2);
}
static void math_sqrt (void)
{
double d = get_and_check_float(1, "sqrt");
double d = lua_check_number(1, "sqrt");
lua_pushnumber (sqrt(d));
}
@ -147,10 +131,10 @@ static void math_pow (void)
static void math_min (void)
{
int i=1;
double dmin = get_and_check_float(i, "min");
double dmin = lua_check_number(i, "min");
while (lua_getparam(++i) != LUA_NOOBJECT)
{
double d = get_and_check_float(i, "min");
double d = lua_check_number(i, "min");
if (d < dmin) dmin = d;
}
lua_pushnumber (dmin);
@ -159,10 +143,10 @@ static void math_min (void)
static void math_max (void)
{
int i=1;
double dmax = get_and_check_float(i, "max");
double dmax = lua_check_number(i, "max");
while (lua_getparam(++i) != LUA_NOOBJECT)
{
double d = get_and_check_float(i, "max");
double d = lua_check_number(i, "max");
if (d > dmax) dmax = d;
}
lua_pushnumber (dmax);
@ -170,33 +154,33 @@ static void math_max (void)
static void math_log (void)
{
double d = get_and_check_float(1, "log");
double d = lua_check_number(1, "log");
lua_pushnumber (log(d));
}
static void math_log10 (void)
{
double d = get_and_check_float(1, "log10");
double d = lua_check_number(1, "log10");
lua_pushnumber (log10(d));
}
static void math_exp (void)
{
double d = get_and_check_float(1, "exp");
double d = lua_check_number(1, "exp");
lua_pushnumber (exp(d));
}
static void math_deg (void)
{
float d = get_and_check_float(1, "deg");
float d = lua_check_number(1, "deg");
lua_pushnumber (d*180./PI);
}
static void math_rad (void)
{
float d = get_and_check_float(1, "rad");
float d = lua_check_number(1, "rad");
lua_pushnumber (d/180.*PI);
}

View File

@ -3,7 +3,7 @@
** String library to LUA
*/
char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
char *rcs_strlib="$Id: strlib.c,v 1.13 1995/10/09 12:49:21 roberto Exp roberto $";
#include <string.h>
#include <stdio.h>
@ -14,27 +14,27 @@ char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
#include "lualib.h"
static void str_error(char *funcname)
void lua_arg_error(char *funcname)
{
char buff[250];
char buff[100];
sprintf(buff, "incorrect arguments to function `%s'", funcname);
lua_error(buff);
}
static char *check_and_get_string (int numArg, char *funcname)
char *lua_check_string (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
if (!(lua_isstring(o) || lua_isnumber(o)))
str_error(funcname);
lua_arg_error(funcname);
return lua_getstring(o);
}
static int check_and_get_int (int numArg, char *funcname)
float lua_check_number (int numArg, char *funcname)
{
lua_Object o = lua_getparam(numArg);
if (!lua_isnumber(o))
str_error(funcname);
return (int)lua_getnumber(o);
lua_arg_error(funcname);
return lua_getnumber(o);
}
static char *newstring (char *s)
@ -54,17 +54,17 @@ static char *newstring (char *s)
*/
static void str_find (void)
{
char *s1 = check_and_get_string(1, "strfind");
char *s2 = check_and_get_string(2, "strfind");
char *s1 = lua_check_string(1, "strfind");
char *s2 = lua_check_string(2, "strfind");
int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
check_and_get_int(3, "strfind")-1;
(int)lua_check_number(3, "strfind")-1;
char *f = strstr(s1+init,s2);
if (f != NULL)
{
int pos = f-s1+1;
if (lua_getparam (4) == LUA_NOOBJECT)
lua_pushnumber (pos);
else if (check_and_get_int(4, "strfind") >= pos+strlen(s2)-1)
else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
lua_pushnumber (pos);
else
lua_pushnil();
@ -80,7 +80,7 @@ static void str_find (void)
*/
static void str_len (void)
{
char *s = check_and_get_string(1, "strlen");
char *s = lua_check_string(1, "strlen");
lua_pushnumber(strlen(s));
}
@ -92,10 +92,10 @@ static void str_len (void)
*/
static void str_sub (void)
{
char *s = check_and_get_string(1, "strsub");
int start = check_and_get_int(2, "strsub");
char *s = lua_check_string(1, "strsub");
int start = (int)lua_check_number(2, "strsub");
int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
check_and_get_int(3, "strsub");
(int)lua_check_number(3, "strsub");
if (end < start || start < 1 || end > strlen(s))
lua_pushliteral("");
else
@ -114,7 +114,7 @@ typedef int (*strfunc)(int s);
static void str_apply (strfunc f, char *funcname)
{
char *s, *c;
c = s = newstring(check_and_get_string(1, funcname));
c = s = newstring(lua_check_string(1, funcname));
while (*c != 0)
{
*c = f(*c);
@ -150,12 +150,12 @@ static void str_upper (void)
*/
static void str_ascii (void)
{
char *s = check_and_get_string(1, "ascii");
char *s = lua_check_string(1, "ascii");
lua_Object o2 = lua_getparam(2);
int pos;
pos = (o2 == LUA_NOOBJECT) ? 0 : check_and_get_int(2, "ascii")-1;
pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1;
if (pos<0 || pos>=strlen(s))
str_error("ascii");
lua_arg_error("ascii");
lua_pushnumber(s[pos]);
}
@ -171,7 +171,7 @@ static void str_int2str (void)
{
if (i > maxparams)
lua_error("too many parameters to function `int2str'");
s[i-1] = check_and_get_int(i, "int2str");
s[i-1] = (int)lua_check_number(i, "int2str");
}
s[i-1] = 0;
lua_pushstring(s);