Hacker News new | ask | show | jobs
by kazinator 3675 days ago
How about a while true loop with cases in it on a state variable.

  goto=42
  while true ; do
    case $goto in
    1 )
      code for label 1
      goes here
      break     # finish
      ;;
    42 )
      code for label 42
      goes here
      goto=1
      ;;
    esac
  done
You don't have the syntactic sugar here, and there are some inconveniences, but think of the positives: this does not claim a new stack frame for each jump, let a lone a new stack frame holding a big string variable that holds most of the script source code.
1 comments

I recall seeing a coroutine library for C that did exactly this using preprocessor magic.

EDIT: /me sighs, this isn't the one I was looking for but it'll do for now. http://www.chiark.greenend.org.uk/~sgtatham/coroutines.html / http://www.chiark.greenend.org.uk/~sgtatham/coroutines.h