Skip to main content

oak_mojo/ast/
mod.rs

1/// Mojo statement types.
2#[derive(Debug, Clone)]
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
4pub enum MojoStatement {
5    /// Function definition statement.
6    Function {
7        /// Function name.
8        name: String,
9        /// Function parameters as (name, type) pairs.
10        params: Vec<(String, Option<String>)>,
11        /// Return type annotation.
12        return_type: Option<String>,
13        /// Function body statements.
14        body: Vec<MojoStatement>,
15    },
16    /// Variable declaration statement.
17    Variable {
18        /// Variable name.
19        name: String,
20        /// Type annotation.
21        ty: Option<String>,
22        /// Initial value expression.
23        value: Option<MojoExpression>,
24        /// Whether this is a let binding (immutable).
25        is_let: bool,
26    },
27    /// Assignment statement.
28    Assignment {
29        /// Assignment target expression.
30        target: MojoExpression,
31        /// Value to assign.
32        value: MojoExpression,
33    },
34    /// If statement.
35    If {
36        /// Condition expression.
37        condition: MojoExpression,
38        /// Then branch statements.
39        then_body: Vec<MojoStatement>,
40        /// Optional else branch statements.
41        else_body: Option<Vec<MojoStatement>>,
42    },
43    /// While loop statement.
44    While {
45        /// Loop condition expression.
46        condition: MojoExpression,
47        /// Loop body statements.
48        body: Vec<MojoStatement>,
49    },
50    /// For loop statement.
51    For {
52        /// Loop variable name.
53        variable: String,
54        /// Iterable expression.
55        iterable: MojoExpression,
56        /// Loop body statements.
57        body: Vec<MojoStatement>,
58    },
59    /// Return statement.
60    Return(Option<MojoExpression>),
61    /// Expression statement.
62    Expression(MojoExpression),
63}
64
65/// Mojo expression types.
66#[derive(Debug, Clone)]
67#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
68pub enum MojoExpression {
69    /// Literal expression.
70    Literal(MojoLiteral),
71    /// Identifier expression.
72    Identifier(String),
73    /// Binary expression with left operand, operator, and right operand.
74    Binary {
75        /// Left operand.
76        left: Box<MojoExpression>,
77        /// Operator.
78        op: String,
79        /// Right operand.
80        right: Box<MojoExpression>,
81    },
82    /// Unary expression with operator and operand.
83    Unary {
84        /// Operator.
85        op: String,
86        /// Operand.
87        right: Box<MojoExpression>,
88    },
89    /// Function call expression.
90    Call {
91        /// Callee expression.
92        callee: Box<MojoExpression>,
93        /// Call arguments.
94        args: Vec<MojoExpression>,
95    },
96}
97
98/// Mojo literal types.
99#[derive(Debug, Clone)]
100#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
101pub enum MojoLiteral {
102    /// Integer literal.
103    Int(i64),
104    /// Float literal.
105    Float(f64),
106    /// String literal.
107    String(String),
108    /// Boolean literal.
109    Bool(bool),
110    /// None literal.
111    None,
112}