1
0
dust/dust-lang/src/parser/mod.rs

1355 lines
44 KiB
Rust
Raw Normal View History

#[cfg(test)]
mod tests;
use std::{
fmt::{self, Display, Formatter},
mem::replace,
num::{ParseFloatError, ParseIntError},
};
2024-09-07 03:30:43 +00:00
use crate::{
2024-09-22 22:17:21 +00:00
AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction, LexError, Lexer,
Operation, Span, Token, TokenKind, TokenOwned, Value,
2024-09-07 03:30:43 +00:00
};
2024-09-07 16:15:47 +00:00
pub fn parse(source: &str) -> Result<Chunk, DustError> {
let lexer = Lexer::new(source);
let mut parser = Parser::new(lexer).map_err(|error| DustError::Parse { error, source })?;
while !parser.is_eof() {
2024-09-07 16:15:47 +00:00
parser
2024-09-19 15:41:18 +00:00
.parse_statement(true)
2024-09-07 16:15:47 +00:00
.map_err(|error| DustError::Parse { error, source })?;
}
Ok(parser.chunk)
}
2024-09-07 03:30:43 +00:00
#[derive(Debug)]
pub struct Parser<'src> {
chunk: Chunk,
lexer: Lexer<'src>,
current_register: u8,
2024-09-07 10:38:12 +00:00
current_token: Token<'src>,
2024-09-07 03:30:43 +00:00
current_position: Span,
previous_token: Token<'src>,
previous_position: Span,
2024-09-07 03:30:43 +00:00
}
impl<'src> Parser<'src> {
pub fn new(mut lexer: Lexer<'src>) -> Result<Self, ParseError> {
let (current_token, current_position) = lexer.next_token()?;
2024-09-07 10:38:12 +00:00
2024-09-17 21:23:37 +00:00
log::trace!("Starting parser with token \"{current_token}\" at {current_position}");
2024-09-07 16:15:47 +00:00
Ok(Parser {
2024-09-07 03:30:43 +00:00
lexer,
chunk: Chunk::new(),
current_register: 0,
2024-09-07 10:38:12 +00:00
current_token,
current_position,
previous_token: Token::Eof,
previous_position: Span(0, 0),
})
}
pub fn take_chunk(self) -> Chunk {
self.chunk
2024-09-07 03:30:43 +00:00
}
fn is_eof(&self) -> bool {
2024-09-07 10:38:12 +00:00
matches!(self.current_token, Token::Eof)
2024-09-07 03:30:43 +00:00
}
fn increment_register(&mut self) -> Result<(), ParseError> {
let current = self.current_register;
if current == u8::MAX {
Err(ParseError::RegisterOverflow {
position: self.current_position,
})
} else {
self.current_register += 1;
Ok(())
}
}
fn decrement_register(&mut self) -> Result<(), ParseError> {
let current = self.current_register;
if current == 0 {
Err(ParseError::RegisterUnderflow {
position: self.current_position,
})
} else {
self.current_register -= 1;
Ok(())
}
}
2024-09-07 03:30:43 +00:00
fn advance(&mut self) -> Result<(), ParseError> {
2024-09-10 02:57:14 +00:00
if self.is_eof() {
return Ok(());
}
2024-09-07 10:38:12 +00:00
let (new_token, position) = self.lexer.next_token()?;
2024-09-07 03:30:43 +00:00
2024-09-18 01:10:44 +00:00
log::trace!("Parsing \"{new_token}\" at {position}");
self.previous_token = replace(&mut self.current_token, new_token);
self.previous_position = replace(&mut self.current_position, position);
2024-09-07 03:30:43 +00:00
Ok(())
}
2024-09-07 16:15:47 +00:00
fn allow(&mut self, allowed: TokenKind) -> Result<bool, ParseError> {
if self.current_token.kind() == allowed {
self.advance()?;
Ok(true)
} else {
Ok(false)
}
}
fn expect(&mut self, expected: TokenKind) -> Result<(), ParseError> {
2024-09-07 10:38:12 +00:00
if self.current_token.kind() == expected {
2024-09-07 03:30:43 +00:00
self.advance()
} else {
Err(ParseError::ExpectedToken {
expected,
2024-09-07 10:38:12 +00:00
found: self.current_token.to_owned(),
2024-09-07 03:30:43 +00:00
position: self.current_position,
})
}
}
fn emit_instruction(&mut self, instruction: Instruction, position: Span) {
self.chunk.push_instruction(instruction, position);
2024-09-07 03:30:43 +00:00
}
fn emit_constant(&mut self, value: Value) -> Result<(), ParseError> {
2024-09-07 10:38:12 +00:00
let position = self.previous_position;
2024-09-11 07:10:12 +00:00
let constant_index = self.chunk.push_constant(value, position)?;
self.emit_instruction(
Instruction::load_constant(self.current_register, constant_index),
position,
);
self.increment_register()?;
2024-09-07 03:30:43 +00:00
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_boolean(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-23 10:42:41 +00:00
let boolean_text = if let Token::Boolean(text) = self.current_token {
text
} else {
return Err(ParseError::ExpectedToken {
expected: TokenKind::Boolean,
found: self.current_token.to_owned(),
position: self.current_position,
});
};
2024-09-19 15:41:18 +00:00
2024-09-23 10:42:41 +00:00
let position = self.current_position;
let boolean = boolean_text.parse::<bool>().unwrap();
2024-09-17 21:23:37 +00:00
2024-09-23 10:42:41 +00:00
self.advance()?;
2024-09-07 10:38:12 +00:00
2024-09-23 10:42:41 +00:00
let previous_operations = self.chunk.get_last_n_operations::<2>();
2024-09-19 00:02:12 +00:00
2024-09-23 10:42:41 +00:00
if let [Some(Operation::LoadBoolean), Some(Operation::LoadBoolean)] = previous_operations {
let (second_boolean, second_position) =
self.chunk.pop_instruction(self.current_position)?;
let (first_boolean, first_position) =
self.chunk.pop_instruction(self.current_position)?;
2024-09-22 22:17:21 +00:00
2024-09-23 10:42:41 +00:00
if first_boolean.first_argument_as_boolean() == boolean {
let skip = first_boolean.second_argument_as_boolean();
2024-09-22 22:17:21 +00:00
2024-09-23 10:42:41 +00:00
self.emit_instruction(
Instruction::load_boolean(self.current_register, boolean, skip),
position,
);
2024-09-22 22:17:21 +00:00
2024-09-23 10:42:41 +00:00
return Ok(());
}
2024-09-22 22:17:21 +00:00
2024-09-23 10:42:41 +00:00
if second_boolean.first_argument_as_boolean() == boolean {
let skip = second_boolean.second_argument_as_boolean();
2024-09-22 22:17:21 +00:00
2024-09-23 10:42:41 +00:00
self.emit_instruction(
Instruction::load_boolean(self.current_register, boolean, skip),
position,
);
2024-09-22 22:17:21 +00:00
2024-09-23 10:42:41 +00:00
return Ok(());
2024-09-19 00:02:12 +00:00
}
2024-09-22 22:17:21 +00:00
2024-09-23 10:42:41 +00:00
self.emit_instruction(first_boolean, first_position);
self.emit_instruction(second_boolean, second_position);
2024-09-07 10:38:12 +00:00
}
2024-09-23 10:42:41 +00:00
let skip = previous_operations[0] == Some(Operation::Jump);
self.emit_instruction(
Instruction::load_boolean(self.current_register, boolean, skip),
position,
);
self.increment_register()?;
2024-09-07 10:38:12 +00:00
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_byte(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-17 21:23:37 +00:00
if let Token::Byte(text) = self.current_token {
self.advance()?;
let byte =
u8::from_str_radix(&text[2..], 16).map_err(|error| ParseError::ParseIntError {
error,
position: self.previous_position,
})?;
2024-09-10 03:45:06 +00:00
let value = Value::byte(byte);
self.emit_constant(value)?;
}
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_character(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-17 21:23:37 +00:00
if let Token::Character(character) = self.current_token {
self.advance()?;
2024-09-10 03:45:06 +00:00
let value = Value::character(character);
self.emit_constant(value)?;
}
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_float(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-17 21:23:37 +00:00
if let Token::Float(text) = self.current_token {
self.advance()?;
let float = text
.parse::<f64>()
.map_err(|error| ParseError::ParseFloatError {
error,
position: self.previous_position,
})?;
2024-09-07 16:15:47 +00:00
let value = Value::float(float);
self.emit_constant(value)?;
}
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_integer(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-17 21:23:37 +00:00
if let Token::Integer(text) = self.current_token {
self.advance()?;
let integer = text
.parse::<i64>()
.map_err(|error| ParseError::ParseIntError {
error,
position: self.previous_position,
})?;
let value = Value::integer(integer);
2024-09-07 03:30:43 +00:00
self.emit_constant(value)?;
}
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_string(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-17 21:23:37 +00:00
if let Token::String(text) = self.current_token {
self.advance()?;
2024-09-07 16:15:47 +00:00
let value = Value::string(text);
self.emit_constant(value)?;
}
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_grouped(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-17 21:23:37 +00:00
self.allow(TokenKind::LeftParenthesis)?;
self.parse_expression()?;
2024-09-17 21:23:37 +00:00
self.expect(TokenKind::RightParenthesis)
}
2024-09-19 15:41:18 +00:00
fn parse_unary(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-17 23:35:33 +00:00
let operator = self.current_token;
let operator_position = self.current_position;
2024-09-17 21:23:37 +00:00
2024-09-17 23:35:33 +00:00
self.advance()?;
self.parse_expression()?;
2024-09-07 08:37:38 +00:00
2024-09-17 21:23:37 +00:00
let (previous_instruction, previous_position) =
self.chunk.pop_instruction(self.current_position)?;
let (push_back, is_constant, argument) = {
match previous_instruction.operation() {
Operation::GetLocal => (false, false, previous_instruction.destination()),
Operation::LoadConstant => {
self.decrement_register()?;
(false, true, previous_instruction.first_argument())
}
2024-09-23 10:42:41 +00:00
Operation::LoadBoolean => (true, false, previous_instruction.destination()),
Operation::Close => {
return Err(ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
});
}
_ => (true, false, previous_instruction.destination()),
2024-09-17 21:23:37 +00:00
}
2024-09-17 23:35:33 +00:00
};
let mut instruction = match operator.kind() {
TokenKind::Bang => Instruction::not(self.current_register, argument),
TokenKind::Minus => Instruction::negate(self.current_register, argument),
2024-09-17 23:35:33 +00:00
_ => {
return Err(ParseError::ExpectedTokenMultiple {
2024-09-18 01:10:44 +00:00
expected: &[TokenKind::Bang, TokenKind::Minus],
2024-09-17 23:35:33 +00:00
found: operator.to_owned(),
position: operator_position,
})
}
};
if is_constant {
instruction.set_first_argument_to_constant();
2024-09-17 21:23:37 +00:00
}
if push_back {
self.emit_instruction(previous_instruction, previous_position);
}
2024-09-17 21:23:37 +00:00
self.emit_instruction(instruction, operator_position);
Ok(())
}
2024-09-23 04:55:39 +00:00
fn handle_binary_argument(
&mut self,
instruction: &Instruction,
) -> Result<(bool, bool, u8), ParseError> {
let mut push_back = false;
let mut is_constant = false;
let argument = match instruction.operation() {
Operation::GetLocal => {
self.decrement_register()?;
instruction.destination()
}
Operation::LoadConstant => {
is_constant = true;
2024-09-23 04:55:39 +00:00
self.decrement_register()?;
instruction.first_argument()
}
Operation::LoadBoolean => {
is_constant = true;
push_back = true;
2024-09-23 10:42:41 +00:00
self.decrement_register()?;
2024-09-23 04:55:39 +00:00
instruction.destination()
}
Operation::Close => {
return Err(ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
});
}
_ => {
push_back = true;
2024-09-15 10:26:31 +00:00
2024-09-23 04:55:39 +00:00
instruction.destination()
}
};
2024-09-18 05:21:40 +00:00
2024-09-23 04:55:39 +00:00
Ok((push_back, is_constant, argument))
}
2024-09-17 17:24:45 +00:00
2024-09-23 04:55:39 +00:00
fn parse_math_binary(&mut self) -> Result<(), ParseError> {
2024-09-19 15:41:18 +00:00
let (left_instruction, left_position) =
2024-09-15 01:05:03 +00:00
self.chunk.pop_instruction(self.current_position)?;
2024-09-23 04:55:39 +00:00
let (push_back_left, left_is_constant, left) =
self.handle_binary_argument(&left_instruction)?;
2024-09-19 15:41:18 +00:00
let operator = self.current_token;
let operator_position = self.current_position;
let rule = ParseRule::from(&operator.kind());
2024-09-23 04:55:39 +00:00
let mut instruction = match operator.kind() {
TokenKind::Plus => Instruction::add(self.current_register, left, 0),
TokenKind::Minus => Instruction::subtract(self.current_register, left, 0),
TokenKind::Star => Instruction::multiply(self.current_register, left, 0),
TokenKind::Slash => Instruction::divide(self.current_register, left, 0),
TokenKind::Percent => Instruction::modulo(self.current_register, left, 0),
2024-09-07 03:30:43 +00:00
_ => {
return Err(ParseError::ExpectedTokenMultiple {
2024-09-17 23:35:33 +00:00
expected: &[
TokenKind::Plus,
TokenKind::Minus,
TokenKind::Star,
TokenKind::Slash,
2024-09-18 01:10:44 +00:00
TokenKind::Percent,
],
found: operator.to_owned(),
2024-09-07 10:38:12 +00:00
position: operator_position,
2024-09-07 03:30:43 +00:00
})
}
2024-09-07 08:37:38 +00:00
};
2024-09-23 04:55:39 +00:00
self.increment_register()?;
2024-09-19 15:41:18 +00:00
self.advance()?;
self.parse(rule.precedence.increment())?;
let (right_instruction, right_position) =
self.chunk.pop_instruction(self.current_position)?;
let (push_back_right, right_is_constant, right) =
2024-09-23 04:55:39 +00:00
self.handle_binary_argument(&right_instruction)?;
2024-09-19 15:41:18 +00:00
instruction.set_second_argument(right);
2024-09-15 08:25:24 +00:00
if left_is_constant {
2024-09-15 05:24:04 +00:00
instruction.set_first_argument_to_constant();
}
2024-09-15 08:25:24 +00:00
if right_is_constant {
2024-09-15 05:24:04 +00:00
instruction.set_second_argument_to_constant();
}
2024-09-23 04:55:39 +00:00
if push_back_left {
self.emit_instruction(left_instruction, left_position);
}
2024-09-17 16:08:40 +00:00
2024-09-23 04:55:39 +00:00
if push_back_right {
self.emit_instruction(right_instruction, right_position);
2024-09-17 17:24:45 +00:00
}
2024-09-23 04:55:39 +00:00
self.emit_instruction(instruction, operator_position);
2024-09-07 03:30:43 +00:00
2024-09-23 04:55:39 +00:00
Ok(())
}
2024-09-19 00:02:12 +00:00
2024-09-23 04:55:39 +00:00
fn parse_comparison_binary(&mut self) -> Result<(), ParseError> {
let (left_instruction, left_position) =
self.chunk.pop_instruction(self.current_position)?;
2024-09-19 15:41:18 +00:00
2024-09-23 04:55:39 +00:00
let (push_back_left, left_is_constant, left) =
self.handle_binary_argument(&left_instruction)?;
2024-09-19 15:41:18 +00:00
2024-09-23 04:55:39 +00:00
let operator = self.current_token;
let operator_position = self.current_position;
let rule = ParseRule::from(&operator.kind());
let mut instruction = match self.current_token.kind() {
TokenKind::DoubleEqual => Instruction::equal(true, left, 0),
TokenKind::BangEqual => Instruction::equal(false, left, 0),
TokenKind::Less => Instruction::less(true, left, 0),
TokenKind::LessEqual => Instruction::less_equal(true, left, 0),
TokenKind::Greater => Instruction::less_equal(false, left, 0),
TokenKind::GreaterEqual => Instruction::less(false, left, 0),
_ => {
return Err(ParseError::ExpectedTokenMultiple {
expected: &[
TokenKind::DoubleEqual,
TokenKind::BangEqual,
TokenKind::Less,
TokenKind::LessEqual,
TokenKind::Greater,
TokenKind::GreaterEqual,
],
found: self.current_token.to_owned(),
position: self.current_position,
})
2024-09-19 15:41:18 +00:00
}
2024-09-23 04:55:39 +00:00
};
2024-09-19 15:41:18 +00:00
2024-09-23 04:55:39 +00:00
self.advance()?;
self.parse(rule.precedence.increment())?;
2024-09-22 22:17:21 +00:00
2024-09-23 04:55:39 +00:00
let (right_instruction, right_position) =
self.chunk.pop_instruction(self.current_position)?;
2024-09-22 22:17:21 +00:00
2024-09-23 04:55:39 +00:00
let (push_back_right, right_is_constant, right) =
self.handle_binary_argument(&right_instruction)?;
instruction.set_second_argument(right);
if left_is_constant {
instruction.set_first_argument_to_constant();
}
if right_is_constant {
instruction.set_second_argument_to_constant();
}
self.emit_instruction(instruction, operator_position);
self.emit_instruction(Instruction::jump(1, true), operator_position);
self.emit_instruction(
Instruction::load_boolean(self.current_register, true, true),
operator_position,
);
self.emit_instruction(
Instruction::load_boolean(self.current_register, false, false),
operator_position,
);
if push_back_left {
self.emit_instruction(left_instruction, left_position);
}
if push_back_right {
self.emit_instruction(right_instruction, right_position);
}
2024-09-07 03:30:43 +00:00
Ok(())
}
2024-09-23 04:55:39 +00:00
fn parse_logical_binary(&mut self) -> Result<(), ParseError> {
2024-09-23 08:01:36 +00:00
let (left_instruction, left_position) =
self.chunk.pop_instruction(self.current_position)?;
let (push_back_left, left_is_constant, _) =
self.handle_binary_argument(&left_instruction)?;
2024-09-23 10:42:41 +00:00
if let Operation::LoadBoolean = left_instruction.operation() {
self.increment_register()?;
}
2024-09-23 08:01:36 +00:00
let operator = self.current_token;
let operator_position = self.current_position;
let rule = ParseRule::from(&operator.kind());
2024-09-23 10:42:41 +00:00
let test_register = self.current_register.saturating_sub(1);
2024-09-23 08:01:36 +00:00
let mut instruction = match operator.kind() {
2024-09-23 10:42:41 +00:00
TokenKind::DoubleAmpersand => Instruction::test(test_register, true),
TokenKind::DoublePipe => Instruction::test(test_register, false),
2024-09-23 08:01:36 +00:00
_ => {
return Err(ParseError::ExpectedTokenMultiple {
expected: &[TokenKind::DoubleAmpersand, TokenKind::DoublePipe],
found: operator.to_owned(),
position: operator_position,
})
}
};
self.advance()?;
self.parse(rule.precedence.increment())?;
let (mut right_instruction, right_position) =
self.chunk.pop_instruction(self.current_position)?;
let (push_back_right, right_is_constant, _) =
self.handle_binary_argument(&right_instruction)?;
2024-09-23 10:42:41 +00:00
let emit_move_to = if self.current_register != test_register {
Some(self.current_register)
} else {
None
};
2024-09-23 08:01:36 +00:00
if left_is_constant {
instruction.set_first_argument_to_constant();
}
if right_is_constant {
if let Operation::LoadBoolean = right_instruction.operation() {
right_instruction.set_second_argument_to_boolean(true);
}
instruction.set_second_argument_to_constant();
}
if push_back_left {
self.emit_instruction(left_instruction, left_position);
}
self.emit_instruction(instruction, operator_position);
self.emit_instruction(Instruction::jump(1, true), operator_position);
if push_back_right {
self.emit_instruction(right_instruction, right_position);
}
2024-09-23 10:42:41 +00:00
if let Some(register) = emit_move_to {
self.emit_instruction(
Instruction::r#move(register, test_register),
operator_position,
);
}
2024-09-23 08:01:36 +00:00
Ok(())
2024-09-23 04:55:39 +00:00
}
2024-09-19 15:41:18 +00:00
fn parse_variable(
&mut self,
allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-19 17:54:28 +00:00
let token = self.current_token;
2024-09-17 21:23:37 +00:00
let start_position = self.current_position;
2024-09-19 22:07:11 +00:00
self.advance()?;
2024-09-15 01:05:03 +00:00
let local_index = self.parse_identifier_from(token, start_position)?;
2024-09-17 21:23:37 +00:00
2024-09-07 21:16:14 +00:00
if allow_assignment && self.allow(TokenKind::Equal)? {
2024-09-22 22:17:21 +00:00
let is_mutable = self.chunk.get_local(local_index, start_position)?.mutable;
if !is_mutable {
return Err(ParseError::CannotMutateImmutableVariable {
identifier: self.chunk.get_identifier(local_index).cloned().unwrap(),
position: start_position,
});
}
2024-09-07 21:16:14 +00:00
self.parse_expression()?;
2024-09-15 01:05:03 +00:00
let (mut previous_instruction, previous_position) =
2024-09-17 21:23:37 +00:00
self.chunk.pop_instruction(self.current_position)?;
2024-09-15 01:05:03 +00:00
2024-09-15 05:24:04 +00:00
if previous_instruction.operation().is_binary() {
2024-09-15 01:05:03 +00:00
let previous_register = self
.chunk
.get_local(local_index, start_position)?
.register_index;
if let Some(register_index) = previous_register {
2024-09-18 15:27:41 +00:00
log::trace!("Condensing SET_LOCAL to binary expression");
2024-09-15 05:24:04 +00:00
previous_instruction.set_destination(register_index);
2024-09-17 21:23:37 +00:00
self.emit_instruction(previous_instruction, self.current_position);
2024-09-19 15:41:18 +00:00
return Ok(());
2024-09-15 01:05:03 +00:00
}
}
2024-09-19 03:02:28 +00:00
self.emit_instruction(previous_instruction, previous_position);
self.emit_instruction(
Instruction::set_local(self.current_register - 1, local_index),
start_position,
);
} else {
self.emit_instruction(
Instruction::get_local(self.current_register, local_index),
2024-09-18 11:58:31 +00:00
self.previous_position,
);
2024-09-19 22:07:11 +00:00
self.increment_register()?;
2024-09-07 21:16:14 +00:00
}
2024-09-07 17:51:05 +00:00
Ok(())
}
2024-09-19 17:54:28 +00:00
fn parse_identifier_from(&mut self, token: Token, position: Span) -> Result<u8, ParseError> {
if let Token::Identifier(text) = token {
2024-09-07 16:15:47 +00:00
let identifier = Identifier::new(text);
2024-09-10 22:19:59 +00:00
if let Ok(local_index) = self.chunk.get_local_index(&identifier, position) {
Ok(local_index)
2024-09-11 07:10:12 +00:00
} else {
Err(ParseError::UndefinedVariable {
identifier,
position,
})
}
2024-09-07 16:15:47 +00:00
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::Identifier,
found: self.current_token.to_owned(),
2024-09-11 07:10:12 +00:00
position,
2024-09-07 16:15:47 +00:00
})
}
}
2024-09-19 15:41:18 +00:00
fn parse_block(
&mut self,
_allow_assignment: bool,
allow_return: bool,
) -> Result<(), ParseError> {
self.advance()?;
2024-09-10 14:44:15 +00:00
self.chunk.begin_scope();
while !self.allow(TokenKind::RightCurlyBrace)? && !self.is_eof() {
2024-09-19 15:41:18 +00:00
self.parse_statement(allow_return)?;
2024-09-10 14:44:15 +00:00
}
self.chunk.end_scope();
2024-09-11 07:10:12 +00:00
2024-09-10 14:44:15 +00:00
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_list(
&mut self,
_allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-18 01:10:44 +00:00
let start = self.current_position.0;
2024-09-17 23:35:33 +00:00
self.advance()?;
2024-09-19 15:41:18 +00:00
let start_register = self.current_register;
2024-09-17 23:35:33 +00:00
let mut length = 0;
while !self.allow(TokenKind::RightSquareBrace)? && !self.is_eof() {
2024-09-18 03:06:58 +00:00
let next_register = self.current_register;
2024-09-18 01:10:44 +00:00
self.parse(Precedence::Assignment)?; // Do not allow assignment
2024-09-17 23:35:33 +00:00
2024-09-18 03:06:58 +00:00
if next_register != self.current_register - 1 {
self.emit_instruction(
Instruction::close(next_register, self.current_register - 1),
self.current_position,
);
}
2024-09-17 23:35:33 +00:00
length += 1;
if !self.allow(TokenKind::Comma)? {
self.expect(TokenKind::RightSquareBrace)?;
break;
}
}
let end = self.current_position.1;
self.emit_instruction(
2024-09-19 15:41:18 +00:00
Instruction::load_list(self.current_register, start_register, length),
2024-09-17 23:35:33 +00:00
Span(start, end),
);
2024-09-19 15:41:18 +00:00
self.increment_register()?;
2024-09-17 23:35:33 +00:00
Ok(())
}
2024-09-19 17:54:28 +00:00
fn parse_if(&mut self, allow_assignment: bool, allow_return: bool) -> Result<(), ParseError> {
self.advance()?;
self.parse_expression()?;
2024-09-22 22:17:21 +00:00
let (second_load_boolean, second_position) =
self.chunk.pop_instruction(self.current_position)?;
let (first_load_boolean, first_position) =
self.chunk.pop_instruction(self.current_position)?;
let length_after_expression = self.chunk.len();
2024-09-19 17:54:28 +00:00
self.parse_block(allow_assignment, allow_return)?;
2024-09-19 15:41:18 +00:00
2024-09-19 17:54:28 +00:00
let jump_start = self.current_register;
let jump_index = self.chunk.len();
if self.allow(TokenKind::Else)? {
2024-09-19 15:41:18 +00:00
if self.allow(TokenKind::If)? {
2024-09-19 17:54:28 +00:00
self.parse_if(allow_assignment, allow_return)?;
2024-09-19 15:41:18 +00:00
} else {
2024-09-19 17:54:28 +00:00
self.parse_block(allow_assignment, allow_return)?;
2024-09-19 15:41:18 +00:00
}
}
2024-09-22 22:17:21 +00:00
if self.chunk.len() == length_after_expression {
self.emit_instruction(first_load_boolean, first_position);
self.emit_instruction(second_load_boolean, second_position);
}
2024-09-19 17:54:28 +00:00
2024-09-23 10:42:41 +00:00
if let [Some(Operation::LoadBoolean), Some(Operation::LoadBoolean)] =
self.chunk.get_last_n_operations()
{
// Do not emit a jump if the last two instructions were LoadBoolean operations. However,
// we need to set them to the same destination register and decrement the register count.
let (mut second_load_boolean, second_position) =
self.chunk.pop_instruction(self.current_position)?;
let (first_load_boolean, _) = self.chunk.get_previous().unwrap();
second_load_boolean.set_destination(first_load_boolean.destination());
self.emit_instruction(second_load_boolean, second_position);
} else if let Some(Operation::LoadBoolean) = self.chunk.get_last_operation() {
2024-09-22 22:17:21 +00:00
// Skip the jump if the last instruction was a LoadBoolean operation. A LoadBoolean can
// skip the following instruction, so a jump is unnecessary.
} else {
let jump_end = self.current_register;
let jump_distance = (jump_end - jump_start).max(1);
let jump = Instruction::jump(jump_distance, true);
self.chunk
.insert_instruction(jump_index, jump, self.current_position);
}
2024-09-19 17:54:28 +00:00
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_while(
&mut self,
allow_assignment: bool,
allow_return: bool,
) -> Result<(), ParseError> {
2024-09-19 03:02:28 +00:00
self.advance()?;
self.parse_expression()?;
2024-09-19 15:41:18 +00:00
self.parse_block(allow_assignment, allow_return)?;
2024-09-19 03:02:28 +00:00
Ok(())
}
fn parse_expression(&mut self) -> Result<(), ParseError> {
2024-09-07 10:38:12 +00:00
self.parse(Precedence::None)
}
2024-09-19 15:41:18 +00:00
fn parse_statement(&mut self, allow_return: bool) -> Result<(), ParseError> {
2024-09-19 17:54:28 +00:00
match self.current_token {
2024-09-07 22:48:01 +00:00
Token::Let => {
2024-09-19 15:41:18 +00:00
self.parse_let_statement(true, allow_return)?;
2024-09-10 14:44:15 +00:00
}
Token::LeftCurlyBrace => {
2024-09-19 15:41:18 +00:00
self.parse_block(true, true)?;
2024-09-07 22:48:01 +00:00
}
_ => {
self.parse_expression()?;
}
};
2024-09-19 15:41:18 +00:00
2024-09-19 22:07:11 +00:00
self.allow(TokenKind::Semicolon)?;
2024-09-07 16:15:47 +00:00
Ok(())
}
2024-09-19 15:41:18 +00:00
fn parse_let_statement(
&mut self,
allow_assignment: bool,
_allow_return: bool,
) -> Result<(), ParseError> {
2024-09-18 01:10:44 +00:00
if !allow_assignment {
return Err(ParseError::ExpectedExpression {
found: self.current_token.to_owned(),
position: self.current_position,
});
}
2024-09-19 17:54:28 +00:00
self.allow(TokenKind::Let)?;
2024-09-17 21:23:37 +00:00
2024-09-18 15:27:41 +00:00
let is_mutable = self.allow(TokenKind::Mut)?;
2024-09-07 16:15:47 +00:00
let position = self.current_position;
let identifier = if let Token::Identifier(text) = self.current_token {
2024-09-09 23:23:49 +00:00
self.advance()?;
2024-09-07 16:15:47 +00:00
Identifier::new(text)
2024-09-09 23:23:49 +00:00
} else {
return Err(ParseError::ExpectedToken {
expected: TokenKind::Identifier,
found: self.current_token.to_owned(),
position: self.current_position,
});
};
self.expect(TokenKind::Equal)?;
2024-09-10 22:19:59 +00:00
self.parse_expression()?;
2024-09-22 22:17:21 +00:00
let (previous_instruction, previous_position) =
*self
.chunk
.get_previous()
.ok_or_else(|| ParseError::ExpectedExpression {
found: self.current_token.to_owned(),
position,
})?;
2024-09-19 02:00:24 +00:00
let register = previous_instruction.destination();
2024-09-18 15:27:41 +00:00
let local_index =
self.chunk
2024-09-19 02:00:24 +00:00
.declare_local(identifier, is_mutable, register, previous_position)?;
// Optimize for assignment to a comparison
2024-09-22 22:17:21 +00:00
// if let Operation::Jump = previous_instruction.operation() {
// let (jump, jump_position) = self.chunk.pop_instruction(self.current_position)?;
// if let Some(Operation::Equal) = self.chunk.get_last_operation() {
// self.emit_instruction(jump, jump_position);
// self.emit_instruction(
// Instruction::load_boolean(self.current_register, true, true),
// self.current_position,
// );
// self.emit_instruction(
// Instruction::load_boolean(self.current_register, false, false),
// self.current_position,
// );
// } else {
// self.emit_instruction(jump, jump_position);
// }
// }
2024-09-15 01:05:03 +00:00
2024-09-18 15:27:41 +00:00
self.emit_instruction(
Instruction::define_local(register, local_index, is_mutable),
position,
);
2024-09-18 11:58:31 +00:00
self.allow(TokenKind::Semicolon)?;
2024-09-07 16:15:47 +00:00
Ok(())
}
fn parse(&mut self, precedence: Precedence) -> Result<(), ParseError> {
2024-09-17 21:23:37 +00:00
let allow_assignment = precedence < Precedence::Assignment;
2024-09-19 15:41:18 +00:00
let allow_return = precedence == Precedence::None;
2024-09-17 21:23:37 +00:00
if let Some(prefix_parser) = ParseRule::from(&self.current_token.kind()).prefix {
2024-09-15 10:26:31 +00:00
log::trace!(
2024-09-19 22:07:11 +00:00
"Parsing \"{}\" as prefix at precedence {precedence}",
2024-09-15 10:26:31 +00:00
self.current_token,
);
2024-09-07 21:16:14 +00:00
2024-09-19 15:41:18 +00:00
prefix_parser(self, allow_assignment, allow_return)?;
2024-09-17 21:23:37 +00:00
}
2024-09-17 17:24:45 +00:00
let mut infix_rule = ParseRule::from(&self.current_token.kind());
while precedence <= infix_rule.precedence {
if let Some(infix_parser) = infix_rule.infix {
2024-09-07 10:38:12 +00:00
log::trace!(
2024-09-19 22:07:11 +00:00
"Parsing \"{}\" as infix at precedence {precedence}",
2024-09-17 21:23:37 +00:00
self.current_token,
2024-09-07 10:38:12 +00:00
);
if allow_assignment && self.current_token == Token::Equal {
2024-09-07 21:16:14 +00:00
return Err(ParseError::InvalidAssignmentTarget {
2024-09-17 21:23:37 +00:00
found: self.current_token.to_owned(),
position: self.current_position,
2024-09-07 21:16:14 +00:00
});
}
2024-09-10 05:04:30 +00:00
infix_parser(self)?;
} else {
break;
}
2024-09-17 21:23:37 +00:00
infix_rule = ParseRule::from(&self.current_token.kind());
}
2024-09-07 03:30:43 +00:00
Ok(())
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Precedence {
None,
Assignment,
Conditional,
LogicalOr,
LogicalAnd,
Equality,
Comparison,
Term,
Factor,
Unary,
Call,
Primary,
}
impl Precedence {
fn increment(&self) -> Self {
match self {
Precedence::None => Precedence::Assignment,
Precedence::Assignment => Precedence::Conditional,
Precedence::Conditional => Precedence::LogicalOr,
Precedence::LogicalOr => Precedence::LogicalAnd,
Precedence::LogicalAnd => Precedence::Equality,
Precedence::Equality => Precedence::Comparison,
Precedence::Comparison => Precedence::Term,
Precedence::Term => Precedence::Factor,
Precedence::Factor => Precedence::Unary,
Precedence::Unary => Precedence::Call,
Precedence::Call => Precedence::Primary,
Precedence::Primary => Precedence::Primary,
}
}
}
impl Display for Precedence {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
2024-09-19 15:41:18 +00:00
type PrefixFunction<'a> = fn(&mut Parser<'a>, bool, bool) -> Result<(), ParseError>;
2024-09-07 21:16:14 +00:00
type InfixFunction<'a> = fn(&mut Parser<'a>) -> Result<(), ParseError>;
#[derive(Debug, Clone, Copy)]
pub struct ParseRule<'a> {
2024-09-07 21:16:14 +00:00
pub prefix: Option<PrefixFunction<'a>>,
pub infix: Option<InfixFunction<'a>>,
pub precedence: Precedence,
}
impl From<&TokenKind> for ParseRule<'_> {
fn from(token_kind: &TokenKind) -> Self {
match token_kind {
2024-09-22 22:17:21 +00:00
TokenKind::Async => todo!(),
TokenKind::Bang => ParseRule {
prefix: Some(Parser::parse_unary),
infix: None,
2024-09-22 22:17:21 +00:00
precedence: Precedence::Unary,
},
2024-09-22 22:17:21 +00:00
TokenKind::BangEqual => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_comparison_binary),
2024-09-22 22:17:21 +00:00
precedence: Precedence::Equality,
2024-09-07 16:15:47 +00:00
},
2024-09-22 22:17:21 +00:00
TokenKind::Bool => todo!(),
2024-09-07 10:38:12 +00:00
TokenKind::Boolean => ParseRule {
prefix: Some(Parser::parse_boolean),
infix: None,
precedence: Precedence::None,
},
2024-09-22 22:17:21 +00:00
TokenKind::Break => todo!(),
2024-09-10 03:45:06 +00:00
TokenKind::Byte => ParseRule {
prefix: Some(Parser::parse_byte),
infix: None,
precedence: Precedence::None,
},
TokenKind::Character => ParseRule {
prefix: Some(Parser::parse_character),
infix: None,
precedence: Precedence::None,
},
TokenKind::Colon => todo!(),
2024-09-17 23:35:33 +00:00
TokenKind::Comma => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
TokenKind::Dot => todo!(),
2024-09-10 05:04:30 +00:00
TokenKind::DoubleAmpersand => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_logical_binary),
2024-09-10 05:04:30 +00:00
precedence: Precedence::LogicalAnd,
},
TokenKind::DoubleEqual => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_comparison_binary),
precedence: Precedence::Equality,
},
TokenKind::DoublePipe => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_logical_binary),
precedence: Precedence::LogicalOr,
},
2024-09-22 22:17:21 +00:00
TokenKind::DoubleDot => todo!(),
TokenKind::Eof => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
2024-09-17 21:23:37 +00:00
TokenKind::Equal => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::Assignment,
},
2024-09-22 22:17:21 +00:00
TokenKind::Else => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
TokenKind::Float => ParseRule {
prefix: Some(Parser::parse_float),
infix: None,
precedence: Precedence::None,
},
TokenKind::FloatKeyword => todo!(),
TokenKind::Greater => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_comparison_binary),
2024-09-22 22:17:21 +00:00
precedence: Precedence::Comparison,
},
TokenKind::GreaterEqual => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_comparison_binary),
2024-09-22 22:17:21 +00:00
precedence: Precedence::Comparison,
},
TokenKind::Identifier => ParseRule {
prefix: Some(Parser::parse_variable),
infix: None,
precedence: Precedence::None,
},
TokenKind::If => ParseRule {
prefix: Some(Parser::parse_if),
infix: None,
precedence: Precedence::None,
},
TokenKind::Int => todo!(),
TokenKind::Integer => ParseRule {
prefix: Some(Parser::parse_integer),
infix: None,
precedence: Precedence::None,
},
2024-09-10 14:44:15 +00:00
TokenKind::LeftCurlyBrace => ParseRule {
prefix: Some(Parser::parse_block),
infix: None,
precedence: Precedence::None,
},
TokenKind::LeftParenthesis => ParseRule {
prefix: Some(Parser::parse_grouped),
infix: None,
precedence: Precedence::None,
},
2024-09-17 23:35:33 +00:00
TokenKind::LeftSquareBrace => ParseRule {
prefix: Some(Parser::parse_list),
infix: None,
precedence: Precedence::None,
},
2024-09-22 22:17:21 +00:00
TokenKind::Less => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_comparison_binary),
2024-09-22 22:17:21 +00:00
precedence: Precedence::Comparison,
},
TokenKind::LessEqual => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_comparison_binary),
2024-09-22 22:17:21 +00:00
precedence: Precedence::Comparison,
},
TokenKind::Let => ParseRule {
prefix: Some(Parser::parse_let_statement),
infix: None,
precedence: Precedence::None,
},
TokenKind::Loop => todo!(),
TokenKind::Map => todo!(),
TokenKind::Minus => ParseRule {
prefix: Some(Parser::parse_unary),
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Term,
},
TokenKind::MinusEqual => todo!(),
2024-09-18 15:27:41 +00:00
TokenKind::Mut => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
TokenKind::Percent => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
2024-09-18 15:27:41 +00:00
precedence: Precedence::Factor,
},
TokenKind::Plus => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Term,
},
TokenKind::PlusEqual => todo!(),
2024-09-10 14:44:15 +00:00
TokenKind::RightCurlyBrace => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
2024-09-07 10:38:12 +00:00
TokenKind::RightParenthesis => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
2024-09-17 23:35:33 +00:00
TokenKind::RightSquareBrace => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
2024-09-07 16:15:47 +00:00
TokenKind::Semicolon => ParseRule {
prefix: None,
infix: None,
precedence: Precedence::None,
},
2024-09-22 22:17:21 +00:00
TokenKind::Slash => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Factor,
},
2024-09-22 22:17:21 +00:00
TokenKind::Star => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Factor,
},
2024-09-22 22:17:21 +00:00
TokenKind::Str => todo!(),
TokenKind::String => ParseRule {
prefix: Some(Parser::parse_string),
infix: None,
precedence: Precedence::None,
},
TokenKind::Struct => todo!(),
TokenKind::While => ParseRule {
prefix: Some(Parser::parse_while),
infix: None,
precedence: Precedence::None,
},
}
}
}
2024-09-07 03:30:43 +00:00
#[derive(Debug, PartialEq)]
pub enum ParseError {
2024-09-19 00:02:12 +00:00
CannotMutateImmutableVariable {
identifier: Identifier,
position: Span,
},
2024-09-07 10:38:12 +00:00
ExpectedExpression {
found: TokenOwned,
position: Span,
},
2024-09-07 03:30:43 +00:00
ExpectedToken {
expected: TokenKind,
found: TokenOwned,
position: Span,
},
ExpectedTokenMultiple {
2024-09-17 23:35:33 +00:00
expected: &'static [TokenKind],
2024-09-07 03:30:43 +00:00
found: TokenOwned,
position: Span,
},
2024-09-07 21:16:14 +00:00
InvalidAssignmentTarget {
found: TokenOwned,
position: Span,
},
2024-09-11 07:10:12 +00:00
UndefinedVariable {
identifier: Identifier,
position: Span,
},
RegisterOverflow {
position: Span,
},
RegisterUnderflow {
position: Span,
},
2024-09-11 07:10:12 +00:00
// Wrappers around foreign errors
Chunk(ChunkError),
2024-09-07 03:30:43 +00:00
Lex(LexError),
ParseFloatError {
error: ParseFloatError,
position: Span,
},
ParseIntError {
error: ParseIntError,
position: Span,
},
2024-09-07 03:30:43 +00:00
}
2024-09-11 07:10:12 +00:00
impl From<ChunkError> for ParseError {
fn from(error: ChunkError) -> Self {
Self::Chunk(error)
}
}
impl AnnotatedError for ParseError {
fn title() -> &'static str {
"Parse Error"
}
fn description(&self) -> &'static str {
match self {
2024-09-19 00:02:12 +00:00
Self::CannotMutateImmutableVariable { .. } => "Cannot mutate immutable variable",
Self::ExpectedExpression { .. } => "Expected an expression",
Self::ExpectedToken { .. } => "Expected a specific token",
Self::ExpectedTokenMultiple { .. } => "Expected one of multiple tokens",
Self::InvalidAssignmentTarget { .. } => "Invalid assignment target",
2024-09-11 07:10:12 +00:00
Self::UndefinedVariable { .. } => "Undefined variable",
Self::RegisterOverflow { .. } => "Register overflow",
Self::RegisterUnderflow { .. } => "Register underflow",
Self::ParseFloatError { .. } => "Failed to parse float",
Self::ParseIntError { .. } => "Failed to parse integer",
2024-09-17 16:08:40 +00:00
Self::Chunk(error) => error.description(),
Self::Lex(error) => error.description(),
}
}
fn details(&self) -> Option<String> {
match self {
2024-09-19 00:02:12 +00:00
Self::CannotMutateImmutableVariable { identifier, .. } => {
Some(format!("Cannot mutate immutable variable \"{identifier}\""))
}
Self::ExpectedExpression { found, .. } => Some(format!("Found \"{found}\"")),
Self::ExpectedToken {
expected, found, ..
} => Some(format!("Expected \"{expected}\", found \"{found}\"")),
Self::ExpectedTokenMultiple {
expected, found, ..
2024-09-19 15:41:18 +00:00
} => {
let expected = expected
.iter()
.map(|kind| kind.to_string() + ", ")
.collect::<String>();
Some(format!("Expected one of {expected}, found \"{found}\""))
}
Self::InvalidAssignmentTarget { found, .. } => {
Some(format!("Invalid assignment target, found \"{found}\""))
}
2024-09-11 07:10:12 +00:00
Self::UndefinedVariable { identifier, .. } => {
Some(format!("Undefined variable \"{identifier}\""))
}
Self::RegisterOverflow { .. } => None,
Self::RegisterUnderflow { .. } => None,
Self::ParseFloatError { error, .. } => Some(error.to_string()),
Self::ParseIntError { error, .. } => Some(error.to_string()),
2024-09-17 16:08:40 +00:00
Self::Chunk(error) => error.details(),
Self::Lex(error) => error.details(),
}
}
fn position(&self) -> Span {
match self {
2024-09-19 00:02:12 +00:00
Self::CannotMutateImmutableVariable { position, .. } => *position,
Self::ExpectedExpression { position, .. } => *position,
Self::ExpectedToken { position, .. } => *position,
Self::ExpectedTokenMultiple { position, .. } => *position,
Self::InvalidAssignmentTarget { position, .. } => *position,
2024-09-11 07:10:12 +00:00
Self::UndefinedVariable { position, .. } => *position,
Self::RegisterOverflow { position } => *position,
Self::RegisterUnderflow { position } => *position,
2024-09-11 07:10:12 +00:00
Self::Chunk(error) => error.position(),
Self::Lex(error) => error.position(),
Self::ParseFloatError { position, .. } => *position,
Self::ParseIntError { position, .. } => *position,
}
2024-09-07 03:30:43 +00:00
}
}
impl From<LexError> for ParseError {
fn from(error: LexError) -> Self {
Self::Lex(error)
}
}