Skip to main content

oak_fortran/ast/
executable_nodes.rs

1use core::range::Range;
2use std::{boxed::Box, string::String, vec::Vec};
3
4use super::{Dimension, ExprNode, SpecificationStmt};
5
6/// Executable statement.
7#[derive(Debug, Clone, PartialEq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum ExecutableStmt {
10    /// Assignment statement.
11    Assignment(AssignmentNode),
12    /// Call statement.
13    Call(CallNode),
14    /// If construct.
15    IfConstruct(IfConstructNode),
16    /// Do construct.
17    DoConstruct(DoConstructNode),
18    /// Select Case.
19    SelectCase(SelectCaseNode),
20    /// Where construct.
21    WhereConstruct(WhereConstructNode),
22    /// Forall construct.
23    ForallConstruct(ForallConstructNode),
24    /// Associate construct.
25    AssociateConstruct(AssociateConstructNode),
26    /// Block construct.
27    BlockConstruct(BlockConstructNode),
28    /// Critical construct.
29    CriticalConstruct(CriticalConstructNode),
30    /// Allocate statement.
31    Allocate(AllocateNode),
32    /// Deallocate statement.
33    Deallocate(DeallocateNode),
34    /// Nullify statement.
35    Nullify(NullifyNode),
36    /// Stop statement.
37    Stop(StopNode),
38    /// Return statement.
39    Return(ReturnNode),
40    /// Continue statement.
41    Continue,
42    /// Cycle statement.
43    Cycle(Option<String>),
44    /// Exit statement.
45    Exit(Option<String>),
46    /// Read statement.
47    Read(ReadNode),
48    /// Write statement.
49    Write(WriteNode),
50    /// Print statement.
51    Print(PrintNode),
52}
53
54/// Assignment node.
55#[derive(Debug, Clone, PartialEq)]
56#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
57pub struct AssignmentNode {
58    /// The left-hand side variable expression.
59    pub variable: Box<ExprNode>,
60    /// The right-hand side expression to assign.
61    pub expression: Box<ExprNode>,
62    /// The byte range of this node in the source text.
63    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
64    pub span: Range<usize>,
65}
66
67/// Call node.
68#[derive(Debug, Clone, PartialEq)]
69#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
70pub struct CallNode {
71    /// The name of the procedure to call.
72    pub procedure_name: String,
73    /// The arguments passed to the procedure.
74    pub arguments: Vec<Box<ExprNode>>,
75    /// The byte range of this node in the source text.
76    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
77    pub span: Range<usize>,
78}
79
80/// If construct node.
81#[derive(Debug, Clone, PartialEq)]
82#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
83pub struct IfConstructNode {
84    /// The condition expression for the if block.
85    pub condition: Box<ExprNode>,
86    /// The statements in the then block.
87    pub then_part: Vec<ExecutableStmt>,
88    /// The else-if blocks with their conditions and statements.
89    pub else_if_parts: Vec<(Box<ExprNode>, Vec<ExecutableStmt>)>,
90    /// The optional else block statements.
91    pub else_part: Option<Vec<ExecutableStmt>>,
92    /// The optional construct name.
93    pub name: Option<String>,
94    /// The byte range of this node in the source text.
95    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
96    pub span: Range<usize>,
97}
98
99/// Do construct node.
100#[derive(Debug, Clone, PartialEq)]
101#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
102pub struct DoConstructNode {
103    /// The optional construct name.
104    pub name: Option<String>,
105    /// The optional loop control (iterative, while, or concurrent).
106    pub control: Option<DoControl>,
107    /// The statements in the loop body.
108    pub body: Vec<ExecutableStmt>,
109    /// The byte range of this node in the source text.
110    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
111    pub span: Range<usize>,
112}
113
114/// Do control.
115#[derive(Debug, Clone, PartialEq)]
116#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
117pub enum DoControl {
118    /// Iterative do loop with variable, start, end, and optional step.
119    Iterative {
120        /// The loop variable name.
121        variable: String,
122        /// The start value expression.
123        start: Box<ExprNode>,
124        /// The end value expression.
125        end: Box<ExprNode>,
126        /// The optional step value expression.
127        step: Option<Box<ExprNode>>,
128    },
129    /// While loop with condition.
130    While(Box<ExprNode>),
131    /// Concurrent loop with header and locality specs.
132    Concurrent {
133        /// The concurrent header with control list and optional mask.
134        header: ConcurrentHeader,
135        /// The locality specifications.
136        locality: Vec<LocalitySpec>,
137    },
138}
139
140/// Concurrent header.
141#[derive(Debug, Clone, PartialEq)]
142#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
143pub struct ConcurrentHeader {
144    /// The list of concurrent controls.
145    pub control_list: Vec<ConcurrentControl>,
146    /// The optional mask expression.
147    pub mask: Option<Box<ExprNode>>,
148}
149
150/// Concurrent control.
151#[derive(Debug, Clone, PartialEq)]
152#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
153pub struct ConcurrentControl {
154    /// The index variable name.
155    pub name: String,
156    /// The start value expression.
157    pub start: Box<ExprNode>,
158    /// The end value expression.
159    pub end: Box<ExprNode>,
160    /// The optional step value expression.
161    pub step: Option<Box<ExprNode>>,
162}
163
164/// Locality specification.
165#[derive(Debug, Clone, PartialEq)]
166#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
167pub enum LocalitySpec {
168    /// Local.
169    Local(Vec<String>),
170    /// Local init.
171    LocalInit(Vec<String>),
172    /// Shared.
173    Shared(Vec<String>),
174    /// Default none.
175    DefaultNone,
176}
177
178/// Select Case node.
179#[derive(Debug, Clone, PartialEq)]
180#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
181pub struct SelectCaseNode {
182    /// The expression to match against cases.
183    pub expression: Box<ExprNode>,
184    /// The list of case blocks.
185    pub cases: Vec<CaseConstruct>,
186    /// The optional construct name.
187    pub name: Option<String>,
188    /// The byte range of this node in the source text.
189    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
190    pub span: Range<usize>,
191}
192
193/// Case construct.
194#[derive(Debug, Clone, PartialEq)]
195#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
196pub struct CaseConstruct {
197    /// The case selector (specific values or default).
198    pub selector: CaseSelector,
199    /// The statements in this case block.
200    pub body: Vec<ExecutableStmt>,
201}
202
203/// Case selector.
204#[derive(Debug, Clone, PartialEq)]
205#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
206pub enum CaseSelector {
207    /// Case.
208    Case(Vec<CaseValue>),
209    /// Default.
210    Default,
211}
212
213/// Case value.
214#[derive(Debug, Clone, PartialEq)]
215#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
216pub enum CaseValue {
217    /// Single value.
218    Single(Box<ExprNode>),
219    /// Range.
220    Range(Option<Box<ExprNode>>, Option<Box<ExprNode>>),
221}
222
223/// Where construct node.
224#[derive(Debug, Clone, PartialEq)]
225#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
226pub struct WhereConstructNode {
227    /// The mask expression for the where block.
228    pub mask: Box<ExprNode>,
229    /// The statements in the where block.
230    pub where_body: Vec<ExecutableStmt>,
231    /// The else-where blocks with optional masks and statements.
232    pub else_where_parts: Vec<(Option<Box<ExprNode>>, Vec<ExecutableStmt>)>,
233    /// The optional construct name.
234    pub name: Option<String>,
235    /// The byte range of this node in the source text.
236    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
237    pub span: Range<usize>,
238}
239
240/// Forall construct node.
241#[derive(Debug, Clone, PartialEq)]
242#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
243pub struct ForallConstructNode {
244    /// The concurrent header with control list and optional mask.
245    pub header: ConcurrentHeader,
246    /// The statements in the forall body.
247    pub body: Vec<ExecutableStmt>,
248    /// The optional construct name.
249    pub name: Option<String>,
250    /// The byte range of this node in the source text.
251    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
252    pub span: Range<usize>,
253}
254
255/// Associate construct node.
256#[derive(Debug, Clone, PartialEq)]
257#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
258pub struct AssociateConstructNode {
259    /// The list of associations.
260    pub associates: Vec<Associate>,
261    /// The statements in the associate body.
262    pub body: Vec<ExecutableStmt>,
263    /// The optional construct name.
264    pub name: Option<String>,
265    /// The byte range of this node in the source text.
266    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
267    pub span: Range<usize>,
268}
269
270/// Associate.
271#[derive(Debug, Clone, PartialEq)]
272#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
273pub struct Associate {
274    /// The name of the associate.
275    pub name: String,
276    /// The expression to associate with.
277    pub expression: Box<ExprNode>,
278}
279
280/// Block construct node.
281#[derive(Debug, Clone, PartialEq)]
282#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
283pub struct BlockConstructNode {
284    /// The specification statements in the block.
285    pub specification_part: Vec<SpecificationStmt>,
286    /// The executable statements in the block.
287    pub execution_part: Vec<ExecutableStmt>,
288    /// The optional construct name.
289    pub name: Option<String>,
290    /// The byte range of this node in the source text.
291    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
292    pub span: Range<usize>,
293}
294
295/// Critical construct node.
296#[derive(Debug, Clone, PartialEq)]
297#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
298pub struct CriticalConstructNode {
299    /// The statements in the critical section.
300    pub body: Vec<ExecutableStmt>,
301    /// The optional construct name.
302    pub name: Option<String>,
303    /// The byte range of this node in the source text.
304    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
305    pub span: Range<usize>,
306}
307
308/// Allocate node.
309#[derive(Debug, Clone, PartialEq)]
310#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
311pub struct AllocateNode {
312    /// The objects to allocate.
313    pub objects: Vec<Allocation>,
314    /// The allocation options.
315    pub options: Vec<AllocOpt>,
316    /// The byte range of this node in the source text.
317    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
318    pub span: Range<usize>,
319}
320
321/// Allocation.
322#[derive(Debug, Clone, PartialEq)]
323#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
324pub struct Allocation {
325    /// The variable to allocate.
326    pub variable: Box<ExprNode>,
327    /// The optional array specification.
328    pub array_spec: Option<Vec<Dimension>>,
329}
330
331/// Allocation option.
332#[derive(Debug, Clone, PartialEq)]
333#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
334pub enum AllocOpt {
335    /// Stat.
336    Stat(Box<ExprNode>),
337    /// Error message.
338    Errmsg(Box<ExprNode>),
339    /// Source.
340    Source(Box<ExprNode>),
341    /// Mold.
342    Mold(Box<ExprNode>),
343}
344
345/// Deallocate node.
346#[derive(Debug, Clone, PartialEq)]
347#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
348pub struct DeallocateNode {
349    /// The objects to deallocate.
350    pub objects: Vec<Box<ExprNode>>,
351    /// The deallocation options.
352    pub options: Vec<DeallocOpt>,
353    /// The byte range of this node in the source text.
354    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
355    pub span: Range<usize>,
356}
357
358/// Deallocation option.
359#[derive(Debug, Clone, PartialEq)]
360#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
361pub enum DeallocOpt {
362    /// Stat.
363    Stat(Box<ExprNode>),
364    /// Error message.
365    Errmsg(Box<ExprNode>),
366}
367
368/// Nullify node.
369#[derive(Debug, Clone, PartialEq)]
370#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
371pub struct NullifyNode {
372    /// The pointer objects to nullify.
373    pub pointers: Vec<Box<ExprNode>>,
374    /// The byte range of this node in the source text.
375    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
376    pub span: Range<usize>,
377}
378
379/// Stop node.
380#[derive(Debug, Clone, PartialEq)]
381#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
382pub struct StopNode {
383    /// The optional stop code expression.
384    pub stop_code: Option<Box<ExprNode>>,
385    /// The optional quiet expression.
386    pub quiet: Option<Box<ExprNode>>,
387    /// The byte range of this node in the source text.
388    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
389    pub span: Range<usize>,
390}
391
392/// Return node.
393#[derive(Debug, Clone, PartialEq)]
394#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
395pub struct ReturnNode {
396    /// The optional return expression.
397    pub expression: Option<Box<ExprNode>>,
398    /// The byte range of this node in the source text.
399    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
400    pub span: Range<usize>,
401}
402
403/// Read node.
404#[derive(Debug, Clone, PartialEq)]
405#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
406pub struct ReadNode {
407    /// The I/O control specifications.
408    pub io_control_spec: Vec<IoControlSpec>,
409    /// The input items to read into.
410    pub input_items: Vec<Box<ExprNode>>,
411    /// The byte range of this node in the source text.
412    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
413    pub span: Range<usize>,
414}
415
416/// Write node.
417#[derive(Debug, Clone, PartialEq)]
418#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
419pub struct WriteNode {
420    /// The I/O control specifications.
421    pub io_control_spec: Vec<IoControlSpec>,
422    /// The output items to write.
423    pub output_items: Vec<Box<ExprNode>>,
424    /// The byte range of this node in the source text.
425    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
426    pub span: Range<usize>,
427}
428
429/// Print node.
430#[derive(Debug, Clone, PartialEq)]
431#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
432pub struct PrintNode {
433    /// The optional format expression.
434    pub format: Option<Box<ExprNode>>,
435    /// The output items to print.
436    pub output_items: Vec<Box<ExprNode>>,
437    /// The byte range of this node in the source text.
438    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
439    pub span: Range<usize>,
440}
441
442/// IO control specification.
443#[derive(Debug, Clone, PartialEq)]
444#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
445pub enum IoControlSpec {
446    /// Unit.
447    Unit(Box<ExprNode>),
448    /// Format.
449    Format(Box<ExprNode>),
450    /// Nml.
451    Nml(Box<ExprNode>),
452    /// Iomsg.
453    Iomsg(Box<ExprNode>),
454    /// Iostat.
455    Iostat(Box<ExprNode>),
456    /// Advance.
457    Advance(Box<ExprNode>),
458    /// Other.
459    Other(String, Box<ExprNode>),
460}