Class: Dhallish::Ast::ListNode

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(list, type_node) ⇒ ListNode

Returns a new instance of ListNode.



286
287
288
289
# File 'lib/ast.rb', line 286

def initialize(list, type_node)
	@list = list
	@type_node = type_node
end

Instance Attribute Details

#listObject

Returns the value of attribute list.



283
284
285
# File 'lib/ast.rb', line 283

def list
  @list
end

#type_nodeObject

Returns the value of attribute type_node.



284
285
286
# File 'lib/ast.rb', line 284

def type_node
  @type_node
end

Instance Method Details

#compute_type(ctx) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/ast.rb', line 291

def compute_type(ctx)
	elem_type = nil
	if !@type_node.nil?
		annot_type = @type_node.compute_type ctx
		assert ("Annotated Type not a type") { annot_type.is_a? Types::Type }
		elem_type = annot_type.
	end

	if @list.length > 0
		if !elem_type.nil?
			assert ("First list element's type mismatches annotated type") { list[0].compute_type(ctx) == elem_type }
		else
			elem_type = list[0].compute_type(ctx)
		end

		@list.each_with_index { |e, idx|
			t = e.compute_type(ctx)
			assert ("Type mismatch: element at index #{idx} has type #{t}, expected #{elem_type}") { t == elem_type }
		}
	end

	Types::List.new elem_type
end

#evaluate(ctx) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/ast.rb', line 315

def evaluate(ctx)
	res = []
	list.each { |node|
		res.append node.evaluate(ctx)
	}
	res
end