2024-09-07 08:34:03 +00:00
|
|
|
use std::{
|
|
|
|
fmt::{self, Display, Formatter},
|
2024-09-07 16:15:47 +00:00
|
|
|
mem,
|
2024-09-07 08:34:03 +00:00
|
|
|
num::ParseIntError,
|
|
|
|
};
|
2024-09-07 03:30:43 +00:00
|
|
|
|
|
|
|
use crate::{
|
2024-09-10 13:26:05 +00:00
|
|
|
dust_error::AnnotatedError, Chunk, ChunkError, DustError, Identifier, Instruction, LexError,
|
2024-09-11 07:10:12 +00:00
|
|
|
Lexer, Span, Token, TokenKind, TokenOwned, Value,
|
2024-09-07 03:30:43 +00:00
|
|
|
};
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
pub fn parse(source: &str) -> Result<Chunk, DustError> {
|
2024-09-07 08:34:03 +00:00
|
|
|
let lexer = Lexer::new(source);
|
|
|
|
let mut parser = Parser::new(lexer);
|
|
|
|
|
|
|
|
while !parser.is_eof() {
|
2024-09-07 16:15:47 +00:00
|
|
|
parser
|
|
|
|
.parse_statement()
|
|
|
|
.map_err(|error| DustError::Parse { error, source })?;
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(parser.chunk)
|
|
|
|
}
|
|
|
|
|
2024-09-07 03:30:43 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Parser<'src> {
|
|
|
|
lexer: Lexer<'src>,
|
2024-09-07 08:34:03 +00:00
|
|
|
chunk: Chunk,
|
2024-09-07 10:38:12 +00:00
|
|
|
previous_token: Token<'src>,
|
|
|
|
previous_position: Span,
|
|
|
|
current_token: Token<'src>,
|
2024-09-07 03:30:43 +00:00
|
|
|
current_position: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Parser<'src> {
|
2024-09-07 10:38:12 +00:00
|
|
|
pub fn new(mut lexer: Lexer<'src>) -> Self {
|
|
|
|
let (current_token, current_position) =
|
|
|
|
lexer.next_token().unwrap_or((Token::Eof, Span(0, 0)));
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
log::trace!("Starting parser with token {current_token} at {current_position}");
|
|
|
|
|
2024-09-07 03:30:43 +00:00
|
|
|
Parser {
|
|
|
|
lexer,
|
2024-09-07 08:34:03 +00:00
|
|
|
chunk: Chunk::new(),
|
2024-09-07 10:38:12 +00:00
|
|
|
previous_token: Token::Eof,
|
|
|
|
previous_position: Span(0, 0),
|
|
|
|
current_token,
|
|
|
|
current_position,
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_eof(&self) -> bool {
|
2024-09-07 10:38:12 +00:00
|
|
|
matches!(self.current_token, Token::Eof)
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn 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-07 10:38:12 +00:00
|
|
|
log::trace!("Advancing to token {new_token} at {position}");
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
self.previous_token = mem::replace(&mut self.current_token, new_token);
|
|
|
|
self.previous_position = mem::replace(&mut self.current_position, position);
|
2024-09-07 03:30:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
fn allow(&mut self, allowed: TokenKind) -> Result<bool, ParseError> {
|
|
|
|
if self.current_token.kind() == allowed {
|
|
|
|
self.advance()?;
|
|
|
|
|
|
|
|
Ok(true)
|
|
|
|
} else {
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expect(&mut self, expected: TokenKind) -> Result<(), ParseError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
if self.current_token.kind() == expected {
|
2024-09-07 03:30:43 +00:00
|
|
|
self.advance()
|
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected,
|
2024-09-07 10:38:12 +00:00
|
|
|
found: self.current_token.to_owned(),
|
2024-09-07 03:30:43 +00:00
|
|
|
position: self.current_position,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
fn emit_byte<T: Into<u8>>(&mut self, into_byte: T, position: Span) {
|
|
|
|
self.chunk.push_code(into_byte.into(), position);
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
fn emit_constant(&mut self, value: Value) -> Result<(), ParseError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
let position = self.previous_position;
|
2024-09-11 07:10:12 +00:00
|
|
|
let constant_index = self.chunk.push_constant(value, position)?;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
self.emit_byte(Instruction::Constant, position);
|
2024-09-07 08:37:38 +00:00
|
|
|
self.emit_byte(constant_index, position);
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-07 03:30:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
fn parse_boolean(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
if let Token::Boolean(text) = self.previous_token {
|
|
|
|
let boolean = text.parse::<bool>().unwrap();
|
|
|
|
let value = Value::boolean(boolean);
|
|
|
|
|
|
|
|
self.emit_constant(value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-10 03:45:06 +00:00
|
|
|
fn parse_byte(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
|
|
|
if let Token::Byte(text) = self.previous_token {
|
2024-09-10 07:42:25 +00:00
|
|
|
let byte =
|
|
|
|
u8::from_str_radix(&text[2..], 16).map_err(|error| ParseError::ParseIntError {
|
|
|
|
error,
|
|
|
|
position: self.previous_position,
|
|
|
|
})?;
|
2024-09-10 03:45:06 +00:00
|
|
|
let value = Value::byte(byte);
|
|
|
|
|
|
|
|
self.emit_constant(value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_character(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
|
|
|
if let Token::Character(character) = self.previous_token {
|
|
|
|
let value = Value::character(character);
|
|
|
|
|
|
|
|
self.emit_constant(value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
fn parse_float(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-07 16:15:47 +00:00
|
|
|
if let Token::Float(text) = self.previous_token {
|
|
|
|
let float = text.parse::<f64>().unwrap();
|
|
|
|
let value = Value::float(float);
|
|
|
|
|
|
|
|
self.emit_constant(value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
fn parse_integer(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
if let Token::Integer(text) = self.previous_token {
|
2024-09-07 08:34:03 +00:00
|
|
|
let integer = text.parse::<i64>().unwrap();
|
|
|
|
let value = Value::integer(integer);
|
2024-09-07 03:30:43 +00:00
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
self.emit_constant(value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
fn parse_string(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-07 16:15:47 +00:00
|
|
|
if let Token::String(text) = self.previous_token {
|
|
|
|
let value = Value::string(text);
|
|
|
|
|
|
|
|
self.emit_constant(value)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
fn parse_grouped(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-07 08:34:03 +00:00
|
|
|
self.parse_expression()?;
|
2024-09-07 16:15:47 +00:00
|
|
|
self.expect(TokenKind::RightParenthesis)
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
fn parse_unary(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-07 16:15:47 +00:00
|
|
|
let operator_position = self.previous_position;
|
2024-09-07 10:38:12 +00:00
|
|
|
let byte = match self.previous_token.kind() {
|
2024-09-10 14:44:15 +00:00
|
|
|
TokenKind::Minus => Instruction::Negate,
|
2024-09-07 10:38:12 +00:00
|
|
|
_ => {
|
|
|
|
return Err(ParseError::ExpectedTokenMultiple {
|
|
|
|
expected: vec![TokenKind::Minus],
|
|
|
|
found: self.previous_token.to_owned(),
|
2024-09-07 16:15:47 +00:00
|
|
|
position: operator_position,
|
2024-09-07 10:38:12 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2024-09-07 08:37:38 +00:00
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
self.parse_expression()?;
|
2024-09-07 16:15:47 +00:00
|
|
|
self.emit_byte(byte, operator_position);
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_binary(&mut self) -> Result<(), ParseError> {
|
2024-09-10 02:57:14 +00:00
|
|
|
log::trace!("Parsing binary expression");
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
let operator_position = self.previous_position;
|
|
|
|
let operator = self.previous_token.kind();
|
2024-09-07 08:34:03 +00:00
|
|
|
let rule = ParseRule::from(&operator);
|
|
|
|
|
|
|
|
self.parse(rule.precedence.increment())?;
|
|
|
|
|
2024-09-07 08:37:38 +00:00
|
|
|
let byte = match operator {
|
2024-09-10 14:44:15 +00:00
|
|
|
TokenKind::Plus => Instruction::Add,
|
|
|
|
TokenKind::Minus => Instruction::Subtract,
|
|
|
|
TokenKind::Star => Instruction::Multiply,
|
|
|
|
TokenKind::Slash => Instruction::Divide,
|
|
|
|
TokenKind::DoubleAmpersand => Instruction::And,
|
2024-09-07 03:30:43 +00:00
|
|
|
_ => {
|
|
|
|
return Err(ParseError::ExpectedTokenMultiple {
|
2024-09-07 08:34:03 +00:00
|
|
|
expected: vec![
|
|
|
|
TokenKind::Plus,
|
|
|
|
TokenKind::Minus,
|
|
|
|
TokenKind::Star,
|
|
|
|
TokenKind::Slash,
|
|
|
|
],
|
2024-09-07 10:38:12 +00:00
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: operator_position,
|
2024-09-07 03:30:43 +00:00
|
|
|
})
|
|
|
|
}
|
2024-09-07 08:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
self.emit_byte(byte, operator_position);
|
2024-09-07 03:30:43 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
fn parse_variable(&mut self, allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-10 22:19:59 +00:00
|
|
|
self.parse_named_variable(allow_assignment)
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
2024-09-10 22:19:59 +00:00
|
|
|
fn parse_named_variable(&mut self, allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-10 02:57:14 +00:00
|
|
|
let token = self.previous_token.to_owned();
|
2024-09-11 07:10:12 +00:00
|
|
|
let identifier_index = self.parse_identifier_from(token, self.previous_position)?;
|
2024-09-07 17:51:05 +00:00
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
if allow_assignment && self.allow(TokenKind::Equal)? {
|
|
|
|
self.parse_expression()?;
|
2024-09-10 14:44:15 +00:00
|
|
|
self.emit_byte(Instruction::SetVariable, self.previous_position);
|
2024-09-07 21:16:14 +00:00
|
|
|
self.emit_byte(identifier_index, self.previous_position);
|
|
|
|
} else {
|
2024-09-10 14:44:15 +00:00
|
|
|
self.emit_byte(Instruction::GetVariable, self.previous_position);
|
2024-09-07 21:16:14 +00:00
|
|
|
self.emit_byte(identifier_index, self.previous_position);
|
|
|
|
}
|
2024-09-07 17:51:05 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
fn parse_identifier_from(
|
|
|
|
&mut self,
|
|
|
|
token: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
) -> Result<u8, ParseError> {
|
2024-09-07 17:51:05 +00:00
|
|
|
if let TokenOwned::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-11 07:10:12 +00:00
|
|
|
if let Ok(identifier_index) = self.chunk.get_identifier_index(&identifier, position) {
|
|
|
|
Ok(identifier_index)
|
|
|
|
} else {
|
|
|
|
Err(ParseError::UndefinedVariable {
|
|
|
|
identifier,
|
|
|
|
position,
|
|
|
|
})
|
|
|
|
}
|
2024-09-07 16:15:47 +00:00
|
|
|
} else {
|
|
|
|
Err(ParseError::ExpectedToken {
|
|
|
|
expected: TokenKind::Identifier,
|
|
|
|
found: self.current_token.to_owned(),
|
2024-09-11 07:10:12 +00:00
|
|
|
position,
|
2024-09-07 16:15:47 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
pub fn parse_block(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
|
|
|
self.chunk.begin_scope();
|
|
|
|
|
|
|
|
while !self.allow(TokenKind::RightCurlyBrace)? && !self.is_eof() {
|
|
|
|
self.parse_statement()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.chunk.end_scope();
|
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
while self
|
|
|
|
.chunk
|
|
|
|
.identifiers()
|
|
|
|
.iter()
|
2024-09-11 08:22:54 +00:00
|
|
|
.next_back()
|
2024-09-11 07:10:12 +00:00
|
|
|
.map_or(false, |local| local.depth > self.chunk.scope_depth())
|
|
|
|
{
|
|
|
|
self.emit_byte(Instruction::Pop, self.current_position);
|
|
|
|
self.chunk.pop_identifier();
|
|
|
|
}
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
fn parse_expression(&mut self) -> Result<(), ParseError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
self.parse(Precedence::None)
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
fn parse_statement(&mut self) -> Result<(), ParseError> {
|
|
|
|
let start = self.current_position.0;
|
2024-09-10 14:44:15 +00:00
|
|
|
let (is_expression_statement, contains_block) = match self.current_token {
|
2024-09-07 22:48:01 +00:00
|
|
|
Token::Let => {
|
2024-09-11 07:10:12 +00:00
|
|
|
self.parse_let_statement(true)?;
|
2024-09-07 16:15:47 +00:00
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
(false, false)
|
|
|
|
}
|
|
|
|
Token::LeftCurlyBrace => {
|
|
|
|
self.parse_expression()?;
|
|
|
|
|
|
|
|
(true, true)
|
2024-09-07 22:48:01 +00:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
self.parse_expression()?;
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
(true, false)
|
2024-09-07 22:48:01 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let has_semicolon = self.allow(TokenKind::Semicolon)?;
|
2024-09-07 16:15:47 +00:00
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
if is_expression_statement && !contains_block && !has_semicolon {
|
2024-09-07 16:15:47 +00:00
|
|
|
let end = self.previous_position.1;
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
self.emit_byte(Instruction::Return, Span(start, end))
|
2024-09-07 16:15:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
fn parse_let_statement(&mut self, _allow_assignment: bool) -> Result<(), ParseError> {
|
2024-09-07 16:15:47 +00:00
|
|
|
self.expect(TokenKind::Let)?;
|
|
|
|
|
|
|
|
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(),
|
|
|
|
position: self.current_position,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
self.expect(TokenKind::Equal)?;
|
2024-09-10 22:19:59 +00:00
|
|
|
self.parse_expression()?;
|
2024-09-10 13:26:05 +00:00
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
let identifier_index = self.chunk.declare_variable(identifier, position)?;
|
2024-09-10 13:26:05 +00:00
|
|
|
|
2024-09-11 07:10:12 +00:00
|
|
|
self.emit_byte(Instruction::DeclareVariable, position);
|
2024-09-10 22:19:59 +00:00
|
|
|
self.emit_byte(identifier_index, position);
|
2024-09-07 16:15:47 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
fn parse(&mut self, precedence: Precedence) -> Result<(), ParseError> {
|
|
|
|
self.advance()?;
|
|
|
|
|
2024-09-10 05:04:30 +00:00
|
|
|
let prefix_parser =
|
|
|
|
if let Some(prefix) = ParseRule::from(&self.previous_token.kind()).prefix {
|
|
|
|
log::trace!(
|
|
|
|
"Parsing {} as prefix with precedence {precedence}",
|
|
|
|
self.previous_token,
|
|
|
|
);
|
2024-09-07 21:16:14 +00:00
|
|
|
|
2024-09-10 05:04:30 +00:00
|
|
|
prefix
|
|
|
|
} else {
|
|
|
|
return Err(ParseError::ExpectedExpression {
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
});
|
|
|
|
};
|
2024-09-07 21:16:14 +00:00
|
|
|
let allow_assignment = precedence <= Precedence::Assignment;
|
|
|
|
|
2024-09-10 05:04:30 +00:00
|
|
|
prefix_parser(self, allow_assignment)?;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
while precedence < ParseRule::from(&self.current_token.kind()).precedence {
|
2024-09-07 08:34:03 +00:00
|
|
|
self.advance()?;
|
|
|
|
|
2024-09-10 05:04:30 +00:00
|
|
|
if let Some(infix_parser) = ParseRule::from(&self.previous_token.kind()).infix {
|
2024-09-07 10:38:12 +00:00
|
|
|
log::trace!(
|
|
|
|
"Parsing {} as infix with precedence {precedence}",
|
|
|
|
self.previous_token,
|
|
|
|
);
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-11 08:22:54 +00:00
|
|
|
if allow_assignment && self.current_token == Token::Equal {
|
2024-09-07 21:16:14 +00:00
|
|
|
return Err(ParseError::InvalidAssignmentTarget {
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-10 05:04:30 +00:00
|
|
|
infix_parser(self)?;
|
2024-09-07 08:34:03 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 03:30:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub enum Precedence {
|
|
|
|
None = 0,
|
|
|
|
Assignment = 1,
|
|
|
|
Conditional = 2,
|
|
|
|
LogicalOr = 3,
|
|
|
|
LogicalAnd = 4,
|
|
|
|
Equality = 5,
|
|
|
|
Comparison = 6,
|
|
|
|
Term = 7,
|
|
|
|
Factor = 8,
|
|
|
|
Unary = 9,
|
|
|
|
Call = 10,
|
|
|
|
Primary = 11,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Precedence {
|
|
|
|
fn from_byte(byte: u8) -> Self {
|
|
|
|
match byte {
|
|
|
|
0 => Self::None,
|
|
|
|
1 => Self::Assignment,
|
|
|
|
2 => Self::Conditional,
|
|
|
|
3 => Self::LogicalOr,
|
|
|
|
4 => Self::LogicalAnd,
|
|
|
|
5 => Self::Equality,
|
|
|
|
6 => Self::Comparison,
|
|
|
|
7 => Self::Term,
|
|
|
|
8 => Self::Factor,
|
|
|
|
9 => Self::Unary,
|
|
|
|
10 => Self::Call,
|
|
|
|
_ => Self::Primary,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn increment(&self) -> Self {
|
|
|
|
Self::from_byte(*self as u8 + 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for Precedence {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
|
|
|
write!(f, "{:?}", self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 21:16:14 +00:00
|
|
|
type PrefixFunction<'a> = fn(&mut Parser<'a>, bool) -> Result<(), ParseError>;
|
|
|
|
type InfixFunction<'a> = fn(&mut Parser<'a>) -> Result<(), ParseError>;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct ParseRule<'a> {
|
2024-09-07 21:16:14 +00:00
|
|
|
pub prefix: Option<PrefixFunction<'a>>,
|
|
|
|
pub infix: Option<InfixFunction<'a>>,
|
2024-09-07 08:34:03 +00:00
|
|
|
pub precedence: Precedence,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<&TokenKind> for ParseRule<'_> {
|
|
|
|
fn from(token_kind: &TokenKind) -> Self {
|
|
|
|
match token_kind {
|
|
|
|
TokenKind::Eof => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 16:15:47 +00:00
|
|
|
TokenKind::Identifier => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_variable),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 10:38:12 +00:00
|
|
|
TokenKind::Boolean => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_boolean),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-10 03:45:06 +00:00
|
|
|
TokenKind::Byte => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_byte),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
|
|
|
TokenKind::Character => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_character),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 16:15:47 +00:00
|
|
|
TokenKind::Float => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_float),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Integer => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_integer),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 16:15:47 +00:00
|
|
|
TokenKind::String => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_string),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Async => todo!(),
|
|
|
|
TokenKind::Bool => todo!(),
|
|
|
|
TokenKind::Break => todo!(),
|
|
|
|
TokenKind::Else => todo!(),
|
|
|
|
TokenKind::FloatKeyword => todo!(),
|
|
|
|
TokenKind::If => todo!(),
|
|
|
|
TokenKind::Int => todo!(),
|
2024-09-07 22:48:01 +00:00
|
|
|
TokenKind::Let => ParseRule {
|
2024-09-11 07:10:12 +00:00
|
|
|
prefix: Some(Parser::parse_let_statement),
|
2024-09-07 22:48:01 +00:00
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Loop => todo!(),
|
|
|
|
TokenKind::Map => todo!(),
|
|
|
|
TokenKind::Str => todo!(),
|
|
|
|
TokenKind::While => todo!(),
|
|
|
|
TokenKind::BangEqual => todo!(),
|
|
|
|
TokenKind::Bang => todo!(),
|
|
|
|
TokenKind::Colon => todo!(),
|
|
|
|
TokenKind::Comma => todo!(),
|
|
|
|
TokenKind::Dot => todo!(),
|
2024-09-10 05:04:30 +00:00
|
|
|
TokenKind::DoubleAmpersand => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_binary),
|
|
|
|
precedence: Precedence::LogicalAnd,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::DoubleDot => todo!(),
|
|
|
|
TokenKind::DoubleEqual => todo!(),
|
|
|
|
TokenKind::DoublePipe => todo!(),
|
|
|
|
TokenKind::Equal => todo!(),
|
|
|
|
TokenKind::Greater => todo!(),
|
|
|
|
TokenKind::GreaterOrEqual => todo!(),
|
2024-09-10 14:44:15 +00:00
|
|
|
TokenKind::LeftCurlyBrace => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_block),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::LeftParenthesis => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_grouped),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
|
|
|
TokenKind::LeftSquareBrace => todo!(),
|
|
|
|
TokenKind::Less => todo!(),
|
|
|
|
TokenKind::LessOrEqual => todo!(),
|
|
|
|
TokenKind::Minus => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_unary),
|
|
|
|
infix: Some(Parser::parse_binary),
|
|
|
|
precedence: Precedence::Term,
|
|
|
|
},
|
|
|
|
TokenKind::MinusEqual => todo!(),
|
|
|
|
TokenKind::Mut => todo!(),
|
|
|
|
TokenKind::Percent => todo!(),
|
|
|
|
TokenKind::Plus => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_binary),
|
|
|
|
precedence: Precedence::Term,
|
|
|
|
},
|
|
|
|
TokenKind::PlusEqual => todo!(),
|
2024-09-10 14:44:15 +00:00
|
|
|
TokenKind::RightCurlyBrace => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 10:38:12 +00:00
|
|
|
TokenKind::RightParenthesis => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::RightSquareBrace => todo!(),
|
2024-09-07 16:15:47 +00:00
|
|
|
TokenKind::Semicolon => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Star => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_binary),
|
|
|
|
precedence: Precedence::Factor,
|
|
|
|
},
|
|
|
|
TokenKind::Struct => todo!(),
|
|
|
|
TokenKind::Slash => ParseRule {
|
|
|
|
prefix: None,
|
|
|
|
infix: Some(Parser::parse_binary),
|
|
|
|
precedence: Precedence::Factor,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 03:30:43 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum ParseError {
|
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 {
|
|
|
|
expected: Vec<TokenKind>,
|
|
|
|
found: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-07 21:16:14 +00:00
|
|
|
InvalidAssignmentTarget {
|
|
|
|
found: TokenOwned,
|
|
|
|
position: Span,
|
|
|
|
},
|
2024-09-11 07:10:12 +00:00
|
|
|
UndefinedVariable {
|
|
|
|
identifier: Identifier,
|
2024-09-10 07:42:25 +00:00
|
|
|
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-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 {
|
|
|
|
Self::ExpectedExpression { .. } => "Expected an expression",
|
|
|
|
Self::ExpectedToken { .. } => "Expected a specific token",
|
|
|
|
Self::ExpectedTokenMultiple { .. } => "Expected one of multiple tokens",
|
|
|
|
Self::InvalidAssignmentTarget { .. } => "Invalid assignment target",
|
2024-09-11 07:10:12 +00:00
|
|
|
Self::UndefinedVariable { .. } => "Undefined variable",
|
2024-09-10 13:26:05 +00:00
|
|
|
Self::Chunk { .. } => "Chunk error",
|
|
|
|
Self::Lex(_) => "Lex error",
|
|
|
|
Self::ParseIntError { .. } => "Failed to parse integer",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn details(&self) -> Option<String> {
|
2024-09-10 07:42:25 +00:00
|
|
|
match self {
|
|
|
|
Self::ExpectedExpression { found, .. } => {
|
2024-09-10 13:26:05 +00:00
|
|
|
Some(format!("Expected an expression, found \"{found}\""))
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
|
|
|
Self::ExpectedToken {
|
|
|
|
expected, found, ..
|
2024-09-10 13:26:05 +00:00
|
|
|
} => Some(format!("Expected \"{expected}\", found \"{found}\"")),
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::ExpectedTokenMultiple {
|
|
|
|
expected, found, ..
|
2024-09-10 13:26:05 +00:00
|
|
|
} => Some(format!("Expected one of {expected:?}, found \"{found}\"")),
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::InvalidAssignmentTarget { found, .. } => {
|
2024-09-10 13:26:05 +00:00
|
|
|
Some(format!("Invalid assignment target \"{found}\""))
|
2024-09-10 07:42:25 +00:00
|
|
|
}
|
2024-09-11 07:10:12 +00:00
|
|
|
Self::UndefinedVariable { identifier, .. } => {
|
|
|
|
Some(format!("Undefined variable \"{identifier}\""))
|
|
|
|
}
|
|
|
|
Self::Chunk(error) => error.details(),
|
2024-09-10 13:26:05 +00:00
|
|
|
Self::Lex(error) => Some(error.to_string()),
|
|
|
|
Self::ParseIntError { error, .. } => Some(error.to_string()),
|
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 {
|
|
|
|
Self::ExpectedExpression { position, .. } => *position,
|
|
|
|
Self::ExpectedToken { position, .. } => *position,
|
|
|
|
Self::ExpectedTokenMultiple { position, .. } => *position,
|
|
|
|
Self::InvalidAssignmentTarget { position, .. } => *position,
|
2024-09-11 07:10:12 +00:00
|
|
|
Self::UndefinedVariable { position, .. } => *position,
|
|
|
|
Self::Chunk(error) => error.position(),
|
2024-09-10 07:42:25 +00:00
|
|
|
Self::Lex(error) => error.position(),
|
|
|
|
Self::ParseIntError { position, .. } => *position,
|
|
|
|
}
|
2024-09-07 03:30:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<LexError> for ParseError {
|
|
|
|
fn from(error: LexError) -> Self {
|
|
|
|
Self::Lex(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2024-09-11 07:10:12 +00:00
|
|
|
use crate::Local;
|
2024-09-09 23:23:49 +00:00
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
use super::*;
|
|
|
|
|
2024-09-10 14:44:15 +00:00
|
|
|
#[test]
|
|
|
|
fn block() {
|
|
|
|
let source = "{ 42; 42 }";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(2, 4)),
|
|
|
|
(0, Span(2, 4)),
|
|
|
|
(Instruction::Constant as u8, Span(6, 8)),
|
|
|
|
(1, Span(6, 8)),
|
|
|
|
(Instruction::Return as u8, Span(6, 8)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42)],
|
|
|
|
vec![]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-07 22:48:01 +00:00
|
|
|
#[test]
|
|
|
|
fn add_variables() {
|
2024-09-10 02:57:14 +00:00
|
|
|
let source = "let x = 42; let y = 42; x + y";
|
2024-09-07 22:48:01 +00:00
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
2024-09-10 02:57:14 +00:00
|
|
|
(Instruction::Constant as u8, Span(8, 10)),
|
|
|
|
(0, Span(8, 10)),
|
2024-09-11 07:10:12 +00:00
|
|
|
(Instruction::DeclareVariable as u8, Span(4, 5)),
|
|
|
|
(0, Span(4, 5)),
|
2024-09-10 02:57:14 +00:00
|
|
|
(Instruction::Constant as u8, Span(20, 22)),
|
|
|
|
(1, Span(20, 22)),
|
2024-09-11 07:10:12 +00:00
|
|
|
(Instruction::DeclareVariable as u8, Span(16, 17)),
|
|
|
|
(1, Span(16, 17)),
|
2024-09-10 02:57:14 +00:00
|
|
|
(Instruction::GetVariable as u8, Span(24, 25)),
|
|
|
|
(0, Span(24, 25)),
|
|
|
|
(Instruction::GetVariable as u8, Span(28, 29)),
|
|
|
|
(1, Span(28, 29)),
|
2024-09-10 13:26:05 +00:00
|
|
|
(Instruction::Add as u8, Span(26, 27)),
|
|
|
|
(Instruction::Return as u8, Span(24, 29)),
|
2024-09-07 22:48:01 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42)],
|
|
|
|
vec![
|
2024-09-09 23:23:49 +00:00
|
|
|
Local {
|
|
|
|
identifier: Identifier::new("x"),
|
2024-09-10 13:26:05 +00:00
|
|
|
depth: 0,
|
2024-09-09 23:23:49 +00:00
|
|
|
},
|
|
|
|
Local {
|
|
|
|
identifier: Identifier::new("y"),
|
2024-09-10 13:26:05 +00:00
|
|
|
depth: 0,
|
2024-09-09 23:23:49 +00:00
|
|
|
},
|
|
|
|
],
|
2024-09-07 22:48:01 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
#[test]
|
|
|
|
fn let_statement() {
|
|
|
|
let source = "let x = 42;";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(8, 10)),
|
|
|
|
(0, Span(8, 10)),
|
2024-09-11 07:10:12 +00:00
|
|
|
(Instruction::DeclareVariable as u8, Span(4, 5)),
|
|
|
|
(0, Span(4, 5)),
|
2024-09-07 16:15:47 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(42)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![Local {
|
|
|
|
identifier: Identifier::new("x"),
|
2024-09-10 13:26:05 +00:00
|
|
|
depth: 0,
|
2024-09-09 23:23:49 +00:00
|
|
|
}],
|
2024-09-07 16:15:47 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn string() {
|
|
|
|
let source = "\"Hello, World!\"";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
2024-09-10 13:26:05 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(0, 15)),
|
|
|
|
(0, Span(0, 15)),
|
|
|
|
(Instruction::Return as u8, Span(0, 15)),
|
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::string("Hello, World!")],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 16:15:47 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
#[test]
|
2024-09-07 10:38:12 +00:00
|
|
|
fn integer() {
|
2024-09-07 08:34:03 +00:00
|
|
|
let source = "42";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
2024-09-10 13:26:05 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(0, 2)),
|
|
|
|
(0, Span(0, 2)),
|
|
|
|
(Instruction::Return as u8, Span(0, 2)),
|
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::integer(42)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 08:34:03 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2024-09-07 10:38:12 +00:00
|
|
|
fn boolean() {
|
|
|
|
let source = "true";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
2024-09-10 13:26:05 +00:00
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(0, 4)),
|
|
|
|
(0, Span(0, 4)),
|
|
|
|
(Instruction::Return as u8, Span(0, 4)),
|
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::boolean(true)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 10:38:12 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn grouping() {
|
|
|
|
let source = "(42 + 42) * 2";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(1, 3)),
|
|
|
|
(0, Span(1, 3)),
|
|
|
|
(Instruction::Constant as u8, Span(6, 8)),
|
|
|
|
(1, Span(6, 8)),
|
|
|
|
(Instruction::Add as u8, Span(4, 5)),
|
2024-09-07 16:15:47 +00:00
|
|
|
(Instruction::Constant as u8, Span(12, 13)),
|
|
|
|
(2, Span(12, 13)),
|
|
|
|
(Instruction::Multiply as u8, Span(10, 11)),
|
2024-09-10 13:26:05 +00:00
|
|
|
(Instruction::Return as u8, Span(0, 13)),
|
2024-09-07 16:15:47 +00:00
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42), Value::integer(2)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 16:15:47 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn negation() {
|
|
|
|
let source = "-(42)";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(2, 4)),
|
|
|
|
(0, Span(2, 4)),
|
|
|
|
(Instruction::Negate as u8, Span(0, 1)),
|
2024-09-10 13:26:05 +00:00
|
|
|
(Instruction::Return as u8, Span(0, 5)),
|
2024-09-07 10:38:12 +00:00
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::integer(42)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 10:38:12 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn addition() {
|
2024-09-07 08:34:03 +00:00
|
|
|
let source = "42 + 42";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(0, 2)),
|
|
|
|
(0, Span(0, 2)),
|
|
|
|
(Instruction::Constant as u8, Span(5, 7)),
|
|
|
|
(1, Span(5, 7)),
|
|
|
|
(Instruction::Add as u8, Span(3, 4)),
|
2024-09-10 13:26:05 +00:00
|
|
|
(Instruction::Return as u8, Span(0, 7)),
|
2024-09-07 08:34:03 +00:00
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::integer(42), Value::integer(42)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 08:34:03 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2024-09-07 10:38:12 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subtraction() {
|
|
|
|
let source = "42 - 42";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(0, 2)),
|
|
|
|
(0, Span(0, 2)),
|
|
|
|
(Instruction::Constant as u8, Span(5, 7)),
|
|
|
|
(1, Span(5, 7)),
|
|
|
|
(Instruction::Subtract as u8, Span(3, 4)),
|
2024-09-10 13:26:05 +00:00
|
|
|
(Instruction::Return as u8, Span(0, 7)),
|
2024-09-07 10:38:12 +00:00
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::integer(42), Value::integer(42)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 10:38:12 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn multiplication() {
|
|
|
|
let source = "42 * 42";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(0, 2)),
|
|
|
|
(0, Span(0, 2)),
|
|
|
|
(Instruction::Constant as u8, Span(5, 7)),
|
|
|
|
(1, Span(5, 7)),
|
|
|
|
(Instruction::Multiply as u8, Span(3, 4)),
|
2024-09-10 13:26:05 +00:00
|
|
|
(Instruction::Return as u8, Span(0, 7)),
|
2024-09-07 10:38:12 +00:00
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::integer(42), Value::integer(42)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 10:38:12 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn division() {
|
|
|
|
let source = "42 / 42";
|
|
|
|
let test_chunk = parse(source);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
test_chunk,
|
|
|
|
Ok(Chunk::with_data(
|
|
|
|
vec![
|
|
|
|
(Instruction::Constant as u8, Span(0, 2)),
|
|
|
|
(0, Span(0, 2)),
|
|
|
|
(Instruction::Constant as u8, Span(5, 7)),
|
|
|
|
(1, Span(5, 7)),
|
|
|
|
(Instruction::Divide as u8, Span(3, 4)),
|
2024-09-10 13:26:05 +00:00
|
|
|
(Instruction::Return as u8, Span(0, 7)),
|
2024-09-07 10:38:12 +00:00
|
|
|
],
|
2024-09-07 16:15:47 +00:00
|
|
|
vec![Value::integer(42), Value::integer(42)],
|
2024-09-09 23:23:49 +00:00
|
|
|
vec![],
|
2024-09-07 10:38:12 +00:00
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|