Skip to main content

yog_script_analyze/ast/
stmt.rs

1//! Blocks and statements (`ys-grammar.md` §3.1).
2
3use super::{Expr, Name, Type};
4use crate::source::token::Span;
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct Block {
8    pub stmts: Vec<Stmt>,
9    pub span: Span,
10}
11
12/// What a local declaration or a `foreach` binds: one name or a
13/// deconstruction (`let (a, b)`).
14#[derive(Debug, Clone, PartialEq)]
15pub enum Binding {
16    One(Name),
17    Tuple(Vec<Name>),
18}
19
20#[derive(Debug, Clone, PartialEq)]
21pub enum Stmt {
22    /// `let x = …;`, `let mut x = …;`, `i32 x;`,
23    /// `let (a, b) = …;`; `ty` None: its value's.
24    Local {
25        ty: Option<Type>,
26        binding: Binding,
27        value: Option<Expr>,
28        /// May be assigned again (`let mut`, a typed local).
29        mutable: bool,
30    },
31    /// `place op= value;`; `op` None for `=`.
32    Assign {
33        place: Expr,
34        op: Option<super::BinOp>,
35        value: Expr,
36    },
37    If {
38        cond: Expr,
39        then: Box<Stmt>,
40        otherwise: Option<Box<Stmt>>,
41    },
42    Foreach {
43        ty: Option<Type>,
44        binding: Binding,
45        iter: Expr,
46        body: Box<Stmt>,
47    },
48    For {
49        init: Option<Box<Stmt>>,
50        cond: Option<Expr>,
51        step: Option<Box<Stmt>>,
52        body: Box<Stmt>,
53    },
54    While {
55        cond: Expr,
56        body: Box<Stmt>,
57    },
58    DoWhile {
59        body: Box<Stmt>,
60        cond: Expr,
61    },
62    Block(Block),
63    Break(Span),
64    Continue(Span),
65    Return(Option<Expr>, Span),
66    /// `skip value;` in a patch's `before`: the method ends with it.
67    Skip(Option<Expr>, Span),
68    Expr(Expr),
69    /// A block lambda's last expression, without `;`: its value.
70    Value(Expr),
71}