Skip to main content

oak_swift/ast/
mod.rs

1#![doc = include_str!("readme.md")]
2use core::range::Range;
3
4/// Swift source file root node
5#[derive(Debug, Clone, PartialEq)]
6#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
7pub struct SwiftRoot {
8    /// The program node containing all statements.
9    pub program: Program,
10    /// The source span of the root node.
11    #[cfg_attr(feature = "serde", serde(with = "oak_core::serde_range"))]
12    pub span: Range<usize>,
13}
14
15/// Swift program
16#[derive(Debug, Clone, PartialEq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
18pub struct Program {
19    /// The list of statements in the program.
20    pub statements: Vec<Statement>,
21}
22
23/// Statement
24#[derive(Debug, Clone, PartialEq)]
25#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
26pub enum Statement {
27    /// Function definition
28    FunctionDef {
29        /// The function name.
30        name: String,
31        /// The function parameters.
32        parameters: Vec<Parameter>,
33        /// The return type of the function.
34        return_type: Option<Type>,
35        /// The function body statements.
36        body: Vec<Statement>,
37    },
38    /// Variable declaration
39    VariableDecl {
40        /// Whether the variable is mutable.
41        is_mutable: bool,
42        /// The variable name.
43        name: String,
44        /// The type annotation of the variable.
45        type_annotation: Option<Type>,
46        /// The initial value of the variable.
47        value: Option<Expression>,
48    },
49    /// Expression statement
50    Expression(Expression),
51    /// Return statement
52    Return(Option<Expression>),
53    /// Conditional statement
54    If {
55        /// The condition expression.
56        test: Expression,
57        /// The body statements when condition is true.
58        body: Vec<Statement>,
59        /// The else clause statements.
60        orelse: Option<Vec<Statement>>,
61    },
62    /// While loop
63    While {
64        /// The loop condition expression.
65        test: Expression,
66        /// The loop body statements.
67        body: Vec<Statement>,
68    },
69    /// For loop
70    For {
71        /// The loop variable name.
72        variable: String,
73        /// The iterable expression.
74        iterable: Expression,
75        /// The loop body statements.
76        body: Vec<Statement>,
77    },
78    /// Block
79    Block(Vec<Statement>),
80}
81
82/// Expression
83#[derive(Debug, Clone, PartialEq)]
84#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
85pub enum Expression {
86    /// Binary operation
87    Binary {
88        /// The left operand.
89        left: Box<Expression>,
90        /// The operator.
91        operator: String,
92        /// The right operand.
93        right: Box<Expression>,
94    },
95    /// Unary operation
96    Unary {
97        /// The operator.
98        operator: String,
99        /// The operand.
100        operand: Box<Expression>,
101    },
102    /// Function call
103    Call {
104        /// The callee expression.
105        callee: Box<Expression>,
106        /// The call arguments.
107        arguments: Vec<Expression>,
108    },
109    /// Member access
110    Member {
111        /// The object expression.
112        object: Box<Expression>,
113        /// The member name.
114        member: String,
115    },
116    /// Identifier
117    Identifier(String),
118    /// Literal
119    Literal(Literal),
120}
121
122/// Literal
123#[derive(Debug, Clone, PartialEq)]
124#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
125pub enum Literal {
126    /// Numeric literal value.
127    Number(String),
128    /// String literal value.
129    String(String),
130    /// Boolean literal value.
131    Boolean(bool),
132    /// Nil literal value.
133    Nil,
134}
135
136/// Parameter
137#[derive(Debug, Clone, PartialEq)]
138#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
139pub struct Parameter {
140    /// The parameter name.
141    pub name: String,
142    /// The type annotation of the parameter.
143    pub type_annotation: Type,
144}
145
146/// Type
147#[derive(Debug, Clone, PartialEq)]
148#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
149pub struct Type {
150    /// The type name.
151    pub name: String,
152}