nac3-spec/toy-impl/test_top_level.py
2020-12-17 15:00:09 +08:00

32 lines
762 B
Python

import ast
from type_def import *
from top_level import *
test = """
class A:
a: int
def foo(a: B):
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}")