Skip to main content

oak_fortran/ast/
expression_nodes.rs

1use core::range::Range;
2use std::{boxed::Box, string::String, vec::Vec};
3
4/// Expression node.
5#[derive(Debug, Clone, PartialEq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub enum ExprNode {
8    /// Literal.
9    Literal(LiteralNode),
10    /// Name.
11    Name(String),
12    /// Array element.
13    ArrayElement(ArrayElementNode),
14    /// Function reference.
15    FunctionReference(FunctionReferenceNode),
16    /// Unary operation.
17    UnaryOp(UnaryOpNode),
18    /// Binary operation.
19    BinaryOp(BinaryOpNode),
20    /// Parenthesized expression.
21    ParenExpr(Box<ExprNode>),
22    /// Structure constructor.
23    StructureConstructor(StructureConstructorNode),
24}
25
26/// Literal node.
27#[derive(Debug, Clone, PartialEq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct LiteralNode {
30    /// The literal value as a string.
31    pub value: String,
32    /// The kind of literal (integer, real, complex, character, logical).
33    pub kind: LiteralKind,
34    /// The byte range of this node in the source text.
35    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
36    pub span: Range<usize>,
37}
38
39/// Literal kind.
40#[derive(Debug, Clone, PartialEq)]
41#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
42pub enum LiteralKind {
43    /// Integer.
44    Integer,
45    /// Real.
46    Real,
47    /// Complex.
48    Complex,
49    /// Character.
50    Character,
51    /// Logical.
52    Logical,
53}
54
55/// Array element node.
56#[derive(Debug, Clone, PartialEq)]
57#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
58pub struct ArrayElementNode {
59    /// The name of the array.
60    pub name: String,
61    /// The subscript expressions.
62    pub subscripts: Vec<Box<ExprNode>>,
63    /// The byte range of this node in the source text.
64    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
65    pub span: Range<usize>,
66}
67
68/// Function reference node.
69#[derive(Debug, Clone, PartialEq)]
70#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
71pub struct FunctionReferenceNode {
72    /// The name of the function being referenced.
73    pub name: String,
74    /// The arguments passed to the function.
75    pub arguments: Vec<Box<ExprNode>>,
76    /// The byte range of this node in the source text.
77    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
78    pub span: Range<usize>,
79}
80
81/// Unary operation node.
82#[derive(Debug, Clone, PartialEq)]
83#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
84pub struct UnaryOpNode {
85    /// The unary operator.
86    pub operator: UnaryOperator,
87    /// The operand expression.
88    pub operand: Box<ExprNode>,
89    /// The byte range of this node in the source text.
90    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
91    pub span: Range<usize>,
92}
93
94/// Unary operator.
95#[derive(Debug, Clone, PartialEq)]
96#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
97pub enum UnaryOperator {
98    /// Not.
99    Not,
100    /// Plus.
101    Plus,
102    /// Minus.
103    Minus,
104}
105
106/// Binary operation node.
107#[derive(Debug, Clone, PartialEq)]
108#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
109pub struct BinaryOpNode {
110    /// The binary operator.
111    pub operator: BinaryOperator,
112    /// The left-hand side expression.
113    pub left: Box<ExprNode>,
114    /// The right-hand side expression.
115    pub right: Box<ExprNode>,
116    /// The byte range of this node in the source text.
117    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
118    pub span: Range<usize>,
119}
120
121/// Binary operator.
122#[derive(Debug, Clone, PartialEq)]
123#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
124pub enum BinaryOperator {
125    /// Add.
126    Add,
127    /// Subtract.
128    Subtract,
129    /// Multiply.
130    Multiply,
131    /// Divide.
132    Divide,
133    /// Power.
134    Power,
135    /// Concat.
136    Concat,
137    /// Equal.
138    Equal,
139    /// Not equal.
140    NotEqual,
141    /// Less than.
142    LessThan,
143    /// Less than or equal.
144    LessThanOrEqual,
145    /// Greater than.
146    GreaterThan,
147    /// Greater than or equal.
148    GreaterThanOrEqual,
149    /// And.
150    And,
151    /// Or.
152    Or,
153    /// Eqv.
154    Eqv,
155    /// Neqv.
156    Neqv,
157}
158
159/// Structure constructor node.
160#[derive(Debug, Clone, PartialEq)]
161#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
162pub struct StructureConstructorNode {
163    /// The name of the derived type being constructed.
164    pub type_name: String,
165    /// The component initializers as (name, value) pairs.
166    pub args: Vec<(Option<String>, Box<ExprNode>)>,
167    /// The byte range of this node in the source text.
168    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
169    pub span: Range<usize>,
170}