miden_assembly_syntax/ast/
op.rs1use core::fmt;
2
3use miden_debug_types::{SourceSpan, Span, Spanned};
4
5use super::{Block, Immediate, Instruction};
6
7#[derive(Clone)]
12#[repr(u8)]
13pub enum Op {
14 If {
18 span: SourceSpan,
19 then_blk: Block,
21 else_blk: Block,
23 } = 0,
24 While { span: SourceSpan, body: Block } = 1,
26 Repeat {
32 span: SourceSpan,
33 count: Immediate<u32>,
34 body: Block,
35 } = 2,
36 Inst(Span<Instruction>) = 3,
38}
39
40impl crate::prettier::PrettyPrint for Op {
41 fn render(&self) -> crate::prettier::Document {
42 use crate::prettier::*;
43
44 match self {
45 Self::If { then_blk, else_blk, .. } => {
46 text("if.true") + then_blk.render() + text("else") + else_blk.render() + text("end")
47 },
48 Self::While { body, .. } => text("while.true") + body.render() + text("end"),
49 Self::Repeat { count, body, .. } => {
50 display(format!("repeat.{count}")) + body.render() + text("end")
51 },
52 Self::Inst(inst) => inst.render(),
53 }
54 }
55}
56
57impl fmt::Debug for Op {
58 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
59 match self {
60 Self::If { then_blk, else_blk, .. } => {
61 f.debug_struct("If").field("then", then_blk).field("else", else_blk).finish()
62 },
63 Self::While { body, .. } => f.debug_tuple("While").field(body).finish(),
64 Self::Repeat { count, body, .. } => {
65 f.debug_struct("Repeat").field("count", count).field("body", body).finish()
66 },
67 Self::Inst(inst) => fmt::Debug::fmt(&**inst, f),
68 }
69 }
70}
71
72impl Eq for Op {}
73
74impl PartialEq for Op {
75 fn eq(&self, other: &Self) -> bool {
76 match (self, other) {
77 (
78 Self::If { then_blk: lt, else_blk: le, .. },
79 Self::If { then_blk: rt, else_blk: re, .. },
80 ) => lt == rt && le == re,
81 (Self::While { body: lbody, .. }, Self::While { body: rbody, .. }) => lbody == rbody,
82 (
83 Self::Repeat { count: lcount, body: lbody, .. },
84 Self::Repeat { count: rcount, body: rbody, .. },
85 ) => lcount == rcount && lbody == rbody,
86 (Self::Inst(l), Self::Inst(r)) => l == r,
87 _ => false,
88 }
89 }
90}
91
92impl Spanned for Op {
93 fn span(&self) -> SourceSpan {
94 match self {
95 Self::If { span, .. } | Self::While { span, .. } | Self::Repeat { span, .. } => *span,
96 Self::Inst(spanned) => spanned.span(),
97 }
98 }
99}