Skip to content

Commit

Permalink
Added custom len function for classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Vardan2009 committed May 28, 2024
1 parent 62ac78a commit 0dd91f0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion core/builtin_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ def execute_print_ret(self, exec_ctx: Context) -> RTResult[Value]:
def execute_len(self, exec_ctx: Context) -> RTResult[Value]:
val = exec_ctx.symbol_table.get("value")
try:
if val is not None and not val.__class__ is Value:
if val is not None and val.__class__ is not Value:
if hasattr(val, "__len__"):
ret = int(val.__len__())
elif hasattr(val, "__exec__len"):
ret = int(val.__exec__len())
else:
raise TypeError()
return RTResult[Value]().success(Number(ret))
Expand Down
2 changes: 1 addition & 1 deletion core/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ class Instance(BaseInstance):
def __init__(self, parent_class: Class) -> None:
super().__init__(parent_class, None)

def __len__(self):
def __exec__len(self):
try:
return self.operator("__len__")[0].value
except AttributeError:
Expand Down

0 comments on commit 0dd91f0

Please sign in to comment.