1
0
This commit is contained in:
Jeff 2024-10-30 00:16:10 -04:00
parent e304195661
commit caf1c22af0
2 changed files with 13 additions and 23 deletions

View File

@ -44,8 +44,6 @@ pub struct Parser<'src> {
previous_token: Token<'src>, previous_token: Token<'src>,
previous_position: Span, previous_position: Span,
parsed_expression: bool,
} }
impl<'src> Parser<'src> { impl<'src> Parser<'src> {
@ -68,7 +66,6 @@ impl<'src> Parser<'src> {
current_position, current_position,
previous_token: Token::Eof, previous_token: Token::Eof,
previous_position: Span(0, 0), previous_position: Span(0, 0),
parsed_expression: false,
}) })
} }
@ -150,7 +147,7 @@ impl<'src> Parser<'src> {
self.chunk.push_instruction(instruction, position); self.chunk.push_instruction(instruction, position);
} }
fn end_statement(&mut self) { fn optimize_statement(&mut self) {
if let Some( if let Some(
[Operation::LoadBoolean | Operation::LoadConstant, Operation::LoadBoolean | Operation::LoadConstant, Operation::Jump, Operation::Equal | Operation::Less | Operation::LessEqual], [Operation::LoadBoolean | Operation::LoadConstant, Operation::LoadBoolean | Operation::LoadConstant, Operation::Jump, Operation::Equal | Operation::Less | Operation::LessEqual],
) = self.get_end_of_statement() ) = self.get_end_of_statement()
@ -177,10 +174,6 @@ impl<'src> Parser<'src> {
second_loader_new.set_c_to_boolean(second_loader.c_is_constant()); second_loader_new.set_c_to_boolean(second_loader.c_is_constant());
*second_loader = second_loader_new; *second_loader = second_loader_new;
let jump = instructions.next().unwrap();
jump.set_b(jump.b() - 1);
} }
self.current_statement_length = 0; self.current_statement_length = 0;
@ -848,7 +841,7 @@ impl<'src> Parser<'src> {
Instruction::set_local(register, local_index), Instruction::set_local(register, local_index),
start_position, start_position,
); );
self.end_statement(); self.optimize_statement();
self.current_is_expression = false; self.current_is_expression = false;
} else { } else {
@ -1023,7 +1016,7 @@ impl<'src> Parser<'src> {
.last() .last()
.map_or(false, |(instruction, _)| instruction.yields_value()); .map_or(false, |(instruction, _)| instruction.yields_value());
self.end_statement(); self.optimize_statement();
Ok(()) Ok(())
} }
@ -1058,7 +1051,7 @@ impl<'src> Parser<'src> {
let jump_back = Instruction::jump(jump_start); let jump_back = Instruction::jump(jump_start);
self.emit_instruction(jump_back, self.current_position); self.emit_instruction(jump_back, self.current_position);
self.end_statement(); self.optimize_statement();
self.current_is_expression = false; self.current_is_expression = false;
@ -1121,7 +1114,7 @@ impl<'src> Parser<'src> {
let end = self.current_position.1; let end = self.current_position.1;
self.emit_instruction(Instruction::r#return(has_return_value), Span(start, end)); self.emit_instruction(Instruction::r#return(has_return_value), Span(start, end));
self.end_statement(); self.optimize_statement();
self.current_is_expression = false; self.current_is_expression = false;
@ -1198,7 +1191,7 @@ impl<'src> Parser<'src> {
Instruction::define_local(register, local_index, is_mutable), Instruction::define_local(register, local_index, is_mutable),
position, position,
); );
self.end_statement(); self.optimize_statement();
self.current_is_expression = false; self.current_is_expression = false;
@ -1331,12 +1324,12 @@ impl<'src> Parser<'src> {
Instruction::define_local(register, local_index, false), Instruction::define_local(register, local_index, false),
identifier_position, identifier_position,
); );
self.end_statement(); self.optimize_statement();
self.current_is_expression = false; self.current_is_expression = false;
} else { } else {
self.emit_constant(function, Span(function_start, function_end))?; self.emit_constant(function, Span(function_start, function_end))?;
self.end_statement(); self.optimize_statement();
self.current_is_expression = true; self.current_is_expression = true;
} }
@ -1398,7 +1391,7 @@ impl<'src> Parser<'src> {
fn parse_semicolon(&mut self, _: Allowed) -> Result<(), ParseError> { fn parse_semicolon(&mut self, _: Allowed) -> Result<(), ParseError> {
self.current_is_expression = false; self.current_is_expression = false;
self.end_statement(); self.optimize_statement();
self.advance() self.advance()
} }

View File

@ -1,12 +1,9 @@
fn fib (n: int) -> int { fn fib (n: int) -> int {
if n <= 0 { if n <= 0 { 0 }
0 if n == 1 {
1
} else { } else {
if n == 1 { fib(n - 1) + fib(n - 2)
1
} else {
fib(n - 1) + fib(n - 2)
}
} }
} }