miden_assembly_syntax/ast/
op.rs

1use core::fmt;
2
3use miden_debug_types::{SourceSpan, Span, Spanned};
4
5use super::{Block, Instruction};
6
7/// Represents the Miden Assembly instruction set syntax
8///
9/// This is separate from [Instruction] in order to distinguish control flow instructions and
10/// instructions with block regions from the rest.
11#[derive(Clone)]
12#[repr(u8)]
13pub enum Op {
14    /// Represents a conditional branch
15    ///
16    /// Can be either `if`..`end`, or `if`..`else`..`end`.
17    If {
18        span: SourceSpan,
19        /// This block is always present and non-empty
20        then_blk: Block,
21        /// This block will be empty if no `else` branch was present
22        else_blk: Block,
23    } = 0,
24    /// Represents a condition-controlled loop
25    While { span: SourceSpan, body: Block } = 1,
26    /// Represents a counter-controlled loop.
27    ///
28    /// NOTE: The iteration count must be known at compile-time, so this is _not_ used for general
29    /// `for`-style loops where the iteration count is dynamic.
30    Repeat {
31        span: SourceSpan,
32        count: u32,
33        body: Block,
34    } = 2,
35    /// A primitive operation, e.g. `add`
36    Inst(Span<Instruction>) = 3,
37}
38
39impl crate::prettier::PrettyPrint for Op {
40    fn render(&self) -> crate::prettier::Document {
41        use crate::prettier::*;
42
43        match self {
44            Self::If { then_blk, else_blk, .. } => {
45                text("if.true") + then_blk.render() + text("else") + else_blk.render() + text("end")
46            },
47            Self::While { body, .. } => text("while.true") + body.render() + text("end"),
48            Self::Repeat { count, body, .. } => {
49                display(format!("repeat.{count}")) + body.render() + text("end")
50            },
51            Self::Inst(inst) => inst.render(),
52        }
53    }
54}
55
56impl fmt::Debug for Op {
57    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58        match self {
59            Self::If { then_blk, else_blk, .. } => {
60                f.debug_struct("If").field("then", then_blk).field("else", else_blk).finish()
61            },
62            Self::While { body, .. } => f.debug_tuple("While").field(body).finish(),
63            Self::Repeat { count, body, .. } => {
64                f.debug_struct("Repeat").field("count", count).field("body", body).finish()
65            },
66            Self::Inst(inst) => fmt::Debug::fmt(&**inst, f),
67        }
68    }
69}
70
71impl Eq for Op {}
72
73impl PartialEq for Op {
74    fn eq(&self, other: &Self) -> bool {
75        match (self, other) {
76            (
77                Self::If { then_blk: lt, else_blk: le, .. },
78                Self::If { then_blk: rt, else_blk: re, .. },
79            ) => lt == rt && le == re,
80            (Self::While { body: lbody, .. }, Self::While { body: rbody, .. }) => lbody == rbody,
81            (
82                Self::Repeat { count: lcount, body: lbody, .. },
83                Self::Repeat { count: rcount, body: rbody, .. },
84            ) => lcount == rcount && lbody == rbody,
85            (Self::Inst(l), Self::Inst(r)) => l == r,
86            _ => false,
87        }
88    }
89}
90
91impl Spanned for Op {
92    fn span(&self) -> SourceSpan {
93        match self {
94            Self::If { span, .. } | Self::While { span, .. } | Self::Repeat { span, .. } => *span,
95            Self::Inst(spanned) => spanned.span(),
96        }
97    }
98}