Skip to content

Commit f5b4d3a

Browse files
committed
Added ability to resolve polymorphic types with polymorphic type catalogs, and updated function call resulting types to have polymorphs substituted
1 parent 00dbc07 commit f5b4d3a

File tree

3 files changed

+58
-3
lines changed

3 files changed

+58
-3
lines changed

src/resolve/error.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ pub enum ResolveErrorKind {
182182
CannotDeclareVariableOutsideFunction,
183183
CannotReturnOutsideFunction,
184184
CannotAssignVariableOutsideFunction,
185+
NonExistentPolymorph(String),
186+
PolymorphIsNotAType(String),
185187
Other {
186188
message: String,
187189
},
@@ -501,6 +503,12 @@ impl Display for ResolveErrorKind {
501503
"Cannot assign variable outside of function"
502504
)?;
503505
}
506+
ResolveErrorKind::NonExistentPolymorph(name) => {
507+
write!(f, "Non-existent polymorph '${}'", name)?;
508+
}
509+
ResolveErrorKind::PolymorphIsNotAType(name) => {
510+
write!(f, "Polymorph '${}' is not a type", name)?;
511+
}
504512
ResolveErrorKind::Other { message } => {
505513
write!(f, "{}", message)?;
506514
}

src/resolve/expr/call.rs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ use crate::{
77
error::{ResolveError, ResolveErrorKind},
88
Initialized,
99
},
10-
resolved::{self, Cast, CastFrom, TypedExpr},
10+
resolved::{self, Cast, CastFrom, PolyValue, TypedExpr},
1111
source_files::Source,
1212
};
13+
use indexmap::IndexMap;
1314
use itertools::Itertools;
1415
use num::BigInt;
1516

@@ -265,8 +266,6 @@ pub fn resolve_call_expr(
265266
};
266267

267268
let function = ctx.resolved_ast.functions.get(callee.function).unwrap();
268-
let return_type = function.return_type.clone();
269-
270269
let num_required = function.parameters.required.len();
271270

272271
for (i, argument) in arguments.iter_mut().enumerate() {
@@ -329,6 +328,8 @@ pub fn resolve_call_expr(
329328
}
330329
}
331330

331+
let return_type = resolve_polymorphs(&callee.recipe, &function.return_type)?;
332+
332333
if let Some(required_ty) = &call.expected_to_return {
333334
let resolved_required_ty = ctx.type_ctx().resolve(required_ty)?;
334335

@@ -355,3 +356,48 @@ pub fn resolve_call_expr(
355356
),
356357
))
357358
}
359+
360+
pub fn resolve_polymorphs<'a>(
361+
polymorphs: &IndexMap<String, PolyValue>,
362+
ty: &resolved::Type,
363+
) -> Result<resolved::Type, ResolveError> {
364+
Ok(match &ty.kind {
365+
resolved::TypeKind::Unresolved => panic!(),
366+
resolved::TypeKind::Boolean
367+
| resolved::TypeKind::Integer(_, _)
368+
| resolved::TypeKind::CInteger(_, _)
369+
| resolved::TypeKind::IntegerLiteral(_)
370+
| resolved::TypeKind::FloatLiteral(_)
371+
| resolved::TypeKind::Void
372+
| resolved::TypeKind::Floating(_) => ty.clone(),
373+
resolved::TypeKind::Pointer(inner) => {
374+
resolved::TypeKind::Pointer(Box::new(resolve_polymorphs(polymorphs, inner)?))
375+
.at(ty.source)
376+
}
377+
resolved::TypeKind::AnonymousStruct() => todo!(),
378+
resolved::TypeKind::AnonymousUnion() => todo!(),
379+
resolved::TypeKind::AnonymousEnum(_) => todo!(),
380+
resolved::TypeKind::FixedArray(fixed_array) => {
381+
resolved::TypeKind::FixedArray(Box::new(resolved::FixedArray {
382+
size: fixed_array.size,
383+
inner: resolve_polymorphs(polymorphs, &fixed_array.inner)?,
384+
}))
385+
.at(ty.source)
386+
}
387+
resolved::TypeKind::FunctionPointer(_) => todo!(),
388+
resolved::TypeKind::Enum(_, _) => todo!(),
389+
resolved::TypeKind::Structure(_, _) => todo!(),
390+
resolved::TypeKind::TypeAlias(_, _) => todo!(),
391+
resolved::TypeKind::Polymorph(name, _) => {
392+
let Some(value) = polymorphs.get(name) else {
393+
return Err(ResolveErrorKind::NonExistentPolymorph(name.clone()).at(ty.source));
394+
};
395+
396+
let PolyValue::PolyType(poly_type) = value else {
397+
return Err(ResolveErrorKind::PolymorphIsNotAType(name.clone()).at(ty.source));
398+
};
399+
400+
poly_type.resolved_type.clone()
401+
}
402+
})
403+
}

src/resolved/polymorph.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,4 @@ impl PolyCatalog {
112112
self.polymorphs.get(name)
113113
}
114114
}
115+

0 commit comments

Comments
 (0)