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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::GValue;

#[derive(Debug, PartialEq, Clone)]
pub struct Bytecode {
    source_instructions: Vec<Instruction>,
    step_instructions: Vec<Instruction>,
}

impl Default for Bytecode {
    fn default() -> Bytecode {
        Bytecode {
            source_instructions: vec![],
            step_instructions: vec![],
        }
    }
}
impl Bytecode {
    pub fn new() -> Bytecode {
        Default::default()
    }

    pub fn add_source(&mut self, source_name: String, args: Vec<GValue>) {
        self.source_instructions
            .push(Instruction::new(source_name, args));
    }
    pub fn add_step(&mut self, step_name: String, args: Vec<GValue>) {
        self.step_instructions
            .push(Instruction::new(step_name, args));
    }

    pub fn steps(&self) -> &Vec<Instruction> {
        &self.step_instructions
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct Instruction {
    operator: String,
    args: Vec<GValue>,
}

impl Instruction {
    pub fn new(operator: String, args: Vec<GValue>) -> Instruction {
        Instruction { operator, args }
    }

    pub fn operator(&self) -> &String {
        &self.operator
    }

    pub fn args(&self) -> &Vec<GValue> {
        &self.args
    }
}