From 76dd657788325fe0f67f995f8995f1142887e3f0 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Wed, 2 Feb 2022 03:05:59 +0700 Subject: [PATCH] fix: len counting label leading to incorrent jmp --- blspc/src/compiler/compile.rs | 16 ++++++++++++++-- example/if.blsp | 6 +++--- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/blspc/src/compiler/compile.rs b/blspc/src/compiler/compile.rs index 9862107..12a255f 100644 --- a/blspc/src/compiler/compile.rs +++ b/blspc/src/compiler/compile.rs @@ -56,9 +56,9 @@ impl Compiler { let mut then = self.compile(cdr[1].clone())?; let mut else_ = self.compile(cdr[2].clone())?; - result.push(Instr::JumpIfFalse { to: then.len() + 1}); // +1 for the jump instr + result.push(Instr::JumpIfFalse { to: len(&then) + 1}); // +1 for the jump instr result.append(&mut then); - result.push(Instr::Jump { to: else_.len() }); + result.push(Instr::Jump { to: len(&else_) }); result.append(&mut else_); } _ => { @@ -158,4 +158,16 @@ impl Compiler { Ok(result) } +} + +fn len(vec: &Vec) -> usize { + let mut result = 0; + for i in vec { + match i { + Instr::Comment { .. } => {}, + Instr::Label { .. } => {}, + _ => { result += 1; }, + } + } + result } \ No newline at end of file diff --git a/example/if.blsp b/example/if.blsp index 2b008d1..6068454 100644 --- a/example/if.blsp +++ b/example/if.blsp @@ -1,4 +1,4 @@ +(fun print_true (print "True")) +(fun print_false (print "False")) (fun main - (if true - (print "True") - (print "False"))) \ No newline at end of file + (if true print_true print_false)) \ No newline at end of file