nac3-spec/toy-impl/test_top_level.py

32 lines
762 B
Python
Raw 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
def bar(a: list[list[virtual[A]]]) -> A:
pass
"""
variables = {'X': TypeVariable('X', []), 'Y': TypeVariable('Y', [])}
types = {'int': PrimitiveType('int'), 'str': PrimitiveType('str')}
ctx = Context(variables, types)
ctx, functions, _ = parse_top_level(ctx, ast.parse(test))
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}")