Skip to content

Commit

Permalink
Implemented initial parsing for polymorph constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Oct 30, 2024
1 parent 1e8a2e9 commit f3aa9bf
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/ast/datatype/kind/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
ast::{fmt_c_integer, FloatSize, IntegerBits},
ir::IntegerSign,
};
use itertools::Itertools;
use std::fmt::Display;

impl Display for &TypeKind {
Expand Down Expand Up @@ -48,8 +49,13 @@ impl Display for &TypeKind {
TypeKind::FunctionPointer(_function) => {
write!(f, "(function pointer type)")?;
}
TypeKind::Polymorph(polymorph) => {
TypeKind::Polymorph(polymorph, constraints) => {
write!(f, "${}", polymorph)?;

if !constraints.is_empty() {
write!(f, ": ")?;
write!(f, "{}", constraints.iter().map(|x| x.to_string()).join("+"))?;
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ast/datatype/kind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum TypeKind {
AnonymousUnion(AnoymousUnion),
AnonymousEnum(AnonymousEnum),
FunctionPointer(FunctionPointer),
Polymorph(String),
Polymorph(String, Vec<Type>),
}

impl TypeKind {
Expand Down
24 changes: 23 additions & 1 deletion src/parser/parse_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,29 @@ impl<'a, I: Inflow<Token>> Parser<'a, I> {
});
}

return Ok(TypeKind::Polymorph(token.kind.unwrap_polymorph()).at(source));
let polymorph = token.kind.unwrap_polymorph();
let mut constraints = vec![];

if self.input.eat(TokenKind::Colon) {
loop {
constraints
.push(self.parse_type(None::<&str>, Some("for polymorph constraint"))?);

// TODO: CLEANUP: Clean this code up
if let TypeKind::Polymorph(..) = constraints.last().unwrap().kind {
return Err(ParseErrorKind::Other {
message: "Polymorphs cannot be used as constraints".into(),
}
.at(constraints.last().unwrap().source));
}

if !self.input.eat(TokenKind::Add) {
break;
}
}
}

return Ok(TypeKind::Polymorph(polymorph, constraints).at(source));
};

let generics = self.parse_generics()?;
Expand Down
6 changes: 5 additions & 1 deletion src/resolve/type_ctx/resolve_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ impl<'a> ResolveTypeCtx<'a> {
},
))
}
ast::TypeKind::Polymorph(polymorph) => {
ast::TypeKind::Polymorph(polymorph, constaints) => {
if !constaints.is_empty() {
todo!("resolve polymorph constaints");
}

Ok(resolved::TypeKind::Polymorph(polymorph.clone()))
}
}
Expand Down

0 comments on commit f3aa9bf

Please sign in to comment.