Compare commits

...

2 commits

Author SHA1 Message Date
Edgar 8319844673
feat: more checks 2024-03-04 08:02:48 +01:00
Edgar f71abb0b26
fix: fix span in lalrpop 2024-03-04 07:55:12 +01:00
2 changed files with 32 additions and 27 deletions

View file

@ -479,8 +479,6 @@ fn lower_assign(builder: &mut BodyBuilder, info: &ast::AssignStmt) -> Result<(),
kind: ty, kind: ty,
}; };
dbg!("here1");
for _ in 0..info.deref_times { for _ in 0..info.deref_times {
match &ty.kind { match &ty.kind {
TypeKind::Ptr(is_mut, inner) => { TypeKind::Ptr(is_mut, inner) => {
@ -500,12 +498,8 @@ fn lower_assign(builder: &mut BodyBuilder, info: &ast::AssignStmt) -> Result<(),
place.projection.push(PlaceElem::Deref); place.projection.push(PlaceElem::Deref);
} }
dbg!("here2");
let (rvalue, _ty, _span) = lower_expr(builder, &info.value, Some(&ty))?; let (rvalue, _ty, _span) = lower_expr(builder, &info.value, Some(&ty))?;
dbg!("here3");
builder.statements.push(Statement { builder.statements.push(Statement {
span: Some(info.name.first.span), span: Some(info.name.first.span),
kind: StatementKind::Assign(place, rvalue), kind: StatementKind::Assign(place, rvalue),
@ -751,7 +745,7 @@ fn lower_binary_expr(
) -> Result<(ir::RValue, TypeKind, Span), LoweringError> { ) -> Result<(ir::RValue, TypeKind, Span), LoweringError> {
trace!("lowering binary op: {:?}", op); trace!("lowering binary op: {:?}", op);
let (lhs, lhs_ty, _) = if type_hint.is_none() { let (lhs, lhs_ty, lhs_span) = if type_hint.is_none() {
let ty = find_expr_type(builder, lhs) let ty = find_expr_type(builder, lhs)
.unwrap_or_else(|| find_expr_type(builder, rhs).expect("cant find type")); .unwrap_or_else(|| find_expr_type(builder, rhs).expect("cant find type"));
lower_expr( lower_expr(
@ -765,7 +759,7 @@ fn lower_binary_expr(
} else { } else {
lower_expr(builder, lhs, type_hint)? lower_expr(builder, lhs, type_hint)?
}; };
let (rhs, rhs_ty, _) = if type_hint.is_none() { let (rhs, rhs_ty, rhs_span) = if type_hint.is_none() {
let ty = find_expr_type(builder, rhs).unwrap_or(lhs_ty.clone()); let ty = find_expr_type(builder, rhs).unwrap_or(lhs_ty.clone());
lower_expr( lower_expr(
builder, builder,
@ -779,6 +773,17 @@ fn lower_binary_expr(
lower_expr(builder, rhs, type_hint)? lower_expr(builder, rhs, type_hint)?
}; };
if lhs_ty != rhs_ty {
return Err(LoweringError::UnexpectedType {
span: rhs_span,
found: rhs_ty,
expected: TypeInfo {
span: Some(lhs_span),
kind: lhs_ty,
},
});
}
let lhs = match lhs { let lhs = match lhs {
RValue::Use(op, _span) => op, RValue::Use(op, _span) => op,
lhs => { lhs => {

View file

@ -348,34 +348,34 @@ pub(crate) Expression: ast::Expression = {
} }
pub BinaryFirstLvlOp: ast::BinaryOp = { pub BinaryFirstLvlOp: ast::BinaryOp = {
<lo:@L> "==" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::Eq, ast::Span::new(hi, lo)), <lo:@L> "==" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::Eq, ast::Span::new(lo, hi)),
<lo:@L> "!=" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::NotEq, ast::Span::new(hi, lo)), <lo:@L> "!=" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::NotEq, ast::Span::new(lo, hi)),
<lo:@L> "<" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::Lt, ast::Span::new(hi, lo)), <lo:@L> "<" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::Lt, ast::Span::new(lo, hi)),
<lo:@L> ">" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::Gt, ast::Span::new(hi, lo)), <lo:@L> ">" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::Gt, ast::Span::new(lo, hi)),
<lo:@L> "<=" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::LtEq, ast::Span::new(hi, lo)), <lo:@L> "<=" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::LtEq, ast::Span::new(lo, hi)),
<lo:@L> ">=" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::GtEq, ast::Span::new(hi, lo)), <lo:@L> ">=" <hi:@R> => ast::BinaryOp::Compare(ast::CmpOp::GtEq, ast::Span::new(lo, hi)),
<lo:@L> "&&" <hi:@R> => ast::BinaryOp::Logic(ast::LogicOp::And, ast::Span::new(hi, lo)), <lo:@L> "&&" <hi:@R> => ast::BinaryOp::Logic(ast::LogicOp::And, ast::Span::new(lo, hi)),
<lo:@L> "||" <hi:@R> => ast::BinaryOp::Logic(ast::LogicOp::Or, ast::Span::new(hi, lo)), <lo:@L> "||" <hi:@R> => ast::BinaryOp::Logic(ast::LogicOp::Or, ast::Span::new(lo, hi)),
} }
pub BinarySecondLvlOp: ast::BinaryOp = { pub BinarySecondLvlOp: ast::BinaryOp = {
<lo:@L> "/" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Div, ast::Span::new(hi, lo)), <lo:@L> "/" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Div, ast::Span::new(lo, hi)),
<lo:@L> "*" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Mul, ast::Span::new(hi, lo)), <lo:@L> "*" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Mul, ast::Span::new(lo, hi)),
<lo:@L> "%" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Mod, ast::Span::new(hi, lo)), <lo:@L> "%" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Mod, ast::Span::new(lo, hi)),
} }
pub BinaryThirdLvlOp: ast::BinaryOp = { pub BinaryThirdLvlOp: ast::BinaryOp = {
<lo:@L> "+" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Add, ast::Span::new(hi, lo)), <lo:@L> "+" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Add, ast::Span::new(lo, hi)),
<lo:@L> "-" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Sub, ast::Span::new(hi, lo)), <lo:@L> "-" <hi:@R> => ast::BinaryOp::Arith(ast::ArithOp::Sub, ast::Span::new(lo, hi)),
<lo:@L> "&" <hi:@R> => ast::BinaryOp::Bitwise(ast::BitwiseOp::And, ast::Span::new(hi, lo)), <lo:@L> "&" <hi:@R> => ast::BinaryOp::Bitwise(ast::BitwiseOp::And, ast::Span::new(lo, hi)),
<lo:@L> "|" <hi:@R> => ast::BinaryOp::Bitwise(ast::BitwiseOp::Or, ast::Span::new(hi, lo)), <lo:@L> "|" <hi:@R> => ast::BinaryOp::Bitwise(ast::BitwiseOp::Or, ast::Span::new(lo, hi)),
<lo:@L> "^" <hi:@R> => ast::BinaryOp::Bitwise(ast::BitwiseOp::Xor, ast::Span::new(hi, lo)), <lo:@L> "^" <hi:@R> => ast::BinaryOp::Bitwise(ast::BitwiseOp::Xor, ast::Span::new(lo, hi)),
} }
pub UnaryOp: ast::UnaryOp = { pub UnaryOp: ast::UnaryOp = {
<lo:@L> "-" <hi:@R> => ast::UnaryOp::ArithNeg(ast::Span::new(hi, lo)), <lo:@L> "-" <hi:@R> => ast::UnaryOp::ArithNeg(ast::Span::new(lo, hi)),
<lo:@L> "!" <hi:@R> => ast::UnaryOp::LogicalNot(ast::Span::new(hi, lo)), <lo:@L> "!" <hi:@R> => ast::UnaryOp::LogicalNot(ast::Span::new(lo, hi)),
<lo:@L> "~" <hi:@R> => ast::UnaryOp::BitwiseNot(ast::Span::new(hi, lo)), <lo:@L> "~" <hi:@R> => ast::UnaryOp::BitwiseNot(ast::Span::new(lo, hi)),
} }
pub(crate) ValueExpr: ast::ValueExpr = { pub(crate) ValueExpr: ast::ValueExpr = {