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 var shield: Float = 0`, `add fn …`.
16    Add(Box<Decl>),
17    /// `add is Chargeable`.
18    AddIs(Type),
19    Before {
20        method: Method,
21        uses: Vec<Local>,
22        body: Block,
23    },
24    After {
25        method: Method,
26        uses: Vec<Local>,
27        body: Block,
28    },
29    Inside {
30        method: Method,
31        points: Vec<Point>,
32    },
33    /// `read foodData { … }`.
34    Read {
35        field: Name,
36        body: Block,
37    },
38    /// `write experienceLevel(value) { … }`.
39    Write {
40        field: Name,
41        value: Name,
42        body: Block,
43    },
44    Replace {
45        method: Method,
46        body: Block,
47    },
48    Priority(i64, Span),
49}
50
51/// A target method by its signature.
52#[derive(Debug, Clone, PartialEq)]
53pub struct Method {
54    pub name: Name,
55    pub params: Vec<Param>,
56    pub ret: Option<Type>,
57}
58
59/// `var local spent: Long #1`.
60#[derive(Debug, Clone, PartialEq)]
61pub struct Local {
62    pub write: bool,
63    pub name: Name,
64    pub ty: Type,
65    pub ordinal: Option<i64>,
66}
67
68/// A point inside a method.
69#[derive(Debug, Clone, PartialEq)]
70pub enum Point {
71    Around {
72        callee: Callee,
73        select: Option<Selector>,
74        body: Block,
75    },
76    /// `at call Vec3.add (x) { … }`: an argument by its name.
77    Arg {
78        callee: Callee,
79        select: Option<Selector>,
80        arg: Name,
81        body: Block,
82    },
83    BeforeCall {
84        callee: Callee,
85        select: Option<Selector>,
86        uses: Vec<Local>,
87        body: Block,
88    },
89    AfterCall {
90        callee: Callee,
91        select: Option<Selector>,
92        uses: Vec<Local>,
93        body: Block,
94    },
95    Field {
96        write: bool,
97        field: Name,
98        select: Option<Selector>,
99        uses: Vec<Local>,
100        body: Block,
101    },
102    New {
103        ty: Type,
104        select: Option<Selector>,
105        uses: Vec<Local>,
106        body: Block,
107    },
108    Constant {
109        value: Expr,
110        to: Expr,
111        select: Option<Selector>,
112    },
113    Local {
114        local: Local,
115        value: Expr,
116    },
117}
118
119/// A called method: `aiStep`, `Vec3.add`, with argument types to tell
120/// overloads apart (`Vec3.add(Double, Double, Double)`).
121#[derive(Debug, Clone, PartialEq)]
122pub struct Callee {
123    pub path: Vec<Name>,
124    pub args: Option<Vec<Type>>,
125}
126
127/// Which of several matching points.
128#[derive(Debug, Clone, PartialEq)]
129pub enum Selector {
130    First,
131    Last,
132    Where(Expr),
133}