|
20 | 20 | from .datatypes import HomogeneousTupleType, InhomogeneousTupleType
|
21 | 21 | from .datatypes import HomogeneousListType, HomogeneousContainerType
|
22 | 22 | from .datatypes import FixedSizeNumericType, HomogeneousSetType, SymbolicType
|
| 23 | +from .datatypes import DictType |
23 | 24 | from .internals import PyccelFunction, Slice, PyccelArrayShapeElement
|
24 | 25 | from .literals import LiteralInteger, LiteralFloat, LiteralComplex, Nil
|
25 | 26 | from .literals import Literal, LiteralImaginaryUnit, convert_to_literal
|
|
37 | 38 | 'PythonComplex',
|
38 | 39 | 'PythonComplexProperty',
|
39 | 40 | 'PythonConjugate',
|
| 41 | + 'PythonDict', |
40 | 42 | 'PythonEnumerate',
|
41 | 43 | 'PythonFloat',
|
42 | 44 | 'PythonImag',
|
@@ -805,6 +807,81 @@ def __init__(self, copied_obj):
|
805 | 807 | self._shape = copied_obj.shape
|
806 | 808 | super().__init__(copied_obj)
|
807 | 809 |
|
| 810 | +#============================================================================== |
| 811 | +class PythonDict(PyccelFunction): |
| 812 | + """ |
| 813 | + Class representing a call to Python's `{}` function. |
| 814 | +
|
| 815 | + Class representing a call to Python's `{}` function which generates a |
| 816 | + literal Python dict. This operator does not handle `**a` expressions. |
| 817 | +
|
| 818 | + Parameters |
| 819 | + ---------- |
| 820 | + keys : iterable[TypedAstNode] |
| 821 | + The keys of the new dictionary. |
| 822 | + values : iterable[TypedAstNode] |
| 823 | + The values of the new dictionary. |
| 824 | + """ |
| 825 | + __slots__ = ('_keys', '_values', '_shape', '_class_type') |
| 826 | + _attribute_nodes = ('_keys', '_values') |
| 827 | + _rank = 1 |
| 828 | + |
| 829 | + def __init__(self, keys, values): |
| 830 | + self._keys = keys |
| 831 | + self._values = values |
| 832 | + super().__init__() |
| 833 | + if pyccel_stage == 'syntactic': |
| 834 | + return |
| 835 | + elif len(keys) != len(values): |
| 836 | + raise TypeError("Unpacking values in a dictionary is not yet supported.") |
| 837 | + elif len(keys) == 0: |
| 838 | + self._shape = (LiteralInteger(0),) |
| 839 | + self._class_type = DictType(GenericType(), GenericType()) |
| 840 | + return |
| 841 | + |
| 842 | + key0 = keys[0] |
| 843 | + val0 = values[0] |
| 844 | + homogeneous_keys = all(k.class_type is not GenericType() for k in keys) and \ |
| 845 | + all(key0.class_type == k.class_type for k in keys[1:]) |
| 846 | + homogeneous_vals = all(v.class_type is not GenericType() for v in values) and \ |
| 847 | + all(val0.class_type == v.class_type for v in values[1:]) |
| 848 | + |
| 849 | + if homogeneous_keys and homogeneous_vals: |
| 850 | + self._class_type = DictType(key0.class_type, val0.class_type) |
| 851 | + |
| 852 | + self._shape = (LiteralInteger(len(keys)), ) |
| 853 | + else: |
| 854 | + raise TypeError("Can't create an inhomogeneous dict") |
| 855 | + |
| 856 | + def __iter__(self): |
| 857 | + return zip(self._keys, self._values) |
| 858 | + |
| 859 | + def __str__(self): |
| 860 | + args = ', '.join(f'{k}: {v}' for k,v in self) |
| 861 | + return f'{{{args}}}' |
| 862 | + |
| 863 | + def __repr__(self): |
| 864 | + args = ', '.join(f'{repr(k)}: {repr(v)}' for k,v in self) |
| 865 | + return f'PythonDict({args})' |
| 866 | + |
| 867 | + @property |
| 868 | + def keys(self): |
| 869 | + """ |
| 870 | + The keys of the new dictionary. |
| 871 | +
|
| 872 | + The keys of the new dictionary. |
| 873 | + """ |
| 874 | + return self._keys |
| 875 | + |
| 876 | + @property |
| 877 | + def values(self): |
| 878 | + """ |
| 879 | + The values of the new dictionary. |
| 880 | +
|
| 881 | + The values of the new dictionary. |
| 882 | + """ |
| 883 | + return self._values |
| 884 | + |
808 | 885 | #==============================================================================
|
809 | 886 | class PythonMap(PyccelFunction):
|
810 | 887 | """
|
|
0 commit comments