2024-09-07 08:34:03 +00:00
|
|
|
use std::{
|
|
|
|
fmt::{self, Display, Formatter},
|
2024-09-07 10:38:12 +00:00
|
|
|
mem::{self, swap},
|
2024-09-07 08:34:03 +00:00
|
|
|
num::ParseIntError,
|
2024-09-07 10:38:12 +00:00
|
|
|
ptr::replace,
|
2024-09-07 08:34:03 +00:00
|
|
|
};
|
2024-09-07 03:30:43 +00:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
Chunk, ChunkError, Instruction, LexError, Lexer, Span, Token, TokenKind, TokenOwned, Value,
|
|
|
|
};
|
|
|
|
|
2024-09-07 08:34:03 +00:00
|
|
|
pub fn parse(source: &str) -> Result<Chunk, ParseError> {
|
|
|
|
let lexer = Lexer::new(source);
|
|
|
|
let mut parser = Parser::new(lexer);
|
|
|
|
|
|
|
|
while !parser.is_eof() {
|
|
|
|
parser.parse(Precedence::None)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 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-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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn consume(&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-07 08:37:38 +00:00
|
|
|
fn emit_byte(&mut self, byte: u8, position: Span) {
|
|
|
|
self.chunk.write(byte, 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> {
|
|
|
|
let constant_index = self.chunk.push_constant(value)?;
|
2024-09-07 10:38:12 +00:00
|
|
|
let position = self.previous_position;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-07 08:37:38 +00:00
|
|
|
self.emit_byte(Instruction::Constant as u8, position);
|
|
|
|
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 10:38:12 +00:00
|
|
|
fn parse_boolean(&mut self) -> Result<(), ParseError> {
|
|
|
|
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-07 08:34:03 +00:00
|
|
|
fn parse_integer(&mut self) -> 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(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_grouped(&mut self) -> Result<(), ParseError> {
|
|
|
|
self.parse_expression()?;
|
2024-09-07 10:38:12 +00:00
|
|
|
self.consume(TokenKind::RightParenthesis)
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_unary(&mut self) -> Result<(), ParseError> {
|
2024-09-07 10:38:12 +00:00
|
|
|
let byte = match self.previous_token.kind() {
|
|
|
|
TokenKind::Minus => Instruction::Negate as u8,
|
|
|
|
_ => {
|
|
|
|
return Err(ParseError::ExpectedTokenMultiple {
|
|
|
|
expected: vec![TokenKind::Minus],
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2024-09-07 08:37:38 +00:00
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
self.parse_expression()?;
|
|
|
|
self.emit_byte(byte, self.previous_position);
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_binary(&mut self) -> Result<(), ParseError> {
|
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 {
|
|
|
|
TokenKind::Plus => Instruction::Add as u8,
|
|
|
|
TokenKind::Minus => Instruction::Subtract as u8,
|
|
|
|
TokenKind::Star => Instruction::Multiply as u8,
|
|
|
|
TokenKind::Slash => Instruction::Divide as u8,
|
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 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
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(&mut self, precedence: Precedence) -> Result<(), ParseError> {
|
|
|
|
self.advance()?;
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
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 08:34:03 +00:00
|
|
|
|
|
|
|
prefix(self)?;
|
|
|
|
} else {
|
2024-09-07 10:38:12 +00:00
|
|
|
return Err(ParseError::ExpectedExpression {
|
|
|
|
found: self.previous_token.to_owned(),
|
|
|
|
position: self.previous_position,
|
2024-09-07 08:34:03 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
while precedence <= ParseRule::from(&self.current_token.kind()).precedence {
|
2024-09-07 08:34:03 +00:00
|
|
|
self.advance()?;
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
let infix_rule = ParseRule::from(&self.previous_token.kind()).infix;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
if let Some(infix) = infix_rule {
|
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
|
|
|
|
|
|
|
infix(self)?;
|
|
|
|
} 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 10:38:12 +00:00
|
|
|
type ParserFunction<'a> = fn(&mut Parser<'a>) -> Result<(), ParseError>;
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Copy)]
|
|
|
|
pub struct ParseRule<'a> {
|
|
|
|
pub prefix: Option<ParserFunction<'a>>,
|
|
|
|
pub infix: Option<ParserFunction<'a>>,
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
TokenKind::Identifier => todo!(),
|
2024-09-07 10:38:12 +00:00
|
|
|
TokenKind::Boolean => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_boolean),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
2024-09-07 08:34:03 +00:00
|
|
|
TokenKind::Character => todo!(),
|
|
|
|
TokenKind::Float => todo!(),
|
|
|
|
TokenKind::Integer => ParseRule {
|
|
|
|
prefix: Some(Parser::parse_integer),
|
|
|
|
infix: None,
|
|
|
|
precedence: Precedence::None,
|
|
|
|
},
|
|
|
|
TokenKind::String => todo!(),
|
|
|
|
TokenKind::Async => todo!(),
|
|
|
|
TokenKind::Bool => todo!(),
|
|
|
|
TokenKind::Break => todo!(),
|
|
|
|
TokenKind::Else => todo!(),
|
|
|
|
TokenKind::FloatKeyword => todo!(),
|
|
|
|
TokenKind::If => todo!(),
|
|
|
|
TokenKind::Int => todo!(),
|
|
|
|
TokenKind::Let => todo!(),
|
|
|
|
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!(),
|
|
|
|
TokenKind::DoubleAmpersand => todo!(),
|
|
|
|
TokenKind::DoubleDot => todo!(),
|
|
|
|
TokenKind::DoubleEqual => todo!(),
|
|
|
|
TokenKind::DoublePipe => todo!(),
|
|
|
|
TokenKind::Equal => todo!(),
|
|
|
|
TokenKind::Greater => todo!(),
|
|
|
|
TokenKind::GreaterOrEqual => todo!(),
|
|
|
|
TokenKind::LeftCurlyBrace => todo!(),
|
|
|
|
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!(),
|
|
|
|
TokenKind::RightCurlyBrace => todo!(),
|
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!(),
|
|
|
|
TokenKind::Semicolon => todo!(),
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Wrappers around foreign errors
|
|
|
|
Chunk(ChunkError),
|
|
|
|
Lex(LexError),
|
|
|
|
ParseIntError(ParseIntError),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ParseIntError> for ParseError {
|
|
|
|
fn from(error: ParseIntError) -> Self {
|
|
|
|
Self::ParseIntError(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<LexError> for ParseError {
|
|
|
|
fn from(error: LexError) -> Self {
|
|
|
|
Self::Lex(error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<ChunkError> for ParseError {
|
|
|
|
fn from(error: ChunkError) -> Self {
|
|
|
|
Self::Chunk(error)
|
|
|
|
}
|
|
|
|
}
|
2024-09-07 08:34:03 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[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(
|
|
|
|
vec![(Instruction::Constant as u8, Span(0, 2)), (0, Span(0, 2))],
|
|
|
|
vec![Value::integer(42)]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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(
|
|
|
|
vec![(Instruction::Constant as u8, Span(0, 4)), (0, Span(0, 4))],
|
|
|
|
vec![Value::boolean(true)]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn grouping() {
|
2024-09-07 08:34:03 +00:00
|
|
|
env_logger::builder().is_test(true).try_init().unwrap();
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
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)),
|
|
|
|
(Instruction::Constant as u8, Span(11, 12)),
|
|
|
|
(0, Span(11, 12)),
|
|
|
|
(Instruction::Multiply as u8, Span(9, 10)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42), Value::integer(2)]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42)]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
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)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42)]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42)]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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)),
|
|
|
|
],
|
|
|
|
vec![Value::integer(42), Value::integer(42)]
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
2024-09-07 08:34:03 +00:00
|
|
|
}
|