Search code examples
compiler-constructionprogramming-languagescompilation

Unifying break, continue, return and throw


When implementing a language that supports all of these constructs, it seems to me that it should be possible to regard break, continue, return and throw as aspects of the same thing, something along the lines of returning from (or performing tail recursion on, in the case of continue) a function further up the call stack, not necessarily the current function.

Obviously I'm not going to be the first person to have thought of this. Does anyone know of any references to discussion (or open source implementation) of this way of doing things?


Solution

  • All of these things can be easily implemented on top of the first class continuations (such as in Scheme).

    But I doubt it can ever be the most efficient implementation, so the best approach so far is to treat them all as different entities - break and continue are resolved at a compilation stage as simple jumps, return implementation depends on calling conventions, throw/catch should also rely on the calling conventions and ABI defined for the target platform.