hm-inference #6
|
@ -36,15 +36,14 @@ pub trait SymbolResolver {
|
||||||
// handle function call etc.
|
// handle function call etc.
|
||||||
}
|
}
|
||||||
|
|
||||||
impl dyn SymbolResolver {
|
// convert type annotation into type
|
||||||
// convert type annotation into type
|
pub fn parse_type_annotation<T>(
|
||||||
pub fn parse_type_annotation<T>(
|
resolver: &dyn SymbolResolver,
|
||||||
&self,
|
|
||||||
top_level: &TopLevelContext,
|
top_level: &TopLevelContext,
|
||||||
unifier: &mut Unifier,
|
unifier: &mut Unifier,
|
||||||
primitives: &PrimitiveStore,
|
primitives: &PrimitiveStore,
|
||||||
expr: &Expr<T>,
|
expr: &Expr<T>,
|
||||||
) -> Result<Type, String> {
|
) -> Result<Type, String> {
|
||||||
use rustpython_parser::ast::ExprKind::*;
|
use rustpython_parser::ast::ExprKind::*;
|
||||||
match &expr.node {
|
match &expr.node {
|
||||||
Name { id, .. } => match id.as_str() {
|
Name { id, .. } => match id.as_str() {
|
||||||
|
@ -54,7 +53,7 @@ impl dyn SymbolResolver {
|
||||||
"bool" => Ok(primitives.bool),
|
"bool" => Ok(primitives.bool),
|
||||||
"None" => Ok(primitives.none),
|
"None" => Ok(primitives.none),
|
||||||
x => {
|
x => {
|
||||||
let obj_id = self.get_identifier_def(x);
|
let obj_id = resolver.get_identifier_def(x);
|
||||||
if let Some(obj_id) = obj_id {
|
if let Some(obj_id) = obj_id {
|
||||||
let defs = top_level.definitions.read();
|
let defs = top_level.definitions.read();
|
||||||
let def = defs[obj_id.0].read();
|
let def = defs[obj_id.0].read();
|
||||||
|
@ -82,7 +81,7 @@ impl dyn SymbolResolver {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// it could be a type variable
|
// it could be a type variable
|
||||||
let ty = self
|
let ty = resolver
|
||||||
.get_symbol_type(unifier, primitives, x)
|
.get_symbol_type(unifier, primitives, x)
|
||||||
.ok_or_else(|| "Cannot use function name as type".to_owned())?;
|
.ok_or_else(|| "Cannot use function name as type".to_owned())?;
|
||||||
if let TypeEnum::TVar { .. } = &*unifier.get_ty(ty) {
|
if let TypeEnum::TVar { .. } = &*unifier.get_ty(ty) {
|
||||||
|
@ -96,21 +95,18 @@ impl dyn SymbolResolver {
|
||||||
Subscript { value, slice, .. } => {
|
Subscript { value, slice, .. } => {
|
||||||
if let Name { id, .. } = &value.node {
|
if let Name { id, .. } = &value.node {
|
||||||
if id == "virtual" {
|
if id == "virtual" {
|
||||||
let ty =
|
let ty = parse_type_annotation(resolver, top_level, unifier, primitives, slice)?;
|
||||||
self.parse_type_annotation(top_level, unifier, primitives, slice)?;
|
|
||||||
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
|
||||||
} else {
|
} else {
|
||||||
let types = if let Tuple { elts, .. } = &slice.node {
|
let types = if let Tuple { elts, .. } = &slice.node {
|
||||||
elts.iter()
|
elts.iter()
|
||||||
.map(|v| {
|
.map(|v| parse_type_annotation(resolver, top_level, unifier, primitives, v))
|
||||||
self.parse_type_annotation(top_level, unifier, primitives, v)
|
|
||||||
})
|
|
||||||
.collect::<Result<Vec<_>, _>>()?
|
.collect::<Result<Vec<_>, _>>()?
|
||||||
} else {
|
} else {
|
||||||
vec![self.parse_type_annotation(top_level, unifier, primitives, slice)?]
|
vec![parse_type_annotation(resolver, top_level, unifier, primitives, slice)?]
|
||||||
};
|
};
|
||||||
|
|
||||||
let obj_id = self
|
let obj_id = resolver
|
||||||
.get_identifier_def(id)
|
.get_identifier_def(id)
|
||||||
.ok_or_else(|| format!("Unknown type annotation {}", id))?;
|
.ok_or_else(|| format!("Unknown type annotation {}", id))?;
|
||||||
let defs = top_level.definitions.read();
|
let defs = top_level.definitions.read();
|
||||||
|
@ -143,7 +139,11 @@ impl dyn SymbolResolver {
|
||||||
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
|
||||||
(attr.clone(), ty)
|
(attr.clone(), ty)
|
||||||
}));
|
}));
|
||||||
Ok(unifier.add_ty(TypeEnum::TObj { obj_id, fields: fields.into(), params: subst.into() }))
|
Ok(unifier.add_ty(TypeEnum::TObj {
|
||||||
|
obj_id,
|
||||||
|
fields: fields.into(),
|
||||||
|
params: subst.into(),
|
||||||
|
}))
|
||||||
} else {
|
} else {
|
||||||
Err("Cannot use function name as type".into())
|
Err("Cannot use function name as type".into())
|
||||||
}
|
}
|
||||||
|
@ -154,5 +154,28 @@ impl dyn SymbolResolver {
|
||||||
}
|
}
|
||||||
_ => Err("unsupported type expression".into()),
|
_ => Err("unsupported type expression".into()),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl dyn SymbolResolver {
|
||||||
|
pub fn parse_type_annotation<T>(
|
||||||
|
&self,
|
||||||
|
top_level: &TopLevelContext,
|
||||||
|
unifier: &mut Unifier,
|
||||||
|
primitives: &PrimitiveStore,
|
||||||
|
expr: &Expr<T>,
|
||||||
|
) -> Result<Type, String> {
|
||||||
|
parse_type_annotation(self, top_level, unifier, primitives, expr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl dyn SymbolResolver + Send {
|
||||||
|
pub fn parse_type_annotation<T>(
|
||||||
|
&self,
|
||||||
|
top_level: &TopLevelContext,
|
||||||
|
unifier: &mut Unifier,
|
||||||
|
primitives: &PrimitiveStore,
|
||||||
|
expr: &Expr<T>,
|
||||||
|
) -> Result<Type, String> {
|
||||||
|
parse_type_annotation(self, top_level, unifier, primitives, expr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue