miden_assembly/ast/
block.rs1use alloc::vec::Vec;
2use core::fmt;
3
4use super::Op;
5use crate::{SourceSpan, Spanned};
6
7#[derive(Clone, Default)]
14pub struct Block {
15 span: SourceSpan,
16 body: Vec<Op>,
17}
18
19impl Block {
20 pub fn new(span: SourceSpan, body: Vec<Op>) -> Self {
22 Self { span, body }
23 }
24
25 pub fn push(&mut self, op: Op) {
27 self.body.push(op);
28 }
29
30 pub fn len(&self) -> usize {
35 self.body.len()
36 }
37
38 pub fn is_empty(&self) -> bool {
40 self.body.is_empty()
41 }
42
43 pub fn iter(&self) -> core::slice::Iter<'_, Op> {
45 self.body.iter()
46 }
47
48 pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, Op> {
50 self.body.iter_mut()
51 }
52}
53
54impl fmt::Debug for Block {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 f.debug_list().entries(&self.body).finish()
57 }
58}
59
60impl crate::prettier::PrettyPrint for Block {
61 fn render(&self) -> crate::prettier::Document {
62 use crate::{Span, ast::Instruction, prettier::*};
63
64 let default_body = [Op::Inst(Span::new(self.span, Instruction::Nop))];
66 let body = match self.body.as_slice() {
67 [] => default_body.as_slice().iter(),
68 body => body.iter(),
69 }
70 .map(PrettyPrint::render)
71 .reduce(|acc, doc| acc + nl() + doc);
72
73 body.map(|body| indent(4, nl() + body)).unwrap_or(Document::Empty)
74 }
75}
76
77impl Spanned for Block {
78 fn span(&self) -> SourceSpan {
79 self.span
80 }
81}
82
83impl Eq for Block {}
84
85impl PartialEq for Block {
86 fn eq(&self, other: &Self) -> bool {
87 self.body == other.body
88 }
89}