Top Level Namespace

Defined Under Namespace

Modules: AbstractClass, RLTK Classes: Class, FalseClass, Integer, Object, TrueClass

Instance Method Summary collapse

Instance Method Details

#check_array_type(array, type, blame = nil, strict = false) ⇒ Object

A helper method for type checking Ruby array values.

Parameters:

  • array (Array<Object>)

    Array of objects to type check.

  • type (Class)

    Class the objects should be an instance of.

  • blame (String, nil) (defaults to: nil)

    Variable name to blame for failed type checks.

  • strict (Boolean) (defaults to: false)

    Strict or non-strict checking. Uses ‘instance_of?` and `is_a?` respectively.

Returns:

  • (Object)

    The object passed in parameter o.

Raises:

  • (ArgumentError)

    An error is raise if the type checking fails.



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rltk/util/monkeys.rb', line 48

def check_array_type(array, type, blame = nil, strict = false)
	array.each do |o|
		type_ok = if strict then o.instance_of?(type) else o.is_a?(type) end
		
		if not type_ok
			if blame
				raise ArgumentError, "Parameter #{blame} must contain instances of the #{type.name} class."
			else
				raise ArgumentError, "Expected an object of type #{type.name}."
			end
		end
	end
end

#check_cg_array_type(array, type = RLTK::CG::Type, blame = 'el_types', strict = false) ⇒ Array<Type>

This helper function checks to make sure that an array of objects are all sub-classses of Type or instances of a sub-class of Type. If a class is present in the array parameter it is expected to be a singleton class and will be instantiated via the instance method.

Parameters:

  • array (Array<Type, Class>)

    Array of objects to type check for code generation type.

  • type (Type) (defaults to: RLTK::CG::Type)

    Class the objects should be an instance (or sub-class) of.

  • blame (String) (defaults to: 'el_types')

    Variable name to blame for failed type checks.

  • strict (Boolean) (defaults to: false)

    Strict or non-strict checking. Uses ‘instance_of?` and `is_a?` respectively.

Returns:

  • (Array<Type>)

    An array containing the objects in array with any singleton classes replaced by their instances.

Raises:

  • (ArgumentError)

    An error is raise if a class is passed in array that hasn’t included the Singleton class, if the class passed in parameter type isn’t a sub-class of Type, or if the type check fails.



485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# File 'lib/rltk/cg/type.rb', line 485

def check_cg_array_type(array, type = RLTK::CG::Type, blame = 'el_types', strict = false)
	array.map do |o|
		if o.is_a?(Class)
			type_ok = if strict then o == type else o.subclass_of?(type) end
			
			if type_ok
				if o.includes_module?(Singleton)
					o.instance
				else
					raise ArgumentError, "The #{o.name} class (passed in parameter #{blame}) must be instantiated directly."
				end
			else
				raise ArgumentError, "The #{o.name} class (passed in parameter #{blame}) does not inherit from the #{type.name} class."
			end
			
		else
			type_ok = if strict then o.instance_of(type) else o.is_a?(type) end
			
			if type_ok
				o
			else
				raise ArgumentError, "Parameter #{blame} must contain instances of the #{type.name} class."
			end
		end
	end
end

#check_cg_type(o, type = RLTK::CG::Type, blame = 'type', strict = false) ⇒ Type

This helper function checks to make sure that an object is a sub-class of Type or an instance of a sub-class of Type. If a class is passed in the o parameter it is expected to be a singleton class and will be instantiated via the instance method.

Parameters:

  • o (Type, Class)

    Object to type check for code generation type.

  • type (Type) (defaults to: RLTK::CG::Type)

    Class the object should be an instance (or sub-class) of.

  • blame (String) (defaults to: 'type')

    Variable name to blame for failed type checks.

  • strict (Boolean) (defaults to: false)

    Strict or non-strict checking. Uses ‘instance_of?` and `is_a?` respectively.

Returns:

  • (Type)

    The object o or an instance of the class passed in parameter o.

Raises:

  • (ArgumentError)

    An error is raise if a class is passed in parameter o that hasn’t included the Singleton class, if the class passed in parameter type isn’t a sub-class of Type, or if the type check fails.



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/rltk/cg/type.rb', line 451

def check_cg_type(o, type = RLTK::CG::Type, blame = 'type', strict = false)
	if o.is_a?(Class)
		type_ok = if strict then o == type else o.subclass_of?(type) end 
		
		if type_ok
			if o.includes_module?(Singleton)
				o.instance
			else
				raise ArgumentError, "The #{o.name} class (passed as parameter #{blame}) must be instantiated directly."
			end
		else
			raise ArgumentError, "The #{o.name} class (passed as parameter #{blame} does not inherit from the #{type.name} class." 
		end
	else
		check_type(o, type, blame, strict)
	end
end

#check_type(o, type, blame = nil, strict = false) ⇒ Object

A helper method for type checking Ruby values.

Parameters:

  • o (Object)

    Object to type check.

  • type (Class)

    Class the object should be an instance of.

  • blame (String, nil) (defaults to: nil)

    Variable name to blame for failed type checks.

  • strict (Boolean) (defaults to: false)

    Strict or non-strict checking. Uses ‘instance_of?` and `is_a?` respectively.

Returns:

  • (Object)

    The object passed as parameter o.

Raises:

  • (ArgumentError)

    An error is raise if the type checking fails.



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rltk/util/monkeys.rb', line 24

def check_type(o, type, blame = nil, strict = false)
	type_ok = if strict then o.instance_of?(type) else o.is_a?(type) end
	
	if type_ok
		o
	else
		if blame
			raise ArgumentError, "Parameter #{blame} must be an instance of the #{type.name} class.  Received an instance of #{o.class.name}."
		else
			raise ArgumentError, "Expected an object of type #{type.name}.  Received an instance of #{o.class.name}."
		end
	end
end

#make_ptr_to_elements(size_or_values, &block) ⇒ FFI::MemoryPointer

A helper function for creating constant array, vector, and struct types. This method should never be used by library users.

Parameters:

  • size_or_values (Array<RLTK::CG::Value>, Integer)

    Number of values or array of values.

  • block (Proc)

    Block evaluated if size is specified.

Returns:

  • (FFI::MemoryPointer)

    An array of pointers to LLVM Values.



1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
# File 'lib/rltk/cg/value.rb', line 1217

def make_ptr_to_elements(size_or_values, &block)
	values =
	case size_or_values
	when Integer
		raise ArgumentError, 'Block not given.' if not block_given?
		
		::Array.new(size_or_values, &block)
	else
		size_or_values
	end
	
	returning(FFI::MemoryPointer.new(:pointer, values.size)) do |ptr|
		ptr.write_array_of_pointer(values)
	end
end