nac3-spec/toy-impl/test_top_level.py

32 lines
760 B
Python
Raw Permalink Normal View History

2020-12-17 14:50:51 +08:00
import ast
from type_def import *
from top_level import *
test = """
class A:
a: int
2020-12-17 15:00:09 +08:00
def foo(a: B):
2020-12-17 14:50:51 +08:00
pass
class B(A):
a: str
2020-12-18 13:03:36 +08:00
def bar(self, a: list[list[virtual[A]]]) -> self:
2020-12-17 14:50:51 +08:00
pass
"""
variables = {'X': TypeVariable('X', []), 'Y': TypeVariable('Y', [])}
types = {'int': PrimitiveType('int'), 'str': PrimitiveType('str')}
ctx = Context(variables, types)
2020-12-18 13:03:36 +08:00
ctx, _ = parse_top_level(ctx, ast.parse(test))
2020-12-17 14:50:51 +08:00
for name, t in ctx.types.items():
if isinstance(t, ClassType):
print(f"class {t.name}")
for name, ty in t.fields.items():
print(f" {name}: {ty}")
for name, (args, result, _) in t.methods.items():
print(f" {name}: ({', '.join([str(v) for v in args])}) -> {result}")