Class: Skeem::SkmBindingBlock

Inherits:
SkmUnaryExpression show all
Defined in:
lib/skeem/skm_unary_expression.rb

Overview

Used to represent local binding constructs (let, let*, letrec, letrec*)

Instance Attribute Summary collapse

Attributes inherited from SkmUnaryExpression

#child

Attributes inherited from SkmElement

#position

Instance Method Summary collapse

Methods inherited from SkmUnaryExpression

#accept

Methods inherited from SkmElement

#accept, #boolean?, #bound!, #callable?, #char?, #complex?, #done!, #eqv?, #inspect, #integer?, #list?, #null?, #number?, #pair?, #procedure?, #quasiquote, #quoted!, #rational?, #real?, #skm_eq?, #skm_equal?, #string?, #symbol?, #unquoted!, #vector?, #verbatim?

Constructor Details

#initialize(theKind, theBindings, aBody) ⇒ SkmBindingBlock

Returns a new instance of SkmBindingBlock.



123
124
125
126
127
# File 'lib/skeem/skm_unary_expression.rb', line 123

def initialize(theKind, theBindings, aBody)
  @kind = theKind
  @bindings = theBindings
  super(nil, aBody)
end

Instance Attribute Details

#bindingsArray<SkmBinding> (readonly)

Returns:



121
122
123
# File 'lib/skeem/skm_unary_expression.rb', line 121

def bindings
  @bindings
end

#kindSymbol (readonly)

Returns One of: :let, :let_star.

Returns:

  • (Symbol)

    One of: :let, :let_star



118
119
120
# File 'lib/skeem/skm_unary_expression.rb', line 118

def kind
  @kind
end

Instance Method Details

#evaluate(aRuntime) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/skeem/skm_unary_expression.rb', line 129

def evaluate(aRuntime)
  aRuntime.push(SkmFrame.new(aRuntime.environment))
  case kind
  when :let
    locals = bindings.map do |bnd|
      SkmBinding.new(bnd.variable, bnd.value.evaluate(aRuntime))
    end
    locals.each do |bnd|
      aRuntime.add_binding(bnd.variable.evaluate(aRuntime), bnd.value)
    end
  when :let_star
    bindings.each do |bnd|
      val = bnd.value.evaluate(aRuntime)
      aRuntime.add_binding(bnd.variable.evaluate(aRuntime), val)
    end
  end

  unless body[:defs].empty?
    body[:defs].each do |dfn|
      dfn.evaluate(aRuntime)
    end
  end

  # $stderr.puts "Environment SkmBindingBlock#evaluate:" + aRuntime.environment.inspect
  raw_result = body[:sequence].evaluate(aRuntime)
  result = raw_result.kind_of?(SkmPair) ? raw_result.last : raw_result
  result = result.doppelganger(aRuntime) if result.kind_of?(SkmLambda)

  aRuntime.pop
  # $stderr.puts "Result SkmBindingBlock#evaluate: " + result.inspect
  result
end