This commit is contained in:
Jeff 2024-09-15 10:20:43 -04:00
parent 9cb6873618
commit 71a4f863e3
3 changed files with 30 additions and 13 deletions

View File

@ -137,6 +137,7 @@ impl Instruction {
}
pub fn set_destination(&mut self, destination: u8) {
self.0 &= 0x00FFFFFF;
self.0 |= (destination as u32) << 24;
}
@ -492,19 +493,17 @@ mod tests {
#[test]
fn add() {
let mut instruction = Instruction::add(0, 1, 2);
let mut instruction = Instruction::add(1, 1, 0);
instruction.set_operation(Operation::Add);
instruction.set_first_argument_to_constant();
instruction.set_second_argument_to_constant();
assert_eq!(instruction.operation(), Operation::Add);
assert_eq!(instruction.destination(), 0);
assert_eq!(instruction.destination(), 1);
assert_eq!(instruction.first_argument(), 1);
assert_eq!(instruction.second_argument(), 2);
assert_eq!(instruction.second_argument(), 0);
assert!(instruction.first_argument_is_constant());
assert!(instruction.second_argument_is_constant());
}
#[test]

View File

@ -71,8 +71,6 @@ impl<'src> Parser<'src> {
} else {
self.current_register += 1;
log::trace!("Incremented register to {}", self.current_register);
Ok(())
}
}
@ -87,8 +85,6 @@ impl<'src> Parser<'src> {
} else {
self.current_register -= 1;
log::trace!("Decremented register to {}", self.current_register);
Ok(())
}
}
@ -100,7 +96,7 @@ impl<'src> Parser<'src> {
let (new_token, position) = self.lexer.next_token()?;
log::trace!("Advancing to token {new_token} at {position}");
log::trace!("Parsing token \"{new_token}\" at {position}");
self.previous_token = replace(&mut self.current_token, new_token);
self.previous_position = replace(&mut self.current_position, position);
@ -369,7 +365,6 @@ impl<'src> Parser<'src> {
if let Some(register_index) = previous_register {
previous_instruction.set_destination(register_index);
self.emit_instruction(previous_instruction, self.previous_position);
self.decrement_register()?;
} else {
@ -379,13 +374,19 @@ impl<'src> Parser<'src> {
self.previous_position,
);
}
} else {
self.emit_instruction(previous_instruction, previous_position);
self.emit_instruction(
Instruction::set_local(self.current_register - 1, local_index),
start_position,
);
}
} else {
self.increment_register()?;
self.emit_instruction(
Instruction::get_local(self.current_register, local_index),
self.previous_position,
);
self.increment_register()?;
}
Ok(())

View File

@ -2,6 +2,23 @@ use crate::Local;
use super::*;
#[test]
fn set_local() {
assert_eq!(
parse("let x = 41; x = 42;"),
Ok(Chunk::with_data(
vec![
(Instruction::load_constant(0, 0), Span(8, 10)),
(Instruction::declare_local(0, 0), Span(4, 5)),
(Instruction::load_constant(1, 1), Span(16, 18)),
(Instruction::set_local(1, 0), Span(12, 13)),
],
vec![Value::integer(41), Value::integer(42)],
vec![Local::new(Identifier::new("x"), 0, Some(0)),]
)),
);
}
#[test]
fn parentheses_precedence() {
assert_eq!(
@ -51,7 +68,7 @@ fn add_multiply_precedence() {
}
#[test]
fn let_statement() {
fn declare_local() {
assert_eq!(
parse("let x = 42;"),
Ok(Chunk::with_data(