1

I am trying to implement continuations with unlimited extent in a Go program (for those curious, I am writing a scheme interpreter). One way to do this is to copy the callstack to the heap so that it can be restored later. Go's garbage collector still needs to know about all the pointers in the copy of the stack. Is there a way to do this in a Go program? Note that I am willing to write code in C or assembly, compatibly with the Go toolchain, if need be.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
Matt
  • 21,026
  • 18
  • 63
  • 115
  • 1
    Not directly. Since it's an interpreter you're writing, there might be some way to in effect create a continuation by blocking the running goroutine on a channel read (the channel is your continuation object), then resuming it by sending to that channel. I don't know enough about the Scheme side of things to confidently sketch out more of what that would look like, though. – twotwotwo Mar 25 '15 at 00:41
  • @twotwotwo: Thanks for the tip! I was thinking there might be a way to do it with goroutines, but I couldn't figure out how. Your suggestion just might work. – Matt Mar 25 '15 at 00:43
  • @twotwotwo: On second thought, it probably won't work because there would be no way to resume the same continuation more than once. Is there a way to copy goroutines? – Matt Mar 25 '15 at 00:45
  • Ah, fascinating. No, I didn't know you could copy a continuation, resume both copies, and have them both start from the same point. You can copy a channel, and if the goroutine blocks on reading from it again you can send to it again, but it'll resume from the new state, not the old. – twotwotwo Mar 25 '15 at 00:53
  • All I can see to get those exact semantics is going all the way to the lowest level and having your own little bytecode interpreter working with stacks of []interface{}s -- obviously a way harder type of project. – twotwotwo Mar 25 '15 at 01:06
  • There are some alleged lisps/schemes listed at http://go-lang.cat-v.org/go-code -- could see if any of them have continuations that can resume from the same place more than once, and how they do it if so. – twotwotwo Mar 25 '15 at 01:07
  • @twotwotwo: One thing I could do is refactor the `eval` function to use its own stack-like structure and loop instead of recursion, but I asked this question to see if there is a way to copy the actual Go callstack. Another method to work around this problem would be [trampolining](https://en.wikipedia.org/wiki/Trampoline_(computing)), but I would like to avoid it. – Matt Mar 25 '15 at 02:13

0 Answers0