module Torc

Constants

VERSION

Public Instance Methods

recur(*args) click to toggle source
# File lib/torc.rb, line 14
def recur(*args)
  name = binding.of_caller(1).eval('__method__')
  if _torc_state[:loop_stack].last == name
    _torc_state[:finished] = false
    return args
  else
    _torc_state[:loop_stack].push name
    begin
      begin
        _torc_state[:finished] = true
        args = __send__ name, *args
        if _torc_state[:swap_to]
          name, args = _torc_state[:swap_to]
          _torc_state[:swap_to] = nil
        end
      end until _torc_state[:finished]
      return args
    ensure
      _torc_state[:loop_stack].pop
    end
  end
end
tail_call(name, *args) click to toggle source
# File lib/torc.rb, line 5
def tail_call(name, *args)
  if _torc_state[:loop_stack].empty?
    recur name, *args
  else
    _torc_state[:swap_to] = [name, args]
    _torc_state[:finished] = false
  end
end

Private Instance Methods

_torc_state() click to toggle source
# File lib/torc.rb, line 39
def _torc_state
  @_torc_state ||= {
    loop_stack: [],
    swap_to:    nil,
    finished:   true,
  }
end