Skip to content

Commit

Permalink
🐛 Fix unit types not having a correct span
Browse files Browse the repository at this point in the history
semver: patch
  • Loading branch information
Somfic committed Nov 2, 2024
1 parent f50bb5a commit c46b92c
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ pub mod parser;
pub mod passer;

const INPUT: &str = "
let a = 1 if true else 2
fn test() {
let a = 12 + {true + false;};
}
";

fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/parser/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ pub enum TypeValue<'de> {
impl Display for Type<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.value {
TypeValue::Unit => write!(f, "an unit"),
TypeValue::Unit => write!(f, "nothing"),
TypeValue::Boolean => write!(f, "a boolean"),
TypeValue::Integer => write!(f, "an integer"),
TypeValue::Decimal => write!(f, "a decimal"),
Expand Down
13 changes: 9 additions & 4 deletions src/parser/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::{
expression, statement, typing, Parser,
};
use crate::lexer::{TokenKind, TokenValue};
use miette::{Result, SourceSpan};
use miette::{Context, Result, SourceSpan};
use std::collections::HashMap;

#[derive(Debug, Clone, PartialEq, PartialOrd)]
Expand Down Expand Up @@ -282,7 +282,8 @@ fn block<'de>(parser: &mut Parser<'de>) -> Result<Expression<'de>> {
break;
}

let statement = crate::parser::statement::parse(parser, true)?;
let statement =
crate::parser::statement::parse(parser, true).wrap_err("while parsing block")?;
statements.push(statement);
}

Expand All @@ -293,13 +294,17 @@ fn block<'de>(parser: &mut Parser<'de>) -> Result<Expression<'de>> {
_ => unreachable!(),
},
_ => Expression::at(
SourceSpan::new(0.into(), 0),
statements
.last()
.map_or(SourceSpan::new(0.into(), 0), |s| s.span),
ExpressionValue::Primitive(Primitive::Unit),
),
}
} else {
Expression::at(
SourceSpan::new(0.into(), 0),
statements
.last()
.map_or(SourceSpan::new(0.into(), 0), |s| s.span),
ExpressionValue::Primitive(Primitive::Unit),
)
};
Expand Down
24 changes: 11 additions & 13 deletions src/passer/typing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ impl Passer for TypingPasser {

let types = expression.possible_types();

// Get only distinct types, by comparing the `TypeValue` enum
let types = types.into_iter().fold(vec![], |mut acc, ty| {
let distinct_types = types.clone().into_iter().fold(vec![], |mut acc, ty| {
if !acc.iter().any(|t: &Type<'_>| t.value == ty.value) {
acc.push(ty);
}
acc
});

if types.is_empty() || types.len() == 1 {
if distinct_types.is_empty() || distinct_types.len() == 1 {
return Ok(PasserResult::default());
}

Expand All @@ -50,17 +49,16 @@ impl Passer for TypingPasser {
fn check_statement(statement: &Statement<'_>) -> Result<PasserResult> {
let mut critical = vec![];

let types = statement
.possible_types()
.into_iter()
.fold(vec![], |mut acc, ty| {
if !acc.iter().any(|t: &Type<'_>| t.value == ty.value) {
acc.push(ty);
}
acc
});
let types = statement.possible_types();

if types.is_empty() || types.len() == 1 {
let distinct_types = types.clone().into_iter().fold(vec![], |mut acc, ty| {
if !acc.iter().any(|t: &Type<'_>| t.value == ty.value) {
acc.push(ty);
}
acc
});

if distinct_types.is_empty() || distinct_types.len() == 1 {
return Ok(PasserResult::default());
}

Expand Down

0 comments on commit c46b92c

Please sign in to comment.