|
1 | 1 | use xlang::abi::collection::{HashMap, HashSet};
|
2 | 2 |
|
3 | 3 | use crate::{
|
4 |
| - ast::{self, Mutability, Safety, StringType}, |
| 4 | + ast::{self, Mutability, Safety, StringType, UnaryOp}, |
5 | 5 | helpers::{CyclicOperationStatus, FetchIncrement, TabPrinter},
|
6 | 6 | interning::Symbol,
|
7 | 7 | span::Span,
|
@@ -82,6 +82,10 @@ pub enum ThirExprInner {
|
82 | 82 | Tuple(Vec<Spanned<ThirExpr>>),
|
83 | 83 | Ctor(Spanned<ThirConstructor>),
|
84 | 84 | MemberAccess(Box<Spanned<ThirExpr>>, Spanned<FieldName>),
|
| 85 | + UnaryExpr( |
| 86 | + Spanned<UnaryOp>, |
| 87 | + Box<Spanned<ThirExpr>>, |
| 88 | + ), |
85 | 89 | BinaryExpr(
|
86 | 90 | Spanned<BinaryOp>,
|
87 | 91 | Box<Spanned<ThirExpr>>,
|
@@ -176,6 +180,9 @@ impl core::fmt::Display for ThirExprInner {
|
176 | 180 | ThirExprInner::BinaryExpr(op, lhs, rhs) => {
|
177 | 181 | write!(f, "({} {} {})", lhs.body, op.body, rhs.body)
|
178 | 182 | }
|
| 183 | + ThirExprInner::UnaryExpr(op, val) => { |
| 184 | + write!(f, "({}{})", op.body, val.body) |
| 185 | + } |
179 | 186 | ThirExprInner::Array(elements) => {
|
180 | 187 | write!(f, "[")?;
|
181 | 188 | let mut sep = "";
|
@@ -673,7 +680,16 @@ impl<'a> ThirConverter<'a> {
|
673 | 680 |
|
674 | 681 | Ok(ThirExpr { ty, cat, inner })
|
675 | 682 | }
|
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 | + } |
677 | 693 | hir::HirExpr::BinaryExpr(op, lhs, rhs) => {
|
678 | 694 | let lhs = self.convert_rvalue(lhs)?;
|
679 | 695 | let rhs = self.convert_rvalue(rhs)?;
|
@@ -1100,6 +1116,10 @@ impl<'a> Inferer<'a> {
|
1100 | 1116 | ThirExprInner::Index(base, index) => {
|
1101 | 1117 | todo!("index")
|
1102 | 1118 | }
|
| 1119 | + ThirExprInner::UnaryExpr(op, val) => { |
| 1120 | + status &= self.unify_types(&mut left.ty, &mut val.ty)?; |
| 1121 | + status &= self.unify_single_expr(val)?; |
| 1122 | + } |
1103 | 1123 | }
|
1104 | 1124 |
|
1105 | 1125 | Ok(status)
|
|
0 commit comments