symbol_resolver: handle list and tuples

This commit is contained in:
pca006132 2021-08-24 14:58:19 +08:00
parent 56f082ca7c
commit c356062239

View File

@ -94,77 +94,110 @@ pub fn parse_type_annotation<T>(
}, },
Subscript { value, slice, .. } => { Subscript { value, slice, .. } => {
if let Name { id, .. } = &value.node { if let Name { id, .. } = &value.node {
if id == "virtual" { match id.as_str() {
let ty = parse_type_annotation( "virtual" => {
resolver, let ty = parse_type_annotation(
top_level_defs,
unifier,
primitives,
slice,
)?;
Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
} else {
let types = if let Tuple { elts, .. } = &slice.node {
elts.iter()
.map(|v| {
parse_type_annotation(
resolver,
top_level_defs,
unifier,
primitives,
v,
)
})
.collect::<Result<Vec<_>, _>>()?
} else {
vec![parse_type_annotation(
resolver, resolver,
top_level_defs, top_level_defs,
unifier, unifier,
primitives, primitives,
slice, slice,
)?] )?;
}; Ok(unifier.add_ty(TypeEnum::TVirtual { ty }))
}
"list" => {
let ty = parse_type_annotation(
resolver,
top_level_defs,
unifier,
primitives,
slice,
)?;
Ok(unifier.add_ty(TypeEnum::TList { ty }))
}
"tuple" => {
if let Tuple { elts, .. } = &slice.node {
let ty = elts
.iter()
.map(|elt| {
parse_type_annotation(
resolver,
top_level_defs,
unifier,
primitives,
elt,
)
})
.collect::<Result<Vec<_>, _>>()?;
Ok(unifier.add_ty(TypeEnum::TTuple { ty }))
} else {
Err("Expected multiple elements for tuple".into())
}
}
_ => {
let types = if let Tuple { elts, .. } = &slice.node {
elts.iter()
.map(|v| {
parse_type_annotation(
resolver,
top_level_defs,
unifier,
primitives,
v,
)
})
.collect::<Result<Vec<_>, _>>()?
} else {
vec![parse_type_annotation(
resolver,
top_level_defs,
unifier,
primitives,
slice,
)?]
};
let obj_id = resolver 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 def = top_level_defs[obj_id.0].read(); let def = top_level_defs[obj_id.0].read();
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def { if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
if types.len() != type_vars.len() { if types.len() != type_vars.len() {
return Err(format!( return Err(format!(
"Unexpected number of type parameters: expected {} but got {}", "Unexpected number of type parameters: expected {} but got {}",
type_vars.len(), type_vars.len(),
types.len() types.len()
)); ));
} }
let mut subst = HashMap::new(); let mut subst = HashMap::new();
for (var, ty) in izip!(type_vars.iter(), types.iter()) { for (var, ty) in izip!(type_vars.iter(), types.iter()) {
let id = if let TypeEnum::TVar { id, .. } = &*unifier.get_ty(var.1) { let id = if let TypeEnum::TVar { id, .. } = &*unifier.get_ty(var.1)
*id {
} else { *id
unreachable!() } else {
}; unreachable!()
subst.insert(id, *ty); };
} subst.insert(id, *ty);
let mut fields = fields }
.iter() let mut fields = fields
.map(|(attr, ty)| { .iter()
.map(|(attr, ty)| {
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
(attr.clone(), ty)
})
.collect::<HashMap<_, _>>();
fields.extend(methods.iter().map(|(attr, ty, _)| {
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty); let ty = unifier.subst(*ty, &subst).unwrap_or(*ty);
(attr.clone(), ty) (attr.clone(), ty)
}) }));
.collect::<HashMap<_, _>>(); Ok(unifier.add_ty(TypeEnum::TObj {
fields.extend(methods.iter().map(|(attr, ty, _)| { obj_id,
let ty = unifier.subst(*ty, &subst).unwrap_or(*ty); fields: fields.into(),
(attr.clone(), ty) params: subst.into(),
})); }))
Ok(unifier.add_ty(TypeEnum::TObj { } else {
obj_id, Err("Cannot use function name as type".into())
fields: fields.into(), }
params: subst.into(),
}))
} else {
Err("Cannot use function name as type".into())
} }
} }
} else { } else {