2009-04-24 13:48:42 -07:00
|
|
|
require 'set'
|
2008-10-12 19:03:06 -07:00
|
|
|
require 'sass/script/string'
|
|
|
|
require 'sass/script/number'
|
|
|
|
require 'sass/script/color'
|
|
|
|
require 'sass/script/functions'
|
|
|
|
require 'sass/script/unary_operation'
|
2006-12-22 10:28:17 +00:00
|
|
|
|
2008-10-12 19:03:06 -07:00
|
|
|
module Sass::Script
|
2009-04-24 20:49:32 -07:00
|
|
|
# A SassScript parse node representing a binary operation,
|
2009-04-24 13:48:42 -07:00
|
|
|
# such as `!a + !b` or `"foo" + 1`.
|
2009-04-25 02:00:36 -07:00
|
|
|
class Operation < Node
|
|
|
|
# @param operand1 [Script::Node] The parse-tree node
|
2009-04-24 20:56:12 -07:00
|
|
|
# for the right-hand side of the operator
|
2009-04-25 02:00:36 -07:00
|
|
|
# @param operand2 [Script::Node] The parse-tree node
|
2009-04-24 20:56:12 -07:00
|
|
|
# for the left-hand side of the operator
|
2009-04-24 13:48:42 -07:00
|
|
|
# @param operator [Symbol] The operator to perform.
|
|
|
|
# This should be one of the binary operator names in {Lexer::OPERATORS}
|
2007-01-28 17:24:08 +00:00
|
|
|
def initialize(operand1, operand2, operator)
|
2006-12-23 22:32:05 +00:00
|
|
|
@operand1 = operand1
|
|
|
|
@operand2 = operand2
|
|
|
|
@operator = operator
|
2006-12-22 10:28:17 +00:00
|
|
|
end
|
2008-04-07 23:09:17 -07:00
|
|
|
|
2009-04-24 13:48:42 -07:00
|
|
|
# @return [String] A human-readable s-expression representation of the operation
|
2008-06-01 15:10:02 -07:00
|
|
|
def inspect
|
|
|
|
"(#{@operator.inspect} #{@operand1.inspect} #{@operand2.inspect})"
|
|
|
|
end
|
|
|
|
|
2009-04-24 13:48:42 -07:00
|
|
|
# Evaluates the operation.
|
|
|
|
#
|
|
|
|
# @param environment [Sass::Environment] The environment in which to evaluate the SassScript
|
|
|
|
# @return [Literal] The SassScript object that is the value of the operation
|
|
|
|
# @raise [Sass::SyntaxError] if the operation is undefined for the operands
|
2008-10-12 20:26:56 -07:00
|
|
|
def perform(environment)
|
|
|
|
literal1 = @operand1.perform(environment)
|
|
|
|
literal2 = @operand2.perform(environment)
|
2006-12-23 22:32:05 +00:00
|
|
|
begin
|
|
|
|
literal1.send(@operator, literal2)
|
2006-12-23 22:57:31 +00:00
|
|
|
rescue NoMethodError => e
|
2007-02-10 02:55:55 +00:00
|
|
|
raise e unless e.name.to_s == @operator.to_s
|
2008-04-19 10:16:10 -07:00
|
|
|
raise Sass::SyntaxError.new("Undefined operation: \"#{literal1} #{@operator} #{literal2}\".")
|
2006-12-23 22:32:05 +00:00
|
|
|
end
|
2006-12-22 10:28:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|