Browse Source

new function "tostring".

Roberto Ierusalimschy 29 years ago
parent
commit
a19f9056f3
3 changed files with 47 additions and 31 deletions
  1. 32 19
      inout.c
  2. 3 2
      inout.h
  3. 12 10
      table.c

+ 32 - 19
inout.c

@@ -5,7 +5,7 @@
 ** Also provides some predefined lua functions.
 */
 
-char *rcs_inout="$Id: inout.c,v 2.24 1995/10/23 13:54:11 roberto Exp roberto $";
+char *rcs_inout="$Id: inout.c,v 2.25 1995/10/25 13:05:51 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -132,27 +132,40 @@ void lua_internaldofile (void)
   lua_pushnil();
 }
  
-/*
-** Internal function: print object values
-*/
-void lua_print (void)
+
+static char *tostring (lua_Object obj)
 {
- int i=1;
- lua_Object obj;
- while ((obj=lua_getparam (i++)) != LUA_NOOBJECT)
- {
-  if      (lua_isnumber(obj))    printf("%g\n",lua_getnumber(obj));
-  else if (lua_isstring(obj))    printf("%s\n",lua_getstring(obj));
-  else if (lua_isfunction(obj))  printf("function: %p\n",(luaI_Address(obj))->value.tf);
-  else if (lua_iscfunction(obj)) printf("cfunction: %p\n",lua_getcfunction(obj)
-);
-  else if (lua_isuserdata(obj))  printf("userdata: %p\n",lua_getuserdata(obj));
-  else if (lua_istable(obj))     printf("table: %p\n",avalue(luaI_Address(obj)));
-  else if (lua_isnil(obj))       printf("nil\n");
-  else                           printf("invalid value to print\n");
- }
+  static char buff[20];
+  if (lua_isstring(obj))
+    return lua_getstring(obj);
+  if (lua_isnumber(obj))
+    sprintf(buff, "%g", lua_getnumber(obj));
+  else if (lua_isfunction(obj))
+    sprintf(buff, "function: %p", (luaI_Address(obj))->value.tf);
+  else if (lua_iscfunction(obj))
+    sprintf(buff, "cfunction: %p", lua_getcfunction(obj));
+  else if (lua_isuserdata(obj))
+    sprintf(buff, "userdata: %p", lua_getuserdata(obj));
+  else if (lua_istable(obj))
+    sprintf(buff, "table: %p", avalue(luaI_Address(obj)));
+  else if (lua_isnil(obj))
+    sprintf(buff, "nil");
+  else buff[0] = 0;
+  return buff;
 }
 
+void luaI_tostring (void)
+{
+  lua_pushstring(tostring(lua_getparam(1)));
+}
+
+void luaI_print (void)
+{
+  int i = 1;
+  lua_Object obj;
+  while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
+    printf("%s\n", tostring(obj));
+}
 
 /*
 ** Internal function: return an object type.

+ 3 - 2
inout.h

@@ -1,5 +1,5 @@
 /*
-** $Id: inout.h,v 1.9 1995/05/16 17:23:58 roberto Exp roberto $
+** $Id: inout.h,v 1.10 1995/10/17 11:58:41 roberto Exp roberto $
 */
 
 
@@ -21,7 +21,8 @@ void lua_closestring  (void);
 
 void    lua_internaldofile (void);
 void    lua_internaldostring (void);
-void    lua_print      (void);
+void    luaI_tostring   (void);
+void    luaI_print      (void);
 void    luaI_type       (void);
 void    lua_obj2number (void);
 void	luaI_error     (void);

+ 12 - 10
table.c

@@ -3,7 +3,7 @@
 ** Module to control static tables
 */
 
-char *rcs_table="$Id: table.c,v 2.39 1996/01/09 20:23:19 roberto Exp $";
+char *rcs_table="$Id: table.c,v 2.40 1996/01/22 14:15:13 roberto Exp roberto $";
 
 /*#include <string.h>*/
 
@@ -44,6 +44,14 @@ static void lua_initsymbol (void)
  Word n;
  lua_maxsymbol = BUFFER_BLOCK;
  lua_table = newvector(lua_maxsymbol, Symbol);
+ n = luaI_findsymbolbyname("nextvar");
+ s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
+ n = luaI_findsymbolbyname("error");
+ s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
+ n = luaI_findsymbolbyname("tonumber");
+ s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
+ n = luaI_findsymbolbyname("setfallback");
+ s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
  n = luaI_findsymbolbyname("next");
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_next;
  n = luaI_findsymbolbyname("dofile");
@@ -52,20 +60,14 @@ static void lua_initsymbol (void)
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = setglobal;
  n = luaI_findsymbolbyname("getglobal");
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = getglobal;
- n = luaI_findsymbolbyname("nextvar");
- s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_nextvar;
  n = luaI_findsymbolbyname("type"); 
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_type;
- n = luaI_findsymbolbyname("tonumber");
- s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_obj2number;
+ n = luaI_findsymbolbyname("tostring");
+ s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_tostring;
  n = luaI_findsymbolbyname("print");
- s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_print;
+ s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_print;
  n = luaI_findsymbolbyname("dostring");
  s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = lua_internaldostring;
- n = luaI_findsymbolbyname("setfallback");
- s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_setfallback;
- n = luaI_findsymbolbyname("error");
- s_tag(n) = LUA_T_CFUNCTION; s_fvalue(n) = luaI_error;
 }