Commit f53e00e3 authored by Robert Bradshaw's avatar Robert Bradshaw

Make sure new can be used as a non-keyword.

parent c176a809
...@@ -300,7 +300,7 @@ def p_yield_expression(s): ...@@ -300,7 +300,7 @@ def p_yield_expression(s):
#power: atom trailer* ('**' factor)* #power: atom trailer* ('**' factor)*
def p_power(s): def p_power(s):
if s.systring == 'new': if s.systring == 'new' and s.peek()[0] == 'IDENT':
return p_new_expr(s) return p_new_expr(s)
n1 = p_atom(s) n1 = p_atom(s)
while s.sy in ('(', '[', '.'): while s.sy in ('(', '[', '.'):
......
...@@ -2194,14 +2194,34 @@ modifiers_and_name_to_type = { ...@@ -2194,14 +2194,34 @@ modifiers_and_name_to_type = {
(1, 0, "bint"): c_bint_type, (1, 0, "bint"): c_bint_type,
} }
def is_promotion(type, other_type): def is_promotion0(src_type, dst_type):
if type.is_numeric and other_type.is_numeric: if src_type.is_numeric and dst_type.is_numeric:
return (type.is_int and type.is_int and type.signed == other_type.signed) \ if src_type.is_int and dst_type.is_int:
or (type.is_float and other_type.is_float) \ if src_type.is_enum:
or (type.is_enum and other_type.is_int) return True
elif src_type.signed:
return dst_type.signed and src_type.rank <= dst_type.rank
elif dst_type.signed: # and not src_type.signed
src_type.rank < dst_type.rank
else:
return src_type.rank <= dst_type.rank
elif src_type.is_float and dst_type.is_float:
return src_type.rank <= dst_type.rank
else:
return False
else: else:
return False return False
def is_promotion(src_type, dst_type):
# It's hard to find a hard definition of promotion, but empirical
# evidence suggests that the below is all that's allowed.
if src_type.is_numeric:
if dst_type.same_as(c_int_type):
return src_type.is_enum or (src_type.is_int and (not src_type.signed) + src_type.rank < dst_type.rank)
elif dst_type.same_as(c_double_type):
return src_type.is_float and src_type.rank <= dst_type.rank
return False
def best_match(args, functions, pos=None): def best_match(args, functions, pos=None):
""" """
Finds the best function to be called Finds the best function to be called
......
...@@ -473,6 +473,14 @@ class PyrexScanner(Scanner): ...@@ -473,6 +473,14 @@ class PyrexScanner(Scanner):
t = "%s %s" % (self.sy, self.systring) t = "%s %s" % (self.sy, self.systring)
print("--- %3d %2d %s" % (line, col, t)) print("--- %3d %2d %s" % (line, col, t))
def peek(self):
saved = self.sy, self.systring
self.next()
next = self.sy, self.systring
self.unread(*next)
self.sy, self.systring = saved
return next
def put_back(self, sy, systring): def put_back(self, sy, systring):
self.unread(self.sy, self.systring) self.unread(self.sy, self.systring)
self.sy = sy self.sy = sy
......
...@@ -470,7 +470,7 @@ class Scope(object): ...@@ -470,7 +470,7 @@ class Scope(object):
if visibility == 'extern' and entry.visibility == 'extern': if visibility == 'extern' and entry.visibility == 'extern':
if self.is_cpp(): if self.is_cpp():
temp = self.add_cfunction(name, type, pos, cname, visibility, modifiers) temp = self.add_cfunction(name, type, pos, cname, visibility, modifiers)
entry.overloaded_alternatives.append(temp) temp.overloaded_alternatives = entry.all_alternatives()
entry = temp entry = temp
else: else:
warning(pos, "Function signature does not match previous declaration", 1) warning(pos, "Function signature does not match previous declaration", 1)
......
...@@ -13,10 +13,11 @@ except: ...@@ -13,10 +13,11 @@ except:
ext_modules=[ ext_modules=[
Extension("primes", ["primes.pyx"]), Extension("primes", ["primes.pyx"]),
Extension("spam", ["spam.pyx"]), Extension("spam", ["spam.pyx"]),
Extension("square", ["square.pyx"], language="c++"),
] ]
for file in glob.glob("*.pyx"): for file in glob.glob("*.pyx"):
if file != "numeric_demo.pyx": if file != "numeric_demo.pyx" and file != "square.pyx":
ext_modules.append(Extension(file[:-4], [file], include_dirs = numpy_include_dirs)) ext_modules.append(Extension(file[:-4], [file], include_dirs = numpy_include_dirs))
setup( setup(
......
cdef extern from *:
int new(int new)
def new(x):
"""
>>> new(3)
3
"""
cdef int new = x
return new
def x(new):
"""
>>> x(10)
110
>>> x(1)
1
"""
if new*new != new:
return new + new**2
return new
class A:
def new(self, n):
"""
>>> a = A()
>>> a.new(3)
6
>>> a.new(5)
120
"""
if n <= 1:
return 1
else:
return n * self.new(n-1)
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