lua-users home
lua-l archive

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


It was thus said that the Great Soni L. once stated:
> 
> On 20/12/14 07:27 PM, Sean Conner wrote:
> >It was thus said that the Great Soni L. once stated:
> >>So let's say I'm hacking the internals and I wanna force a Lua function
> >>to stop and just return whatever I want instead of continuing and/or
> >>returning what it wants to return. How can I do this?
> >   Um ... insert a return statement where you want it to return, with the
> >data you want to return?
> >
> >   -spc
> >
> >
> I want `return Return()` inside a string.gsub to make the string.gsub 
> return whatever I pass to the Return function. I want to be able to pass 
> a function as a callback and force the called function to return early. 
> I want to be able to `x = x and ((x == y and Return()) or (x == z and 
> a)) or b`. I can't do any of this with a plain return statement.

  Just so I'm clear on this---

	x = string.gsub(data,".",function(c)
		if c:match "%d" then
		  Return("Fools!  There is no answer!")
		else
		  return c:upper()
		end
	end)

  So, if you call

	data = "one two three"
	x = string.gsub( --[[ the above code ]] )

  you get "ONE TWO THREE", but if you call:

	data = "one two 23-skidoo"
	x = string.gsub( --[[ the above code ]] )

  you get "Fools!  There is no answer!"  Is that correct?

  If that's not correct, could you please provide an example.

  Also, you might want to look into

	https://github.com/franko/luajit-lang-toolkit

It contains a Lua parser (parses Lua into an AST) in Lua itself, so you
might be able to add whatever extentions you want and test them out.

  -spc (Just a suggestion)