Skip to main content

yog_script_analyze/ast/
patch.rs

1//! Patches (`.yns`, `ys-grammar.md` §6): what reaches into others' classes.
2
3use super::{Block, Decl, Expr, Name, Param, Type};
4use crate::source::token::Span;
5
6/// `patch LivingEntity { … }`.
7#[derive(Debug, Clone, PartialEq)]
8pub struct Patch {
9    pub target: Type,
10    pub items: Vec<PatchItem>,
11}
12
13#[derive(Debug, Clone, PartialEq)]
14pub enum PatchItem {
15    /// `add saved f32 Shield = 0;`, `add void Zap() { … }`.
16    Add(Box<Decl>),
17    /// `add : IChargeable;`.
18    AddBases(Vec<Type>),
19    Before {
20        method: Target,
21        uses: Vec<Local>,
22        body: Block,
23    },
24    After {
25        method: Target,
26        uses: Vec<Local>,
27        body: Block,
28    },
29    Inside {
30        method: Target,
31        points: Vec<Point>,
32    },
33    /// `read FoodData { … }`.
34    Read {
35        field: Name,
36        body: Block,
37    },
38    /// `write ExperienceLevel { … }`: `value` the new one.
39    Write {
40        field: Name,
41        body: Block,
42    },
43    Replace {
44        method: Target,
45        body: Block,
46    },
47    Priority(i64, Span),
48}
49
50/// A target method by its signature: `bool Hurt(DamageSource source, f32 amount)`.
51#[derive(Debug, Clone, PartialEq)]
52pub struct Target {
53    pub ret: Option<Type>,
54    pub name: Name,
55    pub params: Vec<Param>,
56}
57
58/// `mut local i64 spent #1`.
59#[derive(Debug, Clone, PartialEq)]
60pub struct Local {
61    pub write: bool,
62    pub ty: Type,
63    pub name: Name,
64    pub ordinal: Option<i64>,
65}
66
67/// A point inside a method.
68#[derive(Debug, Clone, PartialEq)]
69pub enum Point {
70    Around {
71        callee: Callee,
72        select: Option<Selector>,
73        body: Block,
74    },
75    /// `at call Vec3.Add (x) { … }`: an argument by its name.
76    Arg {
77        callee: Callee,
78        select: Option<Selector>,
79        arg: Name,
80        body: Block,
81    },
82    BeforeCall {
83        callee: Callee,
84        select: Option<Selector>,
85        uses: Vec<Local>,
86        body: Block,
87    },
88    AfterCall {
89        callee: Callee,
90        select: Option<Selector>,
91        uses: Vec<Local>,
92        body: Block,
93    },
94    Field {
95        write: bool,
96        field: Name,
97        select: Option<Selector>,
98        uses: Vec<Local>,
99        body: Block,
100    },
101    New {
102        ty: Type,
103        select: Option<Selector>,
104        uses: Vec<Local>,
105        body: Block,
106    },
107    Constant {
108        value: Expr,
109        to: Expr,
110        select: Option<Selector>,
111    },
112    Local {
113        local: Local,
114        value: Expr,
115    },
116}
117
118/// A called method: `AiStep`, `Vec3.Add`, with argument types to tell
119/// overloads apart (`Vec3.Add(f64, f64, f64)`).
120#[derive(Debug, Clone, PartialEq)]
121pub struct Callee {
122    pub path: Vec<Name>,
123    pub args: Option<Vec<Type>>,
124}
125
126/// Which of several matching points.
127#[derive(Debug, Clone, PartialEq)]
128pub enum Selector {
129    First,
130    Last,
131    Where(Expr),
132}