Class: Skeem::SkmFrame
- Inherits:
-
Object
- Object
- Skeem::SkmFrame
- Defined in:
- lib/skeem/skm_frame.rb
Overview
A frame is a set of bindings of the form { String => SkmElement }. It associate to each identifier (as String) a Skeem object.
Instance Attribute Summary collapse
-
#bindings ⇒ Hash{String => SkmElement}
readonly
Map of variable names => values.
-
#parent ⇒ SkmFrame?
readonly
Link to parent frame (if any).
Instance Method Summary collapse
-
#add_binding(anIdentifier, anExpression) ⇒ Object
Add a binding to this frame.
-
#depth ⇒ Integer
The number of parents this frame has.
- #empty? ⇒ Boolean
-
#fetch(anIdentifier) ⇒ SkmElement
Retrieve the value of an existing variable.
-
#include?(anIdentifier) ⇒ Boolean
Tell the value of an existing variable.
-
#initialize(parentFrame = nil) ⇒ SkmFrame
constructor
Constructor.
- #initialize_copy(original) ⇒ Object
- #inspect ⇒ String
- #size ⇒ Integer
-
#update_binding(anIdentifier, anExpression) ⇒ Object
Update the value of an existing variable.
Constructor Details
#initialize(parentFrame = nil) ⇒ SkmFrame
Constructor
17 18 19 20 |
# File 'lib/skeem/skm_frame.rb', line 17 def initialize(parentFrame = nil) @bindings = {} @parent = parentFrame end |
Instance Attribute Details
#bindings ⇒ Hash{String => SkmElement} (readonly)
Returns Map of variable names => values.
13 14 15 |
# File 'lib/skeem/skm_frame.rb', line 13 def bindings @bindings end |
#parent ⇒ SkmFrame? (readonly)
Returns Link to parent frame (if any).
10 11 12 |
# File 'lib/skeem/skm_frame.rb', line 10 def parent @parent end |
Instance Method Details
#add_binding(anIdentifier, anExpression) ⇒ Object
Add a binding to this frame. There is no check that the variable name is already in use.
33 34 35 |
# File 'lib/skeem/skm_frame.rb', line 33 def add_binding(anIdentifier, anExpression) bind(valid_identifier(anIdentifier), anExpression) end |
#depth ⇒ Integer
The number of parents this frame has.
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/skeem/skm_frame.rb', line 95 def depth count = 0 curr_frame = self while curr_frame.parent count += 1 curr_frame = curr_frame.parent end count end |
#empty? ⇒ Boolean
76 77 78 79 80 81 82 83 |
# File 'lib/skeem/skm_frame.rb', line 76 def empty? my_result = bindings.empty? if my_result && parent my_result = parent.empty? end my_result end |
#fetch(anIdentifier) ⇒ SkmElement
Retrieve the value of an existing variable.
52 53 54 55 56 57 58 59 60 |
# File 'lib/skeem/skm_frame.rb', line 52 def fetch(anIdentifier) variable = valid_identifier(anIdentifier) found = bindings[variable] if found.nil? && parent found = parent.fetch(variable) end found end |
#include?(anIdentifier) ⇒ Boolean
Tell the value of an existing variable.
65 66 67 68 69 70 71 72 73 |
# File 'lib/skeem/skm_frame.rb', line 65 def include?(anIdentifier) variable = valid_identifier(anIdentifier) my_result = bindings.include?(variable) if my_result == false && parent my_result = parent.include?(variable) end my_result end |
#initialize_copy(original) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/skeem/skm_frame.rb', line 22 def initialize_copy(original) @bindings = {} original.bindings.each_pair do |var, val| add_binding(var.dup, val.dup) end end |
#inspect ⇒ String
108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/skeem/skm_frame.rb', line 108 def inspect result = '' if parent result << parent.inspect else return "\n" end result << "\n----\n" bindings.each_pair do |key, expr| result << "#{key.inspect} => #{expr.inspect}\n" end result end |
#size ⇒ Integer
86 87 88 89 90 91 |
# File 'lib/skeem/skm_frame.rb', line 86 def size my_result = bindings.size my_result += parent.size if parent my_result end |
#update_binding(anIdentifier, anExpression) ⇒ Object
Update the value of an existing variable.
40 41 42 43 44 45 46 47 |
# File 'lib/skeem/skm_frame.rb', line 40 def update_binding(anIdentifier, anExpression) variable = valid_identifier(anIdentifier) if bindings.include?(variable) bind(variable, anExpression) elsif parent parent.update_binding(variable, anExpression) end end |