Class: Skeem::SkmFrame

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(parentFrame = nil) ⇒ SkmFrame

Constructor

Parameters:

  • parentFrame (SkmFrame) (defaults to: nil)

    Parent frame of this one.



17
18
19
20
# File 'lib/skeem/skm_frame.rb', line 17

def initialize(parentFrame = nil)
  @bindings = {}
  @parent = parentFrame
end

Instance Attribute Details

#bindingsHash{String => SkmElement} (readonly)

Returns Map of variable names => values.

Returns:

  • (Hash{String => SkmElement})

    Map of variable names => values.



13
14
15
# File 'lib/skeem/skm_frame.rb', line 13

def bindings
  @bindings
end

#parentSkmFrame? (readonly)

Returns Link to parent frame (if any).

Returns:

  • (SkmFrame, nil)

    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.

Parameters:

  • anIdentifier (String, SkmIdentifier)

    The variable name

  • anExpression (SkmElement)

    The Skeem expression to bind



33
34
35
# File 'lib/skeem/skm_frame.rb', line 33

def add_binding(anIdentifier, anExpression)
  bind(valid_identifier(anIdentifier), anExpression)
end

#depthInteger

The number of parents this frame has.

Returns:

  • (Integer)

    The nesting levels



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

Returns:

  • (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.

Parameters:

Returns:



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.

Parameters:

Returns:

  • (Boolean)


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

#inspectString

Returns:

  • (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

#sizeInteger

Returns:

  • (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.

Parameters:

  • anIdentifier (String, SkmIdentifier)

    The variable name.

  • anExpression (SkmElement)

    The Skeem expression to bind



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