xenon-codegen 0.1.0

Codegen and AST for the Xenon programming language
Documentation
use core::fmt;

use crate::{case::Case, expression::Expression};

#[derive(Debug, Clone, Default)]
pub struct SwitchStatement {
    pub condition: Expression,
    pub cases: Vec<Case>,
}
impl SwitchStatement {
    pub fn new(condition: Expression) -> SwitchStatement {
        SwitchStatement {
            condition,
            cases: vec![],
        }
    }

    pub fn is_valid(&self) -> bool {
        if !self.condition.is_valid() {
            return false;
        }

        for i in 0..self.cases.len() {
            if !self.cases[i].is_valid() {
                return false;
            }
        }
        true
    }
}
impl fmt::Display for SwitchStatement {
    fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match writeln!(fmt, "switch ({}) {{", self.condition) {
            Ok(_) => (),
            Err(e) => return Err(e),
        }
        for i in 0..self.cases.len() {
            match write!(fmt, "{}", self.cases[i]) {
                Ok(_) => (),
                Err(e) => return Err(e),
            }
        }
        match write!(fmt, "}}") {
            Ok(_) => (),
            Err(e) => return Err(e),
        }

        Ok(())
    }
}