Commit 45ea0053 authored by Robert Bradshaw's avatar Robert Bradshaw

More operator overloading parsing/testing.

parent 650a248b
...@@ -2058,7 +2058,7 @@ def p_c_func_declarator(s, pos, ctx, base, cmethod_flag): ...@@ -2058,7 +2058,7 @@ def p_c_func_declarator(s, pos, ctx, base, cmethod_flag):
exception_value = exc_val, exception_check = exc_check, exception_value = exc_val, exception_check = exc_check,
nogil = nogil or ctx.nogil or with_gil, with_gil = with_gil) 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, def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
assignable, nonempty): assignable, nonempty):
...@@ -2113,9 +2113,12 @@ def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag, ...@@ -2113,9 +2113,12 @@ def p_c_simple_declarator(s, ctx, empty, is_type, cmethod_flag,
s.expect(']') s.expect(']')
op = '[]' op = '[]'
s.next() s.next()
if op in ['-', '+'] and s.sy == op: if op in ['-', '+', '|', '&'] and s.sy == op:
op = op*2 op = op*2
s.next() s.next()
if s.sy == '=':
op += s.sy
s.next()
if op not in supported_overloaded_operators: if op not in supported_overloaded_operators:
s.error("Overloading operator '%s' not yet supported." % op) s.error("Overloading operator '%s' not yet supported." % op)
name = name+op name = name+op
......
...@@ -20,13 +20,20 @@ cdef extern from "cpp_operators_helper.h": ...@@ -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)
char* operator<<(int)
char* operator>>(int)
def test_unops(): def test_unops():
""" """
>>> test_unops() >>> test_unops()
unary+ unary +
unary- unary -
unary~ unary ~
unary* unary *
""" """
cdef TestOps* t = new TestOps() cdef TestOps* t = new TestOps()
print +t[0] print +t[0]
...@@ -38,10 +45,10 @@ def test_unops(): ...@@ -38,10 +45,10 @@ def test_unops():
def test_incdec(): def test_incdec():
""" """
>>> test_incdec() >>> test_incdec()
unary++ unary ++
unary-- unary --
post++ post ++
post-- post --
""" """
cdef TestOps* t = new TestOps() cdef TestOps* t = new TestOps()
print cython.preincrement(t[0]) print cython.preincrement(t[0])
...@@ -53,11 +60,16 @@ def test_incdec(): ...@@ -53,11 +60,16 @@ def test_incdec():
def test_binop(): def test_binop():
""" """
>>> test_binop() >>> test_binop()
binary+ binary +
binary- binary -
binary* binary *
binary/ binary /
binary% binary %
binary &
binary |
binary ^
binary <<
binary >>
""" """
cdef TestOps* t = new TestOps() cdef TestOps* t = new TestOps()
print t[0] + 1 print t[0] + 1
...@@ -65,4 +77,11 @@ def test_binop(): ...@@ -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
print t[0] ^ 1
print t[0] << 1
print t[0] >> 1
del t del t
#define UN_OP(op) const char* operator op () { return "unary"#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 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 BIN_OP(op) const char* operator op (int x) { return "binary "#op; }
class TestOps { class TestOps {
...@@ -23,5 +23,12 @@ public: ...@@ -23,5 +23,12 @@ public:
BIN_OP(*); BIN_OP(*);
BIN_OP(/); BIN_OP(/);
BIN_OP(%); BIN_OP(%);
BIN_OP(<<);
BIN_OP(>>);
BIN_OP(|);
BIN_OP(&);
BIN_OP(^);
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment