lua-users home
lua-l archive

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


> Personally, I'd settle for a mature version of the token-filter patch
> being accepted. Then one can do just about anything (which is probably
> the most common criticism ;)) including \x(x+1).

To allow people to experiment with that syntax, here is an implementation
using a token filter written in C.

It converts
	\x,y,z(x+(x-2)*g(x,y,z-2*(x+y))-y*z)
to
	function(x,y,z) return x+(x-2)*g(x,y,z-2*(x+y))-y*z end

It works automatically for empty parameter lists:
	\(x+z) --> function() return x+z end

--lhf

/*
* proxy.c
* lexer proxy for Lua parser -- implements '\x,y,z(exp)'
* Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
* 24 Nov 2010 17:17:44
* This code is hereby placed in the public domain.
* Add <<#include "proxy.c">> just before the definition of luaX_next in llex.c
*/

#include <string.h>

static int nexttoken(LexState *ls, SemInfo *seminfo)
{
	static int state=0;
	static int level=0;
	int t;
	switch (state)
	{
	case 0:
		t=llex(ls,seminfo);
		if (t=='\\') {
			state=1;
			t=TK_FUNCTION;
		}
		return t;
	case 1:
		state=2;
		return '(';
	case 2:
		t=llex(ls,seminfo);
		if (t=='(') {
			level=1;
			state=3;
			t=')';
		}
		return t;
	case 3:
		state=4;
		return TK_RETURN;
	case 4:
		t=llex(ls,seminfo);
		if (t=='(') ++level;
		else if (t==')') {
			if (--level==0) {
				state=0;
				t=TK_END;
			}
		}
		return t;
	}
}

#define llex nexttoken