Skip to content

Commit

Permalink
feat: check type correctness
Browse files Browse the repository at this point in the history
  • Loading branch information
edg-l committed Feb 27, 2024
1 parent cc1e367 commit a2a3bdb
Show file tree
Hide file tree
Showing 4 changed files with 193 additions and 32 deletions.
21 changes: 21 additions & 0 deletions lib/edlang_check/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,27 @@ pub fn lowering_error_to_report(
.with_message(format!("Unresolved type {:?}.", name))
.finish()
},
LoweringError::UnexpectedType { span, found, expected } => {
let mut labels = vec![
Label::new((path.clone(), span.into()))
.with_message(format!("Unexpected type '{}', expected '{}'", found, expected.kind))
.with_color(colors.next())
];

if let Some(span) = expected.span {
labels.push(
Label::new((path.clone(), span.into()))
.with_message(format!("expected '{}' due to this type", expected.kind))
.with_color(colors.next())
);
}

Report::build(ReportKind::Error, path.clone(), span.lo)
.with_code("E3")
.with_labels(labels)
.with_message(format!("expected type {}.", expected.kind))
.finish()
},
LoweringError::IdNotFound { span, id } => {
Report::build(ReportKind::Error, path.clone(), span.lo)
.with_code("E_ID")
Expand Down
58 changes: 52 additions & 6 deletions lib/edlang_ir/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
// Based on a cfg

use std::collections::{BTreeMap, HashMap, HashSet};
use std::{
collections::{BTreeMap, HashMap, HashSet},
fmt,
};

use edlang_span::Span;
use smallvec::SmallVec;
Expand Down Expand Up @@ -207,13 +210,13 @@ pub struct SwitchTarget {
pub targets: Vec<usize>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct TypeInfo {
pub span: Option<Span>,
pub kind: TypeKind,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum TypeKind {
Unit,
Bool,
Expand Down Expand Up @@ -268,7 +271,50 @@ impl TypeKind {
}
}

#[derive(Debug, Clone)]
impl fmt::Display for TypeKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
TypeKind::Unit => write!(f, "()"),
TypeKind::Bool => write!(f, "bool"),
TypeKind::Char => write!(f, "char"),
TypeKind::Int(ty) => match ty {
IntTy::I128 => write!(f, "i128"),
IntTy::I64 => write!(f, "i64"),
IntTy::I32 => write!(f, "i32"),
IntTy::I16 => write!(f, "i16"),
IntTy::I8 => write!(f, "i8"),
IntTy::Isize => write!(f, "isize"),
},
TypeKind::Uint(ty) => match ty {
UintTy::U128 => write!(f, "u128"),
UintTy::U64 => write!(f, "u64"),
UintTy::U32 => write!(f, "u32"),
UintTy::U16 => write!(f, "u16"),
UintTy::U8 => write!(f, "u8"),
UintTy::Usize => write!(f, "usize"),
},
TypeKind::Float(ty) => match ty {
FloatTy::F32 => write!(f, "f64"),
FloatTy::F64 => write!(f, "f32"),
},
TypeKind::FnDef(_, _) => todo!(),
TypeKind::Str => write!(f, "str"),
TypeKind::Ptr(is_mut, inner) => {
let word = if *is_mut { "mut" } else { "const" };

write!(f, "*{word} {}", inner.kind)
}
TypeKind::Ref(is_mut, inner) => {
let word = if *is_mut { "mut" } else { "const" };

write!(f, "&{word} {}", inner.kind)
}
TypeKind::Struct(_) => todo!(),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
pub enum IntTy {
I128,
I64,
Expand All @@ -278,7 +324,7 @@ pub enum IntTy {
Isize,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
pub enum UintTy {
U128,
U64,
Expand All @@ -288,7 +334,7 @@ pub enum UintTy {
Usize,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Copy)]
pub enum FloatTy {
F32,
F64,
Expand Down
7 changes: 7 additions & 0 deletions lib/edlang_lowering/src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use edlang_ast::{Ident, Span};
use edlang_ir::{TypeInfo, TypeKind};
use thiserror::Error;

use crate::DefId;
Expand Down Expand Up @@ -27,4 +28,10 @@ pub enum LoweringError {
IdNotFound { span: Span, id: DefId },
#[error("feature not yet implemented: {message}")]
NotYetImplemented { span: Span, message: &'static str },
#[error("unexpected type")]
UnexpectedType {
span: Span,
found: TypeKind,
expected: TypeInfo,
},
}
Loading

0 comments on commit a2a3bdb

Please sign in to comment.