views:

76

answers:

3

I'm designing and currently rethinking a low-level interpreted programming language with similarities to assembler.

I very soon came across the functions/loops/gotos decision problem and thought that while loops like while and for would be too high-level and unfitting, gotos would be too low level, unmaintainable and generally evil again. Functions like you know them from most languages that have return values and arguments aren't fitting in the language's concept either.

So I tried to figure out something between a function and a goto which is capable of

  • Recursion
  • Efficient loops

After some thinking I came up with the idea of subroutines:

  • They have a beginning and an end like a function
  • They have a name but no arguments like a goto
  • You can go into one with jump and go out of it again before its end with return (doesn't give back any result, only stops the subroutine)
  • Handled just like normal code -> Global scope like goto

So I wanted to know:

  • Is the idea above good? What are the (dis)advantages?
  • Would there be a better combination of function and goto or even a completely new idea?
A: 

I think when you're at assembly-level, GOTO stops being evil, and starts just being how programs actually work.

But anyway, do your sub-routines take parameters? Even if they don't, how do your store register state to avoid recursion overflow?

John
I use a stack with the positions of the jumps so the interpreter knows where to return to after a subroutine. However, to avoid memory-eating, slowiness and overflow the stack isn't used when a subroutine calls itself -> loop.
sub
Sub, if you do not store the return address when a subroutine calls itself, it is not really recursion. And there is no point to implement loop by using "recursion". Use goto instead.
PauliL
A: 

The subroutine you are implementing is just the same as GOSUB in Basic or CALL in Assembly.

Recursion is only meaningful if you are using it for recursive algorithms. That requires functions with parameters, local variables and a return-value.

However, in some simple scripting languages that only have global variables, there are PUSH and POP instructions for storing variables in stack (just like registers are pushed and popped in Assembly language). Those can be used for low-level implementation of local variables and recursion.

I have used that method in some Vedit Macro Language examples in Rosetta Code, see for example Towers of Hanoi and Bezier curve.

PauliL
A: 
Norman Ramsey