fix: len counting label leading to incorrent jmp

replace/7746dba3cc6b3860afe1faf69e86ed84ee46988d
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 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<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
(if true
(print "True")
(print "False")))
(if true print_true print_false))