1use crate::frame::FrameField;
2use crate::scene::PlotDescriptor;
3use crate::types::{Type, Value};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
6pub struct ExprId(pub usize);
7
8#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct StatementId(pub usize);
10
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct StatementBlockId(pub usize);
13
14#[derive(Debug, Clone, PartialEq)]
15pub struct StatementBlock {
16 pub statements: Vec<StatementId>,
17}
18
19#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
20pub struct FunctionId(pub usize);
21
22#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
23pub struct LocalSlot(pub usize);
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26pub struct StateSlot(pub usize);
27
28#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
29pub struct SeriesSlot(pub usize);
30
31#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
32pub struct SourceInputSlot(pub usize);
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub struct CallSiteId(pub usize);
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub struct IntrinsicId(pub usize);
39
40#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
41pub struct PlotId(pub usize);
42
43#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
44pub struct ConstantId(pub usize);
45
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum UnaryOperator {
48 Plus,
49 Minus,
50 Not,
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54pub enum BinaryOperator {
55 Add,
56 Subtract,
57 Multiply,
58 Divide,
59 Remainder,
60 Equal,
61 NotEqual,
62 Less,
63 LessEqual,
64 Greater,
65 GreaterEqual,
66 And,
67 Or,
68}
69
70#[derive(Debug, Clone, Copy, PartialEq, Eq)]
71pub enum BarStateField {
72 IsFirst,
73 IsLast,
74 IsConfirmed,
75 IsRealtime,
76}
77
78#[derive(Debug, Clone, PartialEq)]
79pub enum ExpressionKind {
80 Constant(ConstantId),
81 LoadLocal(LocalSlot),
82 LoadState(StateSlot),
83 LoadSeries(SeriesSlot),
84 LoadInput(LocalSlot),
85 LoadSeriesInput(SourceInputSlot),
86 LoadFrame(FrameField),
87 RecordField {
88 source: ExprId,
89 field: String,
90 },
91 LoadBarIndex,
92 LoadBarState(BarStateField),
93 Tuple(Vec<ExprId>),
94 Array(Vec<ExprId>),
95 Record(Vec<(String, ExprId)>),
96 Switch {
97 operand: ExprId,
98 branches: Vec<(ExprId, ExprId)>,
99 fallback: Option<ExprId>,
100 },
101 History {
102 source: ExprId,
103 offset: ExprId,
104 },
105 CollectionIndex {
106 source: ExprId,
107 index: ExprId,
108 },
109 Unary {
110 operator: UnaryOperator,
111 operand: ExprId,
112 },
113 Binary {
114 operator: BinaryOperator,
115 left: ExprId,
116 right: ExprId,
117 },
118 Conditional {
119 condition: ExprId,
120 when_true: ExprId,
121 when_false: ExprId,
122 },
123 Coalesce {
124 source: ExprId,
125 fallback: ExprId,
126 },
127 IntrinsicCall {
128 intrinsic: IntrinsicId,
129 call_site: CallSiteId,
130 arguments: Vec<ExprId>,
131 },
132 FunctionCall {
133 function: FunctionId,
134 call_site: CallSiteId,
135 arguments: Vec<ExprId>,
136 argument_slots: Vec<usize>,
137 },
138}
139
140#[derive(Debug, Clone, PartialEq)]
141pub struct TypedExpression {
142 pub kind: ExpressionKind,
143 pub ty: Type,
144 pub series_slot: Option<SeriesSlot>,
145 pub is_fixed: bool,
146}
147
148#[derive(Debug, Clone, PartialEq)]
149pub enum StatementKind {
150 Binding {
151 target: LocalSlot,
152 expression: ExprId,
153 },
154 LocalUpdate {
155 target: LocalSlot,
156 expression: ExprId,
157 },
158 StateDeclaration {
159 target: StateSlot,
160 initial: ExprId,
161 call_site: CallSiteId,
162 },
163 StateUpdate {
164 target: StateSlot,
165 expression: ExprId,
166 },
167 Plot {
168 plot: PlotId,
169 expression: ExprId,
170 color: Option<ExprId>,
171 },
172 Marker {
173 id: String,
174 expression: ExprId,
175 visible: Option<ExprId>,
176 color: Option<ExprId>,
177 text: Option<String>,
178 },
179 Segment {
180 id: String,
181 start_value: ExprId,
182 end_value: ExprId,
183 start_index: ExprId,
184 end_index: ExprId,
185 visible: Option<ExprId>,
186 color: Option<ExprId>,
187 text: Option<String>,
188 },
189 Region {
190 id: String,
191 key: ExprId,
192 upper: ExprId,
193 lower: ExprId,
194 left_index: ExprId,
195 right_index: ExprId,
196 visible: Option<ExprId>,
197 border_color: Option<ExprId>,
198 fill_color: Option<ExprId>,
199 border_color_slot: Option<String>,
200 fill_color_slot: Option<String>,
201 style_by: Option<ExprId>,
202 true_color_slot: Option<String>,
203 false_color_slot: Option<String>,
204 extend_right: Option<ExprId>,
205 persistent: bool,
206 width: f32,
207 },
208 RegionRemove {
209 id: String,
210 key: ExprId,
211 },
212 HorizontalLevel {
213 id: String,
214 expression: ExprId,
215 color: Option<ExprId>,
216 },
217 OhlcPlot {
218 plot: PlotId,
219 open: ExprId,
220 high: ExprId,
221 low: ExprId,
222 close: ExprId,
223 color: Option<ExprId>,
224 },
225 RangePlot {
226 plot: PlotId,
227 upper: ExprId,
228 lower: ExprId,
229 color: Option<ExprId>,
230 },
231 Expression(ExprId),
232 If {
233 condition: ExprId,
234 then_branch: Vec<StatementId>,
235 else_branch: Vec<StatementId>,
236 },
237 While {
238 condition: ExprId,
239 body: Vec<StatementId>,
240 },
241 ForRange {
242 target: LocalSlot,
243 start: ExprId,
244 end: ExprId,
245 body: Vec<StatementId>,
246 },
247 ForEach {
248 target: LocalSlot,
249 collection: ExprId,
250 body: Vec<StatementId>,
251 },
252 Break,
253 Continue,
254 Return(Option<ExprId>),
255 Destructure {
256 targets: Vec<LocalSlot>,
257 expression: ExprId,
258 },
259}
260
261#[derive(Debug, Clone, PartialEq)]
262pub struct TypedStatement {
263 pub kind: StatementKind,
264}
265
266#[derive(Debug, Clone, PartialEq)]
267pub struct HirFunction {
268 pub id: FunctionId,
269 pub name: String,
270 pub parameters: Vec<LocalSlot>,
271 pub parameter_names: Vec<String>,
272 pub parameter_defaults: Vec<Option<ExprId>>,
273 pub local_slots: Vec<LocalSlot>,
274 pub body: Vec<StatementId>,
275 pub return_type: Type,
276}
277
278#[derive(Debug, Clone, PartialEq)]
279pub struct HirProgram {
280 pub expressions: Vec<TypedExpression>,
281 pub statements: Vec<TypedStatement>,
282 pub functions: Vec<HirFunction>,
283 pub constants: Vec<Value>,
284 pub locals: usize,
285 pub states: usize,
286 pub state_call_sites: Vec<CallSiteId>,
287 pub state_functions: Vec<Option<FunctionId>>,
288 pub series: usize,
289 pub retained_series: Vec<bool>,
290 pub call_sites: usize,
291 pub plots: Vec<PlotDescriptor>,
292 pub inputs: Vec<(LocalSlot, String)>,
293 pub source_inputs: Vec<(SourceInputSlot, String)>,
294 pub local_definitions: Vec<Option<ExprId>>,
295 pub blocks: Vec<StatementBlock>,
296 pub entry_point: StatementBlockId,
297}
298
299impl HirProgram {
300 pub fn empty() -> Self {
301 Self {
302 expressions: Vec::new(),
303 statements: Vec::new(),
304 functions: Vec::new(),
305 constants: Vec::new(),
306 locals: 0,
307 states: 0,
308 state_call_sites: Vec::new(),
309 state_functions: Vec::new(),
310 series: 0,
311 retained_series: Vec::new(),
312 call_sites: 0,
313 plots: Vec::new(),
314 inputs: Vec::new(),
315 source_inputs: Vec::new(),
316 local_definitions: Vec::new(),
317 blocks: vec![StatementBlock {
318 statements: Vec::new(),
319 }],
320 entry_point: StatementBlockId(0),
321 }
322 }
323}