lua-users home
lua-l archive

Re: How to implement this with LuaMacro?

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


On Mon, Jan 25, 2010 at 10:04 AM, Pavel Shevaev <pacha.shevaev@gmail.com> wrote:
> Guys, now I'm trying to implement this and it looks like I need
> variable arguments list within a macro. Is it possible at all?
Very much so. Consider this macro:
macro.define ('sum',{handle_parms = macro.parameter_grabber(')',',','(')},
	function(ls,...)
		local args = {...}
		local subst,put = macro.subst_putter()
		local n = #args
		for i = 1,n do
			put(args[i])
			if i < n then put '+' end
		end
		return subst
	end
)
sum(10,20,30) will expand to 10+20+30
Here handle_parms is a function which explicitly expects an arguments
list: start with '(', use ',' as the delimiter, and end with ')'.
This gives you an array of arguments.
The substitution is here a function, which receives an arbitrary
number of arguments and explicitly creates an output token list.
It's more low-level, but one can create anything this way.
steve d.

AltStyle によって変換されたページ (->オリジナル) /