Simplify the parser by removing a value
This commit is contained in:
parent
9650183c73
commit
ea46bfe5da
@ -48,7 +48,6 @@ struct Parser<'src> {
|
|||||||
lexer: Lexer<'src>,
|
lexer: Lexer<'src>,
|
||||||
chunk: Chunk,
|
chunk: Chunk,
|
||||||
|
|
||||||
current_statement_length: usize,
|
|
||||||
current_is_expression: bool,
|
current_is_expression: bool,
|
||||||
minimum_register: u8,
|
minimum_register: u8,
|
||||||
|
|
||||||
@ -72,7 +71,6 @@ impl<'src> Parser<'src> {
|
|||||||
Ok(Parser {
|
Ok(Parser {
|
||||||
lexer,
|
lexer,
|
||||||
chunk: Chunk::new(None),
|
chunk: Chunk::new(None),
|
||||||
current_statement_length: 0,
|
|
||||||
current_is_expression: false,
|
current_is_expression: false,
|
||||||
minimum_register: 0,
|
minimum_register: 0,
|
||||||
current_token,
|
current_token,
|
||||||
@ -216,14 +214,12 @@ impl<'src> Parser<'src> {
|
|||||||
position.to_string()
|
position.to_string()
|
||||||
);
|
);
|
||||||
|
|
||||||
self.current_statement_length += 1;
|
|
||||||
|
|
||||||
self.chunk.instructions_mut().push((instruction, position));
|
self.chunk.instructions_mut().push((instruction, position));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn optimize_statement(&mut self) {
|
fn optimize_statement(&mut self) {
|
||||||
if matches!(
|
if matches!(
|
||||||
self.get_end_of_statement(),
|
self.get_last_instructions(),
|
||||||
Some([
|
Some([
|
||||||
Operation::LoadBoolean | Operation::LoadConstant,
|
Operation::LoadBoolean | Operation::LoadConstant,
|
||||||
Operation::LoadBoolean | Operation::LoadConstant,
|
Operation::LoadBoolean | Operation::LoadConstant,
|
||||||
@ -254,8 +250,6 @@ impl<'src> Parser<'src> {
|
|||||||
|
|
||||||
*second_loader = second_loader_new;
|
*second_loader = second_loader_new;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.current_statement_length = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn pop_last_instruction(&mut self) -> Result<(Instruction, Span), ParseError> {
|
fn pop_last_instruction(&mut self) -> Result<(Instruction, Span), ParseError> {
|
||||||
@ -272,22 +266,11 @@ impl<'src> Parser<'src> {
|
|||||||
self.chunk
|
self.chunk
|
||||||
.instructions()
|
.instructions()
|
||||||
.iter()
|
.iter()
|
||||||
.rev()
|
.last()
|
||||||
.take(self.current_statement_length)
|
.map(|(instruction, _)| instruction.operation())
|
||||||
.find_map(|(instruction, _)| {
|
|
||||||
if instruction.yields_value() {
|
|
||||||
Some(instruction.operation())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_end_of_statement<const COUNT: usize>(&self) -> Option<[Operation; COUNT]> {
|
fn get_last_instructions<const COUNT: usize>(&self) -> Option<[Operation; COUNT]> {
|
||||||
if self.current_statement_length < COUNT {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut operations = [Operation::Return; COUNT];
|
let mut operations = [Operation::Return; COUNT];
|
||||||
|
|
||||||
for (index, (instruction, _)) in self
|
for (index, (instruction, _)) in self
|
||||||
@ -1023,7 +1006,7 @@ impl<'src> Parser<'src> {
|
|||||||
self.parse_expression()?;
|
self.parse_expression()?;
|
||||||
|
|
||||||
if matches!(
|
if matches!(
|
||||||
self.get_end_of_statement(),
|
self.get_last_instructions(),
|
||||||
Some([
|
Some([
|
||||||
Operation::LoadBoolean,
|
Operation::LoadBoolean,
|
||||||
Operation::LoadBoolean,
|
Operation::LoadBoolean,
|
||||||
@ -1163,7 +1146,7 @@ impl<'src> Parser<'src> {
|
|||||||
self.parse_expression()?;
|
self.parse_expression()?;
|
||||||
|
|
||||||
if matches!(
|
if matches!(
|
||||||
self.get_end_of_statement(),
|
self.get_last_instructions(),
|
||||||
Some([
|
Some([
|
||||||
Operation::LoadBoolean,
|
Operation::LoadBoolean,
|
||||||
Operation::LoadBoolean,
|
Operation::LoadBoolean,
|
||||||
|
Loading…
Reference in New Issue
Block a user