![]() |
Label as a string var - Printable Version +- Forum (https://www.boriel.com/forum) +-- Forum: Compilers and Computer Languages (https://www.boriel.com/forum/forumdisplay.php?fid=12) +--- Forum: ZX Basic Compiler (https://www.boriel.com/forum/forumdisplay.php?fid=11) +--- Thread: Label as a string var (/showthread.php?tid=507) |
Label as a string var - oblo - 11-06-2012 Hi all Long time no see around here :oops: but now I'm getting some free time to spend with ZX Basic, so I hope to come more often ![]() In fact, I have a question: I want to store a label name in a variable, in order to do GOSUBs using a var, not fixed names. I made this code so far, but it fails with the message 'identifier 'eti$' is a var, not a label': Code: #INCLUDE <keys.bas> Can be it done or what I'm asking is impossible right now? Note that I'm asking for a string, not numbers. Thanks and regards Re: Label as a string var - britlion - 11-11-2012 oblo Wrote:Hi all It can't be done that way, since labels are static - they are memory addresses, effectively. So at run time, there's no such thing as "eti#" - a call to eti1 is replaced by a jp nnnn instruction, so far as I can tell. You'd have to make a jump table, or an array of address, and use that if you wanted to be able to leap to specific ones. That or have a routine that has an if else else else else to gosub based on a variable. Compiled code really doesn't allow calculated gotos in this way. Also, it's good programming practice to avoid goto and gosub where possible. I wrote a pac man game too (can I still your awesome pac man beeps intro some time for it? ![]() Re: Label as a string var - oblo - 11-12-2012 Thanks. Now I realize that it's impossible, I have to redone the engine from scratch, but it seems its better than I was doing. About the Pac-Man intro, what do you mean with 'can I still your awesome pac man beeps intro some time for it?'? If you're asking permission for re-use it, please do it ![]() Cheers Re: Label as a string var - britlion - 11-13-2012 I'm playing with <!-- m --><a class="postlink" href="http://www.worldofspectrum.org/infoseekid.cgi?id=0027713">http://www.worldofspectrum.org/infoseek ... id=0027713</a><!-- m --> to see about updating it. I wrote a tutorial for ZX Basic on how I did that - and you may find some hints and tips useful. OCMan is very cool - but hard to read. You still have line numbers. (And my ability to read in Spanish is not good, which doesn't help ![]() I try to write procedurally - so I have things like updateGhosts() displayScore() MovePlayer() As procedures - it should (language aside) hopefully be clear what each line does at the top level. In fact - my main loop and start point is crazy simple to read (and does so much!) - Without comments and fluff - the whole running game looks like this as a loop: Code: 'Main loop So I'm suggesting that you can use this procedural bottom up/ top down approach to make programs easier to read - and being more modular, you can more easily re-use parts in other programs. For example print42 is just a few subroutines, really - but it's easy to plug into another program from the library! |