Ejemplo n.º 1
0
 def evaluate(self, context):
     if self.op == 'or':
         if to_boolean(self.left.evaluate(context)):
             return True
         return to_boolean(self.right.evaluate(context))
     elif self.op == 'and':
         if not to_boolean(self.left.evaluate(context)):
             return False
         return to_boolean(self.right.evaluate(context))
     elif self.op in ['=', '!=', '<', '>', '<=', '>=']:
         return compare(self.op, self.left.evaluate(context),
                        self.right.evaluate(context))
     elif self.op in ['+', '-', '*', 'div', 'mod']:
         x = to_number(self.left.evaluate(context))
         y = to_number(self.right.evaluate(context))
         if self.op == '+':
             return x + y
         elif self.op == '-':
             return x - y
         elif self.op == '*':
             return x * y
         elif self.op == 'div':
             return x / y
         elif self.op == 'mod':
             z = abs(x) % abs(y)
             if x >= 0:
                 return z
             else:
                 return -z
         else:
             assert False
     elif self.op == '|':
         x = self.left.evaluate(context)
         y = self.right.evaluate(context)
         if is_node_set(x) and is_node_set(y):
             # XXX This is incorrect, because it neither preserves
             # document order nor removes duplicates.
             return join_node_sets(x, y)
         else:
             raise XPathEvaluationError, "Operands of '|' must be node sets."
     else:
         assert False
Ejemplo n.º 2
0
 def evaluate(self, context):
     assert self.op == '-'
     return - to_number(self.right.evaluate(context))