xenon_codegen/
while_statement.rs1use core::fmt;
2
3use crate::{expression::Expression, statement::Statement};
4
5#[derive(Debug, Clone)]
6pub struct WhileStatement {
7 pub condition: Expression,
8 pub body: Box<Statement>,
9}
10impl WhileStatement {
11 pub fn new(condition: Expression, body: Statement) -> WhileStatement {
12 WhileStatement {
13 condition,
14 body: Box::new(body),
15 }
16 }
17
18 pub fn is_valid(&self) -> bool {
19 if !self.condition.is_valid() {
20 return false;
21 }
22 if !self.body.is_valid() {
23 return false;
24 }
25 true
26 }
27}
28impl fmt::Display for WhileStatement {
29 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30 match write!(fmt, "while ({}) {}", self.condition, self.body) {
31 Ok(_) => (),
32 Err(e) => return Err(e),
33 }
34 Ok(())
35 }
36}