Class: Dhallish::Ast::LetInNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ast.rb

Overview

(let varname [: expected_type] = varval)+ in … vars shall be [[varname1, expected_type1, varval1], …] where expected_type can be nil if no type was given

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars, inexpr) ⇒ LetInNode

Returns a new instance of LetInNode.



156
157
158
159
# File 'lib/ast.rb', line 156

def initialize(vars, inexpr)
  @vars = vars
  @inexpr = inexpr
end

Instance Attribute Details

#inexprObject

Returns the value of attribute inexpr.



155
156
157
# File 'lib/ast.rb', line 155

def inexpr
  @inexpr
end

#varsObject

Returns the value of attribute vars.



154
155
156
# File 'lib/ast.rb', line 154

def vars
  @vars
end

Instance Method Details

#compute_type(ctx) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ast.rb', line 161

def compute_type(ctx)
  new_ctx = Context.new ctx
  @vars.each { |decl|
    name, annot_type_expr, val = decl

    act_type = val.compute_type(new_ctx)

    if !annot_type_expr.nil?
      type_type = annot_type_expr.compute_type(new_ctx)
      assert ("not a type after type annotation") { type_type.is_a? Types::Type }
      annot_type = type_type.
      unification = Types::unification(annot_type, act_type)
      assert ("Actual type of #{name}'s value (#{act_type}) doesn't match annotated type (#{annot_type})") { !unification.nil? }
    end

    new_ctx[name] = act_type
  }

  @inexpr.compute_type new_ctx
end

#evaluate(ctx) ⇒ Object



182
183
184
185
186
187
188
189
190
# File 'lib/ast.rb', line 182

def evaluate(ctx)
  newctx = Context.new(ctx)
  vars.each { |elm|
    varname, expected_type_ast, expr = elm
    val = expr.evaluate newctx
    newctx[varname] = val
  }
  inexpr.evaluate newctx
end