aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1995-11-10 15:54:31 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1995-11-10 15:54:31 -0200
commit5f664a45168803a12b27e9d96f74cad62869c562 (patch)
treeda93f92cb6e0b237957481847023bebdfaff29b0
parent87fe07c0d4e2fc5ef973e331da82974d2886e80c (diff)
downloadlua-5f664a45168803a12b27e9d96f74cad62869c562.tar.gz
lua-5f664a45168803a12b27e9d96f74cad62869c562.tar.bz2
lua-5f664a45168803a12b27e9d96f74cad62869c562.zip
error functions are shared by all libraries
-rw-r--r--lualib.h8
-rw-r--r--mathlib.c64
-rw-r--r--strlib.c42
3 files changed, 52 insertions, 62 deletions
diff --git a/lualib.h b/lualib.h
index f0e253d6..1ad9b30e 100644
--- a/lualib.h
+++ b/lualib.h
@@ -2,7 +2,7 @@
2** Libraries to be used in LUA programs 2** Libraries to be used in LUA programs
3** Grupo de Tecnologia em Computacao Grafica 3** Grupo de Tecnologia em Computacao Grafica
4** TeCGraf - PUC-Rio 4** TeCGraf - PUC-Rio
5** $Id: lualib.h,v 1.2 1994/08/24 15:29:02 celes Stab roberto $ 5** $Id: lualib.h,v 1.3 1994/12/13 15:59:16 roberto Exp roberto $
6*/ 6*/
7 7
8#ifndef lualib_h 8#ifndef lualib_h
@@ -12,5 +12,11 @@ void iolib_open (void);
12void strlib_open (void); 12void strlib_open (void);
13void mathlib_open (void); 13void mathlib_open (void);
14 14
15
16/* auxiliar functions (private) */
17void lua_arg_error(char *funcname);
18char *lua_check_string (int numArg, char *funcname);
19float lua_check_number (int numArg, char *funcname);
20
15#endif 21#endif
16 22
diff --git a/mathlib.c b/mathlib.c
index 9dd39609..1092052a 100644
--- a/mathlib.c
+++ b/mathlib.c
@@ -3,7 +3,7 @@
3** Mathematics library to LUA 3** Mathematics library to LUA
4*/ 4*/
5 5
6char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $"; 6char *rcs_mathlib="$Id: mathlib.c,v 1.12 1995/10/09 12:48:38 roberto Exp roberto $";
7 7
8#include <stdio.h> /* NULL */ 8#include <stdio.h> /* NULL */
9#include <math.h> 9#include <math.h>
@@ -17,25 +17,9 @@ char *rcs_mathlib="$Id: mathlib.c,v 1.11 1995/10/04 13:52:09 roberto Exp $";
17#define TODEGREE(a) ((a)*180.0/PI) 17#define TODEGREE(a) ((a)*180.0/PI)
18#define TORAD(a) ((a)*PI/180.0) 18#define TORAD(a) ((a)*PI/180.0)
19 19
20
21static void str_error(char *funcname)
22{
23 char buff[250];
24 sprintf(buff, "incorrect arguments to function `%s'", funcname);
25 lua_error(buff);
26}
27
28static float get_and_check_float (int numArg, char *funcname)
29{
30 lua_Object o = lua_getparam(numArg);
31 if (!lua_isnumber(o))
32 str_error(funcname);
33 return lua_getnumber(o);
34}
35
36static void math_abs (void) 20static void math_abs (void)
37{ 21{
38 double d = get_and_check_float(1, "abs"); 22 double d = lua_check_number(1, "abs");
39 if (d < 0) d = -d; 23 if (d < 0) d = -d;
40 lua_pushnumber (d); 24 lua_pushnumber (d);
41} 25}
@@ -43,7 +27,7 @@ static void math_abs (void)
43 27
44static void math_sin (void) 28static void math_sin (void)
45{ 29{
46 double d = get_and_check_float(1, "sin"); 30 double d = lua_check_number(1, "sin");
47 lua_pushnumber (sin(TORAD(d))); 31 lua_pushnumber (sin(TORAD(d)));
48} 32}
49 33
@@ -51,7 +35,7 @@ static void math_sin (void)
51 35
52static void math_cos (void) 36static void math_cos (void)
53{ 37{
54 double d = get_and_check_float(1, "cos"); 38 double d = lua_check_number(1, "cos");
55 lua_pushnumber (cos(TORAD(d))); 39 lua_pushnumber (cos(TORAD(d)));
56} 40}
57 41
@@ -59,64 +43,64 @@ static void math_cos (void)
59 43
60static void math_tan (void) 44static void math_tan (void)
61{ 45{
62 double d = get_and_check_float(1, "tan"); 46 double d = lua_check_number(1, "tan");
63 lua_pushnumber (tan(TORAD(d))); 47 lua_pushnumber (tan(TORAD(d)));
64} 48}
65 49
66 50
67static void math_asin (void) 51static void math_asin (void)
68{ 52{
69 double d = get_and_check_float(1, "asin"); 53 double d = lua_check_number(1, "asin");
70 lua_pushnumber (TODEGREE(asin(d))); 54 lua_pushnumber (TODEGREE(asin(d)));
71} 55}
72 56
73 57
74static void math_acos (void) 58static void math_acos (void)
75{ 59{
76 double d = get_and_check_float(1, "acos"); 60 double d = lua_check_number(1, "acos");
77 lua_pushnumber (TODEGREE(acos(d))); 61 lua_pushnumber (TODEGREE(acos(d)));
78} 62}
79 63
80 64
81static void math_atan (void) 65static void math_atan (void)
82{ 66{
83 double d = get_and_check_float(1, "atan"); 67 double d = lua_check_number(1, "atan");
84 lua_pushnumber (TODEGREE(atan(d))); 68 lua_pushnumber (TODEGREE(atan(d)));
85} 69}
86 70
87 71
88static void math_atan2 (void) 72static void math_atan2 (void)
89{ 73{
90 double d1 = get_and_check_float(1, "atan2"); 74 double d1 = lua_check_number(1, "atan2");
91 double d2 = get_and_check_float(2, "atan2"); 75 double d2 = lua_check_number(2, "atan2");
92 lua_pushnumber (TODEGREE(atan2(d1, d2))); 76 lua_pushnumber (TODEGREE(atan2(d1, d2)));
93} 77}
94 78
95 79
96static void math_ceil (void) 80static void math_ceil (void)
97{ 81{
98 double d = get_and_check_float(1, "ceil"); 82 double d = lua_check_number(1, "ceil");
99 lua_pushnumber (ceil(d)); 83 lua_pushnumber (ceil(d));
100} 84}
101 85
102 86
103static void math_floor (void) 87static void math_floor (void)
104{ 88{
105 double d = get_and_check_float(1, "floor"); 89 double d = lua_check_number(1, "floor");
106 lua_pushnumber (floor(d)); 90 lua_pushnumber (floor(d));
107} 91}
108 92
109static void math_mod (void) 93static void math_mod (void)
110{ 94{
111 int d1 = (int)get_and_check_float(1, "mod"); 95 int d1 = (int)lua_check_number(1, "mod");
112 int d2 = (int)get_and_check_float(2, "mod"); 96 int d2 = (int)lua_check_number(2, "mod");
113 lua_pushnumber (d1%d2); 97 lua_pushnumber (d1%d2);
114} 98}
115 99
116 100
117static void math_sqrt (void) 101static void math_sqrt (void)
118{ 102{
119 double d = get_and_check_float(1, "sqrt"); 103 double d = lua_check_number(1, "sqrt");
120 lua_pushnumber (sqrt(d)); 104 lua_pushnumber (sqrt(d));
121} 105}
122 106
@@ -147,10 +131,10 @@ static void math_pow (void)
147static void math_min (void) 131static void math_min (void)
148{ 132{
149 int i=1; 133 int i=1;
150 double dmin = get_and_check_float(i, "min"); 134 double dmin = lua_check_number(i, "min");
151 while (lua_getparam(++i) != LUA_NOOBJECT) 135 while (lua_getparam(++i) != LUA_NOOBJECT)
152 { 136 {
153 double d = get_and_check_float(i, "min"); 137 double d = lua_check_number(i, "min");
154 if (d < dmin) dmin = d; 138 if (d < dmin) dmin = d;
155 } 139 }
156 lua_pushnumber (dmin); 140 lua_pushnumber (dmin);
@@ -159,10 +143,10 @@ static void math_min (void)
159static void math_max (void) 143static void math_max (void)
160{ 144{
161 int i=1; 145 int i=1;
162 double dmax = get_and_check_float(i, "max"); 146 double dmax = lua_check_number(i, "max");
163 while (lua_getparam(++i) != LUA_NOOBJECT) 147 while (lua_getparam(++i) != LUA_NOOBJECT)
164 { 148 {
165 double d = get_and_check_float(i, "max"); 149 double d = lua_check_number(i, "max");
166 if (d > dmax) dmax = d; 150 if (d > dmax) dmax = d;
167 } 151 }
168 lua_pushnumber (dmax); 152 lua_pushnumber (dmax);
@@ -170,33 +154,33 @@ static void math_max (void)
170 154
171static void math_log (void) 155static void math_log (void)
172{ 156{
173 double d = get_and_check_float(1, "log"); 157 double d = lua_check_number(1, "log");
174 lua_pushnumber (log(d)); 158 lua_pushnumber (log(d));
175} 159}
176 160
177 161
178static void math_log10 (void) 162static void math_log10 (void)
179{ 163{
180 double d = get_and_check_float(1, "log10"); 164 double d = lua_check_number(1, "log10");
181 lua_pushnumber (log10(d)); 165 lua_pushnumber (log10(d));
182} 166}
183 167
184 168
185static void math_exp (void) 169static void math_exp (void)
186{ 170{
187 double d = get_and_check_float(1, "exp"); 171 double d = lua_check_number(1, "exp");
188 lua_pushnumber (exp(d)); 172 lua_pushnumber (exp(d));
189} 173}
190 174
191static void math_deg (void) 175static void math_deg (void)
192{ 176{
193 float d = get_and_check_float(1, "deg"); 177 float d = lua_check_number(1, "deg");
194 lua_pushnumber (d*180./PI); 178 lua_pushnumber (d*180./PI);
195} 179}
196 180
197static void math_rad (void) 181static void math_rad (void)
198{ 182{
199 float d = get_and_check_float(1, "rad"); 183 float d = lua_check_number(1, "rad");
200 lua_pushnumber (d/180.*PI); 184 lua_pushnumber (d/180.*PI);
201} 185}
202 186
diff --git a/strlib.c b/strlib.c
index 12e696a2..12f9dcbf 100644
--- a/strlib.c
+++ b/strlib.c
@@ -3,7 +3,7 @@
3** String library to LUA 3** String library to LUA
4*/ 4*/
5 5
6char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $"; 6char *rcs_strlib="$Id: strlib.c,v 1.13 1995/10/09 12:49:21 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -14,27 +14,27 @@ char *rcs_strlib="$Id: strlib.c,v 1.12 1995/02/06 19:37:51 roberto Exp $";
14#include "lualib.h" 14#include "lualib.h"
15 15
16 16
17static void str_error(char *funcname) 17void lua_arg_error(char *funcname)
18{ 18{
19 char buff[250]; 19 char buff[100];
20 sprintf(buff, "incorrect arguments to function `%s'", funcname); 20 sprintf(buff, "incorrect arguments to function `%s'", funcname);
21 lua_error(buff); 21 lua_error(buff);
22} 22}
23 23
24static char *check_and_get_string (int numArg, char *funcname) 24char *lua_check_string (int numArg, char *funcname)
25{ 25{
26 lua_Object o = lua_getparam(numArg); 26 lua_Object o = lua_getparam(numArg);
27 if (!(lua_isstring(o) || lua_isnumber(o))) 27 if (!(lua_isstring(o) || lua_isnumber(o)))
28 str_error(funcname); 28 lua_arg_error(funcname);
29 return lua_getstring(o); 29 return lua_getstring(o);
30} 30}
31 31
32static int check_and_get_int (int numArg, char *funcname) 32float lua_check_number (int numArg, char *funcname)
33{ 33{
34 lua_Object o = lua_getparam(numArg); 34 lua_Object o = lua_getparam(numArg);
35 if (!lua_isnumber(o)) 35 if (!lua_isnumber(o))
36 str_error(funcname); 36 lua_arg_error(funcname);
37 return (int)lua_getnumber(o); 37 return lua_getnumber(o);
38} 38}
39 39
40static char *newstring (char *s) 40static char *newstring (char *s)
@@ -54,17 +54,17 @@ static char *newstring (char *s)
54*/ 54*/
55static void str_find (void) 55static void str_find (void)
56{ 56{
57 char *s1 = check_and_get_string(1, "strfind"); 57 char *s1 = lua_check_string(1, "strfind");
58 char *s2 = check_and_get_string(2, "strfind"); 58 char *s2 = lua_check_string(2, "strfind");
59 int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 : 59 int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 :
60 check_and_get_int(3, "strfind")-1; 60 (int)lua_check_number(3, "strfind")-1;
61 char *f = strstr(s1+init,s2); 61 char *f = strstr(s1+init,s2);
62 if (f != NULL) 62 if (f != NULL)
63 { 63 {
64 int pos = f-s1+1; 64 int pos = f-s1+1;
65 if (lua_getparam (4) == LUA_NOOBJECT) 65 if (lua_getparam (4) == LUA_NOOBJECT)
66 lua_pushnumber (pos); 66 lua_pushnumber (pos);
67 else if (check_and_get_int(4, "strfind") >= pos+strlen(s2)-1) 67 else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
68 lua_pushnumber (pos); 68 lua_pushnumber (pos);
69 else 69 else
70 lua_pushnil(); 70 lua_pushnil();
@@ -80,7 +80,7 @@ static void str_find (void)
80*/ 80*/
81static void str_len (void) 81static void str_len (void)
82{ 82{
83 char *s = check_and_get_string(1, "strlen"); 83 char *s = lua_check_string(1, "strlen");
84 lua_pushnumber(strlen(s)); 84 lua_pushnumber(strlen(s));
85} 85}
86 86
@@ -92,10 +92,10 @@ static void str_len (void)
92*/ 92*/
93static void str_sub (void) 93static void str_sub (void)
94{ 94{
95 char *s = check_and_get_string(1, "strsub"); 95 char *s = lua_check_string(1, "strsub");
96 int start = check_and_get_int(2, "strsub"); 96 int start = (int)lua_check_number(2, "strsub");
97 int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) : 97 int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) :
98 check_and_get_int(3, "strsub"); 98 (int)lua_check_number(3, "strsub");
99 if (end < start || start < 1 || end > strlen(s)) 99 if (end < start || start < 1 || end > strlen(s))
100 lua_pushliteral(""); 100 lua_pushliteral("");
101 else 101 else
@@ -114,7 +114,7 @@ typedef int (*strfunc)(int s);
114static void str_apply (strfunc f, char *funcname) 114static void str_apply (strfunc f, char *funcname)
115{ 115{
116 char *s, *c; 116 char *s, *c;
117 c = s = newstring(check_and_get_string(1, funcname)); 117 c = s = newstring(lua_check_string(1, funcname));
118 while (*c != 0) 118 while (*c != 0)
119 { 119 {
120 *c = f(*c); 120 *c = f(*c);
@@ -150,12 +150,12 @@ static void str_upper (void)
150*/ 150*/
151static void str_ascii (void) 151static void str_ascii (void)
152{ 152{
153 char *s = check_and_get_string(1, "ascii"); 153 char *s = lua_check_string(1, "ascii");
154 lua_Object o2 = lua_getparam(2); 154 lua_Object o2 = lua_getparam(2);
155 int pos; 155 int pos;
156 pos = (o2 == LUA_NOOBJECT) ? 0 : check_and_get_int(2, "ascii")-1; 156 pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1;
157 if (pos<0 || pos>=strlen(s)) 157 if (pos<0 || pos>=strlen(s))
158 str_error("ascii"); 158 lua_arg_error("ascii");
159 lua_pushnumber(s[pos]); 159 lua_pushnumber(s[pos]);
160} 160}
161 161
@@ -171,7 +171,7 @@ static void str_int2str (void)
171 { 171 {
172 if (i > maxparams) 172 if (i > maxparams)
173 lua_error("too many parameters to function `int2str'"); 173 lua_error("too many parameters to function `int2str'");
174 s[i-1] = check_and_get_int(i, "int2str"); 174 s[i-1] = (int)lua_check_number(i, "int2str");
175 } 175 }
176 s[i-1] = 0; 176 s[i-1] = 0;
177 lua_pushstring(s); 177 lua_pushstring(s);