Class: Heist::Runtime::Continuation

Inherits:
Function
  • Object
show all
Defined in:
lib/heist/runtime/callable/continuation.rb

Overview

The Continuation class encapsulates the first-class continuations as generated by the Scheme (call/cc) procedure. It inherits from Function so as to be recognised as a callable object by the Stack execution model. Each Continuation contains a snapshot of the state of the Stack at the moment it was created, and this state is restored, overriding whatever state is then in effect, when the Continuation is called.

Instance Attribute Summary

Attributes inherited from Function

#body, #name

Instance Method Summary collapse

Methods inherited from Function

#lazy?, #primitive?

Constructor Details

#initialize(stack) ⇒ Continuation

A Continuation is initialized with a Stack object, which it saves a snapshot of. The topmost frame on the stack will typically be a call to (call/cc) or some other Continuation-generating function, and is thus discarded as this call site marks the hole that should be filled when the Continuation is called and the saved stack is resumed.



18
19
20
21
# File 'lib/heist/runtime/callable/continuation.rb', line 18

def initialize(stack)
  @stack  = stack.copy(false)
  @target = stack.last.target
end

Instance Method Details

#apply(params) ⇒ Object

TODO support call-with-values



35
36
37
38
39
# File 'lib/heist/runtime/callable/continuation.rb', line 35

def apply(params)
  stack = @stack.copy
  stack.fill!(@target, Value.new(params.first))
  stack
end

#call(scope, cells) ⇒ Object

Calls the Continuation with the current Scope and a Cons list of parameters to the continuation. Returns a copy of the saved Stack with the innermost hole filled with the value of the parameter to the continuation call. The currently running Stack will see this return value as an instruction to throw out the current state of the stack and replace it with the stack returned by this method before continuing execution.



30
31
32
# File 'lib/heist/runtime/callable/continuation.rb', line 30

def call(scope, cells)
  apply(cells.to_a)
end

#to_sObject Also known as: inspect

Returns a string representation of the Continuation for console output.



43
44
45
# File 'lib/heist/runtime/callable/continuation.rb', line 43

def to_s
  "#<continuation>"
end