Skip to content

Commit

Permalink
add __int__, __hash__, __eq__ methods and missing annotations to Toke…
Browse files Browse the repository at this point in the history
…n, Local, and Argument classes (#103)

* add __int__, __hash__, __eq__ methods and missing annotations to Token, Local, and Argument classes

* Removal of redundant cast. Lint checked.
  • Loading branch information
akhilguruprasad22 authored Mar 20, 2024
1 parent 0ecc39e commit c6600cf
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
9 changes: 9 additions & 0 deletions dncil/clr/argument.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ def __str__(self) -> str:

def __repr__(self) -> str:
return str(self)

def __int__(self) -> int:
return self.index

def __eq__(self, other: object) -> bool:
return isinstance(other, Argument) and self.index == other.index

def __hash__(self) -> int:
return hash(self.index)
9 changes: 9 additions & 0 deletions dncil/clr/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ def __str__(self) -> str:

def __repr__(self) -> str:
return str(self)

def __int__(self) -> int:
return self.index

def __eq__(self, other: object) -> bool:
return isinstance(other, Local) and self.index == other.index

def __hash__(self) -> int:
return hash(self.index)
37 changes: 24 additions & 13 deletions dncil/clr/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,60 @@
# is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.

from __future__ import annotations


class Token(object):
"""store managed token"""

RID_MASK = 0x00FFFFFF
RID_MAX = RID_MASK
TABLE_SHIFT = 24
RID_MASK: int = 0x00FFFFFF
RID_MAX: int = RID_MASK
TABLE_SHIFT: int = 24

def __init__(self, value):
self.value = value
def __init__(self, value: int):
self.value: int = value

@property
def rid(self):
def rid(self) -> int:
"""get token row index"""
return self.value & Token.RID_MASK

@property
def table(self):
def table(self) -> int:
"""get token table index"""
return self.value >> Token.TABLE_SHIFT

def __str__(self):
def __str__(self) -> str:
return "token(0x%08X)" % self.value

def __repr__(self):
def __repr__(self) -> str:
return str(self)

def __int__(self) -> int:
return self.value

def __eq__(self, other: object) -> bool:
return isinstance(other, Token) and self.value == other.value

def __hash__(self) -> int:
return hash(self.value)


class InvalidToken(Token):
"""store invalid managed token"""

def __init__(self, value):
def __init__(self, value: int):
super(InvalidToken, self).__init__(value)

def __str__(self):
def __str__(self) -> str:
return "invalid token(0x%08X)" % self.value


class StringToken(Token):
"""store string managed token"""

def __init__(self, value):
def __init__(self, value: int):
super(StringToken, self).__init__(value)

def __str__(self):
def __str__(self) -> str:
return "string token(0x%08X)" % self.value

0 comments on commit c6600cf

Please sign in to comment.