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):
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
......
......@@ -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
#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(^);
};
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