From ae65b2dd0973378b067ba797af07c7f07f862dbd Mon Sep 17 00:00:00 2001 From: pca006132 Date: Tue, 22 Dec 2020 16:53:44 +0800 Subject: [PATCH] class check --- toy-impl/inheritance.py | 25 +++++++++++++++++++++++++ toy-impl/type_def.py | 6 ++++-- 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 toy-impl/inheritance.py diff --git a/toy-impl/inheritance.py b/toy-impl/inheritance.py new file mode 100644 index 0000000..400d697 --- /dev/null +++ b/toy-impl/inheritance.py @@ -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 + + diff --git a/toy-impl/type_def.py b/toy-impl/type_def.py index 49b37fb..a8d8d2e 100644 --- a/toy-impl/type_def.py +++ b/toy-impl/type_def.py @@ -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