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

2059 lines
66 KiB
Rust
Raw Normal View History

use std::{
fmt::{self, Display, Formatter},
2024-10-19 07:06:14 +00:00
mem::replace,
num::{ParseFloatError, ParseIntError},
vec,
};
2024-09-07 03:30:43 +00:00
2024-09-25 05:27:10 +00:00
use colored::Colorize;
2024-10-09 16:16:46 +00:00
use serde::{Deserialize, Serialize};
2024-09-25 05:27:10 +00:00
2024-09-07 03:30:43 +00:00
use crate::{
2024-10-20 04:46:59 +00:00
AnnotatedError, Chunk, ChunkError, DustError, FunctionType, Identifier, Instruction, LexError,
2024-10-30 07:08:25 +00:00
Lexer, NativeFunction, Operation, Span, Token, TokenKind, TokenOwned, Type, 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-10-19 17:52:23 +00:00
.parse_statement(Allowed {
assignment: true,
explicit_return: false,
2024-10-25 02:37:18 +00:00
implicit_return: true,
2024-10-19 17:52:23 +00:00
})
2024-09-07 16:15:47 +00:00
.map_err(|error| DustError::Parse { error, source })?;
}
Ok(parser.finish())
}
2024-10-09 16:16:46 +00:00
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize)]
2024-09-07 03:30:43 +00:00
pub struct Parser<'src> {
lexer: Lexer<'src>,
chunk: Chunk,
2024-10-25 02:37:18 +00:00
current_statement_length: usize,
current_is_expression: bool,
minimum_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-25 05:27:10 +00:00
log::info!(
"Begin chunk with {} at {}",
2024-09-25 05:27:10 +00:00
current_token.to_string().bold(),
current_position.to_string()
);
2024-09-07 16:15:47 +00:00
Ok(Parser {
2024-09-07 03:30:43 +00:00
lexer,
2024-10-20 04:46:59 +00:00
chunk: Chunk::new(None),
2024-10-25 02:37:18 +00:00
current_statement_length: 0,
current_is_expression: false,
minimum_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 finish(self) -> Chunk {
2024-10-09 02:56:01 +00:00
log::info!("End 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 next_register(&mut self) -> u8 {
2024-10-25 02:37:18 +00:00
self.chunk
.instructions()
.iter()
.rev()
.find_map(|(instruction, _)| {
if instruction.yields_value() {
Some(instruction.a() + 1)
} else {
None
}
})
.unwrap_or(self.minimum_register)
}
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-25 05:27:10 +00:00
log::info!(
2024-10-09 16:16:46 +00:00
"Parsing {} at {}",
2024-09-25 05:27:10 +00:00
new_token.to_string().bold(),
position.to_string()
);
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(())
}
fn allow(&mut self, allowed: Token) -> Result<bool, ParseError> {
if self.current_token == allowed {
2024-09-07 16:15:47 +00:00
self.advance()?;
Ok(true)
} else {
Ok(false)
}
}
fn expect(&mut self, expected: Token) -> Result<(), ParseError> {
if self.current_token == expected {
2024-09-07 03:30:43 +00:00
self.advance()
} else {
Err(ParseError::ExpectedToken {
expected: expected.kind(),
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) {
2024-10-09 16:16:46 +00:00
log::debug!(
"Emitting {} at {}",
instruction.operation().to_string().bold(),
position.to_string()
);
2024-10-25 02:37:18 +00:00
self.current_statement_length += 1;
self.chunk.push_instruction(instruction, position);
}
2024-10-30 04:16:10 +00:00
fn optimize_statement(&mut self) {
2024-10-25 02:37:18 +00:00
if let Some(
[Operation::LoadBoolean | Operation::LoadConstant, Operation::LoadBoolean | Operation::LoadConstant, Operation::Jump, Operation::Equal | Operation::Less | Operation::LessEqual],
) = self.get_end_of_statement()
{
2024-10-25 02:37:18 +00:00
log::trace!("Optimizing boolean comparison");
2024-10-25 02:37:18 +00:00
let mut instructions = self
.chunk
.instructions_mut()
.iter_mut()
.rev()
.map(|(instruction, _)| instruction);
let second_loader = instructions.next().unwrap();
let first_loader = instructions.next().unwrap();
2024-10-25 02:37:18 +00:00
first_loader.set_c_to_boolean(true);
let mut second_loader_new = Instruction::with_operation(second_loader.operation());
second_loader_new.set_a(first_loader.a());
second_loader_new.set_b(second_loader.b());
second_loader_new.set_c(second_loader.c());
second_loader_new.set_b_to_boolean(second_loader.b_is_constant());
second_loader_new.set_c_to_boolean(second_loader.c_is_constant());
2024-10-25 02:37:18 +00:00
*second_loader = second_loader_new;
}
2024-10-25 02:37:18 +00:00
self.current_statement_length = 0;
}
2024-10-25 02:37:18 +00:00
fn get_last_value_operation(&self) -> Option<Operation> {
self.chunk
.instructions()
.iter()
.rev()
.take(self.current_statement_length)
.find_map(|(instruction, _)| {
if instruction.yields_value() {
Some(instruction.operation())
} else {
None
}
})
}
2024-10-25 02:37:18 +00:00
fn get_end_of_statement<const COUNT: usize>(&self) -> Option<[Operation; COUNT]> {
if self.current_statement_length < COUNT {
return None;
}
2024-10-25 02:37:18 +00:00
let mut operations = [Operation::Return; COUNT];
2024-10-25 02:37:18 +00:00
for (index, (instruction, _)) in self
.chunk
.instructions()
.iter()
.rev()
.take(COUNT)
2024-10-25 02:37:18 +00:00
.enumerate()
{
2024-10-25 02:37:18 +00:00
operations[index] = instruction.operation();
}
Some(operations)
}
2024-10-20 14:20:09 +00:00
fn get_last_jump_mut(&mut self) -> Option<&mut Instruction> {
2024-10-25 02:37:18 +00:00
self.chunk
.instructions_mut()
2024-10-20 14:20:09 +00:00
.iter_mut()
.find_map(|(instruction, _)| {
if let Operation::Jump = instruction.operation() {
Some(instruction)
} else {
None
}
})
}
2024-10-30 18:48:30 +00:00
fn get_last_jumpable_mut(&mut self) -> Option<&mut Instruction> {
self.chunk
.instructions_mut()
.iter_mut()
.find_map(|(instruction, _)| {
if let Operation::LoadBoolean | Operation::LoadConstant = instruction.operation() {
Some(instruction)
} else {
None
}
})
}
fn emit_constant(&mut self, value: Value, position: Span) -> Result<(), ParseError> {
2024-09-11 07:10:12 +00:00
let constant_index = self.chunk.push_constant(value, position)?;
let register = self.next_register();
self.emit_instruction(
Instruction::load_constant(register, constant_index, false),
position,
);
2024-09-07 03:30:43 +00:00
Ok(())
}
fn parse_boolean(&mut self, _: Allowed) -> Result<(), ParseError> {
2024-09-23 10:42:41 +00:00
let position = self.current_position;
2024-09-17 21:23:37 +00:00
if let Token::Boolean(text) = self.current_token {
self.advance()?;
2024-09-23 10:42:41 +00:00
let boolean = text.parse::<bool>().unwrap();
2024-10-25 02:37:18 +00:00
let register = self.next_register();
self.emit_instruction(
Instruction::load_boolean(register, boolean, false),
position,
);
self.current_is_expression = true;
Ok(())
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::Boolean,
found: self.current_token.to_owned(),
position,
})
}
2024-09-07 10:38:12 +00:00
}
fn parse_byte(&mut self, _: Allowed) -> Result<(), ParseError> {
let position = self.current_position;
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 })?;
2024-09-10 03:45:06 +00:00
let value = Value::byte(byte);
self.emit_constant(value, position)?;
2024-09-10 03:45:06 +00:00
self.current_is_expression = true;
Ok(())
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::Byte,
found: self.current_token.to_owned(),
position,
})
}
2024-09-10 03:45:06 +00:00
}
fn parse_character(&mut self, _: Allowed) -> Result<(), ParseError> {
let position = self.current_position;
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, position)?;
2024-09-10 03:45:06 +00:00
self.current_is_expression = true;
Ok(())
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::Character,
found: self.current_token.to_owned(),
position,
})
}
2024-09-10 03:45:06 +00:00
}
fn parse_float(&mut self, _: Allowed) -> Result<(), ParseError> {
let position = self.current_position;
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, position)?;
2024-09-07 16:15:47 +00:00
self.current_is_expression = true;
Ok(())
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::Float,
found: self.current_token.to_owned(),
position,
})
}
2024-09-07 16:15:47 +00:00
}
fn parse_integer(&mut self, _: Allowed) -> Result<(), ParseError> {
let position = self.current_position;
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, position)?;
self.current_is_expression = true;
Ok(())
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::Integer,
found: self.current_token.to_owned(),
position,
})
}
}
fn parse_string(&mut self, _: Allowed) -> Result<(), ParseError> {
let position = self.current_position;
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, position)?;
2024-09-07 16:15:47 +00:00
self.current_is_expression = true;
Ok(())
} else {
Err(ParseError::ExpectedToken {
expected: TokenKind::String,
found: self.current_token.to_owned(),
position,
})
}
2024-09-07 16:15:47 +00:00
}
fn parse_grouped(&mut self, _: Allowed) -> Result<(), ParseError> {
self.allow(Token::LeftParenthesis)?;
2024-10-12 14:55:34 +00:00
self.parse_expression()?;
self.expect(Token::RightParenthesis)?;
self.current_is_expression = true;
Ok(())
}
fn parse_unary(&mut self, _: Allowed) -> 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()?;
2024-10-12 14:55:34 +00:00
self.parse_expression()?;
2024-09-07 08:37:38 +00:00
2024-10-25 02:37:18 +00:00
let (previous_instruction, previous_position) = self
.chunk
.instructions_mut()
.pop()
.ok_or_else(|| ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
})?;
2024-09-17 21:23:37 +00:00
let (push_back, is_constant, argument) = {
match previous_instruction.operation() {
2024-09-24 20:49:17 +00:00
Operation::GetLocal => (false, false, previous_instruction.a()),
Operation::LoadConstant => (false, true, previous_instruction.a()),
Operation::LoadBoolean => (true, false, previous_instruction.a()),
Operation::Close => {
return Err(ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
});
}
2024-09-24 20:49:17 +00:00
_ => (true, false, previous_instruction.a()),
2024-09-17 21:23:37 +00:00
}
2024-09-17 23:35:33 +00:00
};
if push_back {
self.emit_instruction(previous_instruction, previous_position);
}
let register = self.next_register();
2024-09-17 23:35:33 +00:00
let mut instruction = match operator.kind() {
TokenKind::Bang => Instruction::not(register, argument),
TokenKind::Minus => Instruction::negate(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 {
2024-09-24 20:49:17 +00:00
instruction.set_b_is_constant();
2024-09-17 21:23:37 +00:00
}
self.emit_instruction(instruction, operator_position);
self.current_is_expression = true;
Ok(())
}
2024-09-23 04:55:39 +00:00
fn handle_binary_argument(
&mut self,
instruction: &Instruction,
2024-10-05 06:07:26 +00:00
) -> Result<(bool, bool, bool, u8), ParseError> {
2024-09-23 04:55:39 +00:00
let mut push_back = false;
let mut is_constant = false;
2024-10-05 08:33:38 +00:00
let mut is_mutable_local = false;
2024-09-23 04:55:39 +00:00
let argument = match instruction.operation() {
Operation::GetLocal => {
let local_index = instruction.b();
let local = self.chunk.get_local(local_index, self.current_position)?;
2024-10-05 08:33:38 +00:00
is_mutable_local = local.is_mutable;
2024-10-12 14:55:34 +00:00
local.register_index
}
2024-09-23 04:55:39 +00:00
Operation::LoadConstant => {
is_constant = true;
2024-09-24 20:49:17 +00:00
instruction.b()
2024-09-23 04:55:39 +00:00
}
2024-09-24 20:49:17 +00:00
Operation::LoadBoolean => instruction.a(),
2024-09-23 04:55:39 +00:00
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
self.next_register()
2024-09-23 04:55:39 +00:00
}
};
2024-09-18 05:21:40 +00:00
2024-10-05 08:33:38 +00:00
Ok((push_back, is_constant, is_mutable_local, argument))
2024-09-23 04:55:39 +00:00
}
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-10-25 02:37:18 +00:00
self.chunk
.instructions_mut()
.pop()
.ok_or_else(|| ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
})?;
2024-10-05 08:33:38 +00:00
let (push_back_left, left_is_constant, left_is_mutable_local, left) =
2024-09-23 04:55:39 +00:00
self.handle_binary_argument(&left_instruction)?;
if push_back_left {
self.emit_instruction(left_instruction, left_position);
}
2024-09-19 15:41:18 +00:00
let operator = self.current_token;
let operator_position = self.current_position;
let rule = ParseRule::from(&operator);
if let Token::PlusEqual | Token::MinusEqual | Token::StarEqual | Token::SlashEqual =
operator
2024-10-05 08:33:38 +00:00
{
if !left_is_mutable_local {
return Err(ParseError::ExpectedMutableVariable {
found: self.previous_token.to_owned(),
position: left_position,
});
}
}
2024-09-24 12:29:33 +00:00
self.advance()?;
2024-10-13 11:14:12 +00:00
self.parse_sub_expression(&rule.precedence)?;
2024-09-24 12:29:33 +00:00
2024-10-05 08:33:38 +00:00
let (right_instruction, right_position) =
2024-10-25 02:37:18 +00:00
self.chunk
.instructions_mut()
.pop()
.ok_or_else(|| ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
})?;
let (push_back_right, right_is_constant, _, right) =
2024-10-05 08:33:38 +00:00
self.handle_binary_argument(&right_instruction)?;
if push_back_right {
self.emit_instruction(right_instruction, right_position);
}
2024-10-05 08:33:38 +00:00
let register = if left_is_mutable_local {
2024-10-05 06:07:26 +00:00
left
} else {
self.next_register()
2024-10-05 06:07:26 +00:00
};
let mut new_instruction = match operator {
Token::Plus => Instruction::add(register, left, right),
Token::PlusEqual => Instruction::add(register, left, right),
Token::Minus => Instruction::subtract(register, left, right),
Token::MinusEqual => Instruction::subtract(register, left, right),
Token::Star => Instruction::multiply(register, left, right),
Token::StarEqual => Instruction::multiply(register, left, right),
Token::Slash => Instruction::divide(register, left, right),
Token::SlashEqual => Instruction::divide(register, left, right),
Token::Percent => Instruction::modulo(register, left, right),
Token::PercentEqual => Instruction::modulo(register, left, right),
2024-09-07 03:30:43 +00:00
_ => {
return Err(ParseError::ExpectedTokenMultiple {
2024-09-17 23:35:33 +00:00
expected: &[
TokenKind::Plus,
2024-10-05 08:33:38 +00:00
TokenKind::PlusEqual,
TokenKind::Minus,
2024-10-05 08:33:38 +00:00
TokenKind::MinusEqual,
TokenKind::Star,
2024-10-05 08:33:38 +00:00
TokenKind::StarEqual,
TokenKind::Slash,
2024-10-05 08:33:38 +00:00
TokenKind::SlashEqual,
2024-09-18 01:10:44 +00:00
TokenKind::Percent,
TokenKind::PercentEqual,
],
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-15 08:25:24 +00:00
if left_is_constant {
2024-09-24 20:49:17 +00:00
new_instruction.set_b_is_constant();
2024-09-15 05:24:04 +00:00
}
2024-09-15 08:25:24 +00:00
if right_is_constant {
2024-09-24 20:49:17 +00:00
new_instruction.set_c_is_constant();
2024-09-15 05:24:04 +00:00
}
self.emit_instruction(new_instruction, operator_position);
2024-09-07 03:30:43 +00:00
if let Token::PlusEqual
| Token::MinusEqual
| Token::StarEqual
| Token::SlashEqual
| Token::PercentEqual = operator
{
self.current_is_expression = false;
} else {
self.current_is_expression = true;
}
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> {
2024-10-25 02:37:18 +00:00
if let Some(Operation::Equal | Operation::Less | Operation::LessEqual) =
self.get_last_value_operation()
2024-10-12 12:16:06 +00:00
{
return Err(ParseError::CannotChainComparison {
position: self.current_position,
});
}
2024-09-23 04:55:39 +00:00
let (left_instruction, left_position) =
2024-10-25 02:37:18 +00:00
self.chunk
.instructions_mut()
.pop()
.ok_or_else(|| ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
})?;
2024-10-05 06:07:26 +00:00
let (push_back_left, left_is_constant, _, left) =
2024-09-23 04:55:39 +00:00
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);
2024-10-09 16:16:46 +00:00
self.advance()?;
2024-10-13 11:14:12 +00:00
self.parse_sub_expression(&rule.precedence)?;
2024-10-09 16:16:46 +00:00
let (right_instruction, right_position) =
2024-10-25 02:37:18 +00:00
self.chunk
.instructions_mut()
.pop()
.ok_or_else(|| ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
})?;
2024-10-09 16:16:46 +00:00
let (push_back_right, right_is_constant, _, right) =
self.handle_binary_argument(&right_instruction)?;
let mut instruction = match operator {
Token::DoubleEqual => Instruction::equal(true, left, right),
Token::BangEqual => Instruction::equal(false, left, right),
Token::Less => Instruction::less(true, left, right),
Token::LessEqual => Instruction::less_equal(true, left, right),
Token::Greater => Instruction::less_equal(false, left, right),
Token::GreaterEqual => Instruction::less(false, left, right),
2024-09-23 04:55:39 +00:00
_ => {
return Err(ParseError::ExpectedTokenMultiple {
expected: &[
TokenKind::DoubleEqual,
TokenKind::BangEqual,
TokenKind::Less,
TokenKind::LessEqual,
TokenKind::Greater,
TokenKind::GreaterEqual,
],
2024-10-09 16:16:46 +00:00
found: operator.to_owned(),
position: operator_position,
2024-09-23 04:55:39 +00:00
})
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-24 03:38:49 +00:00
if left_is_constant {
2024-09-24 20:49:17 +00:00
instruction.set_b_is_constant();
2024-09-23 04:55:39 +00:00
}
if right_is_constant {
2024-09-24 20:49:17 +00:00
instruction.set_c_is_constant();
2024-09-23 04:55:39 +00:00
}
if push_back_left {
self.emit_instruction(left_instruction, left_position);
}
if push_back_right {
self.emit_instruction(right_instruction, right_position);
2024-09-23 12:57:49 +00:00
}
let register = self.next_register();
self.emit_instruction(instruction, operator_position);
2024-10-20 14:20:09 +00:00
2024-10-30 18:48:30 +00:00
self.emit_instruction(Instruction::jump(1, true), operator_position);
self.emit_instruction(
Instruction::load_boolean(register, true, true),
operator_position,
);
self.emit_instruction(
Instruction::load_boolean(register, false, false),
operator_position,
);
2024-09-24 12:29:33 +00:00
self.current_is_expression = true;
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-10-30 18:48:30 +00:00
let start_length = self.chunk.len();
2024-09-23 08:01:36 +00:00
let (left_instruction, left_position) =
2024-10-25 02:37:18 +00:00
self.chunk
.instructions_mut()
.pop()
.ok_or_else(|| ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
})?;
2024-09-23 10:42:41 +00:00
2024-09-23 08:01:36 +00:00
let operator = self.current_token;
let operator_position = self.current_position;
let rule = ParseRule::from(&operator);
2024-09-23 08:01:36 +00:00
2024-10-30 18:48:30 +00:00
let test_instruction = match operator {
Token::DoubleAmpersand => Instruction::test(left_instruction.a(), false),
Token::DoublePipe => Instruction::test(left_instruction.a(), true),
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.emit_instruction(left_instruction, left_position);
2024-10-30 18:48:30 +00:00
self.emit_instruction(test_instruction, operator_position);
2024-10-20 14:20:09 +00:00
2024-10-30 18:48:30 +00:00
let jump_distance = (self.chunk.len() - start_length) as u8;
2024-10-20 14:20:09 +00:00
2024-10-30 18:48:30 +00:00
self.emit_instruction(Instruction::jump(jump_distance, true), operator_position);
2024-10-13 11:14:12 +00:00
self.parse_sub_expression(&rule.precedence)?;
2024-09-23 08:01:36 +00:00
self.current_is_expression = true;
2024-09-23 08:01:36 +00:00
Ok(())
2024-09-23 04:55:39 +00:00
}
fn parse_variable(&mut self, allowed: Allowed) -> 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
let local_index = if let Token::Identifier(text) = token {
if let Ok(local_index) = self.chunk.get_local_index(text, start_position) {
local_index
} else if let Some(name) = self.chunk.name() {
if name.as_str() == text {
let register = self.next_register();
self.emit_instruction(Instruction::load_self(register), start_position);
self.chunk.declare_local(
Identifier::new(text),
None,
false,
register,
start_position,
)?;
self.current_is_expression = true;
return Ok(());
}
return if NativeFunction::from_str(text).is_some() {
self.parse_native_call(allowed)
} else {
Err(ParseError::UndeclaredVariable {
identifier: Identifier::new(text),
position: start_position,
})
};
} else {
return if NativeFunction::from_str(text).is_some() {
self.parse_native_call(allowed)
} else {
Err(ParseError::UndeclaredVariable {
identifier: Identifier::new(text),
position: start_position,
})
};
}
} else {
return Err(ParseError::ExpectedToken {
expected: TokenKind::Identifier,
found: self.current_token.to_owned(),
position: start_position,
});
};
self.advance()?;
2024-10-19 01:34:47 +00:00
let is_mutable = self
.chunk
.get_local(local_index, start_position)?
2024-10-19 01:34:47 +00:00
.is_mutable;
2024-09-17 21:23:37 +00:00
if self.allow(Token::Equal)? {
2024-10-09 16:16:46 +00:00
if !allowed.assignment {
return Err(ParseError::InvalidAssignmentTarget {
found: self.current_token.to_owned(),
position: self.current_position,
});
}
2024-09-22 22:17:21 +00:00
if !is_mutable {
return Err(ParseError::CannotMutateImmutableVariable {
identifier: self.chunk.get_identifier(local_index).cloned().unwrap(),
position: start_position,
});
}
2024-10-12 14:55:34 +00:00
self.parse_expression()?;
2024-09-15 01:05:03 +00:00
2024-10-25 02:37:18 +00:00
let (mut previous_instruction, previous_position) =
self.chunk.instructions_mut().pop().ok_or_else(|| {
ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
}
})?;
2024-09-15 01:05:03 +00:00
2024-09-24 12:29:33 +00:00
if previous_instruction.operation().is_math() {
2024-10-12 14:55:34 +00:00
let register_index = self
2024-09-15 01:05:03 +00:00
.chunk
.get_local(local_index, start_position)?
.register_index;
2024-10-12 14:55:34 +00:00
log::trace!("Condensing SET_LOCAL to binary math expression");
2024-09-18 15:27:41 +00:00
2024-10-12 14:55:34 +00:00
previous_instruction.set_a(register_index);
self.emit_instruction(previous_instruction, self.current_position);
2024-09-19 15:41:18 +00:00
2024-10-12 14:55:34 +00:00
return Ok(());
2024-09-15 01:05:03 +00:00
}
2024-09-19 03:02:28 +00:00
let register = self.next_register();
self.emit_instruction(previous_instruction, previous_position);
self.emit_instruction(
Instruction::set_local(register, local_index),
2024-09-19 03:02:28 +00:00
start_position,
);
2024-10-30 04:16:10 +00:00
self.optimize_statement();
self.current_is_expression = false;
} else {
let register = self.next_register();
self.emit_instruction(
Instruction::get_local(register, local_index),
2024-09-18 11:58:31 +00:00
self.previous_position,
);
self.current_is_expression = true;
2024-09-07 21:16:14 +00:00
}
2024-09-07 17:51:05 +00:00
Ok(())
}
fn parse_type_from(&mut self, token: Token, position: Span) -> Result<Type, ParseError> {
match token {
Token::Bool => Ok(Type::Boolean),
Token::FloatKeyword => Ok(Type::Float),
Token::Int => Ok(Type::Integer),
Token::Str => Ok(Type::String { length: None }),
_ => Err(ParseError::ExpectedTokenMultiple {
expected: &[
TokenKind::Bool,
TokenKind::FloatKeyword,
TokenKind::Int,
TokenKind::Str,
],
found: self.current_token.to_owned(),
position,
}),
}
}
fn parse_block(&mut self, allowed: Allowed) -> Result<(), ParseError> {
self.advance()?;
2024-09-10 14:44:15 +00:00
self.chunk.begin_scope();
while !self.allow(Token::RightCurlyBrace)? && !self.is_eof() {
self.parse_statement(allowed)?;
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(())
}
fn parse_list(&mut self, _: Allowed) -> 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()?;
let start_register = self.next_register();
2024-09-17 23:35:33 +00:00
while !self.allow(Token::RightSquareBrace)? && !self.is_eof() {
let expected_register = self.next_register();
2024-09-18 03:06:58 +00:00
2024-10-12 14:55:34 +00:00
self.parse_expression()?;
2024-09-17 23:35:33 +00:00
let actual_register = self.next_register() - 1;
if expected_register < actual_register {
self.emit_instruction(
Instruction::close(expected_register, actual_register),
2024-09-18 03:06:58 +00:00
self.current_position,
);
}
self.allow(Token::Comma)?;
2024-09-17 23:35:33 +00:00
}
let to_register = self.next_register();
let end_register = to_register.saturating_sub(1);
2024-09-17 23:35:33 +00:00
let end = self.current_position.1;
self.emit_instruction(
Instruction::load_list(to_register, start_register, end_register),
2024-09-17 23:35:33 +00:00
Span(start, end),
);
self.current_is_expression = true;
2024-09-17 23:35:33 +00:00
Ok(())
}
2024-10-09 16:16:46 +00:00
fn parse_if(&mut self, allowed: Allowed) -> Result<(), ParseError> {
self.advance()?;
2024-10-12 14:55:34 +00:00
self.parse_expression()?;
2024-09-22 22:17:21 +00:00
if let Some(
2024-10-25 02:37:18 +00:00
[Operation::LoadBoolean, Operation::LoadBoolean, Operation::Jump, Operation::Equal | Operation::Less | Operation::LessEqual],
) = self.get_end_of_statement()
{
2024-10-25 02:37:18 +00:00
self.chunk.instructions_mut().pop();
self.chunk.instructions_mut().pop();
}
2024-10-12 14:55:34 +00:00
let block_allowed = Allowed {
assignment: allowed.assignment,
explicit_return: allowed.explicit_return,
2024-10-25 02:37:18 +00:00
implicit_return: false,
2024-10-12 14:55:34 +00:00
};
2024-09-24 12:29:33 +00:00
if let Token::LeftCurlyBrace = self.current_token {
2024-10-30 18:48:30 +00:00
let block_start = self.chunk.len();
2024-10-09 16:16:46 +00:00
self.parse_block(block_allowed)?;
2024-10-30 18:48:30 +00:00
let block_end = self.chunk.len();
let jump_distance = (block_end - block_start) as u8;
if let Some(jump) = self.get_last_jump_mut() {
jump.set_b(jump_distance);
}
} else {
return Err(ParseError::ExpectedToken {
expected: TokenKind::LeftCurlyBrace,
found: self.current_token.to_owned(),
position: self.current_position,
});
2024-09-24 04:24:09 +00:00
}
2024-10-30 18:48:30 +00:00
let if_block_is_expression = self
.chunk
.instructions()
.iter()
.find_map(|(instruction, _)| {
if !matches!(instruction.operation(), Operation::Jump) {
Some(true)
} else {
None
}
})
.unwrap_or(false);
2024-10-25 02:37:18 +00:00
if let Token::Else = self.current_token {
2024-10-30 18:48:30 +00:00
let else_start = self.chunk.len();
2024-10-25 02:37:18 +00:00
self.parse_else(allowed, block_allowed)?;
2024-10-30 18:48:30 +00:00
let else_end = self.chunk.len();
let jump_distance = (else_end - else_start) as u8;
self.current_is_expression = if_block_is_expression
&& self
.chunk
.instructions()
.iter()
.find_map(|(instruction, _)| {
if !matches!(instruction.operation(), Operation::Jump) {
Some(true)
} else {
None
}
})
.unwrap_or(false);
if jump_distance == 1 {
if let Some(skippable) = self.get_last_jumpable_mut() {
skippable.set_c_to_boolean(true);
} else {
self.chunk.insert_instruction(
else_start,
Instruction::jump(jump_distance, true),
self.current_position,
)?;
}
} else {
self.chunk.insert_instruction(
else_start,
Instruction::jump(jump_distance, true),
self.current_position,
)?;
}
2024-10-25 02:37:18 +00:00
} else {
self.current_is_expression = false;
}
2024-10-25 02:37:18 +00:00
Ok(())
}
2024-09-24 04:24:09 +00:00
2024-10-25 02:37:18 +00:00
fn parse_else(&mut self, allowed: Allowed, block_allowed: Allowed) -> Result<(), ParseError> {
self.advance()?;
2024-10-25 02:37:18 +00:00
let if_block_end = self.chunk.len();
2024-10-25 02:37:18 +00:00
if let Token::If = self.current_token {
self.parse_if(allowed)?;
} else if let Token::LeftCurlyBrace = self.current_token {
self.parse_block(block_allowed)?;
let else_end = self.chunk.len();
if else_end - if_block_end > 1 {
2024-10-30 18:48:30 +00:00
let jump_distance = (else_end - if_block_end) as u8;
2024-10-25 02:37:18 +00:00
self.chunk.insert_instruction(
if_block_end,
2024-10-30 18:48:30 +00:00
Instruction::jump(jump_distance, true),
2024-10-25 02:37:18 +00:00
self.current_position,
)?;
2024-09-19 15:41:18 +00:00
}
self.current_is_expression = self
2024-10-25 02:37:18 +00:00
.chunk
.instructions()
.last()
.map_or(false, |(instruction, _)| instruction.yields_value());
} else {
2024-10-25 02:37:18 +00:00
return Err(ParseError::ExpectedTokenMultiple {
expected: &[TokenKind::If, TokenKind::LeftCurlyBrace],
found: self.current_token.to_owned(),
position: self.current_position,
});
2024-09-22 22:17:21 +00:00
}
2024-09-19 17:54:28 +00:00
2024-10-25 02:37:18 +00:00
self.current_is_expression = self
.chunk
.instructions()
.last()
.map_or(false, |(instruction, _)| instruction.yields_value());
2024-10-30 04:16:10 +00:00
self.optimize_statement();
Ok(())
}
2024-10-09 16:16:46 +00:00
fn parse_while(&mut self, allowed: Allowed) -> Result<(), ParseError> {
2024-09-19 03:02:28 +00:00
self.advance()?;
2024-10-20 14:20:09 +00:00
2024-10-25 02:37:18 +00:00
let jump_start = self.chunk.len() as u8;
2024-10-20 14:20:09 +00:00
2024-10-12 14:55:34 +00:00
self.parse_expression()?;
if let Some(
2024-10-25 02:37:18 +00:00
[Operation::LoadBoolean, Operation::LoadBoolean, Operation::Jump, Operation::Equal | Operation::Less | Operation::LessEqual],
) = self.get_end_of_statement()
{
2024-10-25 02:37:18 +00:00
self.chunk.instructions_mut().pop();
self.chunk.instructions_mut().pop();
}
2024-10-09 16:16:46 +00:00
self.parse_block(Allowed {
assignment: true,
explicit_return: allowed.explicit_return,
2024-10-25 02:37:18 +00:00
implicit_return: false,
2024-10-09 16:16:46 +00:00
})?;
2024-09-19 03:02:28 +00:00
2024-10-20 14:20:09 +00:00
if let Some(jump) = self.get_last_jump_mut() {
2024-10-30 18:48:30 +00:00
jump.set_b(jump.b() + 1);
2024-10-20 14:20:09 +00:00
}
2024-10-30 18:48:30 +00:00
let jump_end = self.chunk.len() as u8;
let jump_distance = jump_end - jump_start;
let jump_back = Instruction::jump(jump_distance, false);
self.emit_instruction(jump_back, self.current_position);
2024-10-30 04:16:10 +00:00
self.optimize_statement();
self.current_is_expression = false;
2024-09-19 03:02:28 +00:00
Ok(())
}
fn parse_native_call(&mut self, _: Allowed) -> Result<(), ParseError> {
let native_function = if let Token::Identifier(text) = self.current_token {
NativeFunction::from_str(text).unwrap()
} else {
return Err(ParseError::ExpectedToken {
expected: TokenKind::Identifier,
found: self.current_token.to_owned(),
position: self.current_position,
});
};
2024-10-30 07:08:25 +00:00
let start = self.current_position.0;
let start_register = self.next_register();
self.advance()?;
self.expect(Token::LeftParenthesis)?;
2024-10-30 07:08:25 +00:00
while !self.allow(Token::RightParenthesis)? {
let expected_register = self.next_register();
2024-10-30 07:08:25 +00:00
self.parse_expression()?;
2024-10-30 07:08:25 +00:00
let actual_register = self.next_register() - 1;
2024-10-30 07:08:25 +00:00
if expected_register < actual_register {
self.emit_instruction(
Instruction::close(expected_register, actual_register),
self.current_position,
);
2024-10-30 07:08:25 +00:00
}
self.allow(Token::Comma)?;
2024-10-30 07:08:25 +00:00
}
let end = self.previous_position.1;
2024-10-30 07:08:25 +00:00
let to_register = self.next_register();
let argument_count = to_register - start_register;
self.current_is_expression = native_function.returns_value();
2024-10-30 07:08:25 +00:00
self.emit_instruction(
Instruction::call_native(to_register, native_function, argument_count),
2024-10-30 07:08:25 +00:00
Span(start, end),
);
Ok(())
}
fn parse_statement(&mut self, allowed: Allowed) -> Result<(), ParseError> {
self.parse(Precedence::None, allowed)?;
2024-10-09 16:16:46 +00:00
2024-10-25 02:37:18 +00:00
if allowed.implicit_return {
self.parse_implicit_return()?;
}
Ok(())
}
2024-10-12 14:55:34 +00:00
fn parse_expression(&mut self) -> Result<(), ParseError> {
self.parse(
Precedence::None,
Allowed {
assignment: false,
explicit_return: false,
2024-10-25 02:37:18 +00:00
implicit_return: false,
2024-10-12 14:55:34 +00:00
},
)
}
2024-10-13 11:14:12 +00:00
fn parse_sub_expression(&mut self, precedence: &Precedence) -> Result<(), ParseError> {
self.parse(
precedence.increment(),
Allowed {
assignment: false,
explicit_return: false,
2024-10-25 02:37:18 +00:00
implicit_return: false,
2024-10-13 11:14:12 +00:00
},
)
}
2024-10-09 16:16:46 +00:00
fn parse_return(&mut self, allowed: Allowed) -> Result<(), ParseError> {
if !allowed.explicit_return {
return Err(ParseError::UnexpectedReturn {
position: self.current_position,
});
}
let start = self.current_position.0;
2024-10-09 16:16:46 +00:00
self.advance()?;
let has_return_value = if matches!(
2024-10-09 16:16:46 +00:00
self.current_token,
Token::Semicolon | Token::RightCurlyBrace
) {
false
} else {
self.parse_expression()?;
true
};
let end = self.current_position.1;
self.emit_instruction(Instruction::r#return(has_return_value), Span(start, end));
2024-10-30 04:16:10 +00:00
self.optimize_statement();
self.current_is_expression = false;
Ok(())
}
fn parse_implicit_return(&mut self) -> Result<(), ParseError> {
if !self.current_is_expression {
return Ok(());
}
let end_of_statement = matches!(
self.current_token,
Token::Eof | Token::RightCurlyBrace | Token::Semicolon
);
let has_semicolon = self.allow(Token::Semicolon)?;
let returned = self
2024-10-25 02:37:18 +00:00
.chunk
.instructions()
.last()
.map(|(instruction, _)| matches!(instruction.operation(), Operation::Return))
.unwrap_or(false);
if end_of_statement && !has_semicolon && !returned {
self.emit_instruction(Instruction::r#return(true), self.current_position);
}
2024-09-19 22:07:11 +00:00
2024-09-07 16:15:47 +00:00
Ok(())
}
fn parse_let_statement(&mut self, allowed: Allowed) -> Result<(), ParseError> {
if !allowed.assignment {
2024-09-18 01:10:44 +00:00
return Err(ParseError::ExpectedExpression {
found: self.current_token.to_owned(),
position: self.current_position,
});
}
2024-10-09 16:16:46 +00:00
self.advance()?;
2024-09-17 21:23:37 +00:00
let is_mutable = self.allow(Token::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(),
2024-10-09 16:16:46 +00:00
position,
2024-09-09 23:23:49 +00:00
});
};
let r#type = if self.allow(Token::Colon)? {
2024-10-09 16:16:46 +00:00
let r#type = self.parse_type_from(self.current_token, self.current_position)?;
self.advance()?;
Some(r#type)
} else {
None
};
let register = self.next_register();
2024-09-09 23:23:49 +00:00
self.expect(Token::Equal)?;
self.parse_expression()?;
2024-10-09 16:16:46 +00:00
let local_index = self
.chunk
.declare_local(identifier, r#type, is_mutable, register, position)?;
let register = self.next_register().saturating_sub(1);
self.emit_instruction(
Instruction::define_local(register, local_index, is_mutable),
2024-10-09 16:16:46 +00:00
position,
);
2024-10-30 04:16:10 +00:00
self.optimize_statement();
self.current_is_expression = false;
2024-09-07 16:15:47 +00:00
Ok(())
}
fn parse_function(&mut self, _: Allowed) -> Result<(), ParseError> {
let function_start = self.current_position.0;
let mut function_parser = Parser::new(self.lexer)?;
let identifier = if let Token::Identifier(text) = function_parser.current_token {
let position = function_parser.current_position;
2024-10-20 04:46:59 +00:00
let identifier = Identifier::new(text);
function_parser.advance()?;
2024-10-20 04:46:59 +00:00
function_parser.chunk.set_name(identifier.clone());
2024-10-20 04:46:59 +00:00
Some((identifier, position))
} else {
None
};
2024-10-09 02:56:01 +00:00
function_parser.expect(Token::LeftParenthesis)?;
2024-10-09 02:56:01 +00:00
let mut value_parameters: Option<Vec<(Identifier, Type)>> = None;
while function_parser.current_token != Token::RightParenthesis {
let start = function_parser.current_position.0;
let is_mutable = function_parser.allow(Token::Mut)?;
let parameter = if let Token::Identifier(text) = function_parser.current_token {
function_parser.advance()?;
2024-10-09 02:56:01 +00:00
Identifier::new(text)
} else {
return Err(ParseError::ExpectedToken {
expected: TokenKind::Identifier,
found: function_parser.current_token.to_owned(),
position: function_parser.current_position,
});
};
function_parser.expect(Token::Colon)?;
let r#type = function_parser.parse_type_from(
function_parser.current_token,
function_parser.current_position,
)?;
function_parser.advance()?;
let end = function_parser.current_position.1;
2024-10-12 14:55:34 +00:00
if let Some(value_parameters) = value_parameters.as_mut() {
value_parameters.push((parameter.clone(), r#type.clone()));
} else {
value_parameters = Some(vec![(parameter.clone(), r#type.clone())]);
};
let register = value_parameters
.as_ref()
.map(|values| values.len() as u8 - 1)
.unwrap_or(0);
2024-10-12 14:55:34 +00:00
function_parser.chunk.declare_local(
parameter,
Some(r#type),
is_mutable,
register,
Span(start, end),
)?;
2024-10-25 02:37:18 +00:00
function_parser.minimum_register += 1;
function_parser.allow(Token::Comma)?;
}
2024-10-09 02:56:01 +00:00
function_parser.advance()?;
2024-10-19 05:58:30 +00:00
let return_type = if function_parser.allow(Token::ArrowThin)? {
let r#type = function_parser.parse_type_from(
function_parser.current_token,
function_parser.current_position,
)?;
function_parser.advance()?;
Some(Box::new(r#type))
} else {
None
};
function_parser.expect(Token::LeftCurlyBrace)?;
while function_parser.current_token != Token::RightCurlyBrace {
function_parser.parse_statement(Allowed {
assignment: true,
explicit_return: true,
2024-10-25 02:37:18 +00:00
implicit_return: true,
})?;
}
function_parser.advance()?;
2024-10-09 02:56:01 +00:00
self.previous_token = function_parser.previous_token;
self.previous_position = function_parser.previous_position;
self.current_token = function_parser.current_token;
self.current_position = function_parser.current_position;
let function_type = FunctionType {
type_parameters: None,
value_parameters,
return_type,
};
let function = Value::function(function_parser.chunk, function_type.clone());
let function_end = self.current_position.1;
2024-10-09 02:56:01 +00:00
self.lexer.skip_to(function_end);
if let Some((identifier, identifier_position)) = identifier {
let register = self.next_register();
let local_index = self.chunk.declare_local(
identifier,
Some(Type::Function(function_type)),
false,
register,
Span(function_start, function_end),
)?;
self.emit_constant(function, Span(function_start, function_end))?;
self.emit_instruction(
Instruction::define_local(register, local_index, false),
identifier_position,
);
2024-10-30 04:16:10 +00:00
self.optimize_statement();
self.current_is_expression = false;
} else {
self.emit_constant(function, Span(function_start, function_end))?;
2024-10-30 04:16:10 +00:00
self.optimize_statement();
self.current_is_expression = true;
}
2024-10-09 02:56:01 +00:00
Ok(())
}
2024-10-12 14:55:34 +00:00
fn parse_call(&mut self) -> Result<(), ParseError> {
2024-10-25 02:37:18 +00:00
let (last_instruction, _) = self.chunk.instructions().last().copied().ok_or_else(|| {
ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
}
})?;
if !last_instruction.yields_value() {
return Err(ParseError::ExpectedExpression {
found: self.previous_token.to_owned(),
position: self.previous_position,
});
}
2024-10-19 21:24:22 +00:00
let function_register = last_instruction.a();
2024-10-30 07:08:25 +00:00
let start = self.current_position.0;
2024-10-12 14:55:34 +00:00
self.advance()?;
while !self.allow(Token::RightParenthesis)? {
let expected_register = self.next_register();
self.parse_expression()?;
let actual_register = self.next_register() - 1;
if expected_register < actual_register {
self.emit_instruction(
Instruction::close(expected_register, actual_register),
self.current_position,
);
}
2024-10-13 11:14:12 +00:00
self.allow(Token::Comma)?;
2024-10-12 14:55:34 +00:00
}
let end = self.current_position.1;
2024-10-19 21:24:22 +00:00
let to_register = self.next_register();
let argument_count = to_register - function_register - 1;
self.emit_instruction(
2024-10-19 21:24:22 +00:00
Instruction::call(to_register, function_register, argument_count),
Span(start, end),
2024-10-12 14:55:34 +00:00
);
self.current_is_expression = true;
2024-10-12 14:55:34 +00:00
Ok(())
}
fn parse_semicolon(&mut self, _: Allowed) -> Result<(), ParseError> {
self.current_is_expression = false;
2024-10-30 04:16:10 +00:00
self.optimize_statement();
self.advance()
}
fn expect_expression(&mut self, _: Allowed) -> Result<(), ParseError> {
2024-10-13 17:08:12 +00:00
if self.current_token.is_expression() {
Ok(())
} else {
Err(ParseError::ExpectedExpression {
found: self.current_token.to_owned(),
position: self.current_position,
2024-10-13 17:08:12 +00:00
})
}
}
2024-10-09 16:16:46 +00:00
fn parse(&mut self, precedence: Precedence, allowed: Allowed) -> Result<(), ParseError> {
if let Some(prefix_parser) = ParseRule::from(&self.current_token).prefix {
log::debug!(
"{} is prefix with precedence {precedence}",
2024-09-25 05:27:10 +00:00
self.current_token.to_string().bold(),
2024-09-15 10:26:31 +00:00
);
2024-09-07 21:16:14 +00:00
2024-10-09 16:16:46 +00:00
prefix_parser(self, allowed)?;
2024-09-17 21:23:37 +00:00
}
let mut infix_rule = ParseRule::from(&self.current_token);
2024-09-17 17:24:45 +00:00
while precedence <= infix_rule.precedence {
if let Some(infix_parser) = infix_rule.infix {
log::debug!(
"{} is infix with precedence {precedence}",
2024-09-25 05:27:10 +00:00
self.current_token.to_string().bold(),
2024-09-07 10:38:12 +00:00
);
2024-10-09 16:16:46 +00:00
if !allowed.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);
}
2024-10-09 16:16:46 +00:00
Ok(())
2024-09-07 03:30:43 +00:00
}
}
#[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-10-09 16:16:46 +00:00
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Context {
None,
Assignment,
}
#[derive(Debug, Clone, Copy)]
struct Allowed {
2024-10-09 16:16:46 +00:00
pub assignment: bool,
pub explicit_return: bool,
2024-10-25 02:37:18 +00:00
pub implicit_return: bool,
}
type PrefixFunction<'a> = fn(&mut Parser<'a>, Allowed) -> Result<(), ParseError>;
2024-09-07 21:16:14 +00:00
type InfixFunction<'a> = fn(&mut Parser<'a>) -> Result<(), ParseError>;
#[derive(Debug, Clone, Copy)]
2024-10-09 16:16:46 +00:00
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<&Token<'_>> for ParseRule<'_> {
fn from(token: &Token) -> Self {
match token {
2024-10-19 05:58:30 +00:00
Token::ArrowThin => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Async => todo!(),
Token::Bang => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: Some(Parser::parse_unary),
infix: None,
2024-09-22 22:17:21 +00:00
precedence: Precedence::Unary,
},
Token::BangEqual => ParseRule {
2024-09-22 22:17:21 +00:00
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
},
Token::Bool => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Boolean(_) => ParseRule {
2024-09-07 10:38:12 +00:00
prefix: Some(Parser::parse_boolean),
infix: None,
precedence: Precedence::None,
},
Token::Break => todo!(),
Token::Byte(_) => ParseRule {
2024-09-10 03:45:06 +00:00
prefix: Some(Parser::parse_byte),
infix: None,
precedence: Precedence::None,
},
Token::Character(_) => ParseRule {
2024-09-10 03:45:06 +00:00
prefix: Some(Parser::parse_character),
infix: None,
precedence: Precedence::None,
},
Token::Colon => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Comma => ParseRule {
2024-09-17 23:35:33 +00:00
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Dot => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::DoubleAmpersand => ParseRule {
2024-09-10 05:04:30 +00:00
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,
},
Token::DoubleEqual => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_comparison_binary),
precedence: Precedence::Equality,
},
Token::DoublePipe => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_logical_binary),
precedence: Precedence::LogicalOr,
},
Token::DoubleDot => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Eof => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Equal => ParseRule {
2024-09-17 21:23:37 +00:00
prefix: None,
infix: None,
precedence: Precedence::Assignment,
},
Token::Else => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Float(_) => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: Some(Parser::parse_float),
infix: None,
precedence: Precedence::None,
},
Token::FloatKeyword => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Fn => ParseRule {
2024-10-09 02:56:01 +00:00
prefix: Some(Parser::parse_function),
infix: None,
precedence: Precedence::None,
},
Token::Greater => ParseRule {
2024-09-22 22:17:21 +00:00
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,
},
Token::GreaterEqual => ParseRule {
2024-09-22 22:17:21 +00:00
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,
},
Token::Identifier(_) => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: Some(Parser::parse_variable),
infix: None,
precedence: Precedence::None,
},
Token::If => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: Some(Parser::parse_if),
infix: None,
precedence: Precedence::None,
},
Token::Int => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::Integer(_) => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: Some(Parser::parse_integer),
infix: None,
precedence: Precedence::None,
},
Token::LeftCurlyBrace => ParseRule {
2024-09-10 14:44:15 +00:00
prefix: Some(Parser::parse_block),
infix: None,
precedence: Precedence::None,
},
Token::LeftParenthesis => ParseRule {
prefix: Some(Parser::parse_grouped),
2024-10-12 14:55:34 +00:00
infix: Some(Parser::parse_call),
precedence: Precedence::Call,
},
Token::LeftSquareBrace => ParseRule {
2024-09-17 23:35:33 +00:00
prefix: Some(Parser::parse_list),
infix: None,
precedence: Precedence::None,
},
Token::Less => ParseRule {
2024-09-22 22:17:21 +00:00
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,
},
Token::LessEqual => ParseRule {
2024-09-22 22:17:21 +00:00
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,
},
Token::Let => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: Some(Parser::parse_let_statement),
infix: None,
precedence: Precedence::Assignment,
2024-09-22 22:17:21 +00:00
},
Token::Loop => todo!(),
Token::Map => todo!(),
Token::Minus => ParseRule {
prefix: Some(Parser::parse_unary),
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Term,
},
Token::MinusEqual => ParseRule {
2024-10-05 08:33:38 +00:00
prefix: None,
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Mut => ParseRule {
2024-09-18 15:27:41 +00:00
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Percent => ParseRule {
2024-09-18 15:27:41 +00:00
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,
},
Token::PercentEqual => ParseRule {
prefix: None,
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Plus => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Term,
},
Token::PlusEqual => ParseRule {
2024-10-05 08:33:38 +00:00
prefix: None,
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Return => ParseRule {
2024-10-09 16:16:46 +00:00
prefix: Some(Parser::parse_return),
infix: None,
precedence: Precedence::None,
},
Token::RightCurlyBrace => ParseRule {
2024-09-10 14:44:15 +00:00
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::RightParenthesis => ParseRule {
2024-09-07 10:38:12 +00:00
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::RightSquareBrace => ParseRule {
2024-09-17 23:35:33 +00:00
prefix: None,
infix: None,
precedence: Precedence::None,
},
Token::Semicolon => ParseRule {
prefix: Some(Parser::parse_semicolon),
2024-09-07 16:15:47 +00:00
infix: None,
precedence: Precedence::None,
},
Token::Slash => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Factor,
},
Token::SlashEqual => ParseRule {
2024-10-05 08:33:38 +00:00
prefix: None,
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Star => ParseRule {
prefix: None,
2024-09-23 04:55:39 +00:00
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Factor,
},
Token::StarEqual => ParseRule {
2024-10-05 08:33:38 +00:00
prefix: None,
infix: Some(Parser::parse_math_binary),
precedence: Precedence::Assignment,
},
Token::Str => ParseRule {
prefix: Some(Parser::expect_expression),
infix: None,
precedence: Precedence::None,
},
Token::String(_) => ParseRule {
2024-09-22 22:17:21 +00:00
prefix: Some(Parser::parse_string),
infix: None,
precedence: Precedence::None,
},
Token::Struct => todo!(),
Token::While => ParseRule {
2024-09-22 22:17:21 +00:00
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-10-12 12:16:06 +00:00
CannotChainComparison {
position: Span,
},
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-10-05 08:33:38 +00:00
ExpectedMutableVariable {
found: TokenOwned,
position: Span,
},
2024-09-07 21:16:14 +00:00
InvalidAssignmentTarget {
found: TokenOwned,
position: Span,
},
2024-10-09 16:16:46 +00:00
UndeclaredVariable {
2024-09-11 07:10:12 +00:00
identifier: Identifier,
position: Span,
},
2024-10-09 16:16:46 +00:00
UnexpectedReturn {
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-10-12 12:16:06 +00:00
Self::CannotChainComparison { .. } => "Cannot chain comparison",
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",
2024-10-05 08:33:38 +00:00
Self::ExpectedMutableVariable { .. } => "Expected a mutable variable",
Self::InvalidAssignmentTarget { .. } => "Invalid assignment target",
2024-10-09 16:16:46 +00:00
Self::UndeclaredVariable { .. } => "Undeclared variable",
Self::UnexpectedReturn { .. } => "Unexpected return",
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-10-12 12:16:06 +00:00
Self::CannotChainComparison { .. } => {
Some("Cannot chain comparison operations".to_string())
}
2024-09-19 00:02:12 +00:00
Self::CannotMutateImmutableVariable { identifier, .. } => {
Some(format!("Cannot mutate immutable variable {identifier}"))
2024-09-19 00:02:12 +00:00
}
Self::ExpectedExpression { found, .. } => Some(format!("Found {found}")),
Self::ExpectedToken {
expected, found, ..
} => Some(format!("Expected {expected} but found {found}")),
Self::ExpectedTokenMultiple {
expected, found, ..
2024-09-19 15:41:18 +00:00
} => {
2024-09-24 04:24:09 +00:00
let mut details = String::from("Expected");
for (index, token) in expected.iter().enumerate() {
details.push_str(&format!(" {token}"));
2024-09-24 04:24:09 +00:00
if index < expected.len() - 2 {
details.push_str(", ");
}
if index == expected.len() - 2 {
details.push_str(" or");
}
}
details.push_str(&format!(" but found {found}"));
2024-09-19 15:41:18 +00:00
2024-09-24 04:24:09 +00:00
Some(details)
2024-09-19 15:41:18 +00:00
}
2024-10-05 08:33:38 +00:00
Self::ExpectedMutableVariable { found, .. } => {
Some(format!("Expected mutable variable, found {found}"))
2024-10-05 08:33:38 +00:00
}
Self::InvalidAssignmentTarget { found, .. } => {
Some(format!("Invalid assignment target, found {found}"))
}
2024-10-09 16:16:46 +00:00
Self::UndeclaredVariable { identifier, .. } => {
Some(format!("Undeclared variable {identifier}"))
2024-09-11 07:10:12 +00:00
}
2024-10-09 16:16:46 +00:00
Self::UnexpectedReturn { .. } => None,
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-10-12 12:16:06 +00:00
Self::CannotChainComparison { position } => *position,
2024-09-19 00:02:12 +00:00
Self::CannotMutateImmutableVariable { position, .. } => *position,
Self::ExpectedExpression { position, .. } => *position,
Self::ExpectedToken { position, .. } => *position,
Self::ExpectedTokenMultiple { position, .. } => *position,
2024-10-05 08:33:38 +00:00
Self::ExpectedMutableVariable { position, .. } => *position,
Self::InvalidAssignmentTarget { position, .. } => *position,
2024-10-09 16:16:46 +00:00
Self::UndeclaredVariable { position, .. } => *position,
Self::UnexpectedReturn { position } => *position,
Self::RegisterOverflow { position } => *position,
Self::RegisterUnderflow { position } => *position,
Self::ParseFloatError { position, .. } => *position,
Self::ParseIntError { position, .. } => *position,
Self::Chunk(error) => error.position(),
Self::Lex(error) => error.position(),
}
2024-09-07 03:30:43 +00:00
}
}
impl From<LexError> for ParseError {
fn from(error: LexError) -> Self {
Self::Lex(error)
}
}