From 80e9ca42eb70a678e0993df9f8f8a0e3cc0f38e3 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Thu, 14 May 2009 00:58:08 -0700 Subject: [PATCH] Complex type parsing, very basic support if supported by C compiler --- Cython/Compiler/Nodes.py | 3 +++ Cython/Compiler/Parsing.py | 7 ++++++- Cython/Compiler/PyrexTypes.py | 23 ++++++++++++++++++++++- 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py index b6c7f72c..bd8b7d28 100644 --- a/Cython/Compiler/Nodes.py +++ b/Cython/Compiler/Nodes.py @@ -674,6 +674,7 @@ class CSimpleBaseTypeNode(CBaseTypeNode): # is_basic_c_type boolean # signed boolean # longness integer + # complex boolean # is_self_arg boolean Is self argument of C method child_attrs = [] @@ -714,6 +715,8 @@ class CSimpleBaseTypeNode(CBaseTypeNode): self.arg_name = self.name else: error(self.pos, "'%s' is not a type identifier" % self.name) + if self.complex: + type = PyrexTypes.CCompelxType(type) if type: return type else: diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index ac812f73..cf679d07 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -1692,6 +1692,7 @@ def p_c_simple_base_type(s, self_flag, nonempty): is_basic = 0 signed = 1 longness = 0 + complex = 0 module_path = [] pos = s.position() if not s.sy == 'IDENT': @@ -1710,6 +1711,9 @@ def p_c_simple_base_type(s, self_flag, nonempty): s.next() else: name = 'int' + if s.sy == 'IDENT' and s.systring == 'complex': + complex = 1 + s.next() elif looking_at_dotted_name(s): #print "p_c_simple_base_type: looking_at_type_name at", s.position() name = s.systring @@ -1738,7 +1742,8 @@ def p_c_simple_base_type(s, self_flag, nonempty): type_node = Nodes.CSimpleBaseTypeNode(pos, name = name, module_path = module_path, is_basic_c_type = is_basic, signed = signed, - longness = longness, is_self_arg = self_flag) + complex = complex, longness = longness, + is_self_arg = self_flag) # Treat trailing [] on type as buffer access if it appears in a context diff --git a/Cython/Compiler/PyrexTypes.py b/Cython/Compiler/PyrexTypes.py index d4b4f4e4..0c2bc125 100644 --- a/Cython/Compiler/PyrexTypes.py +++ b/Cython/Compiler/PyrexTypes.py @@ -33,6 +33,7 @@ class PyrexType(BaseType): # 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 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 @@ class PyrexType(BaseType): is_int = 0 is_longlong = 0 is_float = 0 + is_complex = 0 is_void = 0 is_array = 0 is_ptr = 0 @@ -698,7 +700,24 @@ class CFloatType(CNumericType): self.math_h_modifier = math_h_modifier def assignable_from_resolved_type(self, src_type): - return src_type.is_numeric or src_type is error_type + return (src_type.is_numeric and not src_type.is_complex) or src_type is error_type + +class CCompelxType(CNumericType): + + is_complex = 1 + + def __init__(self, real_type): + self.real_type = real_type + CNumericType.__init__(self, real_type.rank + 0.5, real_type.signed) + + def sign_and_name(self): + return self.real_type.sign_and_name() + " complex" + + def assignable_from_resolved_type(self, src_type): + return (src_type.is_complex and self.real_type.assignable_from_resolved_type(src_type.real_type) + or src_type.is_numeric and self.real_type.assignable_from_resolved_type(src_type) + or src_type is error_type) + class CArrayType(CType): @@ -1615,3 +1634,5 @@ static INLINE size_t __Pyx_PyInt_AsSize_t(PyObject* x) { } """ + type_conversion_functions + + -- 2.26.2