lua-users home
lua-l archive

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


> I would suggest the dollar ($) sign syntax in lcl.c so that you can
> can refer to variables.

Yes, I've considered that. There are many possible extensions. But I wanted
to keep it simple for a start.

Below is a version of cl.eval that understands $variables.
Here is a simple test:

	jane="tarzan"
	test("eval"," print moon $jane ball 123")
>>>	eval	 print moon $jane ball 123
moon	tarzan	ball	123

--

static int Leval(lua_State *L)			/** eval(s) */
{
	const char *s=luaL_checkstring(L,1);
	size_t l;
	int n;
	const char *b;
	for (n=0;;n++) {
		while (*s==' ' || *s=='\t') s++;
		if (*s==0) break;
		if (*s=='"' || *s=='\'') {
			int q=*s++;
			b=s;
			for (l=0; *s!=q && *s!=0; s++,l++);
			if (*s!=0) s++;
		}
		else {
			b=s;
			for (l=0; *s!=' ' && *s!='\t' && *s!=0; s++,l++);
		}
		if (*b=='$') {
			lua_pushlstring(L,b+1,l-1);
			lua_gettable(L,LUA_GLOBALSINDEX);
		}
		else
			lua_pushlstring(L,b,l);
		if (n==0) lua_gettable(L,LUA_GLOBALSINDEX);
	}
	if (n>0) lua_call(L,n-1,0);
	return 0;
}