nac3-spec/toy-impl/inheritance.py

35 lines
1.1 KiB
Python

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]
for a, b in zip(old[0], new[0]):
if a != b:
raise CustomError(f'{m} is different in {c.name} and {p.name}')
if old[1] != new[1]:
# actually, we should check for equality *modulo variable renaming*
raise CustomError(f'{m} is different in {c.name} and {p.name}')
else:
c.methods[m] = p.methods[m]
for f in p.fields:
if f in c.fields:
if p.fields[f] != c.fields[f]:
raise CustomError(f'{f} is different in {c.name} and {p.name}')
else:
c.fields[f] = p.fields[f]
c.checking = False
c.checked = True