diff --git a/dust-lang/src/chunk/mod.rs b/dust-lang/src/chunk/mod.rs index 10dea6f..94afffa 100644 --- a/dust-lang/src/chunk/mod.rs +++ b/dust-lang/src/chunk/mod.rs @@ -1,18 +1,15 @@ //! Representation of a Dust program or function. //! +//! **Except for testing purposes, a chunk should not be created directly. Instead, use the compiler +//! to generate a chunk from Dust source code.** +//! //! A chunk is output by the compiler to represent all the information needed to execute a Dust //! program. In addition to the program itself, each function in the source is compiled into its own //! chunk and stored in the `prototypes` field of its parent. Thus, a chunk can also represent a //! function prototype. //! //! Chunks have a name when they belong to a named function. They also have a type, so the input -//! parameters and the type of the return value are statically known. The [`Chunk::stack_size`] -//! field can provide the necessary stack size that will be needed by the virtual machine. Chunks -//! cannot be instantiated directly and must be created by the compiler. However, when the Rust -//! compiler is in the "test" or "debug_assertions" configuration (used for all types of test), -//! [`Chunk::with_data`] can be used to create a chunk for comparison to the compiler output. Do not -//! try to run these chunks in a virtual machine. Due to their missing stack size and record index, -//! they will cause a panic or undefined behavior. +//! parameters and the type of the return value are statically known. mod disassembler; mod local; mod scope; diff --git a/dust-lang/src/chunk/scope.rs b/dust-lang/src/chunk/scope.rs index be1e061..e10b80d 100644 --- a/dust-lang/src/chunk/scope.rs +++ b/dust-lang/src/chunk/scope.rs @@ -11,8 +11,7 @@ use serde::{Deserialize, Serialize}; /// The `block index` is a unique identifier for a block within a chunk. It is used to differentiate /// between blocks that are not nested together but have the same depth, i.e. sibling scopes. If the /// `block_index` is 0, then the scope is the root scope of the chunk. The `block_index` is always 0 -/// when the `depth` is 0. See [Chunk::begin_scope][] and [Chunk::end_scope][] to see how scopes are -/// incremented and decremented. +/// when the `depth` is 0. #[derive(Debug, Clone, Copy, Default, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub struct Scope { /// Level of block nesting. diff --git a/dust-lang/src/instruction/mod.rs b/dust-lang/src/instruction/mod.rs index a675fc4..db41c4a 100644 --- a/dust-lang/src/instruction/mod.rs +++ b/dust-lang/src/instruction/mod.rs @@ -16,10 +16,6 @@ //! 32-47 | B field (unsigned 16-bit integer) //! 48-63 | C field (unsigned 16-bit integer) //! -//! **Be careful when working with instructions directly**. When modifying an instruction's fields, -//! you may also need to modify its flags. It is usually best to remove instructions and insert new -//! ones in their place instead of mutating them. -//! //! # Creating Instructions //! //! For each operation, there are two ways to create an instruction: @@ -68,7 +64,6 @@ //! // - `a += 2` //! // - `a = a + 2` //! // - `a = 2 + a` -//! //! let operation = mystery_instruction.operation(); //! let is_add_assign = match operation { //! Operation::ADD => { diff --git a/dust-lang/src/vm/call_frame.rs b/dust-lang/src/vm/call_frame.rs index a51cff3..6263a59 100644 --- a/dust-lang/src/vm/call_frame.rs +++ b/dust-lang/src/vm/call_frame.rs @@ -88,15 +88,6 @@ pub enum Register { Pointer(Pointer), } -impl Register { - #[allow(unused_assignments)] - pub fn close(mut self) { - if let Self::Value(value) = self { - self = Self::Closed(value); - } - } -} - impl Display for Register { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { diff --git a/dust-lang/src/vm/thread.rs b/dust-lang/src/vm/thread.rs index c00c866..4ee1a7f 100644 --- a/dust-lang/src/vm/thread.rs +++ b/dust-lang/src/vm/thread.rs @@ -242,12 +242,17 @@ impl Thread { } pub fn close_boolean_register(&mut self, register_index: usize) { - self.current_frame_mut() + let register = self + .current_frame_mut() .registers .booleans .get_mut(register_index) - .unwrap() - .close(); + .unwrap(); + + *register = match register { + Register::Value(value) => Register::Closed(*value), + _ => panic!("Attempted to close non-value register"), + }; } pub fn get_byte_register(&self, register_index: usize) -> &u8 { @@ -332,12 +337,17 @@ impl Thread { } pub fn close_byte_register(&mut self, register_index: usize) { - self.current_frame_mut() + let register = self + .current_frame_mut() .registers .bytes .get_mut(register_index) - .unwrap() - .close(); + .unwrap(); + + *register = match register { + Register::Value(value) => Register::Closed(*value), + _ => panic!("Attempted to close non-value register"), + }; } pub fn get_character_register(&self, register_index: usize) -> &char { @@ -427,12 +437,17 @@ impl Thread { } pub fn close_character_register(&mut self, register_index: usize) { - self.current_frame_mut() + let register = self + .current_frame_mut() .registers .characters .get_mut(register_index) - .unwrap() - .close(); + .unwrap(); + + *register = match register { + Register::Value(value) => Register::Closed(*value), + _ => panic!("Attempted to close non-value register"), + }; } pub fn get_float_register(&self, register_index: usize) -> &f64 { @@ -520,12 +535,17 @@ impl Thread { } pub fn close_float_register(&mut self, register_index: usize) { - self.current_frame_mut() + let register = self + .current_frame_mut() .registers .floats .get_mut(register_index) - .unwrap() - .close(); + .unwrap(); + + *register = match register { + Register::Value(value) => Register::Closed(*value), + _ => panic!("Attempted to close non-value register"), + }; } pub fn get_integer_register(&self, register_index: usize) -> &i64 { @@ -613,12 +633,17 @@ impl Thread { } pub fn close_integer_register(&mut self, register_index: usize) { - self.current_frame_mut() + let register = self + .current_frame_mut() .registers .integers .get_mut(register_index) - .unwrap() - .close(); + .unwrap(); + + *register = match register { + Register::Value(value) => Register::Closed(*value), + _ => panic!("Attempted to close non-value register"), + }; } pub fn get_string_register(&self, register_index: usize) -> &DustString {