1
0

Pass tests

This commit is contained in:
Jeff 2024-09-24 17:49:30 -04:00
parent 7afde989f9
commit 95e22aa97f

View File

@ -450,7 +450,7 @@ impl<'src> Parser<'src> {
instructions.sort_by_key(|(instruction, _)| instruction.a());
for (mut instruction, position) in instructions {
for (instruction, position) in instructions {
self.emit_instruction(instruction, position);
}
@ -728,8 +728,24 @@ impl<'src> Parser<'src> {
fn parse_if(&mut self, allow_assignment: bool, allow_return: bool) -> Result<(), ParseError> {
self.advance()?;
self.parse_expression()?;
let is_explicit_true = matches!(self.previous_token, Token::Boolean("true"))
&& matches!(self.current_token, Token::LeftCurlyBrace);
let (mut load_boolean, load_boolean_position) =
self.chunk.pop_instruction(self.current_position)?;
debug_assert_eq!(load_boolean.operation(), Operation::LoadBoolean);
load_boolean.set_c_to_boolean(is_explicit_true);
self.emit_instruction(load_boolean, load_boolean_position);
if is_explicit_true {
self.increment_register()?;
}
let jump_position = if matches!(
self.chunk.get_last_n_operations(),
[
@ -746,17 +762,13 @@ impl<'src> Parser<'src> {
self.current_position
};
if let Some(Operation::LoadBoolean) = self.chunk.get_last_operation() {
let (mut load_boolean, load_boolean_position) =
self.chunk.pop_instruction(self.current_position)?;
load_boolean.set_c_to_boolean(true);
self.emit_instruction(load_boolean, load_boolean_position);
self.increment_register()?;
}
let jump_start = self.chunk.len();
let load_boolean_index =
if let Operation::LoadBoolean = self.chunk.get_last_operation().unwrap() {
Some(self.chunk.len().saturating_sub(1))
} else {
None
};
if let Token::LeftCurlyBrace = self.current_token {
self.parse_block(allow_assignment, allow_return)?;
@ -765,12 +777,6 @@ impl<'src> Parser<'src> {
let jump_end = self.chunk.len();
let jump_distance = jump_end.saturating_sub(jump_start);
self.chunk.insert_instruction(
jump_start,
Instruction::jump(jump_distance as u8, true),
jump_position,
);
if self.allow(TokenKind::Else)? {
if let Token::If = self.current_token {
self.parse_if(allow_assignment, allow_return)?;
@ -781,6 +787,14 @@ impl<'src> Parser<'src> {
}
}
self.chunk.insert_instruction(
jump_start,
Instruction::jump(jump_distance as u8, true),
jump_position,
);
if let Some(index) = load_boolean_index {}
Ok(())
}