Struct Chunk

Source
pub struct Chunk {
    pub code: Vec<u8>,
    pub constants: Vec<Value>,
    pub lines: Vec<usize>,
    pub identifiers: Vec<String>,
}
Expand description

A chunk of bytecode representing a compiled program

Fields§

§code: Vec<u8>

The actual bytecode instructions

§constants: Vec<Value>

Constant values used by the program

§lines: Vec<usize>

Source code line numbers for debugging

§identifiers: Vec<String>

Variable and function names used in the program

Implementations§

Source§

impl Chunk

Source

pub fn new() -> Self

Creates a new, empty bytecode chunk

Source

pub fn write_byte(&mut self, byte: u8, line: usize)

Writes a byte to the chunk

§Arguments
  • byte - The byte to write
  • line - The source code line number
Source

pub fn write_op(&mut self, op: OpCode, line: usize)

Writes an opcode to the chunk

§Arguments
  • op - The opcode to write
  • line - The source code line number
Source

pub fn add_constant(&mut self, value: Value) -> usize

Adds a constant to the chunk’s constant pool

§Arguments
  • value - The constant value to add
§Returns

The index of the constant in the constant pool

Source

pub fn add_identifier(&mut self, name: String) -> usize

Adds an identifier to the chunk’s identifier pool

§Arguments
  • name - The identifier name to add
§Returns

The index of the identifier in the identifier pool

Source

pub fn serialize(&self, writer: &mut dyn Write) -> Result<()>

Serializes the chunk to binary data

§Arguments
  • writer - The writer to write the binary data to
§Returns

IO result indicating success or failure

Source

pub fn deserialize(reader: &mut dyn Read) -> Result<Self>

Deserializes a chunk from binary data

§Arguments
  • reader - The reader to read the binary data from
§Returns

The deserialized chunk or an IO error

Trait Implementations§

Source§

impl Debug for Chunk

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Chunk

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl Freeze for Chunk

§

impl RefUnwindSafe for Chunk

§

impl Send for Chunk

§

impl Sync for Chunk

§

impl Unpin for Chunk

§

impl UnwindSafe for Chunk

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.