51
51
from pyccel .ast .literals import LiteralTrue , LiteralFalse , LiteralString
52
52
from pyccel .ast .literals import Nil
53
53
54
- from pyccel .ast .low_level_tools import MacroDefinition , IteratorType
54
+ from pyccel .ast .low_level_tools import MacroDefinition , IteratorType , PairType
55
55
56
56
from pyccel .ast .mathext import math_constants
57
57
@@ -584,6 +584,37 @@ def _build_gFTL_module(self, expr_type):
584
584
raise NotImplementedError ("Support for sets of types which define their own < operator is not yet implemented" )
585
585
imports_and_macros .append (MacroDefinition ('Set' , expr_type ))
586
586
imports_and_macros .append (MacroDefinition ('SetIterator' , IteratorType (expr_type )))
587
+ elif isinstance (expr_type , DictType ):
588
+ include = Import (LiteralString ('map/template.inc' ), Module ('_' , (), ()))
589
+ key_type = expr_type .key_type
590
+ value_type = expr_type .value_type
591
+ imports_and_macros = []
592
+ if isinstance (key_type , FixedSizeNumericType ):
593
+ tmpVar_x = Variable (key_type , 'x' )
594
+ tmpVar_y = Variable (key_type , 'y' )
595
+ if isinstance (key_type .primitive_type , PrimitiveComplexType ):
596
+ complex_tool_import = Import ('pyc_tools_f90' , Module ('pyc_tools_f90' ,(),()))
597
+ self .add_import (complex_tool_import )
598
+ imports_and_macros .append (complex_tool_import )
599
+ compare_func = FunctionDef ('complex_comparison' ,
600
+ [FunctionDefArgument (tmpVar_x ), FunctionDefArgument (tmpVar_y )],
601
+ [FunctionDefResult (Variable (PythonNativeBool (), 'c' ))], [])
602
+ lt_def = FunctionCall (compare_func , [tmpVar_x , tmpVar_y ])
603
+ else :
604
+ lt_def = PyccelAssociativeParenthesis (PyccelLt (tmpVar_x , tmpVar_y ))
605
+ imports_and_macros .extend ([MacroDefinition ('Key' , key_type .primitive_type ),
606
+ MacroDefinition ('Key_KINDLEN(context)' , KindSpecification (key_type )),
607
+ MacroDefinition ('Key_LT(x,y)' , lt_def )])
608
+ else :
609
+ raise NotImplementedError ("Support for dicts whose keys define their own < operator is not yet implemented" )
610
+ if isinstance (value_type , FixedSizeNumericType ):
611
+ imports_and_macros .extend ([MacroDefinition ('T' , value_type .primitive_type ),
612
+ MacroDefinition ('T_KINDLEN(context)' , KindSpecification (value_type ))])
613
+ else :
614
+ raise NotImplementedError (f"Support for dictionary values of type { value_type } not yet implemented" )
615
+ imports_and_macros .append (MacroDefinition ('Pair' , PairType (key_type , value_type )))
616
+ imports_and_macros .append (MacroDefinition ('Map' , expr_type ))
617
+ imports_and_macros .append (MacroDefinition ('MapIterator' , IteratorType (expr_type )))
587
618
else :
588
619
raise NotImplementedError (f"Unkown gFTL import for type { expr_type } " )
589
620
@@ -1130,6 +1161,24 @@ def _print_PythonSet(self, expr):
1130
1161
set_type = self ._print (expr .class_type )
1131
1162
return f'{ set_type } ({ list_arg } )'
1132
1163
1164
+ def _print_PythonDict (self , expr ):
1165
+ if len (expr ) == 0 :
1166
+ list_arg = ''
1167
+ assign = expr .get_direct_user_nodes (lambda a : isinstance (a , Assign ))
1168
+ if assign :
1169
+ dict_type = self ._print (assign [0 ].lhs .class_type )
1170
+ else :
1171
+ raise errors .report ("Can't use an empty dict without assigning it to a variable as the type cannot be deduced" ,
1172
+ severity = 'fatal' , symbol = expr )
1173
+
1174
+ else :
1175
+ class_type = expr .class_type
1176
+ pair_type = self ._print (PairType (class_type .key_type , class_type .value_type ))
1177
+ args = ', ' .join (f'{ pair_type } ({ self ._print (k )} , { self ._print (v )} )' for k ,v in expr )
1178
+ list_arg = f'[{ args } ]'
1179
+ dict_type = self ._print (class_type )
1180
+ return f'{ dict_type } ({ list_arg } )'
1181
+
1133
1182
def _print_InhomogeneousTupleVariable (self , expr ):
1134
1183
fs = ', ' .join (self ._print (f ) for f in expr )
1135
1184
return '[{0}]' .format (fs )
@@ -1988,7 +2037,7 @@ def _print_Allocate(self, expr):
1988
2037
1989
2038
return code
1990
2039
1991
- elif isinstance (class_type , HomogeneousContainerType ):
2040
+ elif isinstance (class_type , ( HomogeneousContainerType , DictType ) ):
1992
2041
return ''
1993
2042
1994
2043
else :
@@ -2006,7 +2055,7 @@ def _print_Deallocate(self, expr):
2006
2055
Pyccel_del_args = [FunctionCallArgument (var )]
2007
2056
return self ._print (FunctionCall (Pyccel__del , Pyccel_del_args ))
2008
2057
2009
- if var .is_alias or isinstance (class_type , (HomogeneousListType , HomogeneousSetType )):
2058
+ if var .is_alias or isinstance (class_type , (HomogeneousListType , HomogeneousSetType , DictType )):
2010
2059
return ''
2011
2060
elif isinstance (class_type , (NumpyNDArrayType , HomogeneousTupleType , StringType )):
2012
2061
var_code = self ._print (var )
@@ -2053,9 +2102,16 @@ def _print_HomogeneousListType(self, expr):
2053
2102
def _print_HomogeneousSetType (self , expr ):
2054
2103
return 'Set_' + self ._print (expr .element_type )
2055
2104
2105
+ def _print_PairType (self , expr ):
2106
+ return 'Pair_' + self ._print (expr .key_type )+ '__' + self ._print (expr .value_type )
2107
+
2108
+ def _print_DictType (self , expr ):
2109
+ return 'Map_' + self ._print (expr .key_type )+ '__' + self ._print (expr .value_type )
2110
+
2056
2111
def _print_IteratorType (self , expr ):
2057
2112
iterable_type = self ._print (expr .iterable_type )
2058
2113
return f"{ iterable_type } _Iterator"
2114
+
2059
2115
def _print_DataType (self , expr ):
2060
2116
return self ._print (expr .name )
2061
2117
0 commit comments