nac3-spec/toy-impl/inheritance.py

35 lines
1.1 KiB
Python
Raw Permalink Normal View History

2020-12-22 16:53:44 +08:00
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]
2020-12-23 13:43:34 +08:00
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]:
2020-12-22 16:53:44 +08:00
# actually, we should check for equality *modulo variable renaming*
2020-12-23 13:43:34 +08:00
raise CustomError(f'{m} is different in {c.name} and {p.name}')
2020-12-22 16:53:44 +08:00
else:
c.methods[m] = p.methods[m]
2020-12-23 13:43:34 +08:00
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]
2020-12-22 16:53:44 +08:00
c.checking = False
c.checked = True