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
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use crate::context::ExpressionContext;
use crate::ExpressionRuntime;
use garnish_lang_common::{Error, Instruction};

impl ExpressionRuntime {
    pub fn instruction_iter<'a, T>(
        &'a mut self,
        start: String,
        context: &'a T,
    ) -> Result<InstructionIter<'a, T>, String>
    where
        T: ExpressionContext,
    {
        InstructionIter::new(self, start, context)
    }
}

pub struct InstructionData {
    instruction: Instruction,
    #[allow(dead_code)]
    instruction_index: usize,
    expression_runtime: ExpressionRuntime,
    #[allow(dead_code)]
    error: Error,
}

impl InstructionData {
    pub fn get_instruction(&self) -> Instruction {
        self.instruction.clone()
    }

    pub fn get_expression_runtime(&self) -> &ExpressionRuntime {
        &self.expression_runtime
    }
}

pub struct InstructionIter<'a, T> {
    expression_runtime: &'a mut ExpressionRuntime,
    context: &'a T,
}

impl<'a, T> InstructionIter<'a, T>
where
    T: ExpressionContext,
{
    fn new(
        expression_runtime: &'a mut ExpressionRuntime,
        start: String,
        context: &'a T,
    ) -> Result<Self, String> {
        let expression_index = match expression_runtime.expression_map.get(&start.to_string()) {
            Some(i) => *i - 1, // expression indices start at 1
            None => Result::Err(format!(
                "Expression '{}' does not exist in instructions.",
                start.to_string()
            ))?,
        };

        let instruction_start = *expression_runtime
            .expression_table
            .get(expression_index)
            .unwrap();

        expression_runtime.ref_cursor = 0;

        expression_runtime.push_frame(instruction_start);

        Ok(InstructionIter {
            expression_runtime,
            context,
        })
    }
}

impl<'a, T> Iterator for InstructionIter<'a, T>
where
    T: ExpressionContext,
{
    type Item = InstructionData;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.expression_runtime.call_stack.is_empty() {
            let index = self.expression_runtime.current_instruction_cursor();
            match self.expression_runtime.next_instruction() {
                Ok(instruction) => {
                    let error = match self.expression_runtime.advance_instruction(self.context) {
                        Ok(_x) => "".into(),
                        Err(e) => e,
                    };

                    return Some(InstructionData {
                        instruction,
                        instruction_index: index,
                        expression_runtime: self.expression_runtime.clone(),
                        error,
                    });
                }
                Err(error) => {
                    return Some(InstructionData {
                        instruction: Instruction::Put,
                        instruction_index: index,
                        expression_runtime: self.expression_runtime.clone(),
                        error,
                    })
                }
            };
        }

        None
    }
}

#[cfg(test)]
mod tests {
    use crate::context::DefaultContext;
    use crate::runtime::ExpressionRuntime;
    use garnish_lang_common::{ExpressionValue, Instruction};
    use garnish_lang_instruction_set_builder::InstructionSetBuilder;

    #[test]
    fn instruction_iterator() {
        let mut instructions = InstructionSetBuilder::new();
        instructions.start_expression("main");

        instructions.put(ExpressionValue::integer(20)).unwrap();
        instructions.put(ExpressionValue::integer(30)).unwrap();
        instructions.perform_addition();

        instructions.end_expression();
        let mut expression_runtime = ExpressionRuntime::new(&instructions);

        let context = DefaultContext {};

        let mut instructions: Vec<Instruction> = vec![];
        for data in expression_runtime
            .instruction_iter("main".to_string(), &context)
            .unwrap()
        {
            instructions.push(data.instruction);
        }

        let expected = vec![
            Instruction::Put,
            Instruction::Put,
            Instruction::PerformAddition,
            Instruction::EndExpression,
        ];

        assert_eq!(instructions, expected);
    }
}