lua-users home
lua-l archive

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



Okay, here's a more polished version (still, not tried). If someone uses it, make sure cpp doesn't add #pragma's or other such stuff in its output (I've used 'cpp -P -xassembler-with-cpp').

	local __retline

#define __GOSUB(xx) __retline='#'.. #__LINE__; __GOTO=xx; __LABEL='#'.. #__LINE__
	#define __RETURN     __GOTO=__retline

I know that the '__GOSUB()' syntax and '__GOTO=' syntaxes are nastily different; don't know a fix for this.. :(

-ak


13.9.2004 kello 20:41, Asko Kauppi kirjoitti:


Well, you can emulate GOSUBs with GOTO, can't you.. (just conceptually,

	a=1
	__GOSUB=77
	a=2
	__GOSUB=77
	a=3
	__LABEL=77
	print(a)
	__RETURN

Where:
	__GOSUB=xx:	__RETURN_TO='#'..linenum; __GOTO=xx; __LABEL='#'..linenum
	__RETURN:	__GOTO=__RETURN_TO

This would probably need the use of C preprocessor (cpp) or something alike to do the macros (& get the linenums!). Is that out of the question?


13.9.2004 kello 20:13, Luiz Henrique de Figueiredo kirjoitti:

 I tried to implement GOTO/GOSUB functionality in Lua.

An year ago, prompted by a similar request here in lua-l, I wrote a simple
patch for luac that did the right thing for the code below:

         a=1
         __GOTO=12
         __LABEL=99
         print("bye",a)
         do return end
         a=2
         __LABEL=12
         print("hello",a)
         a=3
         __GOTO=99

That is, it turned __GOTO into JMPs and __LABEL into NOPS.
This worked great for GOTO but I don't think it will work for GOSUB ... RETURN.
--lhf