Expr

Enum Expr 

Source
pub enum Expr {
Show 14 variants Literal(Literal), Binary(Binary), Unary(Unary), Variable(Variable), Parenthesized(Parenthesized), Call(CallExpr), Assign(Assign), Vec(VecExpr), Access(AccessExpr), Spread(Spread), Null(Token), StructConstructor(StructConstructor), ThenElse(ThenElse), Object(ObjectExpr),
}
Expand description

Enum representing an expression in the AST. Expressions include literals, binary operations, unary operations, and more.

Variants§

§

Literal(Literal)

A literal value (e.g., number, string).

§

Binary(Binary)

A binary operation (e.g., a + b).

§

Unary(Unary)

A unary operation (e.g., -a).

§

Variable(Variable)

A variable reference.

§

Parenthesized(Parenthesized)

A parenthesized expression to override precedence.

§

Call(CallExpr)

A function call expression.

§

Assign(Assign)

An assignment expression (e.g., a = b).

§

Vec(VecExpr)

A vector (list) of expressions.

§

Access(AccessExpr)

An access expression (e.g., struct.name, arr[0], Person::new).

§

Spread(Spread)

A spread operator for variadic arguments. (e.g., ...args)

§

Null(Token)

Null literal.

§

StructConstructor(StructConstructor)

Struct constructor. (e.g., MyStruct { field: value })

§

ThenElse(ThenElse)

Then-else expression. (e.g., if condition then value else other)

§

Object(ObjectExpr)

Object expression.

Implementations§

Source§

impl Expr

Source

pub fn into_stmt(self) -> Stmt

Converts the expression into a statement.

§Returns

A Stmt::Expr variant containing the expression.

Source

pub fn into_variable(self) -> Variable

Converts the expression into a variable.

§Panics

Panics if the expression is not a Variable variant.

§Returns

The Variable struct contained within the expression.

Source

pub fn new_null(token: Token) -> Expr

Creates a new null literal expression.

Source

pub fn new_field_access(base: Expr, field: Expr, token: Token) -> Expr

Creates a new field access expression.

§Arguments
  • base - The base expression being accessed.
  • field - The name of the field to access.
  • token - The token representing the ‘.’ operator.
§Returns

A new Expr::Access variant with AccessKind::Field.

Source

pub fn new_spread(token: Token, expr: Expr) -> Expr

Create a new spread expression.

§Arguments
  • token - The token representing the spread operator.
  • expr - The expression to spread.
§Returns

A new Expr::Spread variant.

Source

pub fn new_index_access(base: Expr, index: Expr, token: Token) -> Expr

Creates a new index access expression.

§Arguments
  • base - The base expression being accessed.
  • index - The index expression.
  • token - The token representing the ‘[’ and ‘]’ operators.
§Returns

A new Expr::Access variant with AccessKind::Index.

Source

pub fn new_then_else( condition: Expr, then_expr: Expr, else_expr: Expr, then_token: Token, else_token: Token, ) -> Expr

Creates a new then-else expression.

§Arguments
  • condition - The condition expression.
  • then_expr - The expression to evaluate if the condition is true.
  • else_expr - The expression to evaluate if the condition is false.
  • then_token - The token representing the then keyword.
  • else_token - The token representing the else keyword.
§Returns

A new Expr::ThenElse variant.

Source

pub fn new_unary(operator: UnOperator, expr: Expr, token: Token) -> Expr

Creates a new unary expression.

§Arguments
  • operator - The unary operator.
  • expr - The operand expression.
  • token - The token representing the unary operation.
§Returns

A new Expr::Unary variant.

Source

pub fn new_assign(left: Expr, op: AssignOperator, right: Expr) -> Expr

Creates a new assignment expression.

§Arguments
  • ident - The token representing the variable identifier.
  • token - The token representing the assignment operation.
  • value - The expression to assign.
§Returns

A new Expr::Assign variant.

Source

pub fn new_binary(left: Expr, operator: BinOperator, right: Expr) -> Expr

Creates a new binary expression.

§Arguments
  • left - The left operand expression.
  • operator - The binary operator.
  • right - The right operand expression.
§Returns

A new Expr::Binary variant.

Source

pub fn new_integer(token: Token, value: i64) -> Expr

Creates a new integer literal expression.

§Arguments
  • token - The token representing the integer literal.
  • value - The integer value.
§Returns

A new Expr::Literal variant with LiteralType::Int.

Source

pub fn new_float(token: Token, value: f64) -> Expr

Creates a new floating-point literal expression.

§Arguments
  • token - The token representing the float literal.
  • value - The floating-point value.
§Returns

A new Expr::Literal variant with LiteralType::Float.

Source

pub fn new_bool(token: Token, value: bool) -> Expr

Creates a new boolean literal expression.

§Arguments
  • token - The token representing the boolean literal.
  • value - The boolean value.
§Returns

A new Expr::Literal variant with LiteralType::Bool.

Source

pub fn new_variable(ident: Token, name: String) -> Expr

Creates a new variable expression.

§Arguments
  • ident - The token representing the variable identifier.
  • name - The name of the variable.
§Returns

A new Expr::Variable variant.

Source

pub fn new_call(callee: String, args: Vec<Expr>, token: Token) -> Expr

Creates a new function call expression.

§Arguments
  • callee - The name of the function being called.
  • args - The list of argument expressions.
  • token - The token representing the function call.
§Returns

A new Expr::Call variant.

Source

pub fn new_string(token: Token, value: String) -> Expr

Creates a new string literal expression.

§Arguments
  • token - The token representing the string literal.
  • value - The string value.
§Returns

A new Expr::Literal variant with LiteralType::String.

Source

pub fn new_char(token: Token, value: char) -> Expr

Creates a new character literal expression.

§Arguments
  • token - The token representing the character literal.
  • value - The character value.
§Returns

A new Expr::Literal variant with LiteralType::Char.

Source

pub fn new_parenthesized(expr: Expr) -> Expr

Creates a new parenthesized expression.

§Arguments
  • expr - The expression to be parenthesized.
§Returns

A new Expr::Parenthesized variant.

Source

pub fn new_vec(exprs: Vec<Expr>) -> Expr

Creates a new vector expression.

§Arguments
  • exprs - The list of expressions in the vector.
§Returns

A new Expr::Vec variant.

Source

pub fn new_struct_constructor( name: String, fields: Vec<(String, Expr)>, token: Token, ) -> Expr

Creates a new struct constructor expression.

§Arguments
  • name - The name of the struct being constructed.
  • fields - The field values for the struct.
  • token - The token representing the struct constructor.
§Returns

A new Expr::StructConstructor variant.

Source

pub fn new_static_method_access(base: Expr, method: Expr, token: Token) -> Expr

Creates a new static method access expression.

§Arguments
  • base - The base expression being accessed.
  • method - The method expression.
  • token - The token representing the ‘::’ operator.
§Returns

A new Expr::Access variant with AccessKind::StaticMethod.

Source

pub fn new_object( fields: IndexMap<String, Expr>, braces: (Token, Token), ) -> Expr

Creates a new object expression.

§Arguments
  • fields - The key-value pairs in the object.
  • braces - The tokens representing the opening and closing braces.
§Returns

A new Expr::Object variant.

Trait Implementations§

Source§

impl Clone for Expr

Source§

fn clone(&self) -> Expr

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<Expr> for Stmt

Source§

fn from(expr: Expr) -> Stmt

Converts an Expr into a Stmt::Expr.

§Arguments
  • expr - The expression to convert into a statement.
§Returns

A Stmt::Expr variant containing the provided expression.

Source§

impl GetSpan for Expr

Source§

fn span(&self) -> TextSpan

Returns the TextSpan associated with the expression in the source code.

Source§

impl PartialEq for Expr

Source§

fn eq(&self, other: &Expr) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for Expr

Auto Trait Implementations§

§

impl Freeze for Expr

§

impl RefUnwindSafe for Expr

§

impl Send for Expr

§

impl Sync for Expr

§

impl Unpin for Expr

§

impl UnwindSafe for Expr

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

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

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more