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 (`var (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    /// `var x = …;`, `int x;`, `var (a, b) = …;`; `ty` None: `var`.
23    Local {
24        ty: Option<Type>,
25        binding: Binding,
26        value: Option<Expr>,
27    },
28    /// `place op= value;`; `op` None for `=`.
29    Assign {
30        place: Expr,
31        op: Option<super::BinOp>,
32        value: Expr,
33    },
34    If {
35        cond: Expr,
36        then: Box<Stmt>,
37        otherwise: Option<Box<Stmt>>,
38    },
39    Foreach {
40        ty: Option<Type>,
41        binding: Binding,
42        iter: Expr,
43        body: Box<Stmt>,
44    },
45    For {
46        init: Option<Box<Stmt>>,
47        cond: Option<Expr>,
48        step: Option<Box<Stmt>>,
49        body: Box<Stmt>,
50    },
51    While {
52        cond: Expr,
53        body: Box<Stmt>,
54    },
55    DoWhile {
56        body: Box<Stmt>,
57        cond: Expr,
58    },
59    Block(Block),
60    Break(Span),
61    Continue(Span),
62    Return(Option<Expr>, Span),
63    /// `skip value;` in a patch's `before`: the method ends with it.
64    Skip(Option<Expr>, Span),
65    Expr(Expr),
66    /// A block lambda's last expression, without `;`: its value.
67    Value(Expr),
68}