开发者

Subroutine & GoTo design

I have a strange question concerning subroutines: As I'm creating a minimal language and I don't want to add high-level loo开发者_如何学Cps like while or for I was planning on just adding gotos to keep it Turing-Complete.

Now I thought, eww - gotos - I wouldn't want to program in that language if I had to use gotos so often. So I thought about adding subroutines instead.

I see the difference as the following:

  • gotos

    • Go to (captain obvious) a previously defined point and continue executing the program from there. Leads to hardly understandable and buggy code, I think that's a fact.
  • subroutines

    • Similiar: You define their starting point somewhere, as you call them the program jumps there - but the subroutine can go back to the point it was called from with return.

Okay. Why didn't I just add the more function-like, nice looking subroutines? Because:

In order to make return work if I call subroutines from within subroutines from within other subroutines, I'd have to use a stack containing the point where the currently running subroutine came from at top.

That would then mean that I would, if I create loops using the subroutines, end up with an extremely memory-eating, overflowing stack with return locations. Not good.

Don't think of my subroutines as functions. They are just gotos that return to the point they were called from, they don't actually give back values like the return x; statement in nearly all today's languages.

Now to my actual questions:

  • How can I solve the above problem with the stack overflow on loops with subroutines? Do I have to add a separate goto language construct without the return option?

  • Assembler doesn't have loops but as I have seen myJumpPoint:, jnz, jz, retn. That means to me that there must also be a stack containing all the return locations.

    • Am I right with that?
    • What about long running loops then? Don't they overflow the stack/eat memory then?
    • Am I getting the retn symbol in assembler totally wrong? If yes, please explain it to me.


jnz, jz are just "goto".

Also, I think implementing all loops as subroutines is less elegant a solution than just adding goto. You seem to be biased against goto. It's just a word. It won't harm you.


Well, couldn't you solve that problem with a conditional jump? A loop checks some condition to see if it has to run again. Go with your subroutines, but add a conditional return, that either returns to the point of call, or returns to the beginning of the routine depending on the evaluation of the condition.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜