2021-07-09 16:06:06 +08:00
|
|
|
from __future__ import annotations
|
2021-07-09 15:27:02 +08:00
|
|
|
import ast
|
|
|
|
from ast_visitor import Visitor
|
|
|
|
from nac3_types import *
|
|
|
|
|
2021-07-10 14:36:28 +08:00
|
|
|
src = """
|
|
|
|
b = test_virtual(virtual(bar, Foo))
|
|
|
|
b = test_virtual(virtual(foo, Foo))
|
|
|
|
b = test_virtual(virtual(foo2, Foo))
|
|
|
|
"""
|
2021-07-09 15:27:02 +08:00
|
|
|
|
|
|
|
foo = TObj('Foo', {
|
2021-07-10 14:36:28 +08:00
|
|
|
'a': TInt,
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
foo2 = TObj('Foo2', {
|
|
|
|
'a': TInt,
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
bar = TObj('Bar', {
|
|
|
|
'a': TInt,
|
|
|
|
'b': TInt
|
|
|
|
}, [], [foo])
|
|
|
|
|
|
|
|
type_mapping = {
|
|
|
|
'Foo': foo,
|
|
|
|
'Foo2': foo2,
|
|
|
|
'Bar': bar,
|
|
|
|
}
|
2021-07-09 15:27:02 +08:00
|
|
|
|
2021-07-10 14:36:28 +08:00
|
|
|
prelude = {
|
|
|
|
'foo': foo,
|
|
|
|
'foo2': foo2,
|
|
|
|
'bar': bar,
|
|
|
|
'test_virtual': TFunc([FuncArg('a', TVirtual(foo), False)], TInt, set())
|
|
|
|
}
|
2021-07-09 15:27:02 +08:00
|
|
|
|
|
|
|
print('-----------')
|
|
|
|
print('prelude')
|
2021-07-10 14:36:28 +08:00
|
|
|
for key, value in prelude.items():
|
2021-07-09 15:27:02 +08:00
|
|
|
print(f'{key}: {value}')
|
|
|
|
print('-----------')
|
|
|
|
|
2021-07-10 14:36:28 +08:00
|
|
|
v = Visitor(src, prelude.copy(), lambda x: type_mapping[x])
|
2021-07-09 15:27:02 +08:00
|
|
|
|
|
|
|
print(src)
|
|
|
|
v.visit(ast.parse(src))
|
|
|
|
|
2021-07-10 14:36:28 +08:00
|
|
|
for a, b in v.virtuals:
|
|
|
|
assert isinstance(a, TObj)
|
|
|
|
assert b is a or b in a.parents
|
|
|
|
|
|
|
|
print('-----------')
|
|
|
|
print('calls')
|
|
|
|
for x in v.calls:
|
|
|
|
x.check()
|
|
|
|
print(f'{x.find()}')
|
|
|
|
|
2021-07-09 15:27:02 +08:00
|
|
|
print('-----------')
|
|
|
|
print('assignments')
|
|
|
|
for key, value in v.assignments.items():
|
|
|
|
if key not in prelude:
|
|
|
|
value.check()
|
|
|
|
print(f'{key}: {value.find()}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# TODO: Occur check
|