From 642dcb065ab7cca3c64632c3d4b728add7e2422b Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 4 Nov 2015 20:23:26 +0200 Subject: [PATCH] fix while statement logic was broken, or inversed had to lay the code out a bit funny, but it works and makes sense --- lib/soml/compiler/while_statement.rb | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/lib/soml/compiler/while_statement.rb b/lib/soml/compiler/while_statement.rb index 0a26b605..e5aa7aa2 100644 --- a/lib/soml/compiler/while_statement.rb +++ b/lib/soml/compiler/while_statement.rb @@ -6,24 +6,23 @@ module Soml branch_type , condition , statements = *statement condition = condition.first - add_code start = Register::Label.new(statement , "while_start" ) + condition_label = Register::Label.new(statement , "condition_label") + # unconditionally branch to the condition upon entering the loop + add_code Register::Branch.new(statement,condition_label) + add_code start = Register::Label.new(statement , "while_start" ) + reset_regs + process_all(statements) + + # This is where the loop starts, though in subsequent iterations it's in the middle + add_code condition_label reset_regs process(condition) branch_class = Object.const_get "Register::Is#{branch_type.capitalize}" # this is where the while ends and both branches meet - merge = Register::Label.new(statement , "while_merge") - add_code branch_class.new( condition , merge ) + add_code branch_class.new( condition , start ) - reset_regs - process_all(statements) - - # unconditionally branch to the start - add_code Register::Branch.new(statement,start) - - # continue execution / compiling at the merge block - add_code merge nil # statements don't return anything end end