1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//! The AST (abstract-syntax-tree) types with an integrated type checker.

/// The main statement enum, allowing for grouping of statements inside of the
/// AST.
#[derive(Debug, PartialEq)]
pub enum StatementNode {
    Function(Function),         // Function
    WhileLoop(WhileLoop),       // While loop
    Variable(Variable),         // Variable decleration
    If(If),                     // If statement
    Expression(ExpressionNode), // Encapsulates a single expression.
    Return(ExpressionNode),     // Return statement
}

/// A node for an if statement, giving an expression to evaluate along with a
/// `body` of multiple statements.
///
/// # Syntax example
///
/// ```zypo
/// if(2 + 1 == 3) {
///     --snip--
/// }
/// ```
#[derive(Debug, PartialEq)]
pub struct If {
    pub condition: ExpressionNode,
    pub body: Vec<StatementNode>,
}

/// The main expression enum, allowing for grouping of all expressions inside
/// of the AST.
#[derive(Debug, PartialEq)]
pub enum ExpressionNode {
    /// A binary operation (basic maths) involving 2 recursive
    /// [ExpressionNode]s.
    BinOp(Box<ExpressionNode>, BinOp, Box<ExpressionNode>),

    /// A constant forming from [Constant]
    Constant(Constant),

    /// Pointer to a [Variable]/[Parameter] that may or may not be existant.
    VariablePoint(String),

    /// Function call. Read [FunctionCall] for more info
    FunctionCall(FunctionCall),
}

/// A binary operation type. Having a seperate enum for this avoids repitition
/// and allows for some optimisations downstream.
#[derive(Debug, PartialEq)]
pub enum BinOp {
    /// Addition. Example: `10+14` is `24`
    Add,

    /// Subtraction. Example: `5-3` is `2`
    Sub,

    /// Division. Example: `32/4` is `8`
    Div,

    /// Multiplication. Example: `5*11` is `55`
    Mul,

    /// Power of. Example: (`3^3` or `3**3`) is `81`
    Power,

    /// Modulo. Example: `20 % 2` is `0`
    Mod,

    /// Equals to. Example: `20 == 2` is `false`
    IsEqual,

    /// Not equals to. Example: `20 == 2` is `true`. This is the opposite of
    /// [BinOp::IsEqual]
    NotEqual,

    /// Greater than operator. Example: `5 > 6` is `false`.
    GreaterThan,

    /// Less than operator. Example: `5 < 6` is `true`.
    LessThan,

    /// Similar to [BinOp::LessThan] and [BinOp::IsEqual] combined. If it is
    /// less than `x` OR equal to `x`.
    LessThanOrEqual,

    /// Similar to [BinOp::GreaterThan] and [BinOp::IsEqual] combined. If it is
    /// greater than `x` OR equal to `x`.
    GreaterThanOrEqual,

    /// Increment x by y. Take this syntax for example: `x += y`.
    PlusAssign,

    /// Decrement x by y. Take this syntax for example: `x += y`.
    SubtractAssign,

    /// An `or` / `||` operator for checking expressions that have to include 1
    /// or more as `true` (similar to how [BinOp::IsEqual] works).
    Or,

    /// An `and` / `&&` operator for checking expressions that have to include
    /// all as `true`.
    And,
}

/// A while loop. This goes down into [WhileLoop].
///
/// # Syntax example
///
/// ```zypo
/// while(50 > 49) {
///     // --snip--
/// }
/// ```
#[derive(Debug, PartialEq)]
pub struct WhileLoop {
    pub condition: ExpressionNode,
    pub body: Vec<StatementNode>,
}

/// A function call expression.
///
/// # Syntax example
///
/// ```zypo
/// hello(34, "hi")
/// ```
#[derive(Debug, PartialEq)]
pub struct FunctionCall {
    pub ident: String,
    pub expr_params: Vec<ExpressionNode>,
}

/// Similar to the [VarType] enum but with defined data in each possible
/// option.
#[derive(Debug, PartialEq)]
pub enum Constant {
    /// Integer constant that translates to [i32].
    Int(i32),

    // String constant that translates to [String].
    Str(String),

    // Bool constant that translates to [bool].
    Bool(bool),
}

/// Variable types for zypo-rs.
///
/// NOTE: Please see [Constant] if you would like to include the literal value,
/// not just the type in certain cases.
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum VarType {
    /// An integer type (whole number).
    Int,

    /// A string type (char array/other types). This is not fully worked out yet.
    Str,

    /// A boolean (true/false).
    Bool,

    /// A null type, primarily used for functions that do not return anything.
    Void,
}

/// A function statement, the basis for the whole language.
///
/// # Syntax example
///
/// ```zypo
/// fun number_adder(first_num: int, second_num: int) -> int {
///     --snip--
/// }
/// ```
#[derive(Debug, PartialEq)]
pub struct Function {
    /// Function identifier.
    pub ident: String,

    /// [Parameter]s declared inside of the function signature.
    pub params: Vec<Parameter>,

    /// The main body inside of the function.
    pub body: Vec<StatementNode>,

    /// A markdown-compatible [String] that is a documenation comment.
    /// 
    /// NOTE: This may be bound to a trait in the future to extend to other
    /// datatypes.
    pub docs: Option<String>,

    /// Expected return type inside of the function signature.
    pub return_type: VarType,
}

/// AST node that contains a parameter (used inside of function declerations or
/// for the start of [Variable]).
///
/// # Syntax example
///
/// ```none
/// my_id: str
/// ```
#[derive(Debug, PartialEq)]
pub struct Parameter {
    /// Name of the parameter
    pub ident: String,

    /// Expected parameter type
    pub ty: VarType,
}

/// Variable that includes a parameter but extends with a recursive
/// [ExpressionNode].
///
/// # Syntax example
///
/// ```zypo
/// var x: int = 3432;
/// ```
#[derive(Debug, PartialEq)]
pub struct Variable {
    /// Variable name/identifier.
    pub ident: String,

    /// The expected type of the varible.
    pub ty: VarType,

    /// An expression body that can be evaluated (1x [ExpressionNode]).
    pub body: Box<ExpressionNode>,
}