use super::control_flow::ControlFlow;
use super::expression::Expr;
#[derive(Debug, Clone, PartialEq)]
pub enum Stmt {
Assign { target: String, value: Expr },
Return(Option<Expr>),
ExprStmt(Expr),
Comment(String),
ControlFlow(Box<ControlFlow>),
}
impl Stmt {
pub fn assign(target: impl Into<String>, value: Expr) -> Self {
Stmt::Assign {
target: target.into(),
value,
}
}
#[must_use]
pub fn ret(value: Expr) -> Self {
Stmt::Return(Some(value))
}
#[must_use]
pub fn ret_void() -> Self {
Stmt::Return(None)
}
#[must_use]
pub fn expr(e: Expr) -> Self {
Stmt::ExprStmt(e)
}
pub fn comment(text: impl Into<String>) -> Self {
Stmt::Comment(text.into())
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Block {
pub stmts: Vec<Stmt>,
}
impl Block {
#[must_use]
pub fn new() -> Self {
Self { stmts: Vec::new() }
}
#[must_use]
pub fn with_stmts(stmts: Vec<Stmt>) -> Self {
Self { stmts }
}
pub fn push(&mut self, stmt: Stmt) {
self.stmts.push(stmt);
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.stmts.is_empty()
}
#[must_use]
pub fn len(&self) -> usize {
self.stmts.len()
}
}
impl From<Vec<Stmt>> for Block {
fn from(stmts: Vec<Stmt>) -> Self {
Self { stmts }
}
}