use crate::{Block, Expression, Identifier, Indent, Node, NodeID, Statement, Type};
use leo_span::Span;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct IterationStatement {
pub variable: Identifier,
pub type_: Option<Type>,
pub start: Expression,
pub stop: Expression,
pub inclusive: bool,
pub block: Block,
pub span: Span,
pub id: NodeID,
}
impl fmt::Display for IterationStatement {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let eq = if self.inclusive { "=" } else { "" };
if let Some(ty) = &self.type_ {
writeln!(f, "for {}: {} in {}..{eq}{} {{", self.variable, ty, self.start, self.stop)?;
} else {
writeln!(f, "for {} in {}..{eq}{} {{", self.variable, self.start, self.stop)?;
}
for stmt in self.block.statements.iter() {
writeln!(f, "{}{}", Indent(stmt), stmt.semicolon())?;
}
writeln!(f, "}}")
}
}
impl From<IterationStatement> for Statement {
fn from(value: IterationStatement) -> Self {
Statement::Iteration(Box::new(value))
}
}
crate::simple_node_impl!(IterationStatement);