forked from M-Labs/nac3
1
0
Fork 0

symbol_resolver: handle list and tuples

This commit is contained in:
pca006132 2021-08-24 14:58:19 +08:00
parent 56f082ca7c
commit c356062239
1 changed files with 96 additions and 63 deletions

View File

@ -94,7 +94,8 @@ 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() {
"virtual" => {
let ty = parse_type_annotation( let ty = parse_type_annotation(
resolver, resolver,
top_level_defs, top_level_defs,
@ -103,7 +104,37 @@ pub fn parse_type_annotation<T>(
slice, slice,
)?; )?;
Ok(unifier.add_ty(TypeEnum::TVirtual { ty })) 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 { } else {
Err("Expected multiple elements for tuple".into())
}
}
_ => {
let types = if let Tuple { elts, .. } = &slice.node { let types = if let Tuple { elts, .. } = &slice.node {
elts.iter() elts.iter()
.map(|v| { .map(|v| {
@ -140,7 +171,8 @@ pub fn parse_type_annotation<T>(
} }
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 *id
} else { } else {
unreachable!() unreachable!()
@ -167,6 +199,7 @@ pub fn parse_type_annotation<T>(
Err("Cannot use function name as type".into()) Err("Cannot use function name as type".into())
} }
} }
}
} else { } else {
Err("unsupported type expression".into()) Err("unsupported type expression".into())
} }