about summary refs log tree commit diff
path: root/tvix/eval/src/chunk.rs
blob: 3475e58f187066a48d7f2bb708f475aca218a4da (plain) (blame)
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
use crate::opcode::{CodeIdx, ConstantIdx, OpCode};
use crate::value::Value;

#[derive(Debug, Default)]
pub struct Chunk {
    pub code: Vec<OpCode>,
    constants: Vec<Value>,
}

impl Chunk {
    pub fn add_op(&mut self, data: OpCode) -> CodeIdx {
        let idx = self.code.len();
        self.code.push(data);
        CodeIdx(idx)
    }

    pub fn add_constant(&mut self, data: Value) -> ConstantIdx {
        let idx = self.constants.len();
        self.constants.push(data);
        ConstantIdx(idx)
    }

    pub fn constant(&self, idx: ConstantIdx) -> &Value {
        &self.constants[idx.0]
    }
}