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

feat: simplify if compilation

This commit is contained in:
Natapat Samutpong 2022-01-27 09:12:49 +07:00
parent 432914bfb6
commit c80637cf6c
2 changed files with 5 additions and 17 deletions

View file

@ -50,12 +50,9 @@ impl Compiler {
"if" => { "if" => {
// TODO: Remove .clone() // TODO: Remove .clone()
let mut cond = self.compile(cdr[0].clone(), depth + 1)?; let mut cond = self.compile(cdr[0].clone(), depth + 1)?;
let cond_register = self.current_register();
result.append(&mut cond); result.append(&mut cond);
result.push(Instr::JumpIfFalse { result.push(Instr::PopJumpIfFalse {
cond: cond_register,
to: 999, // To be replaced later to: 999, // To be replaced later
label: self.next_label(), label: self.next_label(),
}); });
@ -68,8 +65,8 @@ impl Compiler {
let idx = result.len() - 1; let idx = result.len() - 1;
match result[idx] { match result[idx] {
Instr::JumpIfFalse { cond: c, to: _, label: l } => { Instr::PopJumpIfFalse { to: _, label: l } => {
result[idx] = Instr::JumpIfFalse { cond: c, to: else_label, label: l, }; result[idx] = Instr::PopJumpIfFalse { to: else_label, label: l, };
} }
_ => unreachable!(), _ => unreachable!(),
} }

View file

@ -81,14 +81,9 @@ pub enum Instr {
// Stack arithmetic. // Stack arithmetic.
Add { label: usize }, Sub { label: usize }, Add { label: usize }, Sub { label: usize },
Mul { label: usize }, Div { label: usize }, Mul { label: usize }, Div { label: usize },
// Immediate arithmetic.
IAdd { lhs: Register, rhs: Register, to: Register, label: usize },
ISub { lhs: Register, rhs: Register, to: Register, label: usize },
IMul { lhs: Register, rhs: Register, to: Register, label: usize },
IDiv { lhs: Register, rhs: Register, to: Register, label: usize },
// Jumping // Jumping
Jump { to: usize, label: usize }, Jump { to: usize, label: usize },
JumpIfFalse { cond: Register, to: usize, label: usize }, PopJumpIfFalse { to: usize, label: usize },
Return { value: Register, label: usize }, Return { value: Register, label: usize },
} }
@ -105,12 +100,8 @@ impl Display for Instr {
Instr::Sub { label } => write!(f, "{}: SUB", label), Instr::Sub { label } => write!(f, "{}: SUB", label),
Instr::Mul { label } => write!(f, "{}: MUL", label), Instr::Mul { label } => write!(f, "{}: MUL", label),
Instr::Div { label } => write!(f, "{}: DIV", label), Instr::Div { label } => write!(f, "{}: DIV", label),
Instr::IAdd { lhs, rhs, to, label } => write!(f, "{}: IADD {} {} {}", label, lhs, rhs, to),
Instr::ISub { lhs, rhs, to, label } => write!(f, "{}: ISUB {} {} {}", label, lhs, rhs, to),
Instr::IMul { lhs, rhs, to, label } => write!(f, "{}: IMUL {} {} {}", label, lhs, rhs, to),
Instr::IDiv { lhs, rhs, to, label } => write!(f, "{}: IDIV {} {} {}", label, lhs, rhs, to),
Instr::Jump { to, label } => write!(f, "{}: JUMP {}", label, to), Instr::Jump { to, label } => write!(f, "{}: JUMP {}", label, to),
Instr::JumpIfFalse { cond, to, label } => write!(f, "{}: JUMP_IF_FALSE {} {}", label, cond, to), Instr::PopJumpIfFalse { to, label } => write!(f, "{}: POP_JUMP_IF_FALSE {}", label, to),
Instr::Return { value, label } => write!(f, "{}: RETURN {}", label, value), Instr::Return { value, label } => write!(f, "{}: RETURN {}", label, value),
} }
} }