miden-assembly-syntax 0.22.1

Parsing and semantic analysis of the Miden Assembly language
Documentation
use core::ops::ControlFlow;

use miden_debug_types::Spanned;

use crate::{
    MAX_REPEAT_COUNT,
    ast::{Immediate, Op, Visit, visit::visit_op},
    sema::{AnalysisContext, SemanticAnalysisError},
};

pub struct VerifyRepeatCounts<'a> {
    analyzer: &'a mut AnalysisContext,
}

impl<'a> VerifyRepeatCounts<'a> {
    pub fn new(analyzer: &'a mut AnalysisContext) -> Self {
        Self { analyzer }
    }
}

impl Visit for VerifyRepeatCounts<'_> {
    fn visit_op(&mut self, op: &Op) -> ControlFlow<()> {
        if let Op::Repeat { count, .. } = op
            && let Immediate::Value(value) = count
        {
            let repeat_count = value.into_inner();
            if repeat_count == 0 || repeat_count > MAX_REPEAT_COUNT {
                self.analyzer.error(SemanticAnalysisError::InvalidRepeatCount {
                    span: count.span(),
                    min: 1,
                    max: MAX_REPEAT_COUNT,
                });
            }
        }

        visit_op(self, op)
    }
}