lib/ddql/operator.rb in ddql-1.0.0 vs lib/ddql/operator.rb in ddql-1.0.1
- old
+ new
@@ -1,19 +1,22 @@
module DDQL
class Operator
- attr_reader :associativity, :symbol, :name, :type, :precedence, :return_type, :ordinal
+ attr_reader :associativity, :symbol, :name, :type, :precedence, :return_type
- def initialize(symbol, name, type, precedence, right, return_type, ordinal=0)
+ def initialize(symbol, name, type, precedence, right, return_type)
@symbol = symbol
@name = name
@type = type
@precedence = precedence
@associativity = right ? :right : :left
@return_type = return_type
- @ordinal = ordinal
end
+ def any_type?
+ return_type == :any
+ end
+
def boolean?
return_type == :boolean
end
def comparison?
@@ -116,14 +119,18 @@
left.merge(boolean_operator: op[:op]).merge(right)
end
def parse_infix(parser, token, expression)
precedence = self.precedence
- precedence -= 1 if right?
+ precedence -= 0.05 if right?
next_expression = parser.parse(precedence: precedence)
op_expression = token.as_hash
+ if math? && next_expression&.dig(:left, :factor) && next_expression.size == 1
+ return op_expression.merge(left: expression, right: next_expression[:left])
+ end
+
if token.and? || token.or?
return boolean_stmt(expression, op_expression, next_expression)
end
if expression.key?(:op_not) || next_expression.key?(:op_not)
@@ -160,9 +167,12 @@
next_expression = parser.parse(precedence: precedence)
if next_expression.key?(:lstatement) && !next_expression.key?(:rstatement)
next_expression = next_expression[:lstatement]
elsif next_expression.key?(:rstatement) && !next_expression.key?(:lstatement)
next_expression = next_expression[:rstatement]
+ elsif op_expression[:op].key?(:op_not) && next_expression.key?(:op_not)
+ next_expression.delete(:op_not)
+ return next_expression
end
op_expression[:op].merge(next_expression)
end
def redundant?(expression, *keys)