Class: Apricot::FnScope

Inherits:
Scope show all
Defined in:
lib/apricot/scopes.rb

Instance Attribute Summary collapse

Attributes inherited from Scope

#loop_label, #parent, #variables

Instance Method Summary collapse

Constructor Details

#initialize(parent, name) ⇒ FnScope

Returns a new instance of FnScope.



62
63
64
65
66
67
68
69
70
# File 'lib/apricot/scopes.rb', line 62

def initialize(parent, name)
  super(parent)

  if name
    @name = name
    name_slot = @parent.store_new_local(name)
    @self_reference = LocalReference.new(name_slot, 1)
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



60
61
62
# File 'lib/apricot/scopes.rb', line 60

def name
  @name
end

#self_referenceObject (readonly)

Returns the value of attribute self_reference.



60
61
62
# File 'lib/apricot/scopes.rb', line 60

def self_reference
  @self_reference
end

Instance Method Details

#find_recur_targetObject

A (recur) is looking for a recursion target (ie. a loop or a fn overload scope).



84
85
86
# File 'lib/apricot/scopes.rb', line 84

def find_recur_target
  @parent.find_recur_target
end

#find_var(name, depth = 0) ⇒ Object

An identifier or a nested scope is looking up a variable. If the variable is found here, return a reference to it. Otherwise look it up on the parent and increment its depth because it is beyond the bounds of the current block of code (fn).



76
77
78
79
80
# File 'lib/apricot/scopes.rb', line 76

def find_var(name, depth = 0)
  return @self_reference if name == @name

  @parent.find_var(name, depth + 1)
end