From 45ea005302fe449923b9a08f825049a34ae28771 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Thu, 14 Jan 2010 17:54:55 -0800 Subject: [PATCH] More operator overloading parsing/testing. --- Cython/Compiler/Parsing.py | 7 +++-- tests/run/cpp_operators.pyx | 45 +++++++++++++++++++++++--------- tests/run/cpp_operators_helper.h | 13 ++++++--- 3 files changed, 47 insertions(+), 18 deletions(-) diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index 9d0ffecb..a683ed81 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -2058,7 +2058,7 @@ 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): @@ -2113,9 +2113,12 @@ def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag, s.expect(']') op = '[]' s.next() - if op in ['-', '+'] and s.sy == op: + if op in ['-', '+', '|', '&'] and s.sy == op: op = op*2 s.next() + if s.sy == '=': + op += s.sy + s.next() if op not in supported_overloaded_operators: s.error("Overloading operator '%s' not yet supported." % op) name = name+op diff --git a/tests/run/cpp_operators.pyx b/tests/run/cpp_operators.pyx index 338b355f..380f3f7f 100644 --- a/tests/run/cpp_operators.pyx +++ b/tests/run/cpp_operators.pyx @@ -20,13 +20,20 @@ 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) + def test_unops(): """ >>> test_unops() - unary+ - unary- - unary~ - unary* + unary + + unary - + unary ~ + unary * """ cdef TestOps* t = new TestOps() print +t[0] @@ -38,10 +45,10 @@ def test_unops(): def test_incdec(): """ >>> test_incdec() - unary++ - unary-- - post++ - post-- + unary ++ + unary -- + post ++ + post -- """ cdef TestOps* t = new TestOps() print cython.preincrement(t[0]) @@ -53,11 +60,16 @@ def test_incdec(): def test_binop(): """ >>> test_binop() - binary+ - binary- - binary* - binary/ - binary% + binary + + binary - + binary * + binary / + binary % + binary & + binary | + binary ^ + binary << + binary >> """ cdef TestOps* t = new TestOps() print t[0] + 1 @@ -65,4 +77,11 @@ def test_binop(): print t[0] * 1 print t[0] / 1 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 diff --git a/tests/run/cpp_operators_helper.h b/tests/run/cpp_operators_helper.h index 545266af..769f3e7c 100644 --- a/tests/run/cpp_operators_helper.h +++ b/tests/run/cpp_operators_helper.h @@ -1,6 +1,6 @@ -#define UN_OP(op) const char* operator op () { return "unary"#op; } -#define POST_UN_OP(op) const char* operator op (int x) { return "post"#op; } -#define BIN_OP(op) const char* operator op (int x) { return "binary"#op; } +#define UN_OP(op) const char* operator op () { return "unary "#op; } +#define POST_UN_OP(op) const char* operator op (int x) { return "post "#op; } +#define BIN_OP(op) const char* operator op (int x) { return "binary "#op; } class TestOps { @@ -23,5 +23,12 @@ public: BIN_OP(*); BIN_OP(/); BIN_OP(%); + + BIN_OP(<<); + BIN_OP(>>); + + BIN_OP(|); + BIN_OP(&); + BIN_OP(^); }; -- 2.26.2