Class: Copper::Comparison

Inherits:
CopperNode
  • Object
show all
Defined in:
lib/copper/comparison.rb

Instance Method Summary collapse

Instance Method Details

#value(vars = {}) ⇒ Object

Raises:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/copper/comparison.rb', line 4

def value(vars = {})
	lhs = elements[0].value(vars)
	comp_op = elements[1].value(vars)
	rhs = elements[2].value(vars)

	raise ParseError, "cannot compare nil" if rhs.nil? || lhs.nil?

	puts "[DEBUG] Comparing #{lhs} (#{lhs.class.name}) #{comp_op} #{rhs} (#{rhs.class.name})" if $debug
	begin
		case comp_op
		when '='
			return equality(lhs, rhs)
		when '=='
			return equality(lhs, rhs)
		when '>'
			return lhs > rhs
		when '<'
			return lhs < rhs
		when '<='
			return lhs <= rhs
		when '>='
			return lhs > rhs
		when '!='
			return lhs != rhs
		when 'in'
			# this depends on the types
			rhs_class = ::Copper::DataTypes::DataType.get_class(rhs.class.name)
			rhs_obj = rhs_class.new(rhs)
			if rhs_obj.respond_to?(:in)
				return rhs_obj.in(lhs)
			else
				raise ::Copper::RuntimeError, "in is not a valid comparision on #{rhs_class.name}"
			end

		when '->'
			# this is a special case to pipe lhs to console
			console(lhs)
		end
	rescue NoMethodError => exc
		raise ParseError, "comparison error: #{exc.message}"
	end
end