1
0

Small refactor to jump position

This commit is contained in:
Jeff 2024-09-27 13:16:41 -04:00
parent d0d80cf407
commit 88684f49b6
2 changed files with 7 additions and 9 deletions

View File

@ -752,7 +752,7 @@ impl<'src> Parser<'src> {
); );
} }
let jump_position = if matches!( if matches!(
self.chunk.get_last_n_operations(), self.chunk.get_last_n_operations(),
[ [
Some(Operation::LoadBoolean), Some(Operation::LoadBoolean),
@ -760,13 +760,11 @@ impl<'src> Parser<'src> {
Some(Operation::Jump) Some(Operation::Jump)
] ]
) { ) {
self.chunk.pop_instruction(self.current_position)?;
self.chunk.pop_instruction(self.current_position)?; self.chunk.pop_instruction(self.current_position)?;
self.chunk.pop_instruction(self.current_position)?; self.chunk.pop_instruction(self.current_position)?;
self.decrement_register()?; self.decrement_register()?;
self.chunk.pop_instruction(self.current_position)?.1 }
} else {
self.current_position
};
let jump_start = self.chunk.len(); let jump_start = self.chunk.len();
@ -791,7 +789,7 @@ impl<'src> Parser<'src> {
self.chunk.insert_instruction( self.chunk.insert_instruction(
jump_start, jump_start,
Instruction::jump(jump_distance as u8, true), Instruction::jump(jump_distance as u8, true),
jump_position, self.current_position,
); );
if self.allow(TokenKind::Else)? { if self.allow(TokenKind::Else)? {

View File

@ -191,7 +191,7 @@ fn equality_assignment_long() {
.set_c_is_constant(), .set_c_is_constant(),
Span(13, 15) Span(13, 15)
), ),
(Instruction::jump(1, true), Span(13, 15)), (Instruction::jump(1, true), Span(27, 31)),
(Instruction::load_boolean(0, true, false), Span(20, 24)), (Instruction::load_boolean(0, true, false), Span(20, 24)),
(Instruction::load_boolean(0, false, false), Span(34, 39)), (Instruction::load_boolean(0, false, false), Span(34, 39)),
(Instruction::define_local(0, 0, false), Span(4, 5)), (Instruction::define_local(0, 0, false), Span(4, 5)),
@ -241,7 +241,7 @@ fn if_expression() {
.set_c_is_constant(), .set_c_is_constant(),
Span(5, 7) Span(5, 7)
), ),
(Instruction::jump(1, true), Span(5, 7)), (Instruction::jump(1, true), Span(15, 15)),
(Instruction::load_constant(0, 2, false), Span(12, 13)), (Instruction::load_constant(0, 2, false), Span(12, 13)),
], ],
vec![Value::integer(1), Value::integer(1), Value::integer(2)], vec![Value::integer(1), Value::integer(1), Value::integer(2)],
@ -264,7 +264,7 @@ fn if_else_expression() {
.set_c_is_constant(), .set_c_is_constant(),
Span(5, 7) Span(5, 7)
), ),
(Instruction::jump(1, true), Span(5, 7)), (Instruction::jump(1, true), Span(16, 20)),
(Instruction::load_constant(0, 2, true), Span(12, 13)), (Instruction::load_constant(0, 2, true), Span(12, 13)),
(Instruction::load_constant(0, 3, false), Span(23, 24)), (Instruction::load_constant(0, 3, false), Span(23, 24)),
], ],