lua-users home
lua-l archive

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


I tried to implement GOTO/GOSUB functionality in Lua. I need those commands
because I have to keep up compatibility with scripts from older versions of my
software which use these functions quite often. Substituting GOSUBs with
Lua functions does not work, because functions need to be declared before
they can be called.   

The rules for the implementation are:

- Labels must only be declared in the main block of the script, neither inside functions
nor inside if/while/for/repeat statements

- GOTO/GOSUB can be called everywhere except in functions

Now my questions is: What information besides the current PC do I need to save when
doing a GOSUB to be able to return later? In the current implementation I'm
just saving the current PC. That works if you call GOSUB somewhere
in the main block but it does not properly return from the label if you call GOSUB
e.g. inside a FOR loop or something else which uses local variables. So I think I also
need to save the current stack pointer or something. But I'm not so familiar with
the internals of Lua so I thought I better ask you guys first before I try "hacking"
something into it.

Once again in a nutshell:

- when doing a GOSUB, i.e. a jump to somewhere in the main block of the script:
what variables do I need to change besides the PC?

- what variables do I need to preserve to be able to return later to the point where
the GOSUB was called besides the PC?

This shouldn't get too complex because, as I said before, labels are always in the
main block of the script and GOSUB must not be called from functions. 

Any help appreciated.