1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

fix: len counting label leading to incorrent jmp

This commit is contained in:
Natapat Samutpong 2022-02-02 03:05:59 +07:00
parent 9f7e80dae2
commit 76dd657788
2 changed files with 17 additions and 5 deletions

View file

@ -56,9 +56,9 @@ impl Compiler {
let mut then = self.compile(cdr[1].clone())?; let mut then = self.compile(cdr[1].clone())?;
let mut else_ = self.compile(cdr[2].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.append(&mut then);
result.push(Instr::Jump { to: else_.len() }); result.push(Instr::Jump { to: len(&else_) });
result.append(&mut else_); result.append(&mut else_);
} }
_ => { _ => {
@ -159,3 +159,15 @@ impl Compiler {
Ok(result) Ok(result)
} }
} }
fn len(vec: &Vec<Instr>) -> usize {
let mut result = 0;
for i in vec {
match i {
Instr::Comment { .. } => {},
Instr::Label { .. } => {},
_ => { result += 1; },
}
}
result
}

View file

@ -1,4 +1,4 @@
(fun print_true (print "True"))
(fun print_false (print "False"))
(fun main (fun main
(if true (if true print_true print_false))
(print "True")
(print "False")))