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
use crate::Operand;
#[cfg(feature = "alloc")]
use alloc::vec::Vec;
use core::fmt::{self, Display, Formatter};

/// An instruction.
#[derive(Debug, Eq, PartialEq)]
pub enum Instruction {
    /// A `call` instruction.
    Call(u64, Operand),
    /// A `set` instruction.
    Set(Operand),
    /// A `get` instruction.
    Get(Operand),
    /// A `constant` instruction.
    Constant(Operand),
    /// An `if` instruction.
    #[cfg(feature = "alloc")]
    If(Vec<Instruction>),
    /// A `nop` instruction.
    Nop(u64),
    /// A `close` instruction.
    ///
    /// It is used only for encoding.
    #[cfg(feature = "alloc")]
    Close(u64, Vec<Instruction>),
    /// A `skip` instruction.
    ///
    /// It is used only for encoding.
    Skip(u64),
}

impl Instruction {
    pub const CALL: u8 = 0;
    pub const SET: u8 = 1;
    pub const GET: u8 = 2;
    pub const CONSTANT: u8 = 3;
    pub const IF: u8 = 4;
    pub const NOP: u8 = 5;
    pub const CLOSE: u8 = 6;
    pub const SKIP: u8 = 7;

    /// Displays instructions in a slice.
    #[cfg(feature = "alloc")]
    pub fn display_slice(instructions: &[Self]) -> impl Display + '_ {
        DisplayInstructionList::new(instructions, 0)
    }
}

struct DisplayInstruction<'a> {
    instruction: &'a Instruction,
    #[allow(unused)]
    indent: usize,
}

impl<'a> DisplayInstruction<'a> {
    fn new(instruction: &'a Instruction, indent: usize) -> Self {
        Self {
            instruction,
            indent,
        }
    }
}

impl<'a> Display for DisplayInstruction<'a> {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        #[cfg(feature = "alloc")]
        let indent = self.indent + 1;

        write!(formatter, "- ")?;

        match self.instruction {
            Instruction::Call(count, operand) => write!(formatter, "call {} {}", count, operand),
            Instruction::Set(operand) => write!(formatter, "set {}", operand),
            Instruction::Get(operand) => write!(formatter, "get {}", operand),
            Instruction::Constant(operand) => write!(formatter, "constant {}", operand),
            #[cfg(feature = "alloc")]
            Instruction::If(instructions) => {
                write!(formatter, "if")?;
                write!(
                    formatter,
                    "{}",
                    DisplayInstructionList::new(instructions, indent)
                )
            }
            Instruction::Nop(operand) => write!(formatter, "nop {}", operand),
            #[cfg(feature = "alloc")]
            Instruction::Close(arity, instructions) => {
                write!(formatter, "close {}", arity)?;
                write!(
                    formatter,
                    "{}",
                    DisplayInstructionList::new(instructions, indent)
                )
            }
            Instruction::Skip(count) => write!(formatter, "skip {}", count),
        }
    }
}

struct DisplayInstructionList<'a> {
    instructions: &'a [Instruction],
    indent: usize,
}

impl<'a> DisplayInstructionList<'a> {
    #[cfg(feature = "alloc")]
    fn new(instructions: &'a [Instruction], indent: usize) -> Self {
        Self {
            instructions,
            indent,
        }
    }
}

impl<'a> Display for DisplayInstructionList<'a> {
    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
        for instruction in self.instructions {
            writeln!(formatter)?;

            for _ in 0..self.indent {
                write!(formatter, "  ")?
            }

            write!(
                formatter,
                "{}",
                DisplayInstruction::new(instruction, self.indent)
            )?;
        }

        Ok(())
    }
}