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
use super::{AbstractOp, Expression, Imm};
use std::convert::From;
use std::fmt;

/// Macro definition.
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum MacroDefinition {
    /// Instruction macro definition.
    Instruction(InstructionMacroDefinition),

    /// Expression macro definition.
    Expression(ExpressionMacroDefinition),
}

impl MacroDefinition {
    /// Returns the name of the defined macro.
    pub fn name(&self) -> &String {
        match self {
            Self::Instruction(m) => &m.name,
            Self::Expression(m) => &m.name,
        }
    }

    /// Returns the specified parameters of the defined macro.
    pub fn parameters(&self) -> &[String] {
        match self {
            Self::Instruction(m) => &m.parameters,
            Self::Expression(m) => &m.parameters,
        }
    }

    /// Unwraps an `ExpressionMacroDefinition` from a `MacroDefinition`.
    pub fn unwrap_expression(&self) -> &ExpressionMacroDefinition {
        match self {
            Self::Instruction(_) => {
                panic!("unwrapped expression macro, but found instruction macro")
            }
            Self::Expression(m) => m,
        }
    }
}

impl fmt::Display for MacroDefinition {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Instruction(m) => write!(f, "%{}({})", m.name, m.parameters.join(", ")),
            Self::Expression(m) => write!(f, "{}({})", m.name, m.parameters.join(", ")),
        }
    }
}

impl From<InstructionMacroDefinition> for MacroDefinition {
    fn from(item: InstructionMacroDefinition) -> Self {
        MacroDefinition::Instruction(item)
    }
}

impl From<ExpressionMacroDefinition> for MacroDefinition {
    fn from(item: ExpressionMacroDefinition) -> Self {
        MacroDefinition::Expression(item)
    }
}

/// Instruction macro definition op fields.
#[derive(Debug, Default, Clone, Eq, PartialEq)]
pub struct InstructionMacroDefinition {
    /// The name that identifies the macro.
    pub name: String,
    /// The name identifiers for the macro's parameters.
    pub parameters: Vec<String>,
    /// The body of the macro.
    pub contents: Vec<AbstractOp>,
}

/// Instruction macro invocation op.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct InstructionMacroInvocation {
    /// The name of the macro being invoked.
    pub name: String,
    /// The parameters that are being passed into the invocation.
    pub parameters: Vec<Expression>,
}

impl InstructionMacroInvocation {
    /// Construct an instruction macro invocation with zero parameters.
    pub fn with_zero_parameters(name: String) -> Self {
        Self {
            name,
            parameters: vec![],
        }
    }
}

impl fmt::Display for InstructionMacroInvocation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "%{}({})",
            self.name,
            self.parameters
                .iter()
                .map(Expression::to_string)
                .collect::<Vec<String>>()
                .join(", ")
        )
    }
}

/// Expression macro definition op fields.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ExpressionMacroDefinition {
    /// The name that identifies the macro.
    pub name: String,
    /// The name identifiers for the macro's parameters.
    pub parameters: Vec<String>,
    /// The body of the macro.
    pub content: Imm,
}

/// Expression macro invocation imm.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ExpressionMacroInvocation {
    /// The name of the macro being invoked.
    pub name: String,
    /// The parameters that are being passed into the invocation.
    pub parameters: Vec<Expression>,
}

impl fmt::Display for ExpressionMacroInvocation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "{}({})",
            self.name,
            self.parameters
                .iter()
                .map(Expression::to_string)
                .collect::<Vec<String>>()
                .join(", ")
        )
    }
}