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    It,
34    /// `mc >= 1.21`: the running game's version.
35    Mc(VersionCond),
36    Tuple(Vec<Expr>),
37    /// `[a, b]`, `[k => v]`, `[..xs]`: a list, a set or a map by its target.
38    Collection(Vec<Element>),
39    /// `new Pos(1, 2, 3)`, `new(4)`, `new Battery { Count = 2 }`.
40    New {
41        ty: Option<Type>,
42        args: Option<Vec<Arg>>,
43        init: Vec<(Name, Expr)>,
44    },
45    /// `x => …`, `(a, b) => …`, `{ … }` after a call (`it` its parameter).
46    Lambda {
47        params: Vec<LambdaParam>,
48        body: LambdaBody,
49    },
50    Unary {
51        op: UnOp,
52        expr: Box<Expr>,
53    },
54    /// `(int)x`.
55    Cast {
56        ty: Type,
57        expr: Box<Expr>,
58    },
59    Binary {
60        op: BinOp,
61        left: Box<Expr>,
62        right: Box<Expr>,
63    },
64    Range {
65        from: Box<Expr>,
66        to: Box<Expr>,
67        inclusive: bool,
68    },
69    /// `x is Monster m`, `x is not null`.
70    Is {
71        expr: Box<Expr>,
72        pattern: Box<Pattern>,
73    },
74    /// `x as T`: `null` when it isn't one.
75    As {
76        expr: Box<Expr>,
77        ty: Type,
78    },
79    /// `x has Charge`.
80    Has {
81        expr: Box<Expr>,
82        ty: Type,
83    },
84    /// `c ? a : b`.
85    Conditional {
86        cond: Box<Expr>,
87        then: Box<Expr>,
88        otherwise: Box<Expr>,
89    },
90    /// `x switch { … }`.
91    Switch {
92        expr: Box<Expr>,
93        arms: Vec<Arm>,
94    },
95    Call {
96        callee: Box<Expr>,
97        args: Vec<Arg>,
98    },
99    Index {
100        expr: Box<Expr>,
101        index: Box<Expr>,
102    },
103    /// `f<T, U>` before a call: the type arguments.
104    TypeArgs {
105        expr: Box<Expr>,
106        args: Vec<Type>,
107    },
108    /// `x.Name`, `x?.Name`.
109    Member {
110        expr: Box<Expr>,
111        name: Name,
112        safe: bool,
113    },
114    /// `x!`: asserted not null.
115    NotNull(Box<Expr>),
116    /// What couldn't be parsed (reported already).
117    Error,
118}
119
120/// A piece of a string: text, or an interpolated `{expr}`.
121#[derive(Debug, Clone, PartialEq)]
122pub enum StrSeg {
123    Text(String),
124    Expr(Expr),
125}
126
127/// An element of a collection expression.
128#[derive(Debug, Clone, PartialEq)]
129pub enum Element {
130    Item(Expr),
131    Entry(Expr, Expr),
132    Spread(Expr),
133}
134
135/// A lambda's parameter: typed or not.
136#[derive(Debug, Clone, PartialEq)]
137pub struct LambdaParam {
138    pub ty: Option<Type>,
139    pub name: Name,
140}
141
142#[derive(Debug, Clone, PartialEq)]
143pub enum LambdaBody {
144    Expr(Box<Expr>),
145    Block(Vec<Stmt>),
146}
147
148/// `value` or `name: value`.
149#[derive(Debug, Clone, PartialEq)]
150pub struct Arg {
151    pub name: Option<Name>,
152    pub value: Expr,
153}
154
155/// `pattern when guard => value`.
156#[derive(Debug, Clone, PartialEq)]
157pub struct Arm {
158    pub pattern: Pattern,
159    pub guard: Option<Expr>,
160    pub value: Expr,
161}
162
163#[derive(Debug, Clone, Copy, PartialEq, Eq)]
164pub enum UnOp {
165    Neg,
166    Not,
167    Try,
168    Await,
169}
170
171#[derive(Debug, Clone, Copy, PartialEq, Eq)]
172pub enum BinOp {
173    Coalesce,
174    Or,
175    And,
176    Eq,
177    NotEq,
178    Lt,
179    LtEq,
180    Gt,
181    GtEq,
182    In,
183    Add,
184    Sub,
185    Mul,
186    Div,
187    Rem,
188}