Skip to content

Commit cca5699

Browse files
author
Ray Redondo
committed
feat(rust-hir): start work on if statements
1 parent fa487d4 commit cca5699

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

rust/src/sema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3459,6 +3459,7 @@ pub fn tycheck_values(defs: &mut Definitions, curmod: DefId) -> Result<()> {
34593459
let (stmts, safety) = match block.body.body.body {
34603460
hir::HirBlock::Normal(stmts) => (stmts, Safety::Safe),
34613461
hir::HirBlock::Unsafe(stmts) => (stmts, Safety::Unsafe),
3462+
hir::HirBlock::If { .. } => todo!("if"),
34623463
hir::HirBlock::Loop(_) => unreachable!(),
34633464
};
34643465

rust/src/sema/hir.rs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,19 @@ pub enum HirStatement {
163163
},
164164
}
165165

166+
type HirSimpleBlock = Vec<Spanned<HirStatement>>;
167+
166168
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
167169
pub enum HirBlock {
168-
Normal(Vec<Spanned<HirStatement>>),
169-
Unsafe(Vec<Spanned<HirStatement>>),
170-
Loop(Vec<Spanned<HirStatement>>),
170+
Normal(HirSimpleBlock),
171+
Unsafe(HirSimpleBlock),
172+
Loop(HirSimpleBlock),
173+
If {
174+
cond: Spanned<HirExpr>,
175+
block: HirSimpleBlock,
176+
elseifs: Vec<(HirExpr, HirSimpleBlock)>,
177+
elseblock: HirSimpleBlock,
178+
},
171179
}
172180

173181
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
@@ -252,6 +260,32 @@ impl HirFunctionBody {
252260
tabs.fmt(f)?;
253261
f.write_str("}\n")
254262
}
263+
HirBlock::If {
264+
cond,
265+
block,
266+
elseifs,
267+
elseblock,
268+
} => {
269+
tabs.fmt(f)?;
270+
write!(f, "if {} {{\n", cond.body)?;
271+
let nested = tabs.nest();
272+
for stmt in block {
273+
self.display_statement(stmt, f, nested)?;
274+
}
275+
for (cond, block) in elseifs {
276+
write!(f, "}} else if {} {{\n", cond)?;
277+
for stmt in block {
278+
self.display_statement(stmt, f, nested)?;
279+
}
280+
}
281+
if elseblock.len() > 0 {
282+
f.write_str("} else {\n")?;
283+
for stmt in elseblock {
284+
self.display_statement(stmt, f, nested)?;
285+
}
286+
}
287+
f.write_str("}\n")
288+
}
255289
},
256290
HirStatement::Discard(expr) => f.write_fmt(format_args!("{}{};\n", tabs, expr.body)),
257291
HirStatement::Call {

rust/src/sema/tyck.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,7 @@ impl<'a> ThirConverter<'a> {
799799
.map(|stmt| self.convert_statement(stmt))
800800
.collect::<Result<_>>()?,
801801
)),
802+
hir::HirBlock::If { .. } => todo!("if"),
802803
}
803804
})?)),
804805
hir::HirStatement::Discard(expr) => {

0 commit comments

Comments
 (0)