C++ cmp operators.
authorRobert Bradshaw <robertwb@math.washington.edu>
Fri, 15 Jan 2010 02:24:31 +0000 (18:24 -0800)
committerRobert Bradshaw <robertwb@math.washington.edu>
Fri, 15 Jan 2010 02:24:31 +0000 (18:24 -0800)
Cython/Compiler/ExprNodes.py
Cython/Compiler/Parsing.py
tests/run/cpp_operators.pyx
tests/run/cpp_operators_helper.h

index 4f57ebe8fcb4922313adec12520535909befd96a..afbf2f8e4747799fb4338267a6af8ab6136ecdc8 100755 (executable)
@@ -5303,9 +5303,9 @@ class CmpNode(object):
     def is_cpp_comparison(self):
         type1 = self.operand1.type
         type2 = self.operand2.type
-        if type1.is_ptr:
+        if type1.is_reference:
             type1 = type1.base_type
-        if type2.is_ptr:
+        if type2.is_reference:
             type2 = type2.base_type
         return type1.is_cpp_class or type2.is_cpp_class
 
@@ -5569,6 +5569,9 @@ class PrimaryCmpNode(ExprNode, CmpNode):
         self.operand2.analyse_types(env)
         if self.is_cpp_comparison():
             self.analyse_cpp_comparison(env)
+            if self.cascade:
+                error(self.pos, "Cascading comparison not yet supported for cpp types.")
+            return
         if self.cascade:
             self.cascade.analyse_types(env)
 
@@ -5601,9 +5604,9 @@ class PrimaryCmpNode(ExprNode, CmpNode):
     def analyse_cpp_comparison(self, env):
         type1 = self.operand1.type
         type2 = self.operand2.type
-        if type1.is_ptr:
+        if type1.is_reference:
             type1 = type1.base_type
-        if type2.is_ptr:
+        if type2.is_reference:
             type2 = type2.base_type
         entry = env.lookup(type1.name)
         function = entry.type.scope.lookup("operator%s" % self.operator)
@@ -5611,12 +5614,12 @@ class PrimaryCmpNode(ExprNode, CmpNode):
             error(self.pos, "Invalid types for '%s' (%s, %s)" %
                 (self.operator, type1, type2))
             return
-        entry = PyrexTypes.best_match([self.operand1, self.operand2], function.all_alternatives(), self.pos)
+        entry = PyrexTypes.best_match([self.operand2], function.all_alternatives(), self.pos)
         if entry is None:
             self.type = PyrexTypes.error_type
             self.result_code = "<error>"
             return
-        if (entry.type.is_ptr):
+        if entry.type.is_ptr:
             self.type = entry.type.base_type.return_type
         else:
             self.type = entry.type.return_type
index a683ed81b89d04d58d8597332fbc9fd73a9ca40a..64102410d4f7931e3df76439d5e5c74fdeea87c4 100644 (file)
@@ -2058,7 +2058,11 @@ def p_c_func_declarator(s, pos, ctx, base, cmethod_flag):
         exception_value = exc_val, exception_check = exc_check,
         nogil = nogil or ctx.nogil or with_gil, with_gil = with_gil)
 
-supported_overloaded_operators = set(['+', '-', '*', '/', '%', '++', '--', '~', '|', '&', '^', '<<', '>>' ])
+supported_overloaded_operators = set([
+    '+', '-', '*', '/', '%', 
+    '++', '--', '~', '|', '&', '^', '<<', '>>',
+    '==', '!=', '>=', '>', '<=', '<',
+])
 
 def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
                           assignable, nonempty):
index 380f3f7f089c65b5e0ea3bd289d38a6116c88d69..d1b8500765bfd3be69da22fb2f8e0d0c900d2efe 100644 (file)
@@ -27,6 +27,13 @@ cdef extern from "cpp_operators_helper.h":
         char* operator<<(int)
         char* operator>>(int)
 
+        char* operator==(int)
+        char* operator!=(int)
+        char* operator>=(int)
+        char* operator<=(int)
+        char* operator>(int)
+        char* operator<(int)
+
 def test_unops():
     """
     >>> test_unops()
@@ -85,3 +92,22 @@ def test_binop():
     print t[0] << 1
     print t[0] >> 1
     del t
+
+def test_cmp():
+    """
+    >>> test_cmp()
+    binary ==
+    binary !=
+    binary >=
+    binary >
+    binary <=
+    binary <
+    """
+    cdef TestOps* t = new TestOps()
+    print t[0] == 1
+    print t[0] != 1
+    print t[0] >= 1
+    print t[0] > 1
+    print t[0] <= 1
+    print t[0] < 1
+    del t
index 769f3e7c6b3c11073c65a0e2c874bd255ac78e0f..f85218e1dff445fd095c8aef0ee6d78351dabd15 100644 (file)
@@ -30,5 +30,12 @@ public:
     BIN_OP(|);
     BIN_OP(&);
     BIN_OP(^);
-    
+
+    BIN_OP(==);
+    BIN_OP(!=);
+    BIN_OP(<=);
+    BIN_OP(<);
+    BIN_OP(>=);
+    BIN_OP(>);
+
 };