-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchunk.rs
More file actions
28 lines (24 loc) · 820 Bytes
/
Copy pathchunk.rs
File metadata and controls
28 lines (24 loc) · 820 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use crate::constant_pool::ConstantPool;
use crate::instruction::BytecodeInstruction;
use serde::{Deserialize, Serialize};
use techscript_ast::LiteralVal;
/// Executable instructions segment carrying its own local constant pool.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Chunk {
pub instructions: Vec<BytecodeInstruction>,
pub constants: ConstantPool,
}
impl Chunk {
/// Creates an empty Chunk.
pub fn new() -> Self {
Self::default()
}
/// Appends an instruction to the chunk.
pub fn write(&mut self, inst: BytecodeInstruction) {
self.instructions.push(inst);
}
/// Registers a literal constant, returning its deduplicated index.
pub fn add_const(&mut self, val: LiteralVal) -> u32 {
self.constants.add(val)
}
}