lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Wednesday 05 April 2006 3:01 pm, Mark Hamburg wrote:
> Or at the very least, the Lua library needs a standard function to generate
> an iterator from a series of values so that one could write:
>
>     for i, arg in varargs( ... ) do
>         -- code goes here
>     end

it's easy in C (or TCC), no need to wrap on a table:

require "lua_tcc"

params= tcc.compile ([[

	#include "lua.h"
	#include "lauxlib.h"
	
	static int iter (lua_State *L) {
		int n = luaL_checkint (L, 1);
		int i = luaL_checkint (L, 2)+1;
		if (i > n)
			return 0;
		lua_pushnumber (L, i);
		lua_pushvalue (L, lua_upvalueindex (i));
		return 2;
	}

	int params (lua_State *L) {
		int n = lua_gettop (L);
		lua_pushcclosure (L, iter, n);
		lua_pushnumber (L, n);
		lua_pushnumber (L, 0);
		
		return 3;
	}
	
]], "params")

for i,v in params (4, 19, "anything", nil, 10) do
	print (i,v)
end

-- 
Javier

Attachment: pgpBQiCXN64CS.pgp
Description: PGP signature