2021-08-31 15:22:45 +08:00
|
|
|
use crate::{
|
|
|
|
location::Location,
|
|
|
|
symbol_resolver::{SymbolResolver, SymbolValue},
|
|
|
|
toplevel::DefinitionId,
|
|
|
|
typecheck::{
|
|
|
|
type_inferencer::PrimitiveStore,
|
|
|
|
typedef::{Type, Unifier},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use indoc::indoc;
|
2021-09-07 00:20:40 +08:00
|
|
|
use parking_lot::Mutex;
|
|
|
|
use rustpython_parser::parser::parse_program;
|
|
|
|
use std::{collections::HashMap, sync::Arc};
|
2021-08-31 15:22:45 +08:00
|
|
|
use test_case::test_case;
|
|
|
|
|
2021-09-07 00:20:40 +08:00
|
|
|
use super::*;
|
2021-08-31 15:22:45 +08:00
|
|
|
|
|
|
|
struct Resolver {
|
|
|
|
id_to_type: HashMap<String, Type>,
|
|
|
|
id_to_def: HashMap<String, DefinitionId>,
|
|
|
|
class_names: HashMap<String, Type>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl SymbolResolver for Resolver {
|
|
|
|
fn get_symbol_type(&self, _: &mut Unifier, _: &PrimitiveStore, str: &str) -> Option<Type> {
|
|
|
|
self.id_to_type.get(str).cloned()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_symbol_value(&self, _: &str) -> Option<SymbolValue> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_symbol_location(&self, _: &str) -> Option<Location> {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_identifier_def(&self, id: &str) -> Option<DefinitionId> {
|
|
|
|
self.id_to_def.get(id).cloned()
|
|
|
|
}
|
2021-09-06 19:23:04 +08:00
|
|
|
|
|
|
|
fn add_id_def(&mut self, id: String, def: DefinitionId) {
|
|
|
|
self.id_to_def.insert(id, def);
|
|
|
|
}
|
2021-08-31 15:22:45 +08:00
|
|
|
}
|
|
|
|
|
2021-09-06 19:23:04 +08:00
|
|
|
|
|
|
|
|
2021-08-31 15:22:45 +08:00
|
|
|
#[test_case(
|
|
|
|
vec![
|
|
|
|
indoc! {"
|
2021-09-07 00:20:40 +08:00
|
|
|
def fun(a: int32) -> int32:
|
2021-08-31 15:22:45 +08:00
|
|
|
return a
|
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class A:
|
|
|
|
def __init__(self):
|
2021-09-07 00:20:40 +08:00
|
|
|
self.a: int32 = 3
|
2021-08-31 15:22:45 +08:00
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class B:
|
|
|
|
def __init__(self):
|
|
|
|
self.b: float = 4.3
|
|
|
|
|
|
|
|
def fun(self):
|
|
|
|
self.b = self.b + 3.0
|
2021-08-31 15:41:48 +08:00
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
def foo(a: float):
|
|
|
|
a + 1.0
|
2021-08-31 17:40:38 +08:00
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
class C(B):
|
|
|
|
def __init__(self):
|
2021-09-07 00:20:40 +08:00
|
|
|
self.c: int32 = 4
|
2021-08-31 17:40:38 +08:00
|
|
|
self.a: bool = True
|
2021-08-31 15:22:45 +08:00
|
|
|
"}
|
|
|
|
]
|
|
|
|
)]
|
|
|
|
fn test_simple_register(source: Vec<&str>) {
|
|
|
|
let mut composer = TopLevelComposer::new();
|
|
|
|
|
|
|
|
for s in source {
|
|
|
|
let ast = parse_program(s).unwrap();
|
|
|
|
let ast = ast[0].clone();
|
|
|
|
|
|
|
|
composer.register_top_level(ast, None).unwrap();
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 19:23:04 +08:00
|
|
|
|
|
|
|
#[test_case(
|
|
|
|
vec![
|
|
|
|
indoc! {"
|
2021-09-07 00:20:40 +08:00
|
|
|
def fun(a: int32) -> int32:
|
2021-09-06 19:23:04 +08:00
|
|
|
return a
|
|
|
|
"},
|
2021-09-07 00:20:40 +08:00
|
|
|
indoc! {"
|
|
|
|
def foo(a: float):
|
|
|
|
a + 1.0
|
|
|
|
"},
|
|
|
|
indoc! {"
|
|
|
|
def f(b: int64) -> int32:
|
|
|
|
return 3
|
|
|
|
"},
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
"fn[[a=0], 0]",
|
|
|
|
"fn[[a=2], 4]",
|
|
|
|
"fn[[b=1], 0]",
|
|
|
|
],
|
|
|
|
vec![
|
|
|
|
"fun",
|
|
|
|
"foo",
|
|
|
|
"f"
|
2021-09-06 19:23:04 +08:00
|
|
|
]
|
|
|
|
)]
|
2021-09-07 00:20:40 +08:00
|
|
|
fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&str>) {
|
2021-09-06 19:23:04 +08:00
|
|
|
let mut composer = TopLevelComposer::new();
|
|
|
|
|
|
|
|
let resolver = Arc::new(Mutex::new(Box::new(Resolver {
|
|
|
|
id_to_def: Default::default(),
|
|
|
|
id_to_type: Default::default(),
|
|
|
|
class_names: Default::default(),
|
|
|
|
}) as Box<dyn SymbolResolver + Send + Sync>));
|
|
|
|
|
|
|
|
for s in source {
|
|
|
|
let ast = parse_program(s).unwrap();
|
|
|
|
let ast = ast[0].clone();
|
|
|
|
|
|
|
|
let (id, def_id) = composer.register_top_level(ast, Some(resolver.clone())).unwrap();
|
|
|
|
resolver.lock().add_id_def(id, def_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
composer.start_analysis().unwrap();
|
2021-09-07 00:20:40 +08:00
|
|
|
|
|
|
|
for (i, (def, _)) in composer.definition_ast_list.into_iter().enumerate() {
|
|
|
|
let def = &*def.read();
|
|
|
|
if let TopLevelDef::Function { signature, name, .. } = def {
|
|
|
|
let ty_str = composer.unifier.stringify(*signature, &mut |id| id.to_string(), &mut |id| id.to_string());
|
|
|
|
assert_eq!(ty_str, tys[i]);
|
|
|
|
assert_eq!(name, names[i]);
|
|
|
|
}
|
|
|
|
}
|
2021-09-06 19:23:04 +08:00
|
|
|
}
|