Skip to content

Commit 3e7b68a

Browse files
author
Ray Redondo
committed
feat(rust-tyck): unary expr support
1 parent 2ff596b commit 3e7b68a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

rust/src/sema/mir.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,7 @@ impl<'a> MirConverter<'a> {
451451
| ThirExprInner::Ctor(_)
452452
| ThirExprInner::BinaryExpr(_, _, _)
453453
| ThirExprInner::Array(_)
454+
| ThirExprInner::UnaryExpr(_, _)
454455
| ThirExprInner::Index(_, _) => todo!(),
455456
}
456457
}
@@ -596,6 +597,7 @@ impl<'a> MirConverter<'a> {
596597
| ThirExprInner::Ctor(_)
597598
| ThirExprInner::BinaryExpr(_, _, _)
598599
| ThirExprInner::Array(_)
600+
| ThirExprInner::UnaryExpr(_, _)
599601
| ThirExprInner::Index(_, _) => unreachable!("cannot access"),
600602
}
601603
}
@@ -741,6 +743,7 @@ impl<'a> MirConverter<'a> {
741743
super::tyck::ThirExprInner::MemberAccess(_, _) => {
742744
panic!("only top level rvalues get lowered by `lower_expr`")
743745
}
746+
super::tyck::ThirExprInner::UnaryExpr(_, _) => todo!("unary expr"),
744747
}
745748
}
746749

@@ -892,7 +895,8 @@ impl<'a> MirConverter<'a> {
892895
| ThirExprInner::Ctor(_)
893896
| ThirExprInner::BinaryExpr(_, _, _)
894897
| ThirExprInner::Array(_)
895-
| ThirExprInner::Index(_, _) => unreachable!(),
898+
| ThirExprInner::Index(_, _)
899+
| ThirExprInner::UnaryExpr(_, _) => unreachable!(),
896900
}
897901

898902
Ok(())

rust/src/sema/tyck.rs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use xlang::abi::collection::{HashMap, HashSet};
22

33
use crate::{
4-
ast::{self, Mutability, Safety, StringType},
4+
ast::{self, Mutability, Safety, StringType, UnaryOp},
55
helpers::{CyclicOperationStatus, FetchIncrement, TabPrinter},
66
interning::Symbol,
77
span::Span,
@@ -82,6 +82,10 @@ pub enum ThirExprInner {
8282
Tuple(Vec<Spanned<ThirExpr>>),
8383
Ctor(Spanned<ThirConstructor>),
8484
MemberAccess(Box<Spanned<ThirExpr>>, Spanned<FieldName>),
85+
UnaryExpr(
86+
Spanned<UnaryOp>,
87+
Box<Spanned<ThirExpr>>,
88+
),
8589
BinaryExpr(
8690
Spanned<BinaryOp>,
8791
Box<Spanned<ThirExpr>>,
@@ -176,6 +180,9 @@ impl core::fmt::Display for ThirExprInner {
176180
ThirExprInner::BinaryExpr(op, lhs, rhs) => {
177181
write!(f, "({} {} {})", lhs.body, op.body, rhs.body)
178182
}
183+
ThirExprInner::UnaryExpr(op, val) => {
184+
write!(f, "({}{})", op.body, val.body)
185+
}
179186
ThirExprInner::Array(elements) => {
180187
write!(f, "[")?;
181188
let mut sep = "";
@@ -673,7 +680,16 @@ impl<'a> ThirConverter<'a> {
673680

674681
Ok(ThirExpr { ty, cat, inner })
675682
}
676-
hir::HirExpr::UnaryExpr(_, _) => todo!("unary op"),
683+
hir::HirExpr::UnaryExpr(op, val) => {
684+
let val = self.convert_rvalue(val)?;
685+
let ty = match &val.ty {
686+
Type::Int(x) => Type::Int(*x),
687+
Type::Float(x) => Type::Float(*x),
688+
Type::InferableInt(x) => Type::InferableInt(*x),
689+
_ => Type::Inferable(InferId(self.next_infer.fetch_increment())),
690+
};
691+
Ok(ThirExpr { ty, cat: ValueCategory::Rvalue, inner: ThirExprInner::UnaryExpr(*op, Box::new(val)) })
692+
}
677693
hir::HirExpr::BinaryExpr(op, lhs, rhs) => {
678694
let lhs = self.convert_rvalue(lhs)?;
679695
let rhs = self.convert_rvalue(rhs)?;
@@ -1100,6 +1116,10 @@ impl<'a> Inferer<'a> {
11001116
ThirExprInner::Index(base, index) => {
11011117
todo!("index")
11021118
}
1119+
ThirExprInner::UnaryExpr(op, val) => {
1120+
status &= self.unify_types(&mut left.ty, &mut val.ty)?;
1121+
status &= self.unify_single_expr(val)?;
1122+
}
11031123
}
11041124

11051125
Ok(status)

0 commit comments

Comments
 (0)