Shop OBEX P1 Docs P2 Docs Learn Events
Is there a way to simulate a "GOTO" in SPIN? — Parallax Forums

Is there a way to simulate a "GOTO" in SPIN?

DreamwriterDreamwriter Posts: 22
edited 2007-03-23 14:12 in Propeller 1
Man, every programmer atom in my body is crying against this, but the most optimal solution to a problem I have is a GOTO statement. See, the problem is in the largest, most complicated block of code in Hydra Logo, code for running user-created commands (functions), I'm setting it up to add support for tail-end recursion without taking up any extra stack space.

Normally when a function is running, my code runs an interpreter that does a large amount of preparation, then does a loop running each line in the function. If one of those lines calls another function, I basically increment my own stack and re-call the interpreter (which takes up more SPIN stack). My new code checks to see if the new function being called is the very last line in the current function, and if so it instead needs to act as if the interpreter were just called, so it can continue right to the new function without taking up any extra stack space at all. What I've done for now (and it works) is copy all the preparation code from the beginning of the interpreter function so it re-prepares for the new function right there, but that's a lot of code to have in there twice, taking up tons of memory and just making the code that much more unreadable. And it makes extensive use of local variables, so I can't easily set it up as a separate preparation function to just be called twice.

So anyways, the best solution I can think of would be a GOTO statement, a way to just jump straight to (almost) the top of the interpreter function and let it run from there. Anyone know a way to do this? Some way to abuse the ABORT command or something? Thanks!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-03-23 14:12
    I suspect you're going to have to rewrite your preparation routine so it's callable as a function.

    The ABORT statement is just a multi-level return and I don't think it would help you here since your "goto" is not up the call stack.

    Having converted a lot of GOTO code into GOTO-less in the distant past, I can tell you that you often need to rearrange the logic and avoid duplication like what you have ... by making extra subroutines like what you're trying to avoid here.

    Post Edited (Mike Green) : 3/23/2007 2:16:43 PM GMT
Sign In or Register to comment.