/ compiler / ast / src / expressions / repeat.rs
repeat.rs
 1  // Copyright (C) 2019-2025 ADnet Contributors
 2  // This file is part of the ADL library.
 3  
 4  // The ADL library is free software: you can redistribute it and/or modify
 5  // it under the terms of the GNU General Public License as published by
 6  // the Free Software Foundation, either version 3 of the License, or
 7  // (at your option) any later version.
 8  
 9  // The ADL library is distributed in the hope that it will be useful,
10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  // GNU General Public License for more details.
13  
14  // You should have received a copy of the GNU General Public License
15  // along with the ADL library. If not, see <https://www.gnu.org/licenses/>.
16  
17  use super::*;
18  
19  /// An array expression constructed from one repeated element.
20  ///
21  /// E.g., `[1u32; 5]`. Expression `expr` is the element to be repeated; Expression `count` is the number of times to
22  /// repeat it.
23  #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24  pub struct RepeatExpression {
25      /// The element to repeat.
26      pub expr: Expression,
27      /// The number of times to repeat it.
28      pub count: Expression,
29      /// The span from `[` to `]`.
30      pub span: Span,
31      /// The ID of the node.
32      pub id: NodeID,
33  }
34  
35  impl fmt::Display for RepeatExpression {
36      fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37          write!(f, "[{}; {}]", self.expr, self.count)
38      }
39  }
40  
41  impl From<RepeatExpression> for Expression {
42      fn from(value: RepeatExpression) -> Self {
43          Expression::Repeat(Box::new(value))
44      }
45  }
46  
47  crate::simple_node_impl!(RepeatExpression);