aztec-nr - std

Primitive type Expr

Implementations

impl Expr

pub comptime fn as_array(self) -> Option<[Self]>

If this expression is an array literal [elem1, ..., elemN], this returns a slice of each element in the array.

pub comptime fn as_assert(self) -> Option<(Self, Option<Self>)>

If this expression is an assert, this returns the assert expression and the optional message.

pub comptime fn as_assert_eq(self) -> Option<(Self, Self, Option<Self>)>

If this expression is an assert_eq, this returns the left-hand-side and right-hand-side expressions, together with the optional message.

pub comptime fn as_assign(self) -> Option<(Self, Self)>

If this expression is an assignment, this returns a tuple with the left hand side and right hand side in order.

pub comptime fn as_binary_op(self) -> Option<(Self, BinaryOp, Self)>

If this expression is a binary operator operation <lhs> <op> <rhs>, return the left-hand side, operator, and the right-hand side of the operation.

pub comptime fn as_block(self) -> Option<[Self]>

If this expression is a block { stmt1; stmt2; ...; stmtN }, return a slice containing each statement.

pub comptime fn as_bool(self) -> Option<bool>

If this expression is a boolean literal, return that literal.

pub comptime fn as_cast(self) -> Option<(Self, UnresolvedType)>

If this expression is a cast expression expr as type, returns the casted expression and the type to cast to.

pub comptime fn as_comptime(self) -> Option<[Self]>

If this expression is a comptime { stmt1; stmt2; ...; stmtN } block, return each statement in the block.

pub comptime fn as_constructor(self) -> Option<(UnresolvedType, [(Quoted, Self)])>

If this expression is a constructor Type { field1: expr1, ..., fieldN: exprN }, return the type and the fields.

pub comptime fn as_for(self) -> Option<(Quoted, Self, Self)>

If this expression is a for statement over a single expression, return the identifier, the expression and the for loop body.

pub comptime fn as_for_range(self) -> Option<(Quoted, Self, Self, Self)>

If this expression is a for statement over a range, return the identifier, the range start, the range end and the for loop body.

pub comptime fn as_function_call(self) -> Option<(Self, [Self])>

If this expression is a function call foo(arg1, ..., argN), return the function and a slice of each argument.

pub comptime fn as_if(self) -> Option<(Self, Self, Option<Self>)>

If this expression is an if condition { then_branch } else { else_branch }, return the condition, then branch, and else branch. If there is no else branch, None is returned for that branch instead.

pub comptime fn as_index(self) -> Option<(Self, Self)>

If this expression is an index into an array array[index], return the array and the index.

pub comptime fn as_integer(self) -> Option<(Field, bool)>

If this expression is an integer literal, return the integer as a field as well as whether the integer is negative (true) or not (false).

pub comptime fn as_lambda( self, ) -> Option<([(Self, Option<UnresolvedType>)], Option<UnresolvedType>, Self)>

If this expression is a lambda, returns the parameters, return type and body.

pub comptime fn as_let(self) -> Option<(Self, Option<UnresolvedType>, Self)>

If this expression is a let statement, returns the let pattern as an Expr, the optional type annotation, and the assigned expression.

pub comptime fn as_member_access(self) -> Option<(Self, Quoted)>

If this expression is a member access foo.bar, return the struct/tuple expression and the field. The field will be represented as a quoted value.

pub comptime fn as_method_call(self) -> Option<(Self, Quoted, [UnresolvedType], [Self])>

If this expression is a method call foo.bar::<generic1, ..., genericM>(arg1, ..., argN), return the receiver, method name, a slice of each generic argument, and a slice of each argument.

pub comptime fn as_repeated_element_array(self) -> Option<(Self, Self)>

If this expression is a repeated element array [elem; length], return the repeated element and the length expressions.

pub comptime fn as_repeated_element_slice(self) -> Option<(Self, Self)>

If this expression is a repeated element slice [elem; length], return the repeated element and the length expressions.

pub comptime fn as_slice(self) -> Option<[Self]>

If this expression is a slice literal &[elem1, ..., elemN], return each element of the slice.

pub comptime fn as_tuple(self) -> Option<[Self]>

If this expression is a tuple (field1, ..., fieldN), return each element of the tuple.

pub comptime fn as_unary_op(self) -> Option<(UnaryOp, Self)>

If this expression is a unary operation <op> <rhs>, return the unary operator as well as the right-hand side expression.

pub comptime fn as_unsafe(self) -> Option<[Self]>

If this expression is an unsafe { stmt1; ...; stmtN } block, return each statement inside in a slice.

pub comptime fn has_semicolon(self) -> bool

Returns true if this expression is trailed by a semicolon.

Example:

comptime {
    let expr1 = quote { 1 + 2 }.as_expr().unwrap();
    let expr2 = quote { 1 + 2; }.as_expr().unwrap();

    assert(expr1.as_binary_op().is_some());
    assert(expr2.as_binary_op().is_some());

    assert(!expr1.has_semicolon());
    assert(expr2.has_semicolon());
}
pub comptime fn is_break(self) -> bool

Returns true if this expression is break.

pub comptime fn is_continue(self) -> bool

Returns true if this expression is continue.

pub comptime fn modify<Env>(self, f: fn[Env](Self) -> Option<Self>) -> Self

Applies a mapping function to this expression and to all of its sub-expressions. f will be applied to each sub-expression first, then applied to the expression itself.

This happens recursively for every expression within self.

For example, calling modify on (&[1], &[2, 3]) with an f that returns Option::some for expressions that are integers, doubling them, would return (&[2], &[4, 6]).

pub comptime fn quoted(self) -> Quoted

Returns this expression as a Quoted value. It's the same as quote { $self }.

pub comptime fn resolve(self, in_function: Option<FunctionDefinition>) -> TypedExpr

Resolves and type-checks this expression and returns the result as a TypedExpr.

The in_function argument specifies where the expression is resolved:

  • If it's none, the expression is resolved in the function where resolve was called
  • If it's some, the expression is resolved in the given function

If any names used by this expression are not in scope or if there are any type errors, this will give compiler errors as if the expression was written directly into the current comptime function.