Class: BusScheme::StackFrame

Inherits:
Hash show all
Defined in:
lib/stack_frame.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Callable

#call, #call_as

Constructor Details

#initialize(locals, parent, called_as) ⇒ StackFrame

takes a hash and a parent


6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/stack_frame.rb', line 6

def initialize(locals, parent, called_as)
  @parent, @called_as = [parent, called_as]
  @file = @called_as.respond_to?(:file) ? @called_as.file : '(eval)'
  @line = @called_as.respond_to?(:line) ? @called_as.line : 0
  @called_as = '(anonymous)' if called_as.is_a?(Cons) or called_as.is_a?(Array)

  @called_from = if BusScheme.stack.empty? or !BusScheme.stack.last.respond_to? :called_as
                   '(top-level)'
                 else
                   BusScheme.stack.last.called_as
                 end
  
  locals.each { |k, v| immediate_set k, v }
end

Instance Attribute Details

#called_asObject (readonly)

Returns the value of attribute called_as


3
4
5
# File 'lib/stack_frame.rb', line 3

def called_as
  @called_as
end

#called_fromObject (readonly)

Returns the value of attribute called_from


3
4
5
# File 'lib/stack_frame.rb', line 3

def called_from
  @called_from
end

#fileObject (readonly)

Returns the value of attribute file


3
4
5
# File 'lib/stack_frame.rb', line 3

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line


3
4
5
# File 'lib/stack_frame.rb', line 3

def line
  @line
end

Instance Method Details

#[](symbol) ⇒ Object


30
31
32
33
34
35
36
# File 'lib/stack_frame.rb', line 30

def [](symbol)
  if immediate_has_key?(symbol)
    immediate_lookup(symbol)
  else
    @parent && @parent[symbol]
  end
end

#[]=(symbol, value) ⇒ Object


38
39
40
41
42
43
44
# File 'lib/stack_frame.rb', line 38

def []=(symbol, value)
  if !immediate_has_key?(symbol) and @parent && @parent.has_key?(symbol)
    @parent[symbol] = value
  else
    immediate_set symbol, value
  end
end

#filtered?Boolean

special forms should not be shown in stack traces neither should 'begin'

Returns:

  • (Boolean)

52
53
54
55
# File 'lib/stack_frame.rb', line 52

def filtered?
  return false unless @called_as
  @called_as.special_form or @called_as == :begin.sym
end

#has_key?(symbol) ⇒ Boolean

Just your regular hash stuff, only it takes the parent into account

Returns:

  • (Boolean)

26
27
28
# File 'lib/stack_frame.rb', line 26

def has_key?(symbol)
  immediate_has_key?(symbol) or @parent && @parent.has_key?(symbol)
end

#immediate_has_key?Object


21
# File 'lib/stack_frame.rb', line 21

alias_method :immediate_has_key?, :has_key?

#immediate_lookupObject


23
# File 'lib/stack_frame.rb', line 23

alias_method :immediate_lookup, :[]

#immediate_setObject


22
# File 'lib/stack_frame.rb', line 22

alias_method :immediate_set, :[]=

#traceObject


46
47
48
# File 'lib/stack_frame.rb', line 46

def trace
  "#{@file}:#{@line} in #{@called_from}" unless filtered?
end