2024-09-07 08:34:03 +00:00
|
|
|
use std::{
|
|
|
|
fmt::{self, Display, Formatter},
|
2024-10-19 07:06:14 +00:00
|
|
|
mem::replace,
|
2024-09-12 09:08:55 +00:00
|
|
|
num::{ParseFloatError, ParseIntError},
|
2024-10-19 16:05:20 +00:00
|
|
|
vec,
|
2024-09-07 08:34:03 +00:00
|
|
|
};
|
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-13 00:19:21 +00:00
|
|
|
AnnotatedError, Chunk, ChunkError, DustError, FunctionType, Identifier, Instruction, LexError,
|
2024-10-19 02:30:20 +00:00
|
|
|
Lexer, 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> {
|
2024-09-07 08:34:03 +00:00
|
|
|
let lexer = Lexer::new(source);
|
2024-09-12 09:08:55 +00:00
|
|
|
let mut parser = Parser::new(lexer).map_err(|error| DustError::Parse { error, source })?;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
while !parser.is_eof() {
|
2024-09-07 16:15:47 +00:00
|
|
|
parser
|
2024-10-09 16:16:46 +00:00
|
|
|
.parse_statement(
|
|
|
|
Allowed {
|
|
|
|
assignment: true,
|
|
|
|
explicit_return: false,
|
|
|
|
implicit_return: true,
|
|
|
|
},
|
|
|
|
Context::None,
|
|
|
|
)
|
2024-09-07 16:15:47 +00:00
|
|
|
.map_err(|error| DustError::Parse { error, source })?;
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 02:56:01 +00:00
|
|
|
Ok(parser.take_chunk())
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
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> {
|
2024-09-12 03:07:20 +00:00
|
|
|
lexer: Lexer<'src>,
|
2024-10-19 16:05:20 +00:00
|
|
|
chunk: Chunk,
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
current_statement: Vec<(Instruction, Span)>,
|
|
|
|
minimum_register: u8,
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
current_token: Token<'src>,
|
2024-09-07 03:30:43 +00:00
|
|
|
current_position: Span,
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-09-12 03:07:20 +00:00
|
|
|
previous_token: Token<'src>,
|
|
|
|
previous_position: Span,
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
parsed_expression: bool,
|
2024-10-13 00:19:21 +00:00
|
|
|
latest_value_type: Option<Type>,
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Parser<'src> {
|
2024-09-12 09:08:55 +00:00
|
|
|
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!(
|
2024-10-09 10:15:06 +00:00
|
|
|
"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
|
|
|
|
2024-09-12 09:08:55 +00:00
|
|
|
Ok(Parser {
|
2024-09-07 03:30:43 +00:00
|
|
|
lexer,
|
2024-09-07 08:34:03 +00:00
|
|
|
chunk: Chunk::new(),
|
2024-10-19 16:05:20 +00:00
|
|
|
current_statement: Vec::new(),
|
|
|
|
minimum_register: 0,
|
2024-09-07 10:38:12 +00:00
|
|
|
current_token,
|
|
|
|
current_position,
|
2024-09-12 03:07:20 +00:00
|
|
|
previous_token: Token::Eof,
|
|
|
|
previous_position: Span(0, 0),
|
2024-10-12 07:06:44 +00:00
|
|
|
parsed_expression: false,
|
2024-10-13 00:19:21 +00:00
|
|
|
latest_value_type: None,
|
2024-09-12 09:08:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn take_chunk(self) -> Chunk {
|
2024-10-09 02:56:01 +00:00
|
|
|
log::info!("End chunk");
|
|
|
|
|
2024-09-12 09:08:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
fn next_register(&mut self) -> u8 {
|
|
|
|
self.current_statement
|
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find_map(|(instruction, _)| {
|
|
|
|
if instruction.yields_value() {
|
|
|
|
Some(instruction.a() + 1)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2024-09-12 03:07:20 +00:00
|
|
|
})
|
2024-10-19 16:05:20 +00:00
|
|
|
.unwrap_or(self.minimum_register)
|
2024-09-12 03:07:20 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
);
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-12 09:08:55 +00:00
|
|
|
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-10-12 10:27:19 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
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 {
|
2024-10-12 10:27:19 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
fn push_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-19 16:05:20 +00:00
|
|
|
self.current_statement.push((instruction, position));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn commit_current_statement(&mut self) {
|
|
|
|
let next_register = self.next_register();
|
|
|
|
|
|
|
|
for (instruction, position) in self.current_statement.drain(..) {
|
|
|
|
self.chunk.push_instruction(instruction, position);
|
|
|
|
}
|
|
|
|
|
|
|
|
self.minimum_register = next_register;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_staged_operations<const COUNT: usize>(&self) -> Option<[(Operation, Span); COUNT]> {
|
|
|
|
if self.current_statement.len() < COUNT {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut operations = [(Operation::Return, Span(0, 0)); COUNT];
|
|
|
|
|
|
|
|
for (index, (instruction, position)) in
|
|
|
|
self.current_statement.iter().rev().enumerate().take(COUNT)
|
|
|
|
{
|
|
|
|
operations[index] = (instruction.operation(), *position);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(operations)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_staged_jump_mut(&mut self) -> Option<&mut Instruction> {
|
|
|
|
self.current_statement
|
|
|
|
.iter_mut()
|
|
|
|
.rev()
|
|
|
|
.find(|(instruction, _)| matches!(instruction.operation(), Operation::Jump))
|
|
|
|
.map(|(instruction, _)| instruction)
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
fn emit_constant(&mut self, value: Value, position: Span) -> Result<(), ParseError> {
|
2024-10-13 00:19:21 +00:00
|
|
|
self.latest_value_type = Some(value.r#type());
|
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
let constant_index = self.chunk.push_constant(value, position)?;
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if let Some(instruction) = self
|
|
|
|
.current_statement
|
|
|
|
.last()
|
|
|
|
.map(|(instruction, _)| instruction)
|
|
|
|
{
|
|
|
|
if let Operation::LoadConstant = instruction.operation() {
|
|
|
|
self.push_instruction(
|
|
|
|
Instruction::load_constant(instruction.a(), constant_index, false),
|
|
|
|
position,
|
|
|
|
);
|
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.push_instruction(
|
|
|
|
Instruction::load_constant(register, constant_index, false),
|
2024-09-12 03:07:20 +00:00
|
|
|
position,
|
|
|
|
);
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-07 03:30:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
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
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
if let Token::Boolean(text) = self.current_token {
|
|
|
|
self.advance()?;
|
2024-09-23 10:42:41 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
let boolean = text.parse::<bool>().unwrap();
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = if let Some(instruction) = self
|
|
|
|
.current_statement
|
|
|
|
.last()
|
|
|
|
.map(|(instruction, _)| instruction)
|
|
|
|
{
|
|
|
|
if let Operation::LoadBoolean = instruction.operation() {
|
|
|
|
instruction.a()
|
|
|
|
} else {
|
|
|
|
self.next_register()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.next_register()
|
|
|
|
};
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(
|
|
|
|
Instruction::load_boolean(register, boolean, false),
|
2024-10-12 11:12:44 +00:00
|
|
|
position,
|
|
|
|
);
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
self.latest_value_type = Some(Type::Boolean);
|
2024-10-12 11:12:44 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Boolean,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +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()?;
|
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
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);
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
self.emit_constant(value, position)?;
|
2024-09-10 03:45:06 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Byte,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-10 03:45:06 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15: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);
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
self.emit_constant(value, position)?;
|
2024-09-10 03:45:06 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Character,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-10 03:45:06 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15: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()?;
|
|
|
|
|
2024-09-12 09:08:55 +00:00
|
|
|
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);
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
self.emit_constant(value, position)?;
|
2024-09-07 16:15:47 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Float,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +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()?;
|
|
|
|
|
2024-09-12 09:08:55 +00:00
|
|
|
let integer = text
|
|
|
|
.parse::<i64>()
|
|
|
|
.map_err(|error| ParseError::ParseIntError {
|
|
|
|
error,
|
|
|
|
position: self.previous_position,
|
|
|
|
})?;
|
2024-09-07 08:34:03 +00:00
|
|
|
let value = Value::integer(integer);
|
2024-09-07 03:30:43 +00:00
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
self.emit_constant(value, position)?;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Integer,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
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);
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
self.emit_constant(value, position)?;
|
2024-09-07 16:15:47 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::String,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
fn parse_grouped(&mut self, _: Allowed) -> Result<(), ParseError> {
|
2024-10-12 10:27:19 +00:00
|
|
|
self.allow(Token::LeftParenthesis)?;
|
2024-10-12 14:55:34 +00:00
|
|
|
self.parse_expression()?;
|
2024-10-12 10:27:19 +00:00
|
|
|
self.expect(Token::RightParenthesis)?;
|
2024-10-12 07:06:44 +00:00
|
|
|
|
|
|
|
Ok(())
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
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-09-17 21:23:37 +00:00
|
|
|
let (previous_instruction, previous_position) =
|
2024-10-19 16:05:20 +00:00
|
|
|
self.current_statement
|
|
|
|
.pop()
|
|
|
|
.ok_or_else(|| ParseError::ExpectedExpression {
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
})?;
|
2024-09-17 21:23:37 +00:00
|
|
|
|
2024-09-23 05:45:52 +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()),
|
2024-09-23 05:45:52 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2024-09-23 15:15:49 +00:00
|
|
|
if push_back {
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(previous_instruction, previous_position);
|
2024-09-23 15:15:49 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
2024-09-17 23:35:33 +00:00
|
|
|
let mut instruction = match operator.kind() {
|
2024-10-19 16:05:20 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(instruction, operator_position);
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
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() {
|
2024-09-25 13:55:10 +00:00
|
|
|
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-09-25 13:55:10 +00:00
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
local.register_index
|
2024-09-25 13:55:10 +00:00
|
|
|
}
|
2024-09-23 04:55:39 +00:00
|
|
|
Operation::LoadConstant => {
|
|
|
|
is_constant = true;
|
2024-09-12 13:11:49 +00:00
|
|
|
|
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
|
|
|
|
2024-09-24 20:49:17 +00:00
|
|
|
instruction.a()
|
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-19 16:05:20 +00:00
|
|
|
self.current_statement
|
|
|
|
.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)?;
|
2024-09-12 13:11:49 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if push_back_left {
|
|
|
|
self.push_instruction(left_instruction, left_position);
|
|
|
|
}
|
|
|
|
|
2024-09-19 15:41:18 +00:00
|
|
|
let operator = self.current_token;
|
|
|
|
let operator_position = self.current_position;
|
2024-10-12 10:27:19 +00:00
|
|
|
let rule = ParseRule::from(&operator);
|
2024-09-12 13:11:49 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
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-19 16:05:20 +00:00
|
|
|
self.current_statement
|
|
|
|
.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)?;
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if push_back_right {
|
|
|
|
self.push_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 {
|
2024-10-19 16:05:20 +00:00
|
|
|
self.next_register()
|
2024-10-05 06:07:26 +00:00
|
|
|
};
|
2024-10-09 10:15:06 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let mut new_instruction = match operator.kind() {
|
|
|
|
TokenKind::Plus => Instruction::add(register, left, right),
|
|
|
|
TokenKind::PlusEqual => Instruction::add(register, left, right),
|
|
|
|
TokenKind::Minus => Instruction::subtract(register, left, right),
|
|
|
|
TokenKind::MinusEqual => Instruction::subtract(register, left, right),
|
|
|
|
TokenKind::Star => Instruction::multiply(register, left, right),
|
|
|
|
TokenKind::StarEqual => Instruction::multiply(register, left, right),
|
|
|
|
TokenKind::Slash => Instruction::divide(register, left, right),
|
|
|
|
TokenKind::SlashEqual => Instruction::divide(register, left, right),
|
|
|
|
TokenKind::Percent => Instruction::modulo(register, left, right),
|
|
|
|
TokenKind::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: &[
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Plus,
|
2024-10-05 08:33:38 +00:00
|
|
|
TokenKind::PlusEqual,
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Minus,
|
2024-10-05 08:33:38 +00:00
|
|
|
TokenKind::MinusEqual,
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Star,
|
2024-10-05 08:33:38 +00:00
|
|
|
TokenKind::StarEqual,
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Slash,
|
2024-10-05 08:33:38 +00:00
|
|
|
TokenKind::SlashEqual,
|
2024-09-18 01:10:44 +00:00
|
|
|
TokenKind::Percent,
|
2024-10-19 16:05:20 +00:00
|
|
|
TokenKind::PercentEqual,
|
2024-09-07 08:34:03 +00:00
|
|
|
],
|
2024-09-17 21:51:39 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(new_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> {
|
2024-10-19 16:05:20 +00:00
|
|
|
if let Some(
|
|
|
|
[(Operation::Jump, _), (Operation::Equal, _) | (Operation::Less, _) | (Operation::LessEqual, _)],
|
|
|
|
) = self.get_staged_operations()
|
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-19 16:05:20 +00:00
|
|
|
self.current_statement
|
|
|
|
.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;
|
2024-10-12 10:27:19 +00:00
|
|
|
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-19 16:05:20 +00:00
|
|
|
self.current_statement
|
|
|
|
.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.saturating_sub(1), right),
|
|
|
|
Token::BangEqual => Instruction::equal(false, left.saturating_sub(1), right),
|
|
|
|
Token::Less => Instruction::less(true, left.saturating_sub(1), right),
|
|
|
|
Token::LessEqual => Instruction::less_equal(true, left.saturating_sub(1), right),
|
|
|
|
Token::Greater => Instruction::less_equal(false, left.saturating_sub(1), right),
|
|
|
|
Token::GreaterEqual => Instruction::less(false, left.saturating_sub(1), right),
|
2024-10-12 07:06:44 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-09-25 13:55:10 +00:00
|
|
|
if push_back_left {
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(left_instruction, left_position);
|
2024-09-25 13:55:10 +00:00
|
|
|
}
|
2024-09-18 20:43:34 +00:00
|
|
|
|
2024-09-25 13:55:10 +00:00
|
|
|
if push_back_right {
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(right_instruction, right_position);
|
2024-09-23 12:57:49 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(instruction, operator_position);
|
|
|
|
self.push_instruction(Instruction::jump(1, true), operator_position);
|
2024-09-24 12:29:33 +00:00
|
|
|
|
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) =
|
2024-10-19 16:05:20 +00:00
|
|
|
self.current_statement
|
|
|
|
.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;
|
2024-10-12 10:27:19 +00:00
|
|
|
let rule = ParseRule::from(&operator);
|
2024-09-23 08:01:36 +00:00
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
let 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()?;
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(left_instruction, left_position);
|
|
|
|
self.push_instruction(instruction, operator_position);
|
|
|
|
self.push_instruction(Instruction::jump(1, 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
|
|
|
|
|
|
|
Ok(())
|
2024-09-23 04:55:39 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +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
|
|
|
|
|
|
|
self.advance()?;
|
|
|
|
|
2024-09-15 01:05:03 +00:00
|
|
|
let local_index = self.parse_identifier_from(token, start_position)?;
|
2024-10-19 01:34:47 +00:00
|
|
|
let is_mutable = self
|
2024-10-13 20:46:45 +00:00
|
|
|
.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
|
|
|
|
2024-10-12 10:27:19 +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-19 16:05:20 +00:00
|
|
|
let (mut previous_instruction, previous_position) = self
|
|
|
|
.current_statement
|
|
|
|
.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);
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_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
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
|
|
|
|
|
|
|
self.push_instruction(previous_instruction, previous_position);
|
|
|
|
self.push_instruction(
|
|
|
|
Instruction::set_local(register, local_index),
|
2024-09-19 03:02:28 +00:00
|
|
|
start_position,
|
|
|
|
);
|
2024-09-12 17:03:24 +00:00
|
|
|
} else {
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
|
|
|
|
|
|
|
self.push_instruction(
|
|
|
|
Instruction::get_local(register, local_index),
|
2024-09-18 11:58:31 +00:00
|
|
|
self.previous_position,
|
2024-09-12 17:03:24 +00:00
|
|
|
);
|
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
|
|
|
|
2024-09-12 13:11:49 +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 {
|
2024-10-09 16:16:46 +00:00
|
|
|
Err(ParseError::UndeclaredVariable {
|
2024-09-11 07:10:12 +00:00
|
|
|
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-10-09 10:15:06 +00:00
|
|
|
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();
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
while !self.allow(Token::RightCurlyBrace)? && !self.is_eof() {
|
2024-10-09 16:16:46 +00:00
|
|
|
self.parse_statement(allowed, Context::None)?;
|
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-10-09 10:15:06 +00:00
|
|
|
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()?;
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let start_register = self.next_register();
|
2024-09-17 23:35:33 +00:00
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
while !self.allow(Token::RightSquareBrace)? && !self.is_eof() {
|
2024-10-19 16:05:20 +00:00
|
|
|
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-10-19 16:05:20 +00:00
|
|
|
self.commit_current_statement();
|
2024-09-17 23:35:33 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let actual_register = self.next_register() - 1;
|
2024-09-23 15:15:49 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if expected_register < actual_register {
|
|
|
|
self.push_instruction(
|
|
|
|
Instruction::close(expected_register, actual_register),
|
2024-09-18 03:06:58 +00:00
|
|
|
self.current_position,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
self.allow(Token::Comma)?;
|
2024-09-17 23:35:33 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +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;
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(
|
|
|
|
Instruction::load_list(to_register, start_register, end_register),
|
2024-09-17 23:35:33 +00:00
|
|
|
Span(start, end),
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-09 16:16:46 +00:00
|
|
|
fn parse_if(&mut self, allowed: Allowed) -> Result<(), ParseError> {
|
2024-09-25 00:32:52 +00:00
|
|
|
self.advance()?;
|
2024-10-12 14:55:34 +00:00
|
|
|
self.parse_expression()?;
|
2024-09-22 22:17:21 +00:00
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
let block_allowed = Allowed {
|
|
|
|
assignment: allowed.assignment,
|
|
|
|
explicit_return: allowed.explicit_return,
|
|
|
|
implicit_return: false,
|
|
|
|
};
|
|
|
|
|
2024-09-24 12:29:33 +00:00
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
2024-10-09 16:16:46 +00:00
|
|
|
self.parse_block(block_allowed)?;
|
2024-09-24 04:24:09 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let last_operation = self
|
|
|
|
.current_statement
|
|
|
|
.last()
|
|
|
|
.map(|(instruction, _)| instruction.operation());
|
2024-10-05 04:11:03 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if let (Some(Operation::LoadConstant) | Some(Operation::LoadBoolean), Token::Else) =
|
2024-10-05 04:11:03 +00:00
|
|
|
(last_operation, self.current_token)
|
2024-09-25 13:55:10 +00:00
|
|
|
{
|
2024-10-19 16:05:20 +00:00
|
|
|
let (mut load_constant, load_constant_position) = self
|
|
|
|
.current_statement
|
|
|
|
.pop()
|
|
|
|
.ok_or_else(|| ParseError::ExpectedExpression {
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
})?;
|
2024-09-25 13:55:10 +00:00
|
|
|
|
|
|
|
load_constant.set_c_to_boolean(true);
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(load_constant, load_constant_position);
|
2024-09-25 13:55:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
if self.allow(Token::Else)? {
|
2024-09-24 12:29:33 +00:00
|
|
|
if let Token::If = self.current_token {
|
2024-10-19 16:05:20 +00:00
|
|
|
return self.parse_if(allowed);
|
2024-09-24 04:24:09 +00:00
|
|
|
}
|
|
|
|
|
2024-09-24 12:29:33 +00:00
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
2024-10-09 16:16:46 +00:00
|
|
|
self.parse_block(block_allowed)?;
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
return Ok(());
|
2024-09-19 15:41:18 +00:00
|
|
|
}
|
2024-10-19 16:05:20 +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-19 16:05:20 +00:00
|
|
|
self.commit_current_statement();
|
2024-09-18 20:43:34 +00:00
|
|
|
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-09-25 13:55:10 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let jump_start = self.chunk.len() + self.current_statement.len();
|
2024-09-25 13:55:10 +00:00
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
self.parse_expression()?;
|
2024-10-09 16:16:46 +00:00
|
|
|
self.parse_block(Allowed {
|
|
|
|
assignment: true,
|
|
|
|
explicit_return: allowed.explicit_return,
|
|
|
|
implicit_return: false,
|
|
|
|
})?;
|
2024-09-19 03:02:28 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let jump_end = self.chunk.len() + self.current_statement.len();
|
2024-10-05 06:07:26 +00:00
|
|
|
let jump_distance = jump_end.abs_diff(jump_start) as u8;
|
2024-09-25 13:55:10 +00:00
|
|
|
let jump_back = Instruction::jump(jump_distance, false);
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if let Some(jump_over) = self.get_staged_jump_mut() {
|
|
|
|
*jump_over = Instruction::jump(jump_distance - 1, true);
|
2024-09-25 13:55:10 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.commit_current_statement();
|
2024-09-25 13:55:10 +00:00
|
|
|
self.chunk
|
|
|
|
.insert_instruction(jump_end, jump_back, self.current_position);
|
|
|
|
|
2024-09-19 03:02:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-09 16:16:46 +00:00
|
|
|
fn parse_statement(&mut self, allowed: Allowed, context: Context) -> Result<(), ParseError> {
|
2024-10-13 11:14:12 +00:00
|
|
|
self.parse(
|
|
|
|
Precedence::None,
|
|
|
|
Allowed {
|
|
|
|
assignment: true,
|
|
|
|
explicit_return: true,
|
|
|
|
implicit_return: true,
|
|
|
|
},
|
|
|
|
)?;
|
2024-10-02 00:07:57 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let parsed_expression = !self.current_statement.is_empty();
|
|
|
|
let end_of_statement = matches!(
|
|
|
|
self.current_token,
|
|
|
|
Token::Eof | Token::RightCurlyBrace | Token::Semicolon
|
|
|
|
);
|
2024-10-12 11:12:44 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if end_of_statement || context == Context::Assignment {
|
|
|
|
if let Some(
|
|
|
|
[(Operation::Jump, _), (Operation::Equal | Operation::Less | Operation::LessEqual, comparison_position)],
|
|
|
|
) = self.get_staged_operations()
|
2024-10-02 00:07:57 +00:00
|
|
|
{
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
|
|
|
|
|
|
|
self.push_instruction(
|
|
|
|
Instruction::load_boolean(register, true, true),
|
|
|
|
comparison_position,
|
|
|
|
);
|
|
|
|
self.push_instruction(
|
|
|
|
Instruction::load_boolean(register, false, false),
|
|
|
|
comparison_position,
|
|
|
|
);
|
2024-10-02 00:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
let has_semicolon = self.allow(Token::Semicolon)?;
|
2024-10-19 16:05:20 +00:00
|
|
|
let returned = self
|
|
|
|
.current_statement
|
|
|
|
.last()
|
|
|
|
.map(|(instruction, _)| matches!(instruction.operation(), Operation::Return))
|
|
|
|
.unwrap_or(false);
|
2024-10-09 16:16:46 +00:00
|
|
|
|
2024-10-13 17:01:58 +00:00
|
|
|
if allowed.implicit_return
|
2024-10-19 16:05:20 +00:00
|
|
|
&& parsed_expression
|
2024-10-13 17:01:58 +00:00
|
|
|
&& end_of_statement
|
|
|
|
&& !has_semicolon
|
|
|
|
&& !returned
|
|
|
|
{
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(Instruction::r#return(true), self.current_position);
|
|
|
|
self.commit_current_statement();
|
2024-10-09 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2024-10-02 00:07:57 +00:00
|
|
|
Ok(())
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
implicit_return: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
implicit_return: false,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
let start = self.current_position.0;
|
|
|
|
|
2024-10-09 16:16:46 +00:00
|
|
|
self.advance()?;
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
let has_return_value = if matches!(
|
2024-10-09 16:16:46 +00:00
|
|
|
self.current_token,
|
|
|
|
Token::Semicolon | Token::RightCurlyBrace
|
|
|
|
) {
|
2024-10-13 00:19:21 +00:00
|
|
|
false
|
|
|
|
} else {
|
|
|
|
self.parse_expression()?;
|
2024-10-12 07:06:44 +00:00
|
|
|
|
|
|
|
true
|
|
|
|
};
|
2024-10-09 23:03:00 +00:00
|
|
|
let end = self.current_position.1;
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(Instruction::r#return(has_return_value), Span(start, end));
|
|
|
|
self.commit_current_statement();
|
2024-09-19 22:07:11 +00:00
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
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
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
let is_mutable = self.allow(Token::Mut)?;
|
2024-09-07 16:15:47 +00:00
|
|
|
let position = self.current_position;
|
2024-09-10 13:26:05 +00:00
|
|
|
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
|
|
|
|
2024-09-10 13:26:05 +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
|
|
|
});
|
|
|
|
};
|
2024-10-12 10:27:19 +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)
|
2024-10-09 10:15:06 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
2024-09-09 23:23:49 +00:00
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
self.expect(Token::Equal)?;
|
2024-10-09 16:16:46 +00:00
|
|
|
self.parse_statement(
|
|
|
|
Allowed {
|
|
|
|
assignment: false,
|
|
|
|
explicit_return: true,
|
|
|
|
implicit_return: false,
|
|
|
|
},
|
|
|
|
Context::Assignment,
|
|
|
|
)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
|
2024-10-09 16:16:46 +00:00
|
|
|
let local_index = self
|
|
|
|
.chunk
|
|
|
|
.declare_local(identifier, r#type, is_mutable, register, position)?;
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register().saturating_sub(1);
|
2024-10-09 10:15:06 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(
|
|
|
|
Instruction::define_local(register, local_index, is_mutable),
|
2024-10-09 16:16:46 +00:00
|
|
|
position,
|
|
|
|
);
|
2024-10-19 16:05:20 +00:00
|
|
|
self.commit_current_statement();
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
fn parse_function(&mut self, _: Allowed) -> Result<(), ParseError> {
|
|
|
|
let function_start = self.current_position.0;
|
|
|
|
let mut function_parser = Parser::new(self.lexer)?;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
function_parser.expect(Token::LeftParenthesis)?;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
let mut value_parameters: Option<Vec<(Identifier, Type)>> = None;
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
while function_parser.current_token != Token::RightParenthesis {
|
|
|
|
let start = function_parser.current_position.0;
|
2024-10-12 10:27:19 +00:00
|
|
|
let is_mutable = function_parser.allow(Token::Mut)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
let parameter = if let Token::Identifier(text) = function_parser.current_token {
|
|
|
|
function_parser.advance()?;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-10-09 10:15:06 +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,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
function_parser.expect(Token::Colon)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-10-13 00:19:21 +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())]);
|
|
|
|
};
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
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(
|
2024-10-09 10:15:06 +00:00
|
|
|
parameter,
|
|
|
|
Some(r#type),
|
|
|
|
is_mutable,
|
2024-10-19 16:05:20 +00:00
|
|
|
register,
|
2024-10-09 10:15:06 +00:00
|
|
|
Span(start, end),
|
|
|
|
)?;
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if let Some(values) = &value_parameters {
|
|
|
|
function_parser.minimum_register = values.len() as u8;
|
|
|
|
}
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
function_parser.allow(Token::Comma)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
}
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-10-09 10:15:06 +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
|
|
|
|
};
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
function_parser.expect(Token::LeftCurlyBrace)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
|
|
|
|
while function_parser.current_token != Token::RightCurlyBrace {
|
2024-10-09 16:16:46 +00:00
|
|
|
function_parser.parse_statement(
|
|
|
|
Allowed {
|
|
|
|
assignment: true,
|
|
|
|
explicit_return: true,
|
|
|
|
implicit_return: true,
|
|
|
|
},
|
|
|
|
Context::None,
|
|
|
|
)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
let function_type = FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters,
|
|
|
|
return_type,
|
|
|
|
};
|
|
|
|
let function = Value::function(function_parser.chunk, function_type);
|
2024-10-09 10:15:06 +00:00
|
|
|
let function_end = self.current_position.1;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
self.lexer.skip_to(function_end);
|
|
|
|
self.emit_constant(function, Span(function_start, function_end))?;
|
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-19 16:05:20 +00:00
|
|
|
let (last_instruction, _) = self.current_statement.last().copied().ok_or_else(|| {
|
|
|
|
ParseError::ExpectedExpression {
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
}
|
|
|
|
})?;
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
if !last_instruction.yields_value() {
|
2024-10-13 00:19:21 +00:00
|
|
|
return Err(ParseError::ExpectedExpression {
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = self.current_position.0;
|
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
self.advance()?;
|
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let function_register = self.next_register();
|
2024-10-13 06:47:12 +00:00
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
while !self.allow(Token::RightParenthesis)? {
|
2024-10-13 00:19:21 +00:00
|
|
|
self.parse_expression()?;
|
2024-10-13 11:14:12 +00:00
|
|
|
self.allow(Token::Comma)?;
|
2024-10-12 14:55:34 +00:00
|
|
|
}
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
let end = self.current_position.1;
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
self.push_instruction(
|
|
|
|
Instruction::call(register, function_register),
|
2024-10-13 00:19:21 +00:00
|
|
|
Span(start, end),
|
2024-10-12 14:55:34 +00:00
|
|
|
);
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-10-13 17:01:58 +00:00
|
|
|
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 {
|
2024-10-13 17:01:58 +00:00
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
2024-10-13 17:08:12 +00:00
|
|
|
})
|
2024-10-13 17:01:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 16:16:46 +00:00
|
|
|
fn parse(&mut self, precedence: Precedence, allowed: Allowed) -> Result<(), ParseError> {
|
2024-10-12 10:27:19 +00:00
|
|
|
if let Some(prefix_parser) = ParseRule::from(&self.current_token).prefix {
|
2024-09-25 02:58:14 +00:00
|
|
|
log::debug!(
|
2024-10-13 17:01:58 +00:00
|
|
|
"{} 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
|
|
|
}
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-10-12 10:27:19 +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 {
|
2024-09-25 02:58:14 +00:00
|
|
|
log::debug!(
|
2024-10-13 17:01:58 +00:00
|
|
|
"{} 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-09-07 08:34:03 +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)?;
|
2024-09-07 08:34:03 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2024-09-17 21:23:37 +00:00
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
infix_rule = ParseRule::from(&self.current_token);
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-09 16:16:46 +00:00
|
|
|
Ok(())
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum Precedence {
|
2024-09-12 09:08:55 +00:00
|
|
|
None,
|
|
|
|
Assignment,
|
|
|
|
Conditional,
|
|
|
|
LogicalOr,
|
|
|
|
LogicalAnd,
|
|
|
|
Equality,
|
|
|
|
Comparison,
|
|
|
|
Term,
|
|
|
|
Factor,
|
|
|
|
Unary,
|
|
|
|
Call,
|
|
|
|
Primary,
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Precedence {
|
|
|
|
fn increment(&self) -> Self {
|
2024-09-12 09:08:55 +00:00
|
|
|
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,
|
|
|
|
}
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
struct Allowed {
|
2024-10-09 16:16:46 +00:00
|
|
|
pub assignment: bool,
|
|
|
|
pub explicit_return: bool,
|
|
|
|
pub implicit_return: bool,
|
2024-10-09 10:15:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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>;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
#[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>>,
|
2024-09-07 08:34:03 +00:00
|
|
|
pub precedence: Precedence,
|
|
|
|
}
|
|
|
|
|
2024-10-12 10:27:19 +00:00
|
|
|
impl From<&Token<'_>> for ParseRule<'_> {
|
2024-10-12 11:12:44 +00:00
|
|
|
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,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Async => todo!(),
|
|
|
|
Token::Bang => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: Some(Parser::parse_unary),
|
2024-09-07 08:34:03 +00:00
|
|
|
infix: None,
|
2024-09-22 22:17:21 +00:00
|
|
|
precedence: Precedence::Unary,
|
2024-09-07 08:34:03 +00:00
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Bool => ParseRule {
|
|
|
|
prefix: Some(Parser::expect_expression),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Boolean(_) => ParseRule {
|
2024-09-07 10:38:12 +00:00
|
|
|
prefix: Some(Parser::parse_boolean),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Break => todo!(),
|
|
|
|
Token::Byte(_) => ParseRule {
|
2024-09-10 03:45:06 +00:00
|
|
|
prefix: Some(Parser::parse_byte),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Character(_) => ParseRule {
|
2024-09-10 03:45:06 +00:00
|
|
|
prefix: Some(Parser::parse_character),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Colon => ParseRule {
|
|
|
|
prefix: Some(Parser::expect_expression),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Comma => ParseRule {
|
2024-09-17 23:35:33 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Dot => ParseRule {
|
|
|
|
prefix: Some(Parser::expect_expression),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::DoubleEqual => ParseRule {
|
2024-09-18 20:43:34 +00:00
|
|
|
prefix: None,
|
2024-09-23 04:55:39 +00:00
|
|
|
infix: Some(Parser::parse_comparison_binary),
|
2024-09-18 20:43:34 +00:00
|
|
|
precedence: Precedence::Equality,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::DoublePipe => ParseRule {
|
2024-09-17 21:51:39 +00:00
|
|
|
prefix: None,
|
2024-09-23 04:55:39 +00:00
|
|
|
infix: Some(Parser::parse_logical_binary),
|
2024-09-17 21:51:39 +00:00
|
|
|
precedence: Precedence::LogicalOr,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::DoubleDot => ParseRule {
|
|
|
|
prefix: Some(Parser::expect_expression),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Eof => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Equal => ParseRule {
|
2024-09-17 21:23:37 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Else => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Float(_) => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: Some(Parser::parse_float),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::FloatKeyword => ParseRule {
|
|
|
|
prefix: Some(Parser::expect_expression),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Fn => ParseRule {
|
2024-10-09 02:56:01 +00:00
|
|
|
prefix: Some(Parser::parse_function),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Identifier(_) => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: Some(Parser::parse_variable),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::If => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: Some(Parser::parse_if),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Int => ParseRule {
|
|
|
|
prefix: Some(Parser::expect_expression),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Integer(_) => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: Some(Parser::parse_integer),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::LeftCurlyBrace => ParseRule {
|
2024-09-10 14:44:15 +00:00
|
|
|
prefix: Some(Parser::parse_block),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::LeftParenthesis => ParseRule {
|
2024-09-07 08:34:03 +00:00
|
|
|
prefix: Some(Parser::parse_grouped),
|
2024-10-12 14:55:34 +00:00
|
|
|
infix: Some(Parser::parse_call),
|
|
|
|
precedence: Precedence::Call,
|
2024-09-07 08:34:03 +00:00
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::LeftSquareBrace => ParseRule {
|
2024-09-17 23:35:33 +00:00
|
|
|
prefix: Some(Parser::parse_list),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Let => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: Some(Parser::parse_let_statement),
|
|
|
|
infix: None,
|
2024-10-13 17:01:58 +00:00
|
|
|
precedence: Precedence::Assignment,
|
2024-09-22 22:17:21 +00:00
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Loop => todo!(),
|
|
|
|
Token::Map => todo!(),
|
|
|
|
Token::Minus => ParseRule {
|
2024-09-07 08:34:03 +00:00
|
|
|
prefix: Some(Parser::parse_unary),
|
2024-09-23 04:55:39 +00:00
|
|
|
infix: Some(Parser::parse_math_binary),
|
2024-09-07 08:34:03 +00:00
|
|
|
precedence: Precedence::Term,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::MinusEqual => ParseRule {
|
2024-10-05 08:33:38 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_math_binary),
|
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Mut => ParseRule {
|
2024-09-18 15:27:41 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::PercentEqual => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_math_binary),
|
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
|
|
|
Token::Plus => ParseRule {
|
2024-09-07 08:34:03 +00:00
|
|
|
prefix: None,
|
2024-09-23 04:55:39 +00:00
|
|
|
infix: Some(Parser::parse_math_binary),
|
2024-09-07 08:34:03 +00:00
|
|
|
precedence: Precedence::Term,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::PlusEqual => ParseRule {
|
2024-10-05 08:33:38 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_math_binary),
|
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Return => ParseRule {
|
2024-10-09 16:16:46 +00:00
|
|
|
prefix: Some(Parser::parse_return),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::RightCurlyBrace => ParseRule {
|
2024-09-10 14:44:15 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::RightParenthesis => ParseRule {
|
2024-09-07 10:38:12 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::RightSquareBrace => ParseRule {
|
2024-09-17 23:35:33 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Semicolon => ParseRule {
|
2024-10-13 17:01:58 +00:00
|
|
|
prefix: Some(Parser::expect_expression),
|
2024-09-07 16:15:47 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Slash => ParseRule {
|
2024-09-07 08:34:03 +00:00
|
|
|
prefix: None,
|
2024-09-23 04:55:39 +00:00
|
|
|
infix: Some(Parser::parse_math_binary),
|
2024-09-07 08:34:03 +00:00
|
|
|
precedence: Precedence::Factor,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::SlashEqual => ParseRule {
|
2024-10-05 08:33:38 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_math_binary),
|
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Star => ParseRule {
|
2024-09-07 08:34:03 +00:00
|
|
|
prefix: None,
|
2024-09-23 04:55:39 +00:00
|
|
|
infix: Some(Parser::parse_math_binary),
|
2024-09-07 08:34:03 +00:00
|
|
|
precedence: Precedence::Factor,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::StarEqual => ParseRule {
|
2024-10-05 08:33:38 +00:00
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_math_binary),
|
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Str => ParseRule {
|
|
|
|
prefix: Some(Parser::expect_expression),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::String(_) => ParseRule {
|
2024-09-22 22:17:21 +00:00
|
|
|
prefix: Some(Parser::parse_string),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
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 08:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2024-09-07 08:34:03 +00:00
|
|
|
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,
|
2024-09-10 07:42:25 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-10-09 16:16:46 +00:00
|
|
|
UnexpectedReturn {
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-12 03:07:20 +00:00
|
|
|
RegisterOverflow {
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-12 13:11:49 +00:00
|
|
|
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),
|
2024-09-12 09:08:55 +00:00
|
|
|
ParseFloatError {
|
|
|
|
error: ParseFloatError,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-10 07:42:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 13:26:05 +00:00
|
|
|
impl AnnotatedError for ParseError {
|
|
|
|
fn title() -> &'static str {
|
2024-09-10 07:42:25 +00:00
|
|
|
"Parse Error"
|
|
|
|
}
|
|
|
|
|
2024-09-10 13:26:05 +00:00
|
|
|
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",
|
2024-09-10 13:26:05 +00:00
|
|
|
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",
|
2024-09-10 13:26:05 +00:00
|
|
|
Self::InvalidAssignmentTarget { .. } => "Invalid assignment target",
|
2024-10-09 16:16:46 +00:00
|
|
|
Self::UndeclaredVariable { .. } => "Undeclared variable",
|
|
|
|
Self::UnexpectedReturn { .. } => "Unexpected return",
|
2024-09-12 03:07:20 +00:00
|
|
|
Self::RegisterOverflow { .. } => "Register overflow",
|
2024-09-12 13:11:49 +00:00
|
|
|
Self::RegisterUnderflow { .. } => "Register underflow",
|
2024-09-12 09:08:55 +00:00
|
|
|
Self::ParseFloatError { .. } => "Failed to parse float",
|
2024-09-10 13:26:05 +00:00
|
|
|
Self::ParseIntError { .. } => "Failed to parse integer",
|
2024-09-17 16:08:40 +00:00
|
|
|
Self::Chunk(error) => error.description(),
|
|
|
|
Self::Lex(error) => error.description(),
|
2024-09-10 13:26:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn details(&self) -> Option<String> {
|
2024-09-10 07:42:25 +00:00
|
|
|
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, .. } => {
|
2024-10-09 10:15:06 +00:00
|
|
|
Some(format!("Cannot mutate immutable variable {identifier}"))
|
2024-09-19 00:02:12 +00:00
|
|
|
}
|
2024-10-09 10:15:06 +00:00
|
|
|
Self::ExpectedExpression { found, .. } => Some(format!("Found {found}")),
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::ExpectedToken {
|
|
|
|
expected, found, ..
|
2024-10-09 10:15:06 +00:00
|
|
|
} => Some(format!("Expected {expected} but found {found}")),
|
2024-09-10 07:42:25 +00:00
|
|
|
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() {
|
2024-10-09 10:15:06 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-09 10:15:06 +00:00
|
|
|
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, .. } => {
|
2024-10-09 10:15:06 +00:00
|
|
|
Some(format!("Expected mutable variable, found {found}"))
|
2024-10-05 08:33:38 +00:00
|
|
|
}
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::InvalidAssignmentTarget { found, .. } => {
|
2024-10-09 10:15:06 +00:00
|
|
|
Some(format!("Invalid assignment target, found {found}"))
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
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,
|
2024-09-12 03:07:20 +00:00
|
|
|
Self::RegisterOverflow { .. } => None,
|
2024-09-12 13:11:49 +00:00
|
|
|
Self::RegisterUnderflow { .. } => None,
|
2024-09-12 09:08:55 +00:00
|
|
|
Self::ParseFloatError { error, .. } => Some(error.to_string()),
|
2024-09-10 13:26:05 +00:00
|
|
|
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(),
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 13:26:05 +00:00
|
|
|
fn position(&self) -> Span {
|
2024-09-10 07:42:25 +00:00
|
|
|
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,
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::ExpectedExpression { position, .. } => *position,
|
|
|
|
Self::ExpectedToken { position, .. } => *position,
|
|
|
|
Self::ExpectedTokenMultiple { position, .. } => *position,
|
2024-10-05 08:33:38 +00:00
|
|
|
Self::ExpectedMutableVariable { position, .. } => *position,
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::InvalidAssignmentTarget { position, .. } => *position,
|
2024-10-09 16:16:46 +00:00
|
|
|
Self::UndeclaredVariable { position, .. } => *position,
|
|
|
|
Self::UnexpectedReturn { position } => *position,
|
2024-09-12 03:07:20 +00:00
|
|
|
Self::RegisterOverflow { position } => *position,
|
2024-09-12 13:11:49 +00:00
|
|
|
Self::RegisterUnderflow { position } => *position,
|
2024-09-12 09:08:55 +00:00
|
|
|
Self::ParseFloatError { position, .. } => *position,
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::ParseIntError { position, .. } => *position,
|
2024-10-09 10:15:06 +00:00
|
|
|
Self::Chunk(error) => error.position(),
|
|
|
|
Self::Lex(error) => error.position(),
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<LexError> for ParseError {
|
|
|
|
fn from(error: LexError) -> Self {
|
|
|
|
Self::Lex(error)
|
|
|
|
}
|
|
|
|
}
|