pub struct CodeBlock<'a> { /* private fields */ }
Expand description
Code block consists of statements and acts as anonymous namespace scope for items and variable declarations.
Implementations§
Source§impl<'a> CodeBlock<'a>
impl<'a> CodeBlock<'a>
pub fn build(&mut self) -> GeneratedFunction
Sourcepub fn declare(&mut self, name: impl Into<String>, ty: JITType) -> Result<()>
pub fn declare(&mut self, name: impl Into<String>, ty: JITType) -> Result<()>
Declare variable name
of a type.
Sourcepub fn assign(&mut self, name: impl Into<String>, expr: Expr) -> Result<()>
pub fn assign(&mut self, name: impl Into<String>, expr: Expr) -> Result<()>
Assignment statement. Assign a expression value to a variable.
Sourcepub fn declare_as(&mut self, name: impl Into<String>, expr: Expr) -> Result<()>
pub fn declare_as(&mut self, name: impl Into<String>, expr: Expr) -> Result<()>
Declare variable with initialization.
Sourcepub fn call_stmt(
&mut self,
name: impl Into<String>,
args: Vec<Expr>,
) -> Result<()>
pub fn call_stmt( &mut self, name: impl Into<String>, args: Vec<Expr>, ) -> Result<()>
Call external function for side effect only.
Sourcepub fn if_block<C, T, E>(
&mut self,
cond: C,
then_blk: T,
else_blk: E,
) -> Result<()>
pub fn if_block<C, T, E>( &mut self, cond: C, then_blk: T, else_blk: E, ) -> Result<()>
Construct a if-then-else
block with each part provided.
E.g. if n == 0 { r = 0 } else { r = 1} could be write as: x.if_block( |cond| cond.eq(cond.id(“n”)?, cond.lit_i(0)), |t| { t.assign(“r”, t.lit_i(0))?; Ok(()) }, |e| t.assign(“r”, t.lit_i(1))?; Ok(()) }, )?;
Sourcepub fn while_block<C, B>(&mut self, cond: C, body_blk: B) -> Result<()>
pub fn while_block<C, B>(&mut self, cond: C, body_blk: B) -> Result<()>
Construct a while
block with each part provided.
E.g. while n != 0 { n = n - 1;} could be write as: x.while_block( |cond| cond.ne(cond.id(“n”)?, cond.lit_i(0)), |w| { w.assign(“n”, w.sub(w.id(“n”)?, w.lit_i(1))?)?; Ok(()) }, )?;
Sourcepub fn lit(&self, val: impl Into<String>, ty: JITType) -> Expr
pub fn lit(&self, val: impl Into<String>, ty: JITType) -> Expr
Create a literal val
of ty
type.
Sourcepub fn id(&self, name: impl Into<String>) -> Result<Expr>
pub fn id(&self, name: impl Into<String>) -> Result<Expr>
Create a reference to an already defined variable.
Sourcepub fn eq(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn eq(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary comparison expression: lhs == rhs
Sourcepub fn ne(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn ne(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary comparison expression: lhs != rhs
Sourcepub fn le(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn le(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary comparison expression: lhs <= rhs
Sourcepub fn ge(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn ge(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary comparison expression: lhs >= rhs
Sourcepub fn add(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn add(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary arithmetic expression: lhs + rhs
Sourcepub fn sub(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn sub(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary arithmetic expression: lhs - rhs
Sourcepub fn mul(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn mul(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary arithmetic expression: lhs * rhs
Sourcepub fn div(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
pub fn div(&self, lhs: Expr, rhs: Expr) -> Result<Expr>
Binary arithmetic expression: lhs / rhs
Sourcepub fn call(&self, name: impl Into<String>, params: Vec<Expr>) -> Result<Expr>
pub fn call(&self, name: impl Into<String>, params: Vec<Expr>) -> Result<Expr>
Call external function name
with parameters