1
0

Pass all tests

This commit is contained in:
Jeff 2024-11-28 01:55:35 -05:00
parent 8b360d0825
commit 5432001dff
2 changed files with 21 additions and 22 deletions

View File

@ -532,14 +532,15 @@ impl<'src> Compiler<'src> {
let (previous_instruction, previous_type, previous_position) = let (previous_instruction, previous_type, previous_position) =
self.pop_last_instruction()?; self.pop_last_instruction()?;
let argument = if let Some(argument) = previous_instruction.destination_as_argument() { let (argument, push_back) = self.handle_binary_argument(&previous_instruction)?;
argument
} else { if push_back {
return Err(CompileError::ExpectedExpression { self.chunk.instructions_mut().push((
found: self.previous_token.to_owned(), previous_instruction,
position: previous_position, previous_type.clone(),
}); previous_position,
}; ))
}
let destination = Destination::Register(self.next_register()); let destination = Destination::Register(self.next_register());
let instruction = match operator.kind() { let instruction = match operator.kind() {
@ -773,12 +774,16 @@ impl<'src> Compiler<'src> {
}); });
} }
let argument = left_instruction.destination_as_argument().ok_or_else(|| { let (argument, push_back) = self.handle_binary_argument(&left_instruction)?;
CompileError::ExpectedExpression {
found: self.previous_token.to_owned(), if push_back {
position: left_position, self.chunk.instructions_mut().push((
} left_instruction,
})?; left_type.clone(),
left_position,
));
}
let operator = self.current_token; let operator = self.current_token;
let operator_position = self.current_position; let operator_position = self.current_position;
let rule = ParseRule::from(&operator); let rule = ParseRule::from(&operator);
@ -803,7 +808,6 @@ impl<'src> Compiler<'src> {
}); });
self.advance()?; self.advance()?;
self.emit_instruction(left_instruction, left_type, left_position);
self.emit_instruction(test, Type::None, operator_position); self.emit_instruction(test, Type::None, operator_position);
self.emit_instruction(jump, Type::None, operator_position); self.emit_instruction(jump, Type::None, operator_position);
self.parse_sub_expression(&rule.precedence)?; self.parse_sub_expression(&rule.precedence)?;
@ -864,7 +868,7 @@ impl<'src> Compiler<'src> {
self.parse_expression()?; self.parse_expression()?;
let register = self.next_register(); let register = self.next_register() - 1;
let set_local = Instruction::from(SetLocal { let set_local = Instruction::from(SetLocal {
register, register,
local_index, local_index,

View File

@ -114,11 +114,6 @@ fn variable_and() {
Type::None, Type::None,
Span(18, 19) Span(18, 19)
), ),
(
Instruction::get_local(Destination::Register(2), 0),
Type::Boolean,
Span(29, 30)
),
( (
Instruction::test(Argument::Local(0), true), Instruction::test(Argument::Local(0), true),
Type::None, Type::None,
@ -126,7 +121,7 @@ fn variable_and() {
), ),
(Instruction::jump(1, true), Type::None, Span(31, 33)), (Instruction::jump(1, true), Type::None, Span(31, 33)),
( (
Instruction::get_local(Destination::Register(3), 1), Instruction::get_local(Destination::Register(2), 1),
Type::Boolean, Type::Boolean,
Span(34, 35) Span(34, 35)
), ),