From 75e031745b29cd46fd2683328d4cf9e7b6eb3735 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Sat, 28 Jul 2018 17:43:05 -0700 Subject: [PATCH] Add while loop --- src/chalk/codegen.cr | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/chalk/codegen.cr b/src/chalk/codegen.cr index 7075b39..06c569d 100644 --- a/src/chalk/codegen.cr +++ b/src/chalk/codegen.cr @@ -103,8 +103,8 @@ module Chalk entry.is_a?(VarEntry) generate! tree.expr, table, entry.register, free when TreeIf - generate! tree.condition, table, target, free - sne target, 0 + generate! tree.condition, table, free, free + 1 + sne free, 0 jump_inst = jr 0 old_size = @instructions.size @@ -115,6 +115,18 @@ module Chalk old_size = @instructions.size generate! tree.otherwise, table, free, free + 1 if tree.otherwise jump_after.offset = @instructions.size - old_size + 1 + when TreeWhile + before_cond = @instructions.size + generate! tree.condition, table, free, free + 1 + sne target, 0 + cond_jump = jr 0 + + old_size = @instructions.size + generate! tree.block, table, free, free + 1 + after_jump = jr 0 + + cond_jump.offset = @instructions.size - old_size + 1 + after_jump.offset = before_cond - instructions.size + 1 when TreeReturn generate! tree.rvalue, table, RETURN_REG, free ret