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>

source

pub fn build(&mut self) -> GeneratedFunction

source

pub fn declare(&mut self, name: impl Into<String>, ty: JITType) -> Result<()>

Declare variable name of a type.

source

pub fn assign(&mut self, name: impl Into<String>, expr: Expr) -> Result<()>

Assignment statement. Assign a expression value to a variable.

source

pub fn declare_as(&mut self, name: impl Into<String>, expr: Expr) -> Result<()>

Declare variable with initialization.

source

pub fn call_stmt( &mut self, name: impl Into<String>, args: Vec<Expr> ) -> Result<()>

Call external function for side effect only.

source

pub fn if_block<C, T, E>( &mut self, cond: C, then_blk: T, else_blk: E ) -> Result<()>where C: FnMut(&mut CodeBlock<'_>) -> Result<Expr>, T: FnMut(&mut CodeBlock<'_>) -> Result<()>, E: FnMut(&mut CodeBlock<'_>) -> 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(()) }, )?;

source

pub fn while_block<C, B>(&mut self, cond: C, body_blk: B) -> Result<()>where C: FnMut(&mut CodeBlock<'_>) -> Result<Expr>, B: FnMut(&mut CodeBlock<'_>) -> 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(()) }, )?;

source

pub fn lit(&self, val: impl Into<String>, ty: JITType) -> Expr

Create a literal val of ty type.

source

pub fn lit_i(&self, val: impl Into<i64>) -> Expr

Shorthand to create i64 literal

source

pub fn lit_f(&self, val: f32) -> Expr

Shorthand to create f32 literal

source

pub fn lit_d(&self, val: f64) -> Expr

Shorthand to create f64 literal

source

pub fn lit_b(&self, val: bool) -> Expr

Shorthand to create boolean literal

source

pub fn id(&self, name: impl Into<String>) -> Result<Expr>

Create a reference to an already defined variable.

source

pub fn eq(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary comparison expression: lhs == rhs

source

pub fn ne(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary comparison expression: lhs != rhs

source

pub fn lt(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary comparison expression: lhs < rhs

source

pub fn le(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary comparison expression: lhs <= rhs

source

pub fn gt(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary comparison expression: lhs > rhs

source

pub fn ge(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary comparison expression: lhs >= rhs

source

pub fn add(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary arithmetic expression: lhs + rhs

source

pub fn sub(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary arithmetic expression: lhs - rhs

source

pub fn mul(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary arithmetic expression: lhs * rhs

source

pub fn div(&self, lhs: Expr, rhs: Expr) -> Result<Expr>

Binary arithmetic expression: lhs / rhs

source

pub fn call(&self, name: impl Into<String>, params: Vec<Expr>) -> Result<Expr>

Call external function name with parameters

source

pub fn load(&self, ptr: Expr, ty: JITType) -> Result<Expr>

Return the value pointed to by the ptr stored in ptr

source

pub fn store(&mut self, value: Expr, ptr: Expr) -> Result<()>

Store the value in value to the address in ptr

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for CodeBlock<'a>

§

impl<'a> !Send for CodeBlock<'a>

§

impl<'a> !Sync for CodeBlock<'a>

§

impl<'a> Unpin for CodeBlock<'a>

§

impl<'a> !UnwindSafe for CodeBlock<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.