aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-05-31 19:19:35 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1998-05-31 19:19:35 -0300
commit243a8080671c7df29a77a2e4bf8a08b8ee735de9 (patch)
tree70053c7378fe91facf0f25708b4cf4bd5e2e702c
parent62c36a6056537c2ac10de8f54b56a9a9740860b6 (diff)
downloadlua-243a8080671c7df29a77a2e4bf8a08b8ee735de9.tar.gz
lua-243a8080671c7df29a77a2e4bf8a08b8ee735de9.tar.bz2
lua-243a8080671c7df29a77a2e4bf8a08b8ee735de9.zip
'print' now calls 'tostring'
-rw-r--r--lbuiltin.c53
1 files changed, 30 insertions, 23 deletions
diff --git a/lbuiltin.c b/lbuiltin.c
index ed6e17b2..51d53d21 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.26 1998/03/06 16:54:42 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.27 1998/03/09 21:49:52 roberto Exp roberto $
3** Built-in functions 3** Built-in functions
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -131,53 +131,60 @@ static void internaldofile (void)
131} 131}
132 132
133 133
134static char *to_string (lua_Object obj) 134static void to_string (void) {
135{ 135 lua_Object obj = lua_getparam(1);
136 char *buff = luaL_openspace(30); 136 char *buff = luaL_openspace(30);
137 TObject *o = luaA_Address(obj); 137 TObject *o = luaA_Address(obj);
138 switch (ttype(o)) { 138 switch (ttype(o)) {
139 case LUA_T_NUMBER: case LUA_T_STRING: 139 case LUA_T_NUMBER:
140 return lua_getstring(obj); 140 lua_pushstring(lua_getstring(obj));
141 return;
142 case LUA_T_STRING:
143 lua_pushobject(obj);
144 return;
141 case LUA_T_ARRAY: { 145 case LUA_T_ARRAY: {
142 sprintf(buff, "table: %p", (void *)o->value.a); 146 sprintf(buff, "table: %p", (void *)o->value.a);
143 return buff; 147 break;
144 } 148 }
145 case LUA_T_CLOSURE: { 149 case LUA_T_CLOSURE: {
146 sprintf(buff, "function: %p", (void *)o->value.cl); 150 sprintf(buff, "function: %p", (void *)o->value.cl);
147 return buff; 151 break;
148 } 152 }
149 case LUA_T_PROTO: { 153 case LUA_T_PROTO: {
150 sprintf(buff, "function: %p", (void *)o->value.tf); 154 sprintf(buff, "function: %p", (void *)o->value.tf);
151 return buff; 155 break;
152 } 156 }
153 case LUA_T_CPROTO: { 157 case LUA_T_CPROTO: {
154 sprintf(buff, "function: %p", (void *)o->value.f); 158 sprintf(buff, "function: %p", (void *)o->value.f);
155 return buff; 159 break;
156 } 160 }
157 case LUA_T_USERDATA: { 161 case LUA_T_USERDATA: {
158 sprintf(buff, "userdata: %p", o->value.ts->u.d.v); 162 sprintf(buff, "userdata: %p", o->value.ts->u.d.v);
159 return buff; 163 break;
160 } 164 }
161 case LUA_T_NIL: 165 case LUA_T_NIL:
162 return "nil"; 166 lua_pushstring("nil");
167 return;
163 default: 168 default:
164 LUA_INTERNALERROR("invalid type"); 169 LUA_INTERNALERROR("invalid type");
165 return NULL; /* to avoid warnings */
166 } 170 }
167} 171 lua_pushstring(buff);
168
169static void bi_tostring (void)
170{
171 lua_pushstring(to_string(lua_getparam(1)));
172} 172}
173 173
174 174
175static void luaI_print (void) 175static void luaI_print (void) {
176{ 176 TaggedString *ts = luaS_new("tostring");
177 int i = 1;
178 lua_Object obj; 177 lua_Object obj;
179 while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) 178 int i = 1;
180 printf("%s\t", to_string(obj)); 179 while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) {
180 luaA_pushobject(&ts->u.s.globalval);
181 lua_pushobject(obj);
182 luaD_call((L->stack.top-L->stack.stack)-1, 1);
183 if (ttype(L->stack.top-1) != LUA_T_STRING)
184 lua_error("`tostring' must return a string to `print'");
185 printf("%s\t", svalue(L->stack.top-1));
186 L->stack.top--;
187 }
181 printf("\n"); 188 printf("\n");
182} 189}
183 190
@@ -491,7 +498,7 @@ static struct luaL_reg int_funcs[] = {
491 {"gettagmethod", gettagmethod}, 498 {"gettagmethod", gettagmethod},
492 {"settag", settag}, 499 {"settag", settag},
493 {"tonumber", tonumber}, 500 {"tonumber", tonumber},
494 {"tostring", bi_tostring}, 501 {"tostring", to_string},
495 {"tag", luatag}, 502 {"tag", luatag},
496 {"type", luaI_type} 503 {"type", luaI_type}
497}; 504};