use std::fmt::{self, Write};
use crate::{Block, Expr, Format, Formatter, Statement, Variable};
use tamacro::DisplayFromFormat;
#[derive(Debug, Clone, DisplayFromFormat)]
pub struct While {
pub cond: Expr,
pub body: Block,
}
impl While {
pub fn new(cond: Expr) -> WhileBuilder {
WhileBuilder::new(cond)
}
}
impl Format for While {
fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
write!(fmt, "while (")?;
self.cond.format(fmt)?;
write!(fmt, ")")?;
fmt.block(|fmt| self.body.format(fmt))?;
writeln!(fmt)
}
}
pub struct WhileBuilder {
cond: Expr,
body: Block,
}
impl WhileBuilder {
pub fn new(cond: Expr) -> Self {
Self {
cond,
body: Block::new().build(),
}
}
pub fn body(mut self, body: Block) -> Self {
self.body = body;
self
}
pub fn statement(mut self, stmt: Statement) -> Self {
self.body.stmts.push(stmt);
self
}
pub fn build(self) -> While {
While {
cond: self.cond,
body: self.body,
}
}
}
#[derive(Debug, Clone, DisplayFromFormat)]
pub struct DoWhile {
pub cond: Expr,
pub body: Block,
}
impl DoWhile {
pub fn new(cond: Expr) -> DoWhileBuilder {
DoWhileBuilder::new(cond)
}
}
impl Format for DoWhile {
fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
write!(fmt, "do")?;
fmt.block(|fmt| self.body.format(fmt))?;
write!(fmt, " while (")?;
self.cond.format(fmt)?;
writeln!(fmt, ");")
}
}
pub struct DoWhileBuilder {
cond: Expr,
body: Block,
}
impl DoWhileBuilder {
pub fn new(cond: Expr) -> Self {
Self {
cond,
body: Block::new().build(),
}
}
pub fn body(mut self, body: Block) -> Self {
self.body = body;
self
}
pub fn statement(mut self, stmt: Statement) -> Self {
self.body.stmts.push(stmt);
self
}
pub fn build(self) -> DoWhile {
DoWhile {
cond: self.cond,
body: self.body,
}
}
}
#[derive(Debug, Clone, DisplayFromFormat)]
pub enum ForInit {
Decl(Variable),
Expr(Expr),
}
impl Format for ForInit {
fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
match self {
ForInit::Decl(v) => v.format(fmt),
ForInit::Expr(e) => e.format(fmt),
}
}
}
#[derive(Debug, Clone, DisplayFromFormat)]
pub struct For {
pub init: Option<ForInit>,
pub cond: Option<Expr>,
pub step: Option<Expr>,
pub body: Block,
}
impl For {
pub fn new() -> ForBuilder {
ForBuilder::new()
}
}
impl Format for For {
fn format(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
write!(fmt, "for (")?;
if let Some(init) = &self.init {
init.format(fmt)?;
}
write!(fmt, ";")?;
if let Some(cond) = &self.cond {
write!(fmt, " ")?;
cond.format(fmt)?;
}
write!(fmt, ";")?;
if let Some(step) = &self.step {
write!(fmt, " ")?;
step.format(fmt)?;
}
write!(fmt, ")")?;
fmt.block(|fmt| self.body.format(fmt))?;
writeln!(fmt)
}
}
pub struct ForBuilder {
init: Option<ForInit>,
cond: Option<Expr>,
step: Option<Expr>,
body: Block,
}
impl Default for ForBuilder {
fn default() -> Self {
Self::new()
}
}
impl ForBuilder {
pub fn new() -> Self {
Self {
init: None,
cond: None,
step: None,
body: Block::new().build(),
}
}
pub fn init(mut self, init: ForInit) -> Self {
self.init = Some(init);
self
}
pub fn init_decl(self, decl: Variable) -> Self {
self.init(ForInit::Decl(decl))
}
pub fn init_expr(self, expr: Expr) -> Self {
self.init(ForInit::Expr(expr))
}
pub fn cond(mut self, cond: Expr) -> Self {
self.cond = Some(cond);
self
}
pub fn step(mut self, step: Expr) -> Self {
self.step = Some(step);
self
}
pub fn body(mut self, body: Block) -> Self {
self.body = body;
self
}
pub fn statement(mut self, stmt: Statement) -> Self {
self.body.stmts.push(stmt);
self
}
pub fn build(self) -> For {
For {
init: self.init,
cond: self.cond,
step: self.step,
body: self.body,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::*;
#[test]
fn while_stmt() {
let w = WhileBuilder::new(Expr::Bool(true))
.body(
Block::new()
.statement(Statement::Return(Some(Expr::Float(1.23))))
.build(),
)
.build();
let res = r#"while (true) {
return 1.23f;
}
"#;
assert_eq!(w.to_string(), res);
}
#[test]
fn do_while() {
let w = DoWhileBuilder::new(Expr::Bool(true)).build();
let res = "do {\n} while (true);\n";
assert_eq!(w.to_string(), res);
}
#[test]
fn for_stmt() {
let f = ForBuilder::new()
.init_decl(
VariableBuilder::new_with_str("i", Type::new(BaseType::Int).build())
.value(Expr::Int(0))
.build(),
)
.cond(Expr::Binary {
left: Box::new(Expr::Ident("i".to_string())),
op: BinOp::LT,
right: Box::new(Expr::Int(10)),
})
.step(Expr::Unary {
op: UnaryOp::Inc,
expr: Box::new(Expr::Ident("i".to_string())),
})
.body(Block::new().statement(Statement::Continue).build())
.build();
let res = r#"for (int i = 0; i < 10; i++) {
continue;
}
"#;
assert_eq!(f.to_string(), res);
}
}