From 5662ba3d08bfbdf3dabd8daefa9c9c35345f8b6c Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 5 Feb 2025 19:27:51 -0500 Subject: [PATCH] Add string concatenation --- dust-lang/src/native_function/assert.rs | 2 +- dust-lang/src/native_function/random.rs | 2 +- dust-lang/src/vm/action.rs | 38 +++++++++++++++++++++++-- dust-lang/src/vm/thread.rs | 4 +-- 4 files changed, 39 insertions(+), 7 deletions(-) diff --git a/dust-lang/src/native_function/assert.rs b/dust-lang/src/native_function/assert.rs index bb8610b..798c517 100644 --- a/dust-lang/src/native_function/assert.rs +++ b/dust-lang/src/native_function/assert.rs @@ -9,7 +9,7 @@ pub fn panic(data: &mut Thread, _: usize, argument_range: Range) { for register_index in argument_range { let string = data.get_string_register(register_index); - message.push_str(&string); + message.push_str(string); message.push('\n'); } diff --git a/dust-lang/src/native_function/random.rs b/dust-lang/src/native_function/random.rs index a146281..3f4d921 100644 --- a/dust-lang/src/native_function/random.rs +++ b/dust-lang/src/native_function/random.rs @@ -2,7 +2,7 @@ use std::ops::Range; use rand::Rng; -use crate::{Value, vm::Thread}; +use crate::vm::Thread; pub fn random_int(data: &mut Thread, destination: usize, argument_range: Range) { let mut argument_range_iter = argument_range.into_iter(); diff --git a/dust-lang/src/vm/action.rs b/dust-lang/src/vm/action.rs index a4fc683..a99bddb 100644 --- a/dust-lang/src/vm/action.rs +++ b/dust-lang/src/vm/action.rs @@ -120,11 +120,43 @@ pub fn add(instruction: InstructionBuilder, thread: &mut Thread) { }; let result = left_value + right_value; - println!("{left} + {right} = {destination}"); - println!("{left_value} + {right_value} = {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!(), } } diff --git a/dust-lang/src/vm/thread.rs b/dust-lang/src/vm/thread.rs index 4bb455f..4814c69 100644 --- a/dust-lang/src/vm/thread.rs +++ b/dust-lang/src/vm/thread.rs @@ -10,7 +10,7 @@ pub struct Thread { chunk: Arc, call_stack: Vec, pub return_value: Option>, - spawned_threads: Vec>, + _spawned_threads: Vec>, } impl Thread { @@ -24,7 +24,7 @@ impl Thread { chunk, call_stack, return_value: None, - spawned_threads: Vec::new(), + _spawned_threads: Vec::new(), } }