class check
This commit is contained in:
parent
00ed5e867d
commit
ae65b2dd09
25
toy-impl/inheritance.py
Normal file
25
toy-impl/inheritance.py
Normal file
@ -0,0 +1,25 @@
|
||||
from helper import *
|
||||
from type_def import *
|
||||
import copy
|
||||
|
||||
def class_fixup(c: ClassType):
|
||||
if c.checking:
|
||||
raise CustomError(f'Circular inheritance detected')
|
||||
if c.checked:
|
||||
return
|
||||
c.checking = True
|
||||
for p in c.parents:
|
||||
class_fixup(p)
|
||||
for m in p.methods:
|
||||
if m in c.methods:
|
||||
old = p.methods[m]
|
||||
new = c.methods[m]
|
||||
if old[0] != new[0] or old[1] != new[1]:
|
||||
# actually, we should check for equality *modulo variable renaming*
|
||||
raise CustomError(f'incorrect method signature for {m} in {c.name}')
|
||||
else:
|
||||
c.methods[m] = p.methods[m]
|
||||
c.checking = False
|
||||
c.checked = True
|
||||
|
||||
|
@ -73,13 +73,15 @@ class TypeVariable(Type):
|
||||
class ClassType(Type):
|
||||
name: str
|
||||
parents: list['ClassType']
|
||||
methods: dict[str, tuple[list[Type], Type, set[str]]]
|
||||
fields: dict[str, Type]
|
||||
checking: bool
|
||||
checked: bool
|
||||
|
||||
def __init__(self, name: str):
|
||||
super().__init__()
|
||||
self.name = name
|
||||
self.parents = []
|
||||
self.checking = False
|
||||
self.checked = False
|
||||
|
||||
def __str__(self):
|
||||
return self.name
|
||||
|
Loading…
Reference in New Issue
Block a user