1
0

Add string concatenation

This commit is contained in:
Jeff 2025-02-05 19:27:51 -05:00
parent 4775d425a0
commit 5662ba3d08
4 changed files with 39 additions and 7 deletions

View File

@ -9,7 +9,7 @@ pub fn panic(data: &mut Thread, _: usize, argument_range: Range<usize>) {
for register_index in argument_range { for register_index in argument_range {
let string = data.get_string_register(register_index); let string = data.get_string_register(register_index);
message.push_str(&string); message.push_str(string);
message.push('\n'); message.push('\n');
} }

View File

@ -2,7 +2,7 @@ use std::ops::Range;
use rand::Rng; use rand::Rng;
use crate::{Value, vm::Thread}; use crate::vm::Thread;
pub fn random_int(data: &mut Thread, destination: usize, argument_range: Range<usize>) { pub fn random_int(data: &mut Thread, destination: usize, argument_range: Range<usize>) {
let mut argument_range_iter = argument_range.into_iter(); let mut argument_range_iter = argument_range.into_iter();

View File

@ -120,11 +120,43 @@ pub fn add(instruction: InstructionBuilder, thread: &mut Thread) {
}; };
let result = left_value + right_value; let result = left_value + right_value;
println!("{left} + {right} = {destination}");
println!("{left_value} + {right_value} = {result}");
thread.set_integer_register(destination, result); thread.set_integer_register(destination, result);
} }
(TypeCode::STRING, TypeCode::STRING) => {
let left_value = if left_is_constant {
if cfg!(debug_assertions) {
thread.get_constant(left).as_string().unwrap().clone()
} else {
unsafe {
thread
.get_constant(left)
.as_string()
.unwrap_unchecked()
.clone()
}
}
} else {
thread.get_string_register(left).clone()
};
let right_value = if right_is_constant {
if cfg!(debug_assertions) {
thread.get_constant(right).as_string().unwrap().clone()
} else {
unsafe {
thread
.get_constant(right)
.as_string()
.unwrap_unchecked()
.clone()
}
}
} else {
thread.get_string_register(right).clone()
};
let result = left_value + &right_value;
thread.set_string_register(destination, result);
}
_ => unimplemented!(), _ => unimplemented!(),
} }
} }

View File

@ -10,7 +10,7 @@ pub struct Thread {
chunk: Arc<Chunk>, chunk: Arc<Chunk>,
call_stack: Vec<CallFrame>, call_stack: Vec<CallFrame>,
pub return_value: Option<Option<Value>>, pub return_value: Option<Option<Value>>,
spawned_threads: Vec<JoinHandle<()>>, _spawned_threads: Vec<JoinHandle<()>>,
} }
impl Thread { impl Thread {
@ -24,7 +24,7 @@ impl Thread {
chunk, chunk,
call_stack, call_stack,
return_value: None, return_value: None,
spawned_threads: Vec::new(), _spawned_threads: Vec::new(),
} }
} }