pub enum Expr {
Show 25 variants
Int {
value: i64,
span: Span,
},
Float {
value: f64,
span: Span,
},
Byte {
value: u8,
span: Span,
},
Str {
value: String,
span: Span,
},
Bool {
value: bool,
span: Span,
},
Ident {
name: String,
span: Span,
},
Paren {
inner: Box<Expr>,
span: Span,
},
Tuple {
elems: Vec<Expr>,
span: Span,
},
ArrayLit {
elems: Vec<Expr>,
span: Span,
},
ArrayRepeat {
value: Box<Expr>,
count: Box<Expr>,
span: Span,
},
StructLit {
name: String,
fields: Vec<FieldInit>,
span: Span,
},
FieldAccess {
obj: Box<Expr>,
name: String,
span: Span,
},
MethodCall {
receiver: Box<Expr>,
name: String,
args: Vec<Expr>,
span: Span,
},
Call {
callee: Box<Expr>,
args: Vec<Expr>,
span: Span,
},
Index {
obj: Box<Expr>,
index: Box<Expr>,
span: Span,
},
Try {
expr: Box<Expr>,
span: Span,
},
Unary {
op: UnaryOp,
operand: Box<Expr>,
span: Span,
},
Binary {
op: BinOp,
lhs: Box<Expr>,
rhs: Box<Expr>,
span: Span,
},
Range {
start: Option<Box<Expr>>,
end: Option<Box<Expr>>,
inclusive: bool,
span: Span,
},
Pipeline {
lhs: Box<Expr>,
call: Box<Expr>,
span: Span,
},
Comptime {
body: Box<Expr>,
span: Span,
},
Block {
block: Block,
span: Span,
},
Match {
scrutinee: Box<Expr>,
arms: Vec<MatchArm>,
span: Span,
},
OrElse {
expr: Box<Expr>,
fallback: Box<Expr>,
span: Span,
},
Interpolation {
parts: Vec<InterpPart>,
span: Span,
},
}Expand description
an expression. literals, identifiers, parenthesized and tuple expressions,
array literals (including the [v; n] repeat form), struct literals, field
access, method calls, calls, indexing, ? propagation, unary and binary
operators, ranges, the |> pipeline, comptime, block expressions, match,
the or fallback, and string interpolation. there is deliberately no If
variant – if/else is a Stmt in v1.
nothing is desugared: Expr::Pipeline stays a pipeline (not f(x, a)),
Expr::Interpolation stays a parts list (not a + chain), Expr::Match
and Expr::Block produce values directly. that faithfulness is what lets
Phase 3 diagnostics point at the source as written; lowering happens in
Phase 4 codegen.
Variants§
Int
an integer literal (already decoded; a leading - is a separate
Expr::Unary).
Float
a float literal (already decoded).
Byte
a byte literal b'X', the byte it denotes.
Str
a string literal with no interpolation, escapes already decoded.
Bool
a boolean literal, true or false.
Ident
an identifier reference (a variable, a function name, a type name in value position – the parser does not distinguish; Phase 3 resolves it).
Paren
( inner ) – a parenthesized expression. kept as a node so the span and
the parentheses are visible to diagnostics; semantically transparent.
Tuple
( e1, e2, ... ) – a tuple. a single element with a trailing comma is a
one-tuple; without the comma it is just a Expr::Paren.
ArrayLit
[ e1, e2, ... ] – an array literal listing its elements.
ArrayRepeat
[ value; count ] – an array literal repeating value count times.
StructLit
Name { field: e, ... } – a struct literal. name is the struct’s
name; whether it names a real struct is Phase 3’s call.
FieldAccess
obj.name – field access. distinct from Expr::MethodCall: this is
the form with no ( ... ) after the .name.
MethodCall
receiver.name(args) – a method call. distinct from a field access
followed by a call; Phase 3 resolves it to fn Type.name(self, ...).
Call
callee(args) – a call expression (callee is usually an
Expr::Ident, but can be any expression that yields something
callable).
Index
obj[index] – an index expression.
Try
expr? – error propagation: if expr is an error, return it from the
enclosing function; otherwise unwrap it. binds tightest (with ., call,
index), so f()?.x is (f()?).x and a + b? is a + (b?).
Unary
a unary-operator application: !operand or -operand.
Binary
a binary-operator application: lhs op rhs. covers arithmetic,
comparison, equality, and the boolean && / ||. the or fallback is
not here – it is Expr::OrElse.
Range
start .. end or start ..= end – a range. start and end are each
optional to leave room for ..end / start.. forms; inclusive is true
for ..=. ranges are ordinary expressions (an iterable for for).
Pipeline
lhs |> call – the pipeline operator. kept as a faithful node, NOT
lowered to call(lhs, ...); that desugaring is Phase 4 codegen. call
is whatever expression followed |> (typically a call or a bare function
name); the parser does not rewrite it.
Comptime
comptime body – evaluate body during compilation, embed the result
as a constant. a prefix operator at unary precedence, so comptime a + b
is (comptime a) + b; body may also be a block expression
(comptime { ... }).
Block
{ ...; trailing } used as an expression – its value is the block’s
trailing expression (or void). one of only two value-producing block
forms; the other is Expr::Match.
Match
match scrutinee { arm, ... } – pattern matching with at least one arm.
kept as a faithful node. each arm has a pattern, an optional if guard,
and a body (an expression or a block); Phase 3 checks exhaustiveness.
OrElse
expr or fallback – inline fallback for a Result/Option: the value
of expr if it is Ok/Some, else fallback. modelled as its own
node (not a BinOp) because it is faithful surface syntax with the
loosest precedence, left-associative; a or b or c is (a or b) or c.
Interpolation
a string with {expr} interpolations – parts alternates literal text
and embedded expressions, in source order. NOT desugared to a + chain;
the conversion-and-concatenation happens in Phase 4 codegen.
Implementations§
Source§impl Expr
impl Expr
Sourcepub fn span(&self) -> Span
pub fn span(&self) -> Span
the source span of this expression.
every variant carries its span field; this match is exhaustive over
all of them, which is the real guarantee that “every expression node has
a span”. the parser computes each span from the first token to the last
(via Span::to) when it builds the node.