summaryrefslogtreecommitdiff
path: root/lbuiltin.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-10-18 14:33:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-10-18 14:33:36 -0200
commitac30aad09b8f793f42ccec1d3c463683f8989384 (patch)
tree0925b6a7800dca02aad3933d51b89acd84b18f03 /lbuiltin.c
parent2c89651fc6ed7b5b53a3dc3bef9d13c049eb1000 (diff)
downloadlua-ac30aad09b8f793f42ccec1d3c463683f8989384.tar.gz
lua-ac30aad09b8f793f42ccec1d3c463683f8989384.tar.bz2
lua-ac30aad09b8f793f42ccec1d3c463683f8989384.zip
new functions "foreach" and "foreachvar"
Diffstat (limited to 'lbuiltin.c')
-rw-r--r--lbuiltin.c101
1 files changed, 79 insertions, 22 deletions
diff --git a/lbuiltin.c b/lbuiltin.c
index 58c3e57b..e6225456 100644
--- a/lbuiltin.c
+++ b/lbuiltin.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lbuiltin.c,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $ 2** $Id: lbuiltin.c,v 1.2 1997/09/26 15:02:26 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*/
@@ -10,7 +10,9 @@
10#include "lapi.h" 10#include "lapi.h"
11#include "lauxlib.h" 11#include "lauxlib.h"
12#include "lbuiltin.h" 12#include "lbuiltin.h"
13#include "ldo.h"
13#include "lmem.h" 14#include "lmem.h"
15#include "lobject.h"
14#include "lstring.h" 16#include "lstring.h"
15#include "ltable.h" 17#include "ltable.h"
16#include "ltm.h" 18#include "ltm.h"
@@ -18,35 +20,73 @@
18 20
19 21
20 22
23static lua_Object tablearg (int arg)
24{
25 lua_Object o = lua_getparam(arg);
26 luaL_arg_check(lua_istable(o), arg, "table expected");
27 return o;
28}
29
30static lua_Object functionarg (int arg)
31{
32 lua_Object o = lua_getparam(arg);
33 luaL_arg_check(lua_isfunction(o), arg, "function expected");
34 return o;
35}
36
37
38static void pushstring (TaggedString *s)
39{
40 TObject o;
41 o.ttype = LUA_T_STRING;
42 o.value.ts = s;
43 luaA_pushobject(&o);
44}
45
46
21static void nextvar (void) 47static void nextvar (void)
22{ 48{
23 lua_Object v = luaL_nonnullarg(1); 49 TObject *o = luaA_Address(luaL_nonnullarg(1));
24 TaggedString *g; 50 TaggedString *g;
25 if (lua_isnil(v)) 51 if (ttype(o) == LUA_T_NIL)
26 g = (TaggedString *)luaS_root.next; 52 g = (TaggedString *)luaS_root.next;
27 else { 53 else {
28 TObject *o = luaA_Address(v);
29 luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected"); 54 luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
30 g = tsvalue(o); 55 g = tsvalue(o);
56 /* check whether name is in global var list */
31 luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected"); 57 luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
32 g = (TaggedString *)g->head.next; 58 g = (TaggedString *)g->head.next;
33 } 59 }
34 while (g && g->u.globalval.ttype == LUA_T_NIL) 60 while (g && g->u.globalval.ttype == LUA_T_NIL) /* skip globals with nil */
35 g = (TaggedString *)g->head.next; 61 g = (TaggedString *)g->head.next;
36 if (g) { 62 if (g) {
37 lua_pushstring(g->str); 63 pushstring(g);
38 luaA_pushobject(&g->u.globalval); 64 luaA_pushobject(&g->u.globalval);
39 } 65 }
40} 66}
41 67
42 68
69static void foreachvar (void)
70{
71 TObject *f = luaA_Address(functionarg(1));
72 GCnode *g;
73 for (g = luaS_root.next; g; g = g->next) {
74 TaggedString *s = (TaggedString *)g;
75 if (s->u.globalval.ttype != LUA_T_NIL) {
76 luaA_pushobject(f);
77 pushstring(s);
78 luaA_pushobject(&s->u.globalval);
79 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 0);
80 }
81 }
82}
83
84
43static void next (void) 85static void next (void)
44{ 86{
45 lua_Object o = lua_getparam(1); 87 lua_Object o = tablearg(1);
46 lua_Object r = luaL_nonnullarg(2); 88 lua_Object r = luaL_nonnullarg(2);
47 Node *n; 89 Node *n = luaH_next(luaA_Address(o), luaA_Address(r));
48 luaL_arg_check(lua_istable(o), 1, "table expected");
49 n = luaH_next(luaA_Address(o), luaA_Address(r));
50 if (n) { 90 if (n) {
51 luaA_pushobject(&n->ref); 91 luaA_pushobject(&n->ref);
52 luaA_pushobject(&n->val); 92 luaA_pushobject(&n->val);
@@ -54,6 +94,23 @@ static void next (void)
54} 94}
55 95
56 96
97static void foreach (void)
98{
99 TObject *t = luaA_Address(tablearg(1));
100 TObject *f = luaA_Address(functionarg(2));
101 Node *nd = avalue(t)->node;
102 int i;
103 for (i=0; i<avalue(t)->nhash; (i++, nd++)) {
104 if (ttype(ref(nd)) != LUA_T_NIL && ttype(val(nd)) != LUA_T_NIL) {
105 luaA_pushobject(f);
106 luaA_pushobject(ref(nd));
107 luaA_pushobject(val(nd));
108 luaD_call((luaD_stack.top-luaD_stack.stack)-2, 0);
109 }
110 }
111}
112
113
57static void internaldostring (void) 114static void internaldostring (void)
58{ 115{
59 lua_Object err = lua_getparam(2); 116 lua_Object err = lua_getparam(2);
@@ -120,7 +177,8 @@ static void luaI_print (void)
120 int i = 1; 177 int i = 1;
121 lua_Object obj; 178 lua_Object obj;
122 while ((obj = lua_getparam(i++)) != LUA_NOOBJECT) 179 while ((obj = lua_getparam(i++)) != LUA_NOOBJECT)
123 printf("%s\n", to_string(obj)); 180 printf("%s\t", to_string(obj));
181 printf("\n");
124} 182}
125 183
126 184
@@ -198,13 +256,11 @@ static int getnarg (lua_Object table)
198 256
199static void luaI_call (void) 257static void luaI_call (void)
200{ 258{
201 lua_Object f = lua_getparam(1); 259 lua_Object f = functionarg(1);
202 lua_Object arg = lua_getparam(2); 260 lua_Object arg = tablearg(2);
203 int withtable = (strcmp(luaL_opt_string(3, ""), "pack") == 0); 261 int pack = (strcmp(luaL_opt_string(3, ""), "pack") == 0);
204 int narg, i; 262 int narg = getnarg(arg);
205 luaL_arg_check(lua_isfunction(f), 1, "function expected"); 263 int i;
206 luaL_arg_check(lua_istable(arg), 2, "table expected");
207 narg = getnarg(arg);
208 /* push arg[1...n] */ 264 /* push arg[1...n] */
209 for (i=0; i<narg; i++) { 265 for (i=0; i<narg; i++) {
210 lua_Object temp; 266 lua_Object temp;
@@ -216,7 +272,7 @@ static void luaI_call (void)
216 } 272 }
217 if (lua_callfunction(f)) 273 if (lua_callfunction(f))
218 lua_error(NULL); 274 lua_error(NULL);
219 else if (withtable) 275 else if (pack)
220 luaA_packresults(); 276 luaA_packresults();
221 else 277 else
222 luaA_passresults(); 278 luaA_passresults();
@@ -225,8 +281,7 @@ static void luaI_call (void)
225 281
226static void settag (void) 282static void settag (void)
227{ 283{
228 lua_Object o = lua_getparam(1); 284 lua_Object o = tablearg(1);
229 luaL_arg_check(lua_istable(o), 1, "table expected");
230 lua_pushobject(o); 285 lua_pushobject(o);
231 lua_settag(luaL_check_number(2)); 286 lua_settag(luaL_check_number(2));
232} 287}
@@ -278,7 +333,7 @@ static void gettagmethod (void)
278 333
279static void seterrormethod (void) 334static void seterrormethod (void)
280{ 335{
281 lua_Object nf = luaL_nonnullarg(1); 336 lua_Object nf = functionarg(1);
282 lua_pushobject(nf); 337 lua_pushobject(nf);
283 lua_pushobject(lua_seterrormethod()); 338 lua_pushobject(lua_seterrormethod());
284} 339}
@@ -361,6 +416,8 @@ static struct luaL_reg int_funcs[] = {
361 {"dofile", internaldofile}, 416 {"dofile", internaldofile},
362 {"dostring", internaldostring}, 417 {"dostring", internaldostring},
363 {"error", luaI_error}, 418 {"error", luaI_error},
419 {"foreach", foreach},
420 {"foreachvar", foreachvar},
364 {"getglobal", getglobal}, 421 {"getglobal", getglobal},
365 {"newtag", newtag}, 422 {"newtag", newtag},
366 {"next", next}, 423 {"next", next},