From ef01a86c2ffff051086799585bfc47b1a4e39282 Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 30 Mar 2020 18:23:32 +0800 Subject: [PATCH] support while loop --- src/main.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main.rs b/src/main.rs index c4768c54..128e1036 100644 --- a/src/main.rs +++ b/src/main.rs @@ -423,6 +423,32 @@ impl<'ctx> CodeGen<'ctx> { self.builder.build_unconditional_branch(cont_bb); self.builder.position_at_end(cont_bb); }, + While { test, body, orelse } => { + let parent = self.builder.get_insert_block().unwrap().get_parent().unwrap(); + let test_bb = self.context.append_basic_block(parent, "test"); + self.builder.build_unconditional_branch(test_bb); + self.builder.position_at_end(test_bb); + let test = self.compile_expression(test)?; + if test.get_type() != self.context.bool_type().into() { + return Err(self.compile_error(CompileErrorKind::IncompatibleTypes)); + } + + let then_bb = self.context.append_basic_block(parent, "then"); + let else_bb = self.context.append_basic_block(parent, "else"); + let cont_bb = self.context.append_basic_block(parent, "ifcont"); + self.builder.build_conditional_branch(test.into_int_value(), then_bb, else_bb); + + self.builder.position_at_end(then_bb); + self.compile_suite(body, return_type)?; + self.builder.build_unconditional_branch(test_bb); + + self.builder.position_at_end(else_bb); + if let Some(orelse) = orelse { + self.compile_suite(orelse, return_type)?; + } + self.builder.build_unconditional_branch(cont_bb); + self.builder.position_at_end(cont_bb); + }, Return { value: Some(value) } => { if let Some(return_type) = return_type { let value = self.compile_expression(value)?;