class check

This commit is contained in:
pca006132 2020-12-22 16:53:44 +08:00 committed by pca006132
parent 00ed5e867d
commit ae65b2dd09
2 changed files with 29 additions and 2 deletions

25
toy-impl/inheritance.py Normal file
View 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

View File

@ -73,13 +73,15 @@ class TypeVariable(Type):
class ClassType(Type): class ClassType(Type):
name: str name: str
parents: list['ClassType'] parents: list['ClassType']
methods: dict[str, tuple[list[Type], Type, set[str]]] checking: bool
fields: dict[str, Type] checked: bool
def __init__(self, name: str): def __init__(self, name: str):
super().__init__() super().__init__()
self.name = name self.name = name
self.parents = [] self.parents = []
self.checking = False
self.checked = False
def __str__(self): def __str__(self):
return self.name return self.name