Commit 7cec62b7 authored by Stefan Behnel's avatar Stefan Behnel

support tuples as compile time constants

parent eabbb046
...@@ -59,6 +59,7 @@ cdef p_atom(PyrexScanner s) ...@@ -59,6 +59,7 @@ cdef p_atom(PyrexScanner s)
@cython.locals(value=unicode) @cython.locals(value=unicode)
cdef p_int_literal(PyrexScanner s) cdef p_int_literal(PyrexScanner s)
cdef p_name(PyrexScanner s, name) cdef p_name(PyrexScanner s, name)
cdef wrap_compile_time_constant(pos, value)
cdef p_cat_string_literal(PyrexScanner s) cdef p_cat_string_literal(PyrexScanner s)
cdef p_opt_string_literal(PyrexScanner s, required_type=*) cdef p_opt_string_literal(PyrexScanner s, required_type=*)
cdef bint check_for_non_ascii_characters(unicode string) cdef bint check_for_non_ascii_characters(unicode string)
......
...@@ -678,27 +678,43 @@ def p_int_literal(s): ...@@ -678,27 +678,43 @@ def p_int_literal(s):
unsigned = unsigned, unsigned = unsigned,
longness = longness) longness = longness)
def p_name(s, name): def p_name(s, name):
pos = s.position() pos = s.position()
if not s.compile_time_expr and name in s.compile_time_env: if not s.compile_time_expr and name in s.compile_time_env:
value = s.compile_time_env.lookup_here(name) value = s.compile_time_env.lookup_here(name)
rep = repr(value) node = wrap_compile_time_constant(pos, value)
if isinstance(value, bool): if node is not None:
return ExprNodes.BoolNode(pos, value = value) return node
elif isinstance(value, int): return ExprNodes.NameNode(pos, name=name)
return ExprNodes.IntNode(pos, value = rep)
elif isinstance(value, long):
return ExprNodes.IntNode(pos, value = rep, longness = "L") def wrap_compile_time_constant(pos, value):
elif isinstance(value, float): rep = repr(value)
return ExprNodes.FloatNode(pos, value = rep) if isinstance(value, bool):
elif isinstance(value, _unicode): return ExprNodes.BoolNode(pos, value=value)
return ExprNodes.UnicodeNode(pos, value = value) elif isinstance(value, int):
elif isinstance(value, _bytes): return ExprNodes.IntNode(pos, value=rep)
return ExprNodes.BytesNode(pos, value = value) elif isinstance(value, long):
return ExprNodes.IntNode(pos, value=rep, longness="L")
elif isinstance(value, float):
return ExprNodes.FloatNode(pos, value=rep)
elif isinstance(value, _unicode):
return ExprNodes.UnicodeNode(pos, value=value)
elif isinstance(value, _bytes):
return ExprNodes.BytesNode(pos, value=value)
elif isinstance(value, tuple):
args = [wrap_compile_time_constant(pos, arg)
for arg in value]
if None not in args:
return ExprNodes.TupleNode(pos, args=args)
else: else:
error(pos, "Invalid type for compile-time constant: %s" # error already reported
% value.__class__.__name__) return None
return ExprNodes.NameNode(pos, name = name) error(pos, "Invalid type for compile-time constant: %s"
% value.__class__.__name__)
return None
def p_cat_string_literal(s): def p_cat_string_literal(s):
# A sequence of one or more adjacent string literals. # A sequence of one or more adjacent string literals.
......
...@@ -8,7 +8,7 @@ if sys.version_info[0] < 3: ...@@ -8,7 +8,7 @@ if sys.version_info[0] < 3:
__doc__ = __doc__.replace(u" b'", u" '") __doc__ = __doc__.replace(u" b'", u" '")
DEF TUPLE = (1, 2, u"buckle my shoe") DEF TUPLE = (1, 2, "buckle my shoe")
DEF TRUE_FALSE = (True, False) DEF TRUE_FALSE = (True, False)
DEF CHAR = c'x' DEF CHAR = c'x'
...@@ -89,10 +89,21 @@ def s(): ...@@ -89,10 +89,21 @@ def s():
cdef char* s = STR cdef char* s = STR
return s return s
# this does not work! def constant_tuple():
#def t(): """
# cdef object t = TUPLE >>> constant_tuple()
# return t (1, 2, 'buckle my shoe')
"""
cdef object t = TUPLE
return t
def tuple_indexing():
"""
>>> tuple_indexing()
2
"""
cdef int two = INT_TUPLE1[-1]
return two
def two(): def two():
""" """
...@@ -102,12 +113,6 @@ def two(): ...@@ -102,12 +113,6 @@ def two():
cdef int two = TWO cdef int two = TWO
return two return two
# this doesn't currently work!
#def two2():
# cdef int two
# two = INT_TUPLE1[-1]
# return two
def five(): def five():
""" """
>>> five() >>> five()
......
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