1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use atomic_refcell::{AtomicRef, AtomicRefMut};
use legion::{Resources, World};
use crate::{
compile::CompilationResult, lowering::NameTable, parse::DocumentV1,
BuildContext, Diagnostics, FeatureFlags,
};
pub trait Hooks {
fn before_parse(&mut self, _ctx: &mut dyn Context) -> Continuation {
Continuation::Continue
}
fn after_parse(&mut self, ctx: &mut dyn AfterParseContext) -> Continuation {
if ctx.diagnostics().has_errors() {
Continuation::Halt
} else {
Continuation::Continue
}
}
fn after_lowering(
&mut self,
ctx: &mut dyn AfterLoweringContext,
) -> Continuation {
if ctx.diagnostics().has_errors() {
Continuation::Halt
} else {
Continuation::Continue
}
}
fn after_type_checking(
&mut self,
ctx: &mut dyn AfterTypeCheckingContext,
) -> Continuation {
if ctx.diagnostics().has_errors() {
Continuation::Halt
} else {
Continuation::Continue
}
}
fn after_codegen(
&mut self,
ctx: &mut dyn AfterCodegenContext,
) -> Continuation {
if ctx.diagnostics().has_errors() {
Continuation::Halt
} else {
Continuation::Continue
}
}
fn after_compile(
&mut self,
ctx: &mut dyn AfterCompileContext,
) -> Continuation {
if ctx.diagnostics().has_errors() {
Continuation::Halt
} else {
Continuation::Continue
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Continuation {
Continue,
Halt,
}
pub trait Context {
fn resources(&self) -> &Resources;
fn resources_mut(&mut self) -> &mut Resources;
fn world(&self) -> &World;
fn world_mut(&mut self) -> &mut World;
fn world_and_resources(&mut self) -> (&mut World, &mut Resources);
fn build_context(&self) -> AtomicRef<'_, BuildContext> {
self.resources().get().unwrap()
}
fn feature_flags(&self) -> AtomicRef<'_, FeatureFlags> {
self.resources().get().unwrap()
}
}
pub trait AfterParseContext: Context {
fn document(&self) -> AtomicRef<'_, DocumentV1> {
self.resources().get().unwrap()
}
fn document_mut(&self) -> AtomicRefMut<'_, DocumentV1> {
self.resources().get_mut().unwrap()
}
fn diagnostics(&self) -> AtomicRef<'_, Diagnostics> {
self.resources().get().unwrap()
}
fn diagnostics_mut(&self) -> AtomicRefMut<'_, Diagnostics> {
self.resources().get_mut().unwrap()
}
}
pub trait AfterLoweringContext: AfterParseContext {
fn names(&self) -> AtomicRef<'_, NameTable> {
self.resources().get().unwrap()
}
}
pub trait AfterTypeCheckingContext: AfterLoweringContext {}
pub trait AfterCodegenContext: AfterTypeCheckingContext {}
pub trait AfterCompileContext: AfterCodegenContext {
fn take_compilation_result(&mut self) -> CompilationResult {
self.resources_mut().remove().unwrap()
}
}
pub(crate) struct Ctx<'world, 'res> {
pub(crate) world: &'world mut World,
pub(crate) res: &'res mut Resources,
}
impl<'world, 'res> Context for Ctx<'world, 'res> {
fn resources(&self) -> &Resources { self.res }
fn resources_mut(&mut self) -> &mut Resources { self.res }
fn world(&self) -> &World { self.world }
fn world_mut(&mut self) -> &mut World { self.world }
fn world_and_resources(&mut self) -> (&mut World, &mut Resources) {
(self.world, self.res)
}
}
impl<'world, 'res> AfterParseContext for Ctx<'world, 'res> {}
impl<'world, 'res> AfterLoweringContext for Ctx<'world, 'res> {}
impl<'world, 'res> AfterTypeCheckingContext for Ctx<'world, 'res> {}
impl<'world, 'res> AfterCodegenContext for Ctx<'world, 'res> {}
impl<'world, 'res> AfterCompileContext for Ctx<'world, 'res> {}