Skip to main content

dlang/bytecode_vm/
bytecode.rs

1pub mod compiler;
2pub mod errors;
3pub mod instruction;
4pub mod instructions;
5pub mod opcode;
6mod symbol;
7
8use std::fmt::Display;
9
10use crate::object::{Object, ObjectTrait};
11
12use self::{errors::BytecodeError, instructions::Instructions};
13
14#[derive(Debug)]
15pub struct Bytecode {
16    pub constants: Vec<Object>,
17    pub instructions: Instructions,
18}
19
20impl Bytecode {
21    pub fn create() -> Result<Self, BytecodeError> {
22        let new_instruction = Instructions::create()?;
23
24        Ok(Self {
25            constants: Vec::new(),
26            instructions: new_instruction,
27        })
28    }
29}
30
31impl Display for Bytecode {
32    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33        let mut buf = String::new();
34        buf += "\nCONSTS\n";
35        for (idx, cons) in self.constants.iter().enumerate() {
36            buf += &format!("{:0>6}\t\t", idx);
37            buf += &cons.to_str();
38            buf += "\n";
39        }
40
41        buf += "\nINSTRUCTIONS\n";
42
43        buf += &self.instructions.to_string();
44
45        f.write_str(&buf)
46    }
47}