2024-11-06 20:40:37 +00:00
|
|
|
//! Compilation tools and errors
|
2024-11-06 00:38:26 +00:00
|
|
|
//!
|
2024-11-06 20:40:37 +00:00
|
|
|
//! This module provides two compilation options:
|
|
|
|
//! - [`compile`], which compiles the entire input and returns a chunk
|
|
|
|
//! - [`Compiler`], which compiles the input a token at a time while assembling a chunk
|
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-09-07 03:30:43 +00:00
|
|
|
use crate::{
|
2024-11-06 18:50:49 +00:00
|
|
|
optimize, AnnotatedError, Chunk, ChunkError, DustError, FunctionType, Instruction, LexError,
|
|
|
|
Lexer, Local, NativeFunction, Operation, Scope, Span, Token, TokenKind, TokenOwned, Type,
|
|
|
|
Value,
|
2024-09-07 03:30:43 +00:00
|
|
|
};
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
/// Compiles the input and returns a chunk.
|
2024-11-06 00:38:26 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ```
|
2024-11-06 20:40:37 +00:00
|
|
|
/// # use dust_lang::compile;
|
2024-11-06 00:38:26 +00:00
|
|
|
/// let source = "40 + 2 == 42";
|
2024-11-06 20:40:37 +00:00
|
|
|
/// let chunk = compile(source).unwrap();
|
2024-11-06 00:38:26 +00:00
|
|
|
///
|
|
|
|
/// assert_eq!(chunk.len(), 6);
|
|
|
|
/// ```
|
2024-11-06 20:40:37 +00:00
|
|
|
pub fn compile(source: &str) -> Result<Chunk, DustError> {
|
2024-09-07 08:34:03 +00:00
|
|
|
let lexer = Lexer::new(source);
|
2024-11-06 20:40:37 +00:00
|
|
|
let mut compiler =
|
|
|
|
Compiler::new(lexer).map_err(|error| DustError::Compile { error, source })?;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
compiler
|
2024-11-03 01:24:41 +00:00
|
|
|
.parse_top_level()
|
2024-11-06 20:40:37 +00:00
|
|
|
.map_err(|error| DustError::Compile { error, source })?;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
Ok(compiler.finish())
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
/// Low-level tool for compiling the input a token at a time while assembling a chunk.
|
2024-11-06 00:38:26 +00:00
|
|
|
///
|
2024-11-06 20:40:37 +00:00
|
|
|
/// See the [`compile`] function an example of how to create and use a Compiler.
|
2024-11-06 18:09:29 +00:00
|
|
|
#[derive(Debug, Eq, PartialEq, PartialOrd, Ord)]
|
2024-11-06 20:40:37 +00:00
|
|
|
pub struct Compiler<'src> {
|
2024-10-19 16:05:20 +00:00
|
|
|
chunk: Chunk,
|
2024-11-06 18:09:29 +00:00
|
|
|
lexer: Lexer<'src>,
|
2024-11-06 20:40:37 +00:00
|
|
|
optimization_count: usize,
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
previous_is_expression: bool,
|
2024-10-19 16:05:20 +00:00
|
|
|
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-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
impl<'src> Compiler<'src> {
|
|
|
|
pub fn new(mut lexer: Lexer<'src>) -> Result<Self, CompileError> {
|
2024-09-12 09:08:55 +00:00
|
|
|
let (current_token, current_position) = lexer.next_token()?;
|
2024-11-06 18:09:29 +00:00
|
|
|
let chunk = Chunk::new(None);
|
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-11-06 20:40:37 +00:00
|
|
|
Ok(Compiler {
|
2024-11-06 18:09:29 +00:00
|
|
|
chunk,
|
2024-09-07 03:30:43 +00:00
|
|
|
lexer,
|
2024-11-06 18:09:29 +00:00
|
|
|
optimization_count: 0,
|
|
|
|
previous_is_expression: false,
|
2024-10-19 16:05:20 +00:00
|
|
|
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-09-12 09:08:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-20 04:06:22 +00:00
|
|
|
pub fn finish(self) -> Chunk {
|
2024-11-06 20:40:37 +00:00
|
|
|
log::info!("End chunk with {} optimizations", self.optimization_count);
|
2024-10-09 02:56:01 +00:00
|
|
|
|
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 {
|
2024-11-07 07:10:02 +00:00
|
|
|
let current = self
|
|
|
|
.chunk
|
2024-10-25 02:37:18 +00:00
|
|
|
.instructions()
|
2024-10-19 16:05:20 +00:00
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.find_map(|(instruction, _)| {
|
|
|
|
if instruction.yields_value() {
|
2024-11-07 07:10:02 +00:00
|
|
|
Some(instruction.a())
|
2024-10-19 16:05:20 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2024-09-12 03:07:20 +00:00
|
|
|
})
|
2024-11-07 07:10:02 +00:00
|
|
|
.unwrap_or(self.minimum_register);
|
|
|
|
|
|
|
|
current.overflowing_add(1).0
|
2024-09-12 03:07:20 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn advance(&mut self) -> Result<(), CompileError> {
|
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-11-06 20:40:37 +00:00
|
|
|
fn get_local(&self, index: u8) -> Result<&Local, CompileError> {
|
2024-11-04 20:38:58 +00:00
|
|
|
self.chunk
|
2024-11-06 18:50:49 +00:00
|
|
|
.get_local(index)
|
2024-11-06 20:40:37 +00:00
|
|
|
.map_err(|error| CompileError::Chunk {
|
2024-11-06 18:50:49 +00:00
|
|
|
error,
|
2024-11-04 20:38:58 +00:00
|
|
|
position: self.current_position,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn get_local_index(&self, identifier_text: &str) -> Result<u8, CompileError> {
|
2024-11-04 20:38:58 +00:00
|
|
|
self.chunk
|
|
|
|
.locals()
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.rev()
|
|
|
|
.find_map(|(index, local)| {
|
|
|
|
let identifier = self
|
|
|
|
.chunk
|
|
|
|
.constants()
|
|
|
|
.get(local.identifier_index as usize)?
|
|
|
|
.as_string()?;
|
|
|
|
|
|
|
|
if identifier == identifier_text {
|
|
|
|
Some(index as u8)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2024-11-06 20:40:37 +00:00
|
|
|
.ok_or(CompileError::UndeclaredVariable {
|
2024-11-04 20:38:58 +00:00
|
|
|
identifier: identifier_text.to_string(),
|
|
|
|
position: self.current_position,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn declare_local(
|
|
|
|
&mut self,
|
|
|
|
identifier: &str,
|
|
|
|
r#type: Option<Type>,
|
|
|
|
is_mutable: bool,
|
2024-11-05 17:44:16 +00:00
|
|
|
scope: Scope,
|
2024-11-04 20:38:58 +00:00
|
|
|
register_index: u8,
|
|
|
|
) -> (u8, u8) {
|
|
|
|
log::debug!("Declare local {identifier}");
|
|
|
|
|
|
|
|
let identifier = Value::string(identifier);
|
|
|
|
let identifier_index = self.chunk.push_or_get_constant(identifier);
|
|
|
|
|
|
|
|
self.chunk.locals_mut().push(Local::new(
|
|
|
|
identifier_index,
|
|
|
|
r#type,
|
|
|
|
is_mutable,
|
2024-11-05 17:44:16 +00:00
|
|
|
scope,
|
2024-11-04 20:38:58 +00:00
|
|
|
register_index,
|
|
|
|
));
|
|
|
|
|
|
|
|
(self.chunk.locals().len() as u8 - 1, identifier_index)
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn allow(&mut self, allowed: Token) -> Result<bool, CompileError> {
|
2024-10-12 10:27:19 +00:00
|
|
|
if self.current_token == allowed {
|
2024-09-07 16:15:47 +00:00
|
|
|
self.advance()?;
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn expect(&mut self, expected: Token) -> Result<(), CompileError> {
|
2024-10-12 10:27:19 +00:00
|
|
|
if self.current_token == expected {
|
2024-09-07 03:30:43 +00:00
|
|
|
self.advance()
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::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 23:13:19 +00:00
|
|
|
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-11-04 20:38:58 +00:00
|
|
|
self.chunk.instructions_mut().push((instruction, position));
|
2024-10-19 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn pop_last_instruction(&mut self) -> Result<(Instruction, Span), CompileError> {
|
2024-11-05 21:25:55 +00:00
|
|
|
self.chunk
|
|
|
|
.instructions_mut()
|
|
|
|
.pop()
|
2024-11-06 20:40:37 +00:00
|
|
|
.ok_or_else(|| CompileError::ExpectedExpression {
|
2024-11-05 21:25:55 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-25 02:37:18 +00:00
|
|
|
fn get_last_value_operation(&self) -> Option<Operation> {
|
|
|
|
self.chunk
|
|
|
|
.instructions()
|
|
|
|
.iter()
|
2024-11-06 03:10:13 +00:00
|
|
|
.last()
|
|
|
|
.map(|(instruction, _)| instruction.operation())
|
2024-10-19 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 03:10:13 +00:00
|
|
|
fn get_last_instructions<const COUNT: usize>(&self) -> Option<[Operation; COUNT]> {
|
2024-10-25 02:37:18 +00:00
|
|
|
let mut operations = [Operation::Return; COUNT];
|
2024-10-19 16:05:20 +00:00
|
|
|
|
2024-10-25 02:37:18 +00:00
|
|
|
for (index, (instruction, _)) in self
|
|
|
|
.chunk
|
|
|
|
.instructions()
|
2024-10-21 02:18:58 +00:00
|
|
|
.iter()
|
|
|
|
.rev()
|
|
|
|
.take(COUNT)
|
2024-10-25 02:37:18 +00:00
|
|
|
.enumerate()
|
2024-10-19 16:05:20 +00:00
|
|
|
{
|
2024-10-25 02:37:18 +00:00
|
|
|
operations[index] = instruction.operation();
|
2024-10-19 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(operations)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn emit_constant(&mut self, value: Value, position: Span) -> Result<(), CompileError> {
|
2024-11-04 20:38:58 +00:00
|
|
|
let constant_index = self.chunk.push_or_get_constant(value);
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 16:05:20 +00:00
|
|
|
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-11-06 20:50:05 +00:00
|
|
|
fn parse_boolean(&mut self) -> Result<(), CompileError> {
|
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-25 02:37:18 +00:00
|
|
|
let register = self.next_register();
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 16:05:20 +00:00
|
|
|
Instruction::load_boolean(register, boolean, false),
|
2024-10-12 11:12:44 +00:00
|
|
|
position,
|
|
|
|
);
|
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::ExpectedToken {
|
2024-10-12 11:12:44 +00:00
|
|
|
expected: TokenKind::Boolean,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_byte(&mut self) -> Result<(), CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
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)
|
2024-11-06 20:40:37 +00:00
|
|
|
.map_err(|error| CompileError::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-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::ExpectedToken {
|
2024-10-12 11:12:44 +00:00
|
|
|
expected: TokenKind::Byte,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-10 03:45:06 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_character(&mut self) -> Result<(), CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
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-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::ExpectedToken {
|
2024-10-12 11:12:44 +00:00
|
|
|
expected: TokenKind::Character,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-10 03:45:06 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_float(&mut self) -> Result<(), CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
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>()
|
2024-11-06 20:40:37 +00:00
|
|
|
.map_err(|error| CompileError::ParseFloatError {
|
2024-09-12 09:08:55 +00:00
|
|
|
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-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::ExpectedToken {
|
2024-10-12 11:12:44 +00:00
|
|
|
expected: TokenKind::Float,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_integer(&mut self) -> Result<(), CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
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>()
|
2024-11-06 20:40:37 +00:00
|
|
|
.map_err(|error| CompileError::ParseIntError {
|
2024-09-12 09:08:55 +00:00
|
|
|
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-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::ExpectedToken {
|
2024-10-12 11:12:44 +00:00
|
|
|
expected: TokenKind::Integer,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_string(&mut self) -> Result<(), CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
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-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 11:12:44 +00:00
|
|
|
Ok(())
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::ExpectedToken {
|
2024-10-12 11:12:44 +00:00
|
|
|
expected: TokenKind::String,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_grouped(&mut self) -> Result<(), CompileError> {
|
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
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 07:06:44 +00:00
|
|
|
Ok(())
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_unary(&mut self) -> Result<(), CompileError> {
|
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-11-05 21:25:55 +00:00
|
|
|
let (previous_instruction, previous_position) = self.pop_last_instruction()?;
|
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 => {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedExpression {
|
2024-09-23 05:45:52 +00:00
|
|
|
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 23:13:19 +00:00
|
|
|
self.emit_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
|
|
|
_ => {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::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 23:13:19 +00:00
|
|
|
self.emit_instruction(instruction, operator_position);
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +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-11-06 20:40:37 +00:00
|
|
|
) -> Result<(bool, bool, bool, u8), CompileError> {
|
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();
|
2024-11-04 20:38:58 +00:00
|
|
|
let local = self.get_local(local_index)?;
|
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
|
|
|
}
|
|
|
|
Operation::Close => {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedExpression {
|
2024-09-23 04:55:39 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
push_back = true;
|
2024-09-15 10:26:31 +00:00
|
|
|
|
2024-11-01 01:40:42 +00:00
|
|
|
if instruction.yields_value() {
|
|
|
|
instruction.a()
|
|
|
|
} else {
|
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
fn parse_math_binary(&mut self) -> Result<(), CompileError> {
|
2024-09-19 15:41:18 +00:00
|
|
|
let (left_instruction, left_position) =
|
2024-11-06 20:40:37 +00:00
|
|
|
self.chunk.instructions_mut().pop().ok_or_else(|| {
|
|
|
|
CompileError::ExpectedExpression {
|
2024-10-19 16:05:20 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
2024-11-06 20:40:37 +00:00
|
|
|
}
|
|
|
|
})?;
|
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 {
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(left_instruction, left_position);
|
2024-10-19 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedMutableVariable {
|
2024-10-05 08:33:38 +00:00
|
|
|
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-11-05 21:25:55 +00:00
|
|
|
let (right_instruction, right_position) = self.pop_last_instruction()?;
|
2024-10-19 16:05:20 +00:00
|
|
|
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 {
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(right_instruction, right_position);
|
2024-10-19 16:05:20 +00:00
|
|
|
}
|
|
|
|
|
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-20 04:06:22 +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
|
|
|
_ => {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::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 23:13:19 +00:00
|
|
|
self.emit_instruction(new_instruction, operator_position);
|
2024-09-07 03:30:43 +00:00
|
|
|
|
2024-10-20 04:06:22 +00:00
|
|
|
if let Token::PlusEqual
|
|
|
|
| Token::MinusEqual
|
|
|
|
| Token::StarEqual
|
|
|
|
| Token::SlashEqual
|
|
|
|
| Token::PercentEqual = operator
|
|
|
|
{
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = false;
|
2024-10-20 22:51:04 +00:00
|
|
|
} else {
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 04:06:22 +00:00
|
|
|
}
|
|
|
|
|
2024-09-23 04:55:39 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2024-09-19 00:02:12 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_comparison_binary(&mut self) -> Result<(), CompileError> {
|
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
|
|
|
{
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::CannotChainComparison {
|
2024-10-12 12:16:06 +00:00
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-23 04:55:39 +00:00
|
|
|
let (left_instruction, left_position) =
|
2024-11-06 20:40:37 +00:00
|
|
|
self.chunk.instructions_mut().pop().ok_or_else(|| {
|
|
|
|
CompileError::ExpectedExpression {
|
2024-10-19 16:05:20 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
2024-11-06 20:40:37 +00:00
|
|
|
}
|
|
|
|
})?;
|
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-11-06 20:40:37 +00:00
|
|
|
self.chunk.instructions_mut().pop().ok_or_else(|| {
|
|
|
|
CompileError::ExpectedExpression {
|
2024-10-19 16:05:20 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
2024-11-06 20:40:37 +00:00
|
|
|
}
|
|
|
|
})?;
|
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 {
|
2024-10-20 04:06:22 +00:00
|
|
|
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
|
|
|
_ => {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedTokenMultiple {
|
2024-09-23 04:55:39 +00:00
|
|
|
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 23:13:19 +00:00
|
|
|
self.emit_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 23:13:19 +00:00
|
|
|
self.emit_instruction(right_instruction, right_position);
|
2024-09-23 12:57:49 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 17:39:50 +00:00
|
|
|
let register = self.next_register();
|
|
|
|
|
2024-10-19 23:13:19 +00:00
|
|
|
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);
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 17:39:50 +00:00
|
|
|
Instruction::load_boolean(register, true, true),
|
|
|
|
operator_position,
|
|
|
|
);
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 17:39:50 +00:00
|
|
|
Instruction::load_boolean(register, false, false),
|
|
|
|
operator_position,
|
|
|
|
);
|
2024-09-24 12:29:33 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-09-07 03:30:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_logical_binary(&mut self) -> Result<(), CompileError> {
|
2024-10-30 18:48:30 +00:00
|
|
|
let start_length = self.chunk.len();
|
2024-11-05 21:25:55 +00:00
|
|
|
let (left_instruction, left_position) = self.pop_last_instruction()?;
|
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-30 18:48:30 +00:00
|
|
|
let test_instruction = match operator {
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::DoubleAmpersand => Instruction::test(left_instruction.a(), false),
|
|
|
|
Token::DoublePipe => Instruction::test(left_instruction.a(), true),
|
2024-09-23 08:01:36 +00:00
|
|
|
_ => {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedTokenMultiple {
|
2024-09-23 08:01:36 +00:00
|
|
|
expected: &[TokenKind::DoubleAmpersand, TokenKind::DoublePipe],
|
|
|
|
found: operator.to_owned(),
|
|
|
|
position: operator_position,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
self.advance()?;
|
2024-10-19 23:13:19 +00:00
|
|
|
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
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-09-23 08:01:36 +00:00
|
|
|
Ok(())
|
2024-09-23 04:55:39 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_variable(&mut self) -> Result<(), CompileError> {
|
2024-09-17 21:23:37 +00:00
|
|
|
let start_position = self.current_position;
|
2024-11-06 08:36:46 +00:00
|
|
|
let identifier = if let Token::Identifier(text) = self.current_token {
|
|
|
|
self.advance()?;
|
2024-09-19 22:07:11 +00:00
|
|
|
|
2024-11-06 08:36:46 +00:00
|
|
|
text
|
2024-10-20 06:30:22 +00:00
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedToken {
|
2024-10-20 06:30:22 +00:00
|
|
|
expected: TokenKind::Identifier,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position: start_position,
|
|
|
|
});
|
|
|
|
};
|
2024-11-06 08:36:46 +00:00
|
|
|
let local_index = if let Ok(local_index) = self.get_local_index(identifier) {
|
|
|
|
local_index
|
|
|
|
} else if let Some(native_function) = NativeFunction::from_str(identifier) {
|
|
|
|
return self.parse_native_call(native_function);
|
|
|
|
} else if Some(identifier) == self.chunk.name().map(|string| string.as_str()) {
|
|
|
|
let register = self.next_register();
|
|
|
|
let scope = self.chunk.current_scope();
|
2024-10-30 13:11:28 +00:00
|
|
|
|
2024-11-06 08:36:46 +00:00
|
|
|
self.emit_instruction(Instruction::load_self(register), start_position);
|
|
|
|
self.declare_local(identifier, None, false, scope, register);
|
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-11-06 08:36:46 +00:00
|
|
|
|
|
|
|
return Ok(());
|
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::UndeclaredVariable {
|
2024-11-06 08:36:46 +00:00
|
|
|
identifier: identifier.to_string(),
|
|
|
|
position: start_position,
|
|
|
|
});
|
|
|
|
};
|
2024-10-30 13:11:28 +00:00
|
|
|
|
2024-11-05 21:07:51 +00:00
|
|
|
let (is_mutable, local_scope) = {
|
|
|
|
let local = self.get_local(local_index)?;
|
|
|
|
|
|
|
|
(local.is_mutable, local.scope)
|
|
|
|
};
|
|
|
|
let current_scope = self.chunk.current_scope();
|
|
|
|
|
|
|
|
if !current_scope.contains(&local_scope) {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::VariableOutOfScope {
|
2024-11-05 21:07:51 +00:00
|
|
|
identifier: self.chunk.get_identifier(local_index).unwrap(),
|
|
|
|
position: start_position,
|
|
|
|
variable_scope: local_scope,
|
|
|
|
access_scope: current_scope,
|
|
|
|
});
|
|
|
|
}
|
2024-11-04 20:38:58 +00:00
|
|
|
|
2024-11-05 21:07:51 +00:00
|
|
|
if self.allow(Token::Equal)? {
|
2024-09-22 22:17:21 +00:00
|
|
|
if !is_mutable {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::CannotMutateImmutableVariable {
|
2024-11-04 20:38:58 +00:00
|
|
|
identifier: self.chunk.get_identifier(local_index).unwrap(),
|
2024-09-22 22:17:21 +00:00
|
|
|
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(|| {
|
2024-11-06 20:40:37 +00:00
|
|
|
CompileError::ExpectedExpression {
|
2024-10-25 02:37:18 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
}
|
2024-10-19 16:05:20 +00:00
|
|
|
})?;
|
2024-09-15 01:05:03 +00:00
|
|
|
|
2024-09-24 12:29:33 +00:00
|
|
|
if previous_instruction.operation().is_math() {
|
2024-11-04 20:38:58 +00:00
|
|
|
let register_index = self.get_local(local_index)?.register_index;
|
2024-09-15 01:05:03 +00:00
|
|
|
|
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 23:13:19 +00:00
|
|
|
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
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
|
|
|
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(previous_instruction, previous_position);
|
|
|
|
self.emit_instruction(
|
2024-10-19 16:05:20 +00:00
|
|
|
Instruction::set_local(register, local_index),
|
2024-09-19 03:02:28 +00:00
|
|
|
start_position,
|
|
|
|
);
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = false;
|
2024-09-12 17:03:24 +00:00
|
|
|
} else {
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = self.next_register();
|
|
|
|
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 16:05:20 +00:00
|
|
|
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-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-09-07 21:16:14 +00:00
|
|
|
}
|
2024-09-07 17:51:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_type_from(&mut self, token: Token, position: Span) -> Result<Type, CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
match token {
|
|
|
|
Token::Bool => Ok(Type::Boolean),
|
|
|
|
Token::FloatKeyword => Ok(Type::Float),
|
|
|
|
Token::Int => Ok(Type::Integer),
|
|
|
|
Token::Str => Ok(Type::String { length: None }),
|
2024-11-06 20:40:37 +00:00
|
|
|
_ => Err(CompileError::ExpectedTokenMultiple {
|
2024-10-09 10:15:06 +00:00
|
|
|
expected: &[
|
|
|
|
TokenKind::Bool,
|
|
|
|
TokenKind::FloatKeyword,
|
|
|
|
TokenKind::Int,
|
|
|
|
TokenKind::Str,
|
|
|
|
],
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position,
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_block(&mut self) -> Result<(), CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
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-11-06 20:50:05 +00:00
|
|
|
self.parse(Precedence::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-11-06 20:50:05 +00:00
|
|
|
fn parse_list(&mut self) -> Result<(), CompileError> {
|
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-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 {
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 16:05:20 +00:00
|
|
|
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();
|
2024-09-17 23:35:33 +00:00
|
|
|
let end = self.current_position.1;
|
|
|
|
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-11-06 08:36:46 +00:00
|
|
|
Instruction::load_list(to_register, start_register),
|
2024-09-17 23:35:33 +00:00
|
|
|
Span(start, end),
|
|
|
|
);
|
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-09-17 23:35:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_if(&mut self) -> Result<(), CompileError> {
|
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-11-06 03:07:54 +00:00
|
|
|
if matches!(
|
2024-11-06 03:10:13 +00:00
|
|
|
self.get_last_instructions(),
|
2024-11-06 03:07:54 +00:00
|
|
|
Some([
|
|
|
|
Operation::LoadBoolean,
|
|
|
|
Operation::LoadBoolean,
|
|
|
|
Operation::Jump,
|
|
|
|
Operation::Equal | Operation::Less | Operation::LessEqual
|
2024-11-06 19:46:23 +00:00
|
|
|
])
|
2024-11-06 03:07:54 +00:00
|
|
|
) {
|
2024-10-25 02:37:18 +00:00
|
|
|
self.chunk.instructions_mut().pop();
|
|
|
|
self.chunk.instructions_mut().pop();
|
2024-11-06 19:46:23 +00:00
|
|
|
self.chunk.instructions_mut().pop();
|
2024-10-19 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
let if_block_start = self.chunk.len();
|
|
|
|
let if_block_start_position = self.current_position;
|
2024-10-12 14:55:34 +00:00
|
|
|
|
2024-09-24 12:29:33 +00:00
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
2024-11-06 20:50:05 +00:00
|
|
|
self.parse_block()?;
|
2024-10-20 22:51:04 +00:00
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedToken {
|
2024-10-20 22:51:04 +00:00
|
|
|
expected: TokenKind::LeftCurlyBrace,
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
2024-09-24 04:24:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
let if_block_end = self.chunk.len();
|
|
|
|
let mut if_block_distance = (if_block_end - if_block_start) as u8;
|
|
|
|
|
2024-10-30 18:48:30 +00:00
|
|
|
let if_block_is_expression = self
|
|
|
|
.chunk
|
|
|
|
.instructions()
|
|
|
|
.iter()
|
2024-11-03 01:24:41 +00:00
|
|
|
.rev()
|
2024-10-30 18:48:30 +00:00
|
|
|
.find_map(|(instruction, _)| {
|
2024-11-03 01:24:41 +00:00
|
|
|
if !matches!(instruction.operation(), Operation::Jump | Operation::Move) {
|
|
|
|
Some(instruction.yields_value())
|
2024-10-30 18:48:30 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(false);
|
2024-09-25 13:55:10 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
let if_last_register = self.next_register().saturating_sub(1);
|
2024-11-06 18:09:29 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
if let Token::Else = self.current_token {
|
|
|
|
self.advance()?;
|
2024-11-06 18:09:29 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
if let Token::LeftCurlyBrace = self.current_token {
|
2024-11-06 20:50:05 +00:00
|
|
|
self.parse_block()?;
|
2024-11-06 19:46:23 +00:00
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedTokenMultiple {
|
2024-11-06 19:46:23 +00:00
|
|
|
expected: &[TokenKind::If, TokenKind::LeftCurlyBrace],
|
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
2024-11-06 18:09:29 +00:00
|
|
|
}
|
2024-11-06 19:46:23 +00:00
|
|
|
} else {
|
|
|
|
self.previous_is_expression = false;
|
|
|
|
}
|
2024-10-30 18:48:30 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
self.previous_is_expression = if_block_is_expression && self.previous_is_expression;
|
2024-11-01 06:51:18 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
let else_block_end = self.chunk.len();
|
|
|
|
let else_block_distance = (else_block_end - if_block_end) as u8;
|
2024-10-30 18:48:30 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
match else_block_distance {
|
|
|
|
0 => {}
|
|
|
|
1 => {
|
2024-10-30 18:48:30 +00:00
|
|
|
if let Some(skippable) = self.get_last_jumpable_mut() {
|
|
|
|
skippable.set_c_to_boolean(true);
|
|
|
|
} else {
|
2024-11-06 19:46:23 +00:00
|
|
|
if_block_distance += 1;
|
|
|
|
|
2024-11-04 20:38:58 +00:00
|
|
|
self.chunk.instructions_mut().insert(
|
2024-11-06 19:46:23 +00:00
|
|
|
if_block_end,
|
2024-11-04 20:38:58 +00:00
|
|
|
(
|
2024-11-06 19:46:23 +00:00
|
|
|
Instruction::jump(else_block_distance, true),
|
2024-11-04 20:38:58 +00:00
|
|
|
self.current_position,
|
|
|
|
),
|
|
|
|
);
|
2024-10-30 18:48:30 +00:00
|
|
|
}
|
2024-11-06 19:46:23 +00:00
|
|
|
}
|
|
|
|
2.. => {
|
|
|
|
if_block_distance += 1;
|
|
|
|
|
2024-11-04 20:38:58 +00:00
|
|
|
self.chunk.instructions_mut().insert(
|
2024-11-06 19:46:23 +00:00
|
|
|
if_block_end,
|
2024-11-04 20:38:58 +00:00
|
|
|
(
|
2024-11-06 19:46:23 +00:00
|
|
|
Instruction::jump(else_block_distance, true),
|
2024-11-04 20:38:58 +00:00
|
|
|
self.current_position,
|
|
|
|
),
|
|
|
|
);
|
2024-10-30 18:48:30 +00:00
|
|
|
}
|
2024-10-25 02:37:18 +00:00
|
|
|
}
|
2024-10-21 02:18:58 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
self.chunk.instructions_mut().insert(
|
|
|
|
if_block_start,
|
|
|
|
(
|
|
|
|
Instruction::jump(if_block_distance, true),
|
|
|
|
if_block_start_position,
|
|
|
|
),
|
|
|
|
);
|
2024-10-12 07:06:44 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
if self.chunk.len() >= 4 {
|
|
|
|
let possible_comparison_statement = {
|
|
|
|
let start = self.chunk.len() - 4;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
&mut self.chunk.instructions_mut()[start..]
|
|
|
|
};
|
2024-10-25 02:37:18 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
self.optimization_count += optimize(possible_comparison_statement);
|
2024-11-06 19:46:23 +00:00
|
|
|
}
|
2024-10-25 02:37:18 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
let else_last_register = self.next_register().saturating_sub(1);
|
2024-10-30 18:48:30 +00:00
|
|
|
|
2024-11-06 19:46:23 +00:00
|
|
|
if if_last_register < else_last_register {
|
|
|
|
self.emit_instruction(
|
|
|
|
Instruction::r#move(else_last_register, if_last_register),
|
|
|
|
self.current_position,
|
|
|
|
);
|
2024-09-22 22:17:21 +00:00
|
|
|
}
|
2024-09-19 17:54:28 +00:00
|
|
|
|
2024-09-18 20:43:34 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_while(&mut self) -> Result<(), CompileError> {
|
2024-09-19 03:02:28 +00:00
|
|
|
self.advance()?;
|
2024-10-20 14:20:09 +00:00
|
|
|
|
2024-11-01 00:33:46 +00:00
|
|
|
let expression_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()?;
|
2024-10-19 17:39:50 +00:00
|
|
|
|
2024-11-06 03:07:54 +00:00
|
|
|
if matches!(
|
2024-11-06 03:10:13 +00:00
|
|
|
self.get_last_instructions(),
|
2024-11-06 03:07:54 +00:00
|
|
|
Some([
|
|
|
|
Operation::LoadBoolean,
|
|
|
|
Operation::LoadBoolean,
|
|
|
|
Operation::Jump,
|
|
|
|
Operation::Equal | Operation::Less | Operation::LessEqual
|
|
|
|
],)
|
|
|
|
) {
|
2024-10-25 02:37:18 +00:00
|
|
|
self.chunk.instructions_mut().pop();
|
|
|
|
self.chunk.instructions_mut().pop();
|
2024-11-01 00:33:46 +00:00
|
|
|
self.chunk.instructions_mut().pop();
|
2024-10-19 17:39:50 +00:00
|
|
|
}
|
|
|
|
|
2024-11-01 00:33:46 +00:00
|
|
|
let block_start = self.chunk.len();
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
self.parse_block()?;
|
2024-09-19 03:02:28 +00:00
|
|
|
|
2024-11-01 00:33:46 +00:00
|
|
|
let block_end = self.chunk.len() as u8;
|
|
|
|
|
2024-11-04 20:38:58 +00:00
|
|
|
self.chunk.instructions_mut().insert(
|
2024-11-01 00:33:46 +00:00
|
|
|
block_start,
|
2024-11-04 20:38:58 +00:00
|
|
|
(
|
|
|
|
Instruction::jump(block_end - block_start as u8 + 1, true),
|
|
|
|
self.current_position,
|
|
|
|
),
|
|
|
|
);
|
2024-10-20 14:20:09 +00:00
|
|
|
|
2024-11-01 00:33:46 +00:00
|
|
|
let jump_back_distance = block_end - expression_start + 1;
|
|
|
|
let jump_back = Instruction::jump(jump_back_distance, false);
|
2024-09-25 13:55:10 +00:00
|
|
|
|
2024-10-20 04:06:22 +00:00
|
|
|
self.emit_instruction(jump_back, self.current_position);
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = false;
|
2024-10-20 04:06:22 +00:00
|
|
|
|
2024-09-19 03:02:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_native_call(&mut self, function: NativeFunction) -> Result<(), CompileError> {
|
2024-11-06 08:36:46 +00:00
|
|
|
let start = self.previous_position.0;
|
2024-10-30 07:08:25 +00:00
|
|
|
let start_register = self.next_register();
|
|
|
|
|
2024-10-30 13:11:28 +00:00
|
|
|
self.expect(Token::LeftParenthesis)?;
|
2024-10-30 07:08:25 +00:00
|
|
|
|
2024-10-30 13:11:28 +00:00
|
|
|
while !self.allow(Token::RightParenthesis)? {
|
|
|
|
let expected_register = self.next_register();
|
2024-10-30 07:08:25 +00:00
|
|
|
|
2024-10-30 13:11:28 +00:00
|
|
|
self.parse_expression()?;
|
2024-10-30 07:08:25 +00:00
|
|
|
|
2024-10-30 13:11:28 +00:00
|
|
|
let actual_register = self.next_register() - 1;
|
2024-10-30 07:08:25 +00:00
|
|
|
|
2024-10-30 13:11:28 +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
|
|
|
}
|
2024-10-30 13:11:28 +00:00
|
|
|
|
|
|
|
self.allow(Token::Comma)?;
|
2024-10-30 07:08:25 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 13:50:45 +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;
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = function.r#type().return_type.is_some();
|
2024-10-30 07:08:25 +00:00
|
|
|
|
|
|
|
self.emit_instruction(
|
2024-11-06 08:36:46 +00:00
|
|
|
Instruction::call_native(to_register, function, argument_count),
|
2024-10-30 07:08:25 +00:00
|
|
|
Span(start, end),
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_top_level(&mut self) -> Result<(), CompileError> {
|
2024-11-03 01:24:41 +00:00
|
|
|
loop {
|
2024-11-06 20:50:05 +00:00
|
|
|
self.parse(Precedence::None)?;
|
2024-11-03 01:24:41 +00:00
|
|
|
|
|
|
|
if self.is_eof() || self.allow(Token::RightCurlyBrace)? {
|
|
|
|
self.parse_implicit_return()?;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_expression(&mut self) -> Result<(), CompileError> {
|
2024-11-06 20:50:05 +00:00
|
|
|
self.parse(Precedence::None)?;
|
2024-11-05 21:25:55 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
if !self.previous_is_expression || self.chunk.is_empty() {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedExpression {
|
2024-11-05 21:25:55 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2024-10-12 14:55:34 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_sub_expression(&mut self, precedence: &Precedence) -> Result<(), CompileError> {
|
2024-11-06 20:50:05 +00:00
|
|
|
self.parse(precedence.increment())
|
2024-10-13 11:14:12 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_return_statement(&mut self) -> Result<(), CompileError> {
|
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 23:13:19 +00:00
|
|
|
self.emit_instruction(Instruction::r#return(has_return_value), Span(start, end));
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = false;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_implicit_return(&mut self) -> Result<(), CompileError> {
|
2024-11-06 19:46:23 +00:00
|
|
|
if self.allow(Token::Semicolon)? {
|
2024-11-01 13:55:15 +00:00
|
|
|
self.emit_instruction(Instruction::r#return(false), self.current_position);
|
2024-11-03 01:24:41 +00:00
|
|
|
} else {
|
|
|
|
self.emit_instruction(
|
2024-11-06 18:09:29 +00:00
|
|
|
Instruction::r#return(self.previous_is_expression),
|
2024-11-03 01:24:41 +00:00
|
|
|
self.current_position,
|
|
|
|
);
|
2024-10-20 22:51:04 +00:00
|
|
|
}
|
2024-09-19 22:07:11 +00:00
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_let_statement(&mut self) -> Result<(), CompileError> {
|
2024-10-09 16:16:46 +00:00
|
|
|
self.advance()?;
|
2024-09-17 21:23:37 +00:00
|
|
|
|
2024-11-05 17:44:16 +00:00
|
|
|
let scope = self.chunk.current_scope();
|
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-11-04 20:38:58 +00:00
|
|
|
text
|
2024-09-09 23:23:49 +00:00
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedToken {
|
2024-09-09 23:23:49 +00:00
|
|
|
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-19 17:39:50 +00:00
|
|
|
self.parse_expression()?;
|
2024-10-09 10:15:06 +00:00
|
|
|
|
2024-11-05 17:44:16 +00:00
|
|
|
let (local_index, _) = self.declare_local(identifier, r#type, is_mutable, scope, register);
|
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 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 16:05:20 +00:00
|
|
|
Instruction::define_local(register, local_index, is_mutable),
|
2024-10-09 16:16:46 +00:00
|
|
|
position,
|
|
|
|
);
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = false;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_function(&mut self) -> Result<(), CompileError> {
|
2024-10-09 10:15:06 +00:00
|
|
|
let function_start = self.current_position.0;
|
2024-11-06 20:40:37 +00:00
|
|
|
let mut function_compiler = Compiler::new(self.lexer)?;
|
|
|
|
let identifier = if let Token::Identifier(text) = function_compiler.current_token {
|
|
|
|
let position = function_compiler.current_position;
|
2024-10-19 23:13:19 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.advance()?;
|
|
|
|
function_compiler.chunk.set_name(text.to_string());
|
2024-10-19 23:13:19 +00:00
|
|
|
|
2024-11-04 20:38:58 +00:00
|
|
|
Some((text, position))
|
2024-10-19 23:13:19 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.expect(Token::LeftParenthesis)?;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-11-04 20:38:58 +00:00
|
|
|
let mut value_parameters: Option<Vec<(u8, Type)>> = None;
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
while function_compiler.current_token != Token::RightParenthesis {
|
|
|
|
let is_mutable = function_compiler.allow(Token::Mut)?;
|
|
|
|
let parameter = if let Token::Identifier(text) = function_compiler.current_token {
|
|
|
|
function_compiler.advance()?;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-11-04 20:38:58 +00:00
|
|
|
text
|
2024-10-09 10:15:06 +00:00
|
|
|
} else {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedToken {
|
2024-10-09 10:15:06 +00:00
|
|
|
expected: TokenKind::Identifier,
|
2024-11-06 20:40:37 +00:00
|
|
|
found: function_compiler.current_token.to_owned(),
|
|
|
|
position: function_compiler.current_position,
|
2024-10-09 10:15:06 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.expect(Token::Colon)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
let r#type = function_compiler.parse_type_from(
|
|
|
|
function_compiler.current_token,
|
|
|
|
function_compiler.current_position,
|
2024-10-09 10:15:06 +00:00
|
|
|
)?;
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.advance()?;
|
2024-10-09 10:15:06 +00:00
|
|
|
|
2024-10-19 16:05:20 +00:00
|
|
|
let register = value_parameters
|
|
|
|
.as_ref()
|
2024-11-04 23:07:58 +00:00
|
|
|
.map(|values| values.len() as u8)
|
2024-10-19 16:05:20 +00:00
|
|
|
.unwrap_or(0);
|
2024-11-06 20:40:37 +00:00
|
|
|
let scope = function_compiler.chunk.current_scope();
|
|
|
|
let (_, identifier_index) = function_compiler.declare_local(
|
2024-10-09 10:15:06 +00:00
|
|
|
parameter,
|
2024-11-04 20:38:58 +00:00
|
|
|
Some(r#type.clone()),
|
2024-10-09 10:15:06 +00:00
|
|
|
is_mutable,
|
2024-11-05 17:44:16 +00:00
|
|
|
scope,
|
2024-10-19 16:05:20 +00:00
|
|
|
register,
|
2024-11-04 20:38:58 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if let Some(value_parameters) = value_parameters.as_mut() {
|
|
|
|
value_parameters.push((identifier_index, r#type));
|
|
|
|
} else {
|
|
|
|
value_parameters = Some(vec![(identifier_index, r#type)]);
|
|
|
|
};
|
2024-10-09 10:15:06 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.minimum_register += 1;
|
2024-10-19 16:05:20 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.allow(Token::Comma)?;
|
2024-10-09 10:15:06 +00:00
|
|
|
}
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.advance()?;
|
2024-10-19 05:58:30 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
let return_type = if function_compiler.allow(Token::ArrowThin)? {
|
|
|
|
let r#type = function_compiler.parse_type_from(
|
|
|
|
function_compiler.current_token,
|
|
|
|
function_compiler.current_position,
|
2024-10-19 05:58:30 +00:00
|
|
|
)?;
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.advance()?;
|
2024-10-19 05:58:30 +00:00
|
|
|
|
|
|
|
Some(Box::new(r#type))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
function_compiler.expect(Token::LeftCurlyBrace)?;
|
|
|
|
function_compiler.parse_top_level()?;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
self.previous_token = function_compiler.previous_token;
|
|
|
|
self.previous_position = function_compiler.previous_position;
|
|
|
|
self.current_token = function_compiler.current_token;
|
|
|
|
self.current_position = function_compiler.current_position;
|
2024-10-09 02:56:01 +00:00
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
let function_type = FunctionType {
|
|
|
|
type_parameters: None,
|
|
|
|
value_parameters,
|
|
|
|
return_type,
|
|
|
|
};
|
2024-11-06 20:40:37 +00:00
|
|
|
let function = Value::function(function_compiler.finish(), function_type.clone());
|
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);
|
2024-10-19 23:13:19 +00:00
|
|
|
|
|
|
|
if let Some((identifier, identifier_position)) = identifier {
|
|
|
|
let register = self.next_register();
|
2024-11-05 17:44:16 +00:00
|
|
|
let scope = self.chunk.current_scope();
|
2024-11-04 20:38:58 +00:00
|
|
|
let (local_index, _) = self.declare_local(
|
2024-10-19 23:13:19 +00:00
|
|
|
identifier,
|
|
|
|
Some(Type::Function(function_type)),
|
|
|
|
false,
|
2024-11-05 17:44:16 +00:00
|
|
|
scope,
|
2024-10-19 23:13:19 +00:00
|
|
|
register,
|
2024-11-04 20:38:58 +00:00
|
|
|
);
|
2024-10-19 23:13:19 +00:00
|
|
|
|
|
|
|
self.emit_constant(function, Span(function_start, function_end))?;
|
|
|
|
self.emit_instruction(
|
|
|
|
Instruction::define_local(register, local_index, false),
|
|
|
|
identifier_position,
|
|
|
|
);
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = false;
|
2024-10-19 23:13:19 +00:00
|
|
|
} else {
|
|
|
|
self.emit_constant(function, Span(function_start, function_end))?;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-19 23:13:19 +00:00
|
|
|
}
|
2024-10-09 02:56:01 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
fn parse_call(&mut self) -> Result<(), CompileError> {
|
2024-11-05 21:25:55 +00:00
|
|
|
let (last_instruction, _) =
|
|
|
|
self.chunk
|
|
|
|
.instructions()
|
|
|
|
.last()
|
2024-11-06 20:40:37 +00:00
|
|
|
.ok_or_else(|| CompileError::ExpectedExpression {
|
2024-11-05 21:25:55 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::ExpectedExpression {
|
2024-10-13 00:19:21 +00:00
|
|
|
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-13 00:19:21 +00:00
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
self.advance()?;
|
|
|
|
|
|
|
|
while !self.allow(Token::RightParenthesis)? {
|
2024-10-20 04:06:22 +00:00
|
|
|
let expected_register = self.next_register();
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
self.parse_expression()?;
|
2024-10-20 04:06:22 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-10-13 00:19:21 +00:00
|
|
|
let end = self.current_position.1;
|
2024-10-19 21:24:22 +00:00
|
|
|
let to_register = self.next_register();
|
2024-10-19 23:13:19 +00:00
|
|
|
let argument_count = to_register - function_register - 1;
|
2024-10-13 00:19:21 +00:00
|
|
|
|
2024-10-19 23:13:19 +00:00
|
|
|
self.emit_instruction(
|
2024-10-19 21:24:22 +00:00
|
|
|
Instruction::call(to_register, function_register, argument_count),
|
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-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = true;
|
2024-10-20 22:51:04 +00:00
|
|
|
|
2024-10-12 14:55:34 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse_semicolon(&mut self) -> Result<(), CompileError> {
|
2024-11-05 21:25:55 +00:00
|
|
|
self.advance()?;
|
|
|
|
|
2024-11-06 18:09:29 +00:00
|
|
|
self.previous_is_expression = false;
|
2024-11-05 21:25:55 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn expect_expression(&mut self) -> Result<(), CompileError> {
|
2024-11-06 20:40:37 +00:00
|
|
|
Err(CompileError::ExpectedExpression {
|
2024-11-05 21:25:55 +00:00
|
|
|
found: self.current_token.to_owned(),
|
|
|
|
position: self.current_position,
|
|
|
|
})
|
2024-10-13 17:01:58 +00:00
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
fn parse(&mut self, precedence: Precedence) -> Result<(), CompileError> {
|
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-11-06 20:50:05 +00:00
|
|
|
prefix_parser(self)?;
|
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-11-06 20:50:05 +00:00
|
|
|
if self.current_token == Token::Equal {
|
2024-11-06 20:40:37 +00:00
|
|
|
return Err(CompileError::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-11-06 20:50:05 +00:00
|
|
|
/// Operator precedence levels.
|
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-11-06 20:50:05 +00:00
|
|
|
type Parser<'a> = fn(&mut Compiler<'a>) -> Result<(), CompileError>;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
/// Rule that defines how to parse a token.
|
2024-09-07 08:34:03 +00:00
|
|
|
#[derive(Debug, Clone, Copy)]
|
2024-10-09 16:16:46 +00:00
|
|
|
struct ParseRule<'a> {
|
2024-11-06 20:50:05 +00:00
|
|
|
pub prefix: Option<Parser<'a>>,
|
|
|
|
pub infix: Option<Parser<'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 {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-19 05:58:30 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Async => todo!(),
|
|
|
|
Token::Bang => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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 {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-13 17:01:58 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Boolean(_) => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_boolean),
|
2024-09-07 10:38:12 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Break => todo!(),
|
|
|
|
Token::Byte(_) => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_byte),
|
2024-09-10 03:45:06 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Character(_) => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_character),
|
2024-09-10 03:45:06 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Colon => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-13 17:01:58 +00:00
|
|
|
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 {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-13 17:01:58 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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 {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-13 17:01:58 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_float),
|
2024-09-22 22:17:21 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::FloatKeyword => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-13 17:01:58 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Fn => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_function),
|
2024-10-09 02:56:01 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_variable),
|
2024-09-22 22:17:21 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::If => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_if),
|
2024-09-22 22:17:21 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Int => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-13 17:01:58 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Integer(_) => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_integer),
|
2024-09-22 22:17:21 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::LeftCurlyBrace => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_block),
|
2024-09-10 14:44:15 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::LeftParenthesis => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_grouped),
|
|
|
|
infix: Some(Compiler::parse_call),
|
2024-10-12 14:55:34 +00:00
|
|
|
precedence: Precedence::Call,
|
2024-09-07 08:34:03 +00:00
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::LeftSquareBrace => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_list),
|
2024-09-17 23:35:33 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_let_statement),
|
2024-09-22 22:17:21 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_unary),
|
|
|
|
infix: Some(Compiler::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,
|
2024-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::parse_math_binary),
|
2024-10-05 08:33:38 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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,
|
2024-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::parse_math_binary),
|
2024-10-12 10:27:19 +00:00
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
|
|
|
Token::Plus => ParseRule {
|
2024-09-07 08:34:03 +00:00
|
|
|
prefix: None,
|
2024-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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,
|
2024-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::parse_math_binary),
|
2024-10-05 08:33:38 +00:00
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Return => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_return_statement),
|
2024-10-09 16:16:46 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_semicolon),
|
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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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,
|
2024-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::parse_math_binary),
|
2024-10-05 08:33:38 +00:00
|
|
|
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-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::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,
|
2024-11-06 20:40:37 +00:00
|
|
|
infix: Some(Compiler::parse_math_binary),
|
2024-10-05 08:33:38 +00:00
|
|
|
precedence: Precedence::Assignment,
|
|
|
|
},
|
2024-10-13 17:01:58 +00:00
|
|
|
Token::Str => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::expect_expression),
|
2024-10-13 17:01:58 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::String(_) => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_string),
|
2024-09-22 22:17:21 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-10-12 10:27:19 +00:00
|
|
|
Token::Struct => todo!(),
|
|
|
|
Token::While => ParseRule {
|
2024-11-06 20:40:37 +00:00
|
|
|
prefix: Some(Compiler::parse_while),
|
2024-09-22 22:17:21 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
/// Compilation errors
|
2024-11-06 18:50:49 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
2024-11-06 20:40:37 +00:00
|
|
|
pub enum CompileError {
|
2024-11-04 20:38:58 +00:00
|
|
|
// Token errors
|
|
|
|
ExpectedToken {
|
|
|
|
expected: TokenKind,
|
|
|
|
found: TokenOwned,
|
2024-10-12 12:16:06 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-04 20:38:58 +00:00
|
|
|
ExpectedTokenMultiple {
|
|
|
|
expected: &'static [TokenKind],
|
|
|
|
found: TokenOwned,
|
2024-09-19 00:02:12 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-04 20:38:58 +00:00
|
|
|
|
2024-11-06 20:50:05 +00:00
|
|
|
// Parsing errors
|
2024-11-04 20:38:58 +00:00
|
|
|
CannotChainComparison {
|
2024-09-07 08:34:03 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-04 20:38:58 +00:00
|
|
|
ExpectedExpression {
|
2024-09-07 03:30:43 +00:00
|
|
|
found: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-06 20:50:05 +00:00
|
|
|
InvalidAssignmentTarget {
|
|
|
|
found: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
|
|
|
UnexpectedReturn {
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-04 20:38:58 +00:00
|
|
|
|
|
|
|
// Variable errors
|
|
|
|
CannotMutateImmutableVariable {
|
|
|
|
identifier: String,
|
2024-09-07 03:30:43 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
2024-10-05 08:33:38 +00:00
|
|
|
ExpectedMutableVariable {
|
|
|
|
found: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-04 20:38:58 +00:00
|
|
|
UndeclaredVariable {
|
|
|
|
identifier: String,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-05 21:07:51 +00:00
|
|
|
VariableOutOfScope {
|
|
|
|
identifier: String,
|
|
|
|
variable_scope: Scope,
|
|
|
|
access_scope: Scope,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-11-04 20:38:58 +00:00
|
|
|
|
2024-11-06 18:50:49 +00:00
|
|
|
// Wrappers around foreign errors
|
|
|
|
Chunk {
|
|
|
|
error: ChunkError,
|
2024-09-12 13:11:49 +00:00
|
|
|
position: Span,
|
|
|
|
},
|
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-11-06 20:40:37 +00:00
|
|
|
impl AnnotatedError for CompileError {
|
2024-09-10 13:26:05 +00:00
|
|
|
fn title() -> &'static str {
|
2024-11-06 20:40:37 +00:00
|
|
|
"Compilation Error"
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
|
|
|
|
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-11-06 18:50:49 +00:00
|
|
|
Self::Chunk { .. } => "Chunk error",
|
2024-09-10 13:26:05 +00:00
|
|
|
Self::ExpectedExpression { .. } => "Expected an expression",
|
2024-11-04 20:38:58 +00:00
|
|
|
Self::ExpectedMutableVariable { .. } => "Expected a mutable variable",
|
2024-09-10 13:26:05 +00:00
|
|
|
Self::ExpectedToken { .. } => "Expected a specific token",
|
|
|
|
Self::ExpectedTokenMultiple { .. } => "Expected one of multiple tokens",
|
|
|
|
Self::InvalidAssignmentTarget { .. } => "Invalid assignment target",
|
2024-11-04 20:38:58 +00:00
|
|
|
Self::Lex(error) => error.description(),
|
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-11-04 20:38:58 +00:00
|
|
|
Self::UndeclaredVariable { .. } => "Undeclared variable",
|
|
|
|
Self::UnexpectedReturn { .. } => "Unexpected return",
|
2024-11-05 21:07:51 +00:00
|
|
|
Self::VariableOutOfScope { .. } => "Variable out of scope",
|
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-11-06 18:50:49 +00:00
|
|
|
Self::Chunk { error, .. } => Some(error.to_string()),
|
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-11-04 20:38:58 +00:00
|
|
|
Self::Lex(error) => error.details(),
|
2024-11-06 18:50:49 +00:00
|
|
|
|
2024-11-04 20:38:58 +00:00
|
|
|
Self::ParseFloatError { error, .. } => Some(error.to_string()),
|
|
|
|
Self::ParseIntError { error, .. } => Some(error.to_string()),
|
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-11-05 21:07:51 +00:00
|
|
|
Self::VariableOutOfScope { identifier, .. } => {
|
|
|
|
Some(format!("Variable {identifier} is out of scope"))
|
|
|
|
}
|
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-11-06 18:50:49 +00:00
|
|
|
Self::Chunk { position, .. } => *position,
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::ExpectedExpression { position, .. } => *position,
|
2024-11-04 20:38:58 +00:00
|
|
|
Self::ExpectedMutableVariable { position, .. } => *position,
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::ExpectedToken { position, .. } => *position,
|
|
|
|
Self::ExpectedTokenMultiple { position, .. } => *position,
|
|
|
|
Self::InvalidAssignmentTarget { position, .. } => *position,
|
2024-11-04 20:38:58 +00:00
|
|
|
Self::Lex(error) => error.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-11-04 20:38:58 +00:00
|
|
|
Self::UndeclaredVariable { position, .. } => *position,
|
|
|
|
Self::UnexpectedReturn { position } => *position,
|
2024-11-05 21:07:51 +00:00
|
|
|
Self::VariableOutOfScope { position, .. } => *position,
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-06 20:40:37 +00:00
|
|
|
impl From<LexError> for CompileError {
|
2024-09-07 03:30:43 +00:00
|
|
|
fn from(error: LexError) -> Self {
|
|
|
|
Self::Lex(error)
|
|
|
|
}
|
|
|
|
}
|