Skip to main content

miden_assembly_syntax/sema/passes/
verify_repeat.rs

1use core::ops::ControlFlow;
2
3use miden_debug_types::Spanned;
4
5use crate::{
6    MAX_REPEAT_COUNT,
7    ast::{Immediate, Op, Visit, visit::visit_op},
8    sema::{AnalysisContext, SemanticAnalysisError},
9};
10
11pub struct VerifyRepeatCounts<'a> {
12    analyzer: &'a mut AnalysisContext,
13}
14
15impl<'a> VerifyRepeatCounts<'a> {
16    pub fn new(analyzer: &'a mut AnalysisContext) -> Self {
17        Self { analyzer }
18    }
19}
20
21impl Visit for VerifyRepeatCounts<'_> {
22    fn visit_op(&mut self, op: &Op) -> ControlFlow<()> {
23        if let Op::Repeat { count, .. } = op
24            && let Immediate::Value(value) = count
25        {
26            let repeat_count = value.into_inner();
27            if repeat_count == 0 || repeat_count > MAX_REPEAT_COUNT {
28                self.analyzer.error(SemanticAnalysisError::InvalidRepeatCount {
29                    span: count.span(),
30                    min: 1,
31                    max: MAX_REPEAT_COUNT,
32                });
33            }
34        }
35
36        visit_op(self, op)
37    }
38}