vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
use super::Node;
use crate::ir::model::expr::Expr;

impl Node {
    /// `let name = value;`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::{Expr, Node};
    /// let _ = Node::let_bind("x", Expr::u32(1));
    /// ```
    #[must_use]
    #[inline]
    pub fn let_bind(name: impl Into<String>, value: Expr) -> Self {
        Self::Let {
            name: name.into(),
            value,
        }
    }

    /// `name = value;`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::{Expr, Node};
    /// let _ = Node::assign("x", Expr::u32(2));
    /// ```
    #[must_use]
    #[inline]
    pub fn assign(name: &str, value: Expr) -> Self {
        Self::Assign {
            name: name.to_string(),
            value,
        }
    }

    /// `buffer[index] = value;`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::{Expr, Node};
    /// let _ = Node::store("out", Expr::u32(0), Expr::u32(1));
    /// ```
    #[must_use]
    #[inline]
    pub fn store(buffer: &str, index: Expr, value: Expr) -> Self {
        Self::Store {
            buffer: buffer.to_string(),
            index,
            value,
        }
    }

    /// `if cond { then } else { otherwise }`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::{Expr, Node};
    /// let _ = Node::if_then_else(Expr::bool(true), vec![Node::Return], vec![]);
    /// ```
    #[must_use]
    #[inline]
    pub fn if_then_else(cond: Expr, then: Vec<Self>, otherwise: Vec<Self>) -> Self {
        Self::If {
            cond,
            then,
            otherwise,
        }
    }

    /// `if cond { then }`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::{Expr, Node};
    /// let _ = Node::if_then(Expr::bool(true), vec![Node::Return]);
    /// ```
    #[must_use]
    #[inline]
    pub fn if_then(cond: Expr, then: Vec<Self>) -> Self {
        Self::If {
            cond,
            then,
            otherwise: Vec::new(),
        }
    }

    /// `for var in from..to { body }`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::{Expr, Node};
    /// let _ = Node::loop_for("i", Expr::u32(0), Expr::u32(4), vec![]);
    /// ```
    #[must_use]
    #[inline]
    pub fn loop_for(var: &str, from: Expr, to: Expr, body: Vec<Self>) -> Self {
        Self::Loop {
            var: var.to_string(),
            from,
            to,
            body,
        }
    }

    /// `for var in from..to { body }`
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::{Expr, Node};
    ///
    /// let node = Node::loop_("i", Expr::u32(0), Expr::u32(4), vec![Node::Return]);
    /// assert!(matches!(node, Node::Loop { .. }));
    /// ```
    #[must_use]
    #[inline]
    pub fn loop_(var: &str, from: Expr, to: Expr, body: Vec<Self>) -> Self {
        Self::loop_for(var, from, to, body)
    }

    /// Sequence of statements.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Node;
    ///
    /// assert!(matches!(Node::block(vec![Node::Return]), Node::Block(_)));
    /// ```
    #[must_use]
    #[inline]
    pub fn block(nodes: Vec<Self>) -> Self {
        Self::Block(nodes)
    }

    /// Early return from the entry point.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Node;
    ///
    /// assert!(matches!(Node::return_(), Node::Return));
    /// ```
    #[must_use]
    pub const fn return_() -> Self {
        Self::Return
    }

    /// Workgroup barrier statement.
    ///
    /// # Examples
    ///
    /// ```
    /// use vyre::ir::Node;
    ///
    /// assert!(matches!(Node::barrier(), Node::Barrier));
    /// ```
    #[must_use]
    pub const fn barrier() -> Self {
        Self::Barrier
    }
}