Skip to main content

yog_script_analyze/ast/
expr.rs

1//! Expressions (`ys-grammar.md` §3.2–3.4).
2
3use super::{Name, Pattern, Stmt, Type, VersionCond};
4use crate::source::token::Span;
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct Expr {
8    pub kind: ExprKind,
9    pub span: Span,
10}
11
12#[derive(Debug, Clone, PartialEq)]
13pub enum ExprKind {
14    Int(i64),
15    Float(f64),
16    Unit {
17        value: String,
18        unit: String,
19    },
20    Str(Vec<StrSeg>),
21    RawStr(String),
22    Char(char),
23    Text(String),
24    Content {
25        kind: String,
26        id: String,
27    },
28    Tag(String),
29    Bool(bool),
30    Null,
31    Ident(String),
32    This,
33    /// `base`: `this` as its base class (`base.Tick()`).
34    Base,
35    It,
36    /// `out let x`, `out i32 x`: a local declared where a method gives it.
37    Declare {
38        ty: Option<Type>,
39        name: Name,
40        mutable: bool,
41    },
42    /// `typeof(T)`: its `System.Type`.
43    TypeOf(Type),
44    /// `nameof(x.Y)`: its last name, as a string.
45    NameOf(Box<Expr>),
46    /// `default`, `default(T)`: the type's zero value.
47    Default(Option<Type>),
48    /// `mc >= 1.21`: the running game's version.
49    Mc(VersionCond),
50    Tuple(Vec<Expr>),
51    /// `[a, b]`, `[k => v]`, `[..xs]`: a list, a set or a map by its target.
52    Collection(Vec<Element>),
53    /// `new Pos(1, 2, 3)`, `new(4)`, `new Battery { Count = 2 }`.
54    New {
55        ty: Option<Type>,
56        args: Option<Vec<Arg>>,
57        init: Vec<(Name, Expr)>,
58    },
59    /// `x => …`, `(a, b) => …`, `{ … }` after a call (`it` its parameter).
60    Lambda {
61        params: Vec<LambdaParam>,
62        body: LambdaBody,
63    },
64    Unary {
65        op: UnOp,
66        expr: Box<Expr>,
67    },
68    /// `(i32)x`.
69    Cast {
70        ty: Type,
71        expr: Box<Expr>,
72    },
73    Binary {
74        op: BinOp,
75        left: Box<Expr>,
76        right: Box<Expr>,
77    },
78    Range {
79        from: Box<Expr>,
80        to: Box<Expr>,
81        inclusive: bool,
82    },
83    /// `x is Monster m`, `x is not null`.
84    Is {
85        expr: Box<Expr>,
86        pattern: Box<Pattern>,
87    },
88    /// `x as T`: `null` when it isn't one.
89    As {
90        expr: Box<Expr>,
91        ty: Type,
92    },
93    /// `x has Charge`.
94    Has {
95        expr: Box<Expr>,
96        ty: Type,
97    },
98    /// `c ? a : b`.
99    Conditional {
100        cond: Box<Expr>,
101        then: Box<Expr>,
102        otherwise: Box<Expr>,
103    },
104    /// `x switch { … }`.
105    Switch {
106        expr: Box<Expr>,
107        arms: Vec<Arm>,
108    },
109    Call {
110        callee: Box<Expr>,
111        args: Vec<Arg>,
112    },
113    Index {
114        expr: Box<Expr>,
115        index: Box<Expr>,
116    },
117    /// `f<T, U>` before a call: the type arguments.
118    TypeArgs {
119        expr: Box<Expr>,
120        args: Vec<Type>,
121    },
122    /// `x.Name`, `x?.Name`.
123    Member {
124        expr: Box<Expr>,
125        name: Name,
126        safe: bool,
127    },
128    /// `panic!(…)`: a built-in macro's call (a declared macro's is
129    /// expanded before the parser sees it).
130    Macro {
131        name: Name,
132        args: Vec<Expr>,
133    },
134    /// `move x`: `x` handed on, gone after.
135    Move(Box<Expr>),
136    /// `x!`: asserted not null.
137    NotNull(Box<Expr>),
138    /// What couldn't be parsed (reported already).
139    Error,
140}
141
142/// A piece of a string: text, or an interpolated `{expr}`.
143#[derive(Debug, Clone, PartialEq)]
144pub enum StrSeg {
145    Text(String),
146    Expr(Expr),
147}
148
149/// An element of a collection expression.
150#[derive(Debug, Clone, PartialEq)]
151pub enum Element {
152    Item(Expr),
153    Entry(Expr, Expr),
154    Spread(Expr),
155}
156
157/// A lambda's parameter: typed or not.
158#[derive(Debug, Clone, PartialEq)]
159pub struct LambdaParam {
160    pub ty: Option<Type>,
161    pub name: Name,
162}
163
164#[derive(Debug, Clone, PartialEq)]
165pub enum LambdaBody {
166    Expr(Box<Expr>),
167    Block(Vec<Stmt>),
168}
169
170/// `value` or `name: value`.
171#[derive(Debug, Clone, PartialEq)]
172pub struct Arg {
173    pub name: Option<Name>,
174    pub pass: Option<super::Pass>,
175    pub value: Expr,
176}
177
178/// `pattern when guard => value`.
179#[derive(Debug, Clone, PartialEq)]
180pub struct Arm {
181    pub pattern: Pattern,
182    pub guard: Option<Expr>,
183    pub value: Expr,
184}
185
186#[derive(Debug, Clone, Copy, PartialEq, Eq)]
187pub enum UnOp {
188    Neg,
189    Not,
190    Try,
191    Await,
192}
193
194#[derive(Debug, Clone, Copy, PartialEq, Eq)]
195pub enum BinOp {
196    Coalesce,
197    Or,
198    And,
199    Eq,
200    NotEq,
201    Lt,
202    LtEq,
203    Gt,
204    GtEq,
205    In,
206    Add,
207    Sub,
208    Mul,
209    Div,
210    Rem,
211}