aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-03 20:34:29 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1994-11-03 20:34:29 -0200
commit31d58e2f0193998d28f1c0587573fdc35da383fd (patch)
treee49c42ea8c3674c8e6d868a54e3dd100fef1e240
parent42ef3f938876088a1c28afd3f7e7b25273f1c976 (diff)
downloadlua-31d58e2f0193998d28f1c0587573fdc35da383fd.tar.gz
lua-31d58e2f0193998d28f1c0587573fdc35da383fd.tar.bz2
lua-31d58e2f0193998d28f1c0587573fdc35da383fd.zip
more functions from opcode.c
'open_file' and 'open_string' return an error message lua_type renamed to luaI_type (I for Internal, as this function is not exported outside lua)
-rw-r--r--inout.c52
-rw-r--r--inout.h9
2 files changed, 44 insertions, 17 deletions
diff --git a/inout.c b/inout.c
index 143acece..3c63c638 100644
--- a/inout.c
+++ b/inout.c
@@ -5,7 +5,7 @@
5** Also provides some predefined lua functions. 5** Also provides some predefined lua functions.
6*/ 6*/
7 7
8char *rcs_inout="$Id: inout.c,v 2.5 1994/10/17 19:04:19 celes Exp roberto $"; 8char *rcs_inout="$Id: inout.c,v 2.6 1994/11/02 20:29:39 roberto Exp roberto $";
9 9
10#include <stdio.h> 10#include <stdio.h>
11#include <stdlib.h> 11#include <stdlib.h>
@@ -60,16 +60,20 @@ static int stringinput (void)
60 60
61/* 61/*
62** Function to open a file to be input unit. 62** Function to open a file to be input unit.
63** Return 0 on success or 1 on error. 63** Return 0 on success or error message on error.
64*/ 64*/
65int lua_openfile (char *fn) 65char *lua_openfile (char *fn)
66{ 66{
67 lua_linenumber = 1; 67 lua_linenumber = 1;
68 lua_setinput (fileinput); 68 lua_setinput (fileinput);
69 fp = fopen (fn, "r"); 69 fp = fopen (fn, "r");
70 if (fp == NULL) return 1; 70 if (fp == NULL)
71 if (lua_addfile (fn)) return 1; 71 {
72 return 0; 72 static char buff[32];
73 sprintf(buff, "unable to open file %.10s", fn);
74 return buff;
75 }
76 return lua_addfile (fn);
73} 77}
74 78
75/* 79/*
@@ -88,7 +92,7 @@ void lua_closefile (void)
88/* 92/*
89** Function to open a string to be input unit 93** Function to open a string to be input unit
90*/ 94*/
91int lua_openstring (char *s) 95char *lua_openstring (char *s)
92{ 96{
93 lua_linenumber = 1; 97 lua_linenumber = 1;
94 lua_setinput (stringinput); 98 lua_setinput (stringinput);
@@ -96,9 +100,8 @@ int lua_openstring (char *s)
96 { 100 {
97 char sn[64]; 101 char sn[64];
98 sprintf (sn, "String: %10.10s...", s); 102 sprintf (sn, "String: %10.10s...", s);
99 if (lua_addfile (sn)) return 1; 103 return lua_addfile (sn);
100 } 104 }
101 return 0;
102} 105}
103 106
104/* 107/*
@@ -208,19 +211,21 @@ void lua_print (void)
208 else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj) 211 else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction (obj)
209); 212);
210 else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata (obj)); 213 else if (lua_isuserdata(obj)) printf("userdata: %p\n",lua_getuserdata (obj));
211 else if (lua_istable(obj)) printf("table: %p\n",obj); 214 else if (lua_istable(obj)) printf("table: %p\n",avalue(obj));
212 else if (lua_isnil(obj)) printf("nil\n"); 215 else if (lua_isnil(obj)) printf("nil\n");
213 else printf("invalid value to print\n"); 216 else printf("invalid value to print\n");
214 } 217 }
215} 218}
216 219
217 220
218/* 221/*
219** Internal function: return an object type. 222** Internal function: return an object type.
220*/ 223*/
221void lua_type (void) 224void luaI_type (void)
222{ 225{
223 Object *o = lua_getparam(1); 226 Object *o = lua_getparam(1);
227 if (o == NULL)
228 lua_error("no parameter to function 'type'");
224 switch (tag(o)) 229 switch (tag(o))
225 { 230 {
226 case LUA_T_NIL : 231 case LUA_T_NIL :
@@ -247,3 +252,24 @@ void lua_type (void)
247 } 252 }
248} 253}
249 254
255/*
256** Internal function: convert an object to a number
257*/
258void lua_obj2number (void)
259{
260 lua_Object o = lua_getparam(1);
261 if (lua_isnumber(o))
262 lua_pushobject(o);
263 else if (lua_isstring(o))
264 {
265 char c;
266 float f;
267 if (sscanf(lua_getstring(o),"%f %c",&f,&c) == 1)
268 lua_pushnumber(f);
269 else
270 lua_pushnil();
271 }
272 else
273 lua_pushnil();
274}
275
diff --git a/inout.h b/inout.h
index f8cc0e93..0714f1a4 100644
--- a/inout.h
+++ b/inout.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: inout.h,v 1.2 1994/10/11 14:38:17 celes Exp roberto $ 2** $Id: inout.h,v 1.3 1994/11/02 20:29:39 roberto Exp roberto $
3*/ 3*/
4 4
5 5
@@ -10,9 +10,9 @@ extern int lua_linenumber;
10extern int lua_debug; 10extern int lua_debug;
11extern int lua_debugline; 11extern int lua_debugline;
12 12
13int lua_openfile (char *fn); 13char *lua_openfile (char *fn);
14void lua_closefile (void); 14void lua_closefile (void);
15int lua_openstring (char *s); 15char *lua_openstring (char *s);
16void lua_closestring (void); 16void lua_closestring (void);
17int lua_pushfunction (char *file, int function); 17int lua_pushfunction (char *file, int function);
18void lua_popfunction (void); 18void lua_popfunction (void);
@@ -21,6 +21,7 @@ void lua_reportbug (char *s);
21void lua_internaldofile (void); 21void lua_internaldofile (void);
22void lua_internaldostring (void); 22void lua_internaldostring (void);
23void lua_print (void); 23void lua_print (void);
24void lua_type (void); 24void luaI_type (void);
25void lua_obj2number (void);
25 26
26#endif 27#endif