lua-users home
lua-l archive

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


On 11/19/18, Muh Muhten <muh.muhten@gmail.com> wrote:
> % ./lua -e 'print((\f((\a(a(a)))(\x(f(\v(x(x)(v)))))))(\fac(\x(x == 0
> and 1 or x*fac(x-1))))(10))'

Of course, the number of brackets used here is ... inspiring. One
might prefer a more nostalgic notation. With credit where it is due:
http://lua-users.org/lists/lua-l/2010-11/msg00776.html

% ./lua -e 'print((\f.(\a.a(a))(\x.f(\v.x(x)(v))))(\fac.\x. x == 0 and
1 or x*fac(x-1))(10))'
3628800

/* proxy.c
 * lexer proxy for Lua parser -- implements nested lambdas:
 * '(\x,y.\z.exp)' -> 'function (x, y) return function (z) return exp end end'
 * '\' must immediately follow either '(' or the beginning of another lambda's
 * body, and closes at the matching ')'. This may include multiple expressions.
 *
 * Michael Zuo <muh.muhten@gmail.com>, 19 nov 2018:
 * This code is hereby placed in the public domain.
 *
 * Based on lhf's work: http://lua-users.org/lists/lua-l/2010-11/msg00808.html
 * 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
 */

static int nexttoken (LexState *ls, SemInfo *seminfo) {
	static char needs_end[LUAI_MAXCCALLS];
	static unsigned state = 0, level = 1;
	int t;

	switch (state) {
	case 0: case 5:
		t = llex(ls, seminfo);
		if (t == '\\' && state == 5) {
			needs_end[level++] = 1;
			state = 1;
			return TK_FUNCTION;
		}
		else if (t == '(') {
			level++;
			state = 5;
			return t;
		}
		else if (t == ')' && needs_end[--level]) {
			state = 4;
			return TK_END;
		}
		state = 0;
		return t;
	case 1:
		state = 2;
		return '(';
	case 2:
		t = llex(ls, seminfo);
		if (t == '.') {
			state = 3;
			return ')';
		}
		return t;
	case 3:
		state = 5;
		return TK_RETURN;
	case 4:
		needs_end[level] = 0;
		if (needs_end[--level])
			return TK_END;
		state = 0;
		return ')';
	default: lua_assert(0); return 0;
	}
}
#define llex nexttoken