IdentifierDataType.dereference()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 5
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 5
loc 5
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nop 1
crap 1
1 1
from typing import Union
2
3 1
from sdoc.sdoc1.data_type.ArrayDataType import ArrayDataType
4 1
from sdoc.sdoc1.data_type.DataType import DataType
5 1
from sdoc.sdoc1.data_type.StringDataType import StringDataType
6
7
8 1 View Code Duplication
class IdentifierDataType(DataType):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
9
    """
10
    Class for identifiers.
11
    """
12
13
    # ------------------------------------------------------------------------------------------------------------------
14 1
    def __init__(self, scope: ArrayDataType, name: Union[int, str]):
15
        """
16
        Object constructor.
17
18
        :param ArrayDataType scope: The scope of the identifier.
19
        :param int|str name: The name of the identifier.
20
        """
21 1
        self._scope: ArrayDataType = scope
22
        """
23
        The scope of this identifier.
24
        """
25
26 1
        self._name: Union[int, str] = name
27 1
        """
28
        The name of this identifier.
29
        """
30
31
    # ------------------------------------------------------------------------------------------------------------------
32 1
    def debug(self, indent: int = 0) -> str:
33
        """
34
        Returns a string for debugging.
35
36
        :param int indent: Unused.
37
        """
38 1
        if not self._scope.has_element(self._name):
39 1
            return "'{0!s}' = {1!s}".format(self._name, 'UNDEFINED')
40
41
        # Setting first indentation.
42 1
        first_indent = len("{0!s} = ".format(self._name))
43
44 1
        return "{0!s} = {1!s}".format(self._name, self._scope.get_reference(self._name).debug(first_indent))
45
46
    # ------------------------------------------------------------------------------------------------------------------
47 1
    def dereference(self) -> DataType:
48
        """
49
        Returns a clone of the referenced data type.
50
        """
51 1
        return self._scope.get_reference(self._name).dereference()
52
53
    # ------------------------------------------------------------------------------------------------------------------
54 1
    def get_value(self) -> Union[int, str]:
55
        """
56
        Returns the underling value of this data type.
57
        """
58 1
        return self._scope.get_reference(self._name).get_value()
59
60
    # ------------------------------------------------------------------------------------------------------------------
61 1
    def get_name(self) -> Union[int, str]:
62
        """
63
        Returns the name of this identifier.
64
        """
65
        return self._name
66
67
    # ------------------------------------------------------------------------------------------------------------------
68 1
    def get_type_id(self) -> int:
69
        """
70
        Returns the ID of this data type.
71
72
        :rtype: int
73
        """
74
        return self._scope.get_reference(self._name).get_type_id()
75
76
    # ------------------------------------------------------------------------------------------------------------------
77 1
    def is_constant(self) -> bool:
78
        """
79
        Returns False always.
80
        """
81
        return False
82
83
    # ------------------------------------------------------------------------------------------------------------------
84 1
    def is_defined(self) -> bool:
85
        """
86
        Returns True if the reference is defined, i.e. if the element exists in the underling array. Returns False
87
        otherwise.
88
        """
89
        return self._scope.has_element(self._name)
90
91
    # ------------------------------------------------------------------------------------------------------------------
92 1
    def is_scalar(self) -> bool:
93
        """
94
        Returns True if this data type is a scalar. Returns False otherwise.
95
        """
96
        return bool(self._scope.get_reference(self._name).get_value())
97
98
    # ------------------------------------------------------------------------------------------------------------------
99 1
    def is_true(self) -> bool:
100
        """
101
        Returns True if this data type evaluates to True. Returns False otherwise.
102
        """
103
        return bool(self._scope.get_reference(self._name).get_value())
104
105
    # ------------------------------------------------------------------------------------------------------------------
106 1
    def get_array_element(self, key: DataType) -> DataType:
107
        """
108
        Sets the value for this identifier as an array element.
109
110
        :param DataType key: The key.
111
        """
112 1
        tmp = self._scope.get_array(self._name)
113
114 1
        return IdentifierDataType(tmp, key.get_value())
115
116
    # ------------------------------------------------------------------------------------------------------------------
117 1
    def set_value(self, value: DataType) -> DataType:
118
        """
119
        Sets the value for this identifier.
120
121
        :param DataType value: The value.
122
        """
123 1
        return self._scope.add_element(StringDataType(self._name), value)
124
125
# ----------------------------------------------------------------------------------------------------------------------
126