diff -r 904b5b1bcd3e Cython/Compiler/ExprNodes.py --- a/Cython/Compiler/ExprNodes.py Sun Dec 28 17:54:53 2008 +0100 +++ b/Cython/Compiler/ExprNodes.py Mon Dec 29 18:32:00 2008 -0200 @@ -1018,30 +1018,21 @@ code.error_goto_if_null(self.result(), self.pos))) -class ImagNode(AtomicNewTempExprNode): +class ImagNode(ConstNode): # Imaginary number literal # # value float imaginary part + type = PyrexTypes.c_complex_type + def calculate_constant_result(self): self.constant_result = complex(0.0, self.value) - + def compile_time_value(self, denv): return complex(0.0, self.value) - - def analyse_types(self, env): - self.type = py_object_type - self.gil_check(env) - self.is_temp = 1 - - gil_message = "Constructing complex number" - - def generate_result_code(self, code): - code.putln( - "%s = PyComplex_FromDoubles(0.0, %s); %s" % ( - self.result(), - self.value, - code.error_goto_if_null(self.result(), self.pos))) + + def calculate_result_code(self): + return '__Pyx_complex(0,%s)' % self.value class NameNode(AtomicExprNode): diff -r 904b5b1bcd3e Cython/Compiler/Parsing.py --- a/Cython/Compiler/Parsing.py Sun Dec 28 17:54:53 2008 +0100 +++ b/Cython/Compiler/Parsing.py Mon Dec 29 18:32:00 2008 -0200 @@ -1811,7 +1811,7 @@ else: return 0 -basic_c_type_names = ("void", "char", "int", "float", "double", "Py_ssize_t", "bint") +basic_c_type_names = ("void", "char", "int", "float", "double", "complex", "Py_ssize_t", "bint") sign_and_longness_words = ("short", "long", "signed", "unsigned") diff -r 904b5b1bcd3e Cython/Compiler/PyrexTypes.py --- a/Cython/Compiler/PyrexTypes.py Sun Dec 28 17:54:53 2008 +0100 +++ b/Cython/Compiler/PyrexTypes.py Mon Dec 29 18:32:00 2008 -0200 @@ -31,6 +31,7 @@ # is_int boolean Is a C integer type # is_longlong boolean Is a long long or unsigned long long. # is_float boolean Is a C floating point type + # is_complex boolean Is a C complex floating point type # is_void boolean Is the C void type # is_array boolean Is a C array type # is_ptr boolean Is a C pointer type @@ -83,6 +84,7 @@ is_int = 0 is_longlong = 0 is_float = 0 + is_complex = 0 is_void = 0 is_array = 0 is_ptr = 0 @@ -462,9 +464,9 @@ default_value = "0" parsetuple_formats = ( # rank -> format - "BHIkK????", # unsigned - "bhilL?fd?", # assumed signed - "bhilL?fd?", # explicitly signed + "BHIkK?????", # unsigned + "bhilL?fdD?", # assumed signed + "bhilL?fdD?", # explicitly signed ) sign_words = ("unsigned ", "", "signed ") @@ -515,6 +517,7 @@ self.from_py_function = self.get_type_conversion() def get_type_conversion(self): + if 'complex' in self.sign_and_name(): return '__pyx_PyInt_AsLong' # XXX Better way ?? # error on overflow c_type = self.sign_and_name() c_name = c_type.replace(' ', '_'); @@ -608,6 +611,18 @@ def assignable_from_resolved_type(self, src_type): return src_type.is_numeric or src_type is error_type +class CComplexType(CNumericType): + + is_complex = 1 + to_py_function = "__pyx_PyComplex_FromCComplex" + from_py_function = "__pyx_PyComplex_AsCComplex" + + def __init__(self, rank, pymemberdef_typecode = None, typestring=None): + CNumericType.__init__(self, rank, 1, pymemberdef_typecode, typestring = typestring) + + def assignable_from_resolved_type(self, src_type): + return src_type.is_numeric or src_type is error_type + class CArrayType(CType): # base_type CType Element type @@ -1159,6 +1174,7 @@ "float", # 6 "double", # 7 "long double", # 8 + "complex", # 9 ) py_object_type = PyObjectType() @@ -1191,6 +1207,8 @@ c_double_type = CFloatType(7, "T_DOUBLE", typestring="d") c_longdouble_type = CFloatType(8, typestring="g") +c_complex_type = CComplexType(9, typestring="D") + c_null_ptr_type = CNullPtrType(c_void_type) c_char_array_type = CCharArrayType(None) c_char_ptr_type = CCharPtrType() @@ -1239,6 +1257,7 @@ (0, 6): c_float_type, (0, 7): c_double_type, (0, 8): c_longdouble_type, + (0, 9): c_complex_type, } modifiers_and_name_to_type = { @@ -1258,6 +1277,7 @@ (1, 0, "float"): c_float_type, (1, 0, "double"): c_double_type, (1, 1, "double"): c_longdouble_type, + (1, 0, "complex"): c_complex_type, (1, 0, "object"): py_object_type, (1, 0, "bint"): c_bint_type, (2, 0, "char"): c_schar_type,