zypo_lib/parser/ast.rs
1//! The AST (abstract-syntax-tree) types with an integrated type checker.
2
3/// The main statement enum, allowing for grouping of statements inside of the
4/// AST.
5#[derive(Debug, PartialEq)]
6pub enum StatementNode {
7 Function(Function), // Function
8 WhileLoop(WhileLoop), // While loop
9 Variable(Variable), // Variable decleration
10 If(If), // If statement
11 Expression(ExpressionNode), // Encapsulates a single expression.
12 Return(ExpressionNode), // Return statement
13}
14
15/// A node for an if statement, giving an expression to evaluate along with a
16/// `body` of multiple statements.
17///
18/// # Syntax example
19///
20/// ```zypo
21/// if(2 + 1 == 3) {
22/// --snip--
23/// }
24/// ```
25#[derive(Debug, PartialEq)]
26pub struct If {
27 pub condition: ExpressionNode,
28 pub body: Vec<StatementNode>,
29}
30
31/// The main expression enum, allowing for grouping of all expressions inside
32/// of the AST.
33#[derive(Debug, PartialEq)]
34pub enum ExpressionNode {
35 /// A binary operation (basic maths) involving 2 recursive
36 /// [ExpressionNode]s.
37 BinOp(Box<ExpressionNode>, BinOp, Box<ExpressionNode>),
38
39 /// A constant forming from [Constant]
40 Constant(Constant),
41
42 /// Pointer to a [Variable]/[Parameter] that may or may not be existant.
43 VariablePoint(String),
44
45 /// Function call. Read [FunctionCall] for more info
46 FunctionCall(FunctionCall),
47}
48
49/// A binary operation type. Having a seperate enum for this avoids repitition
50/// and allows for some optimisations downstream.
51#[derive(Debug, PartialEq)]
52pub enum BinOp {
53 /// Addition. Example: `10+14` is `24`
54 Add,
55
56 /// Subtraction. Example: `5-3` is `2`
57 Sub,
58
59 /// Division. Example: `32/4` is `8`
60 Div,
61
62 /// Multiplication. Example: `5*11` is `55`
63 Mul,
64
65 /// Power of. Example: (`3^3` or `3**3`) is `81`
66 Power,
67
68 /// Modulo. Example: `20 % 2` is `0`
69 Mod,
70
71 /// Equals to. Example: `20 == 2` is `false`
72 IsEqual,
73
74 /// Not equals to. Example: `20 == 2` is `true`. This is the opposite of
75 /// [BinOp::IsEqual]
76 NotEqual,
77
78 /// Greater than operator. Example: `5 > 6` is `false`.
79 GreaterThan,
80
81 /// Less than operator. Example: `5 < 6` is `true`.
82 LessThan,
83
84 /// Similar to [BinOp::LessThan] and [BinOp::IsEqual] combined. If it is
85 /// less than `x` OR equal to `x`.
86 LessThanOrEqual,
87
88 /// Similar to [BinOp::GreaterThan] and [BinOp::IsEqual] combined. If it is
89 /// greater than `x` OR equal to `x`.
90 GreaterThanOrEqual,
91
92 /// Increment x by y. Take this syntax for example: `x += y`.
93 PlusAssign,
94
95 /// Decrement x by y. Take this syntax for example: `x += y`.
96 SubtractAssign,
97
98 /// An `or` / `||` operator for checking expressions that have to include 1
99 /// or more as `true` (similar to how [BinOp::IsEqual] works).
100 Or,
101
102 /// An `and` / `&&` operator for checking expressions that have to include
103 /// all as `true`.
104 And,
105}
106
107/// A while loop. This goes down into [WhileLoop].
108///
109/// # Syntax example
110///
111/// ```zypo
112/// while(50 > 49) {
113/// // --snip--
114/// }
115/// ```
116#[derive(Debug, PartialEq)]
117pub struct WhileLoop {
118 pub condition: ExpressionNode,
119 pub body: Vec<StatementNode>,
120}
121
122/// A function call expression.
123///
124/// # Syntax example
125///
126/// ```zypo
127/// hello(34, "hi")
128/// ```
129#[derive(Debug, PartialEq)]
130pub struct FunctionCall {
131 pub ident: String,
132 pub expr_params: Vec<ExpressionNode>,
133}
134
135/// Similar to the [VarType] enum but with defined data in each possible
136/// option.
137#[derive(Debug, PartialEq)]
138pub enum Constant {
139 /// Integer constant that translates to [i32].
140 Int(i32),
141
142 // String constant that translates to [String].
143 Str(String),
144
145 // Bool constant that translates to [bool].
146 Bool(bool),
147}
148
149/// Variable types for zypo-rs.
150///
151/// NOTE: Please see [Constant] if you would like to include the literal value,
152/// not just the type in certain cases.
153#[derive(Debug, PartialEq, Clone, Copy)]
154pub enum VarType {
155 /// An integer type (whole number).
156 Int,
157
158 /// A string type (char array/other types). This is not fully worked out yet.
159 Str,
160
161 /// A boolean (true/false).
162 Bool,
163
164 /// A null type, primarily used for functions that do not return anything.
165 Void,
166}
167
168/// A function statement, the basis for the whole language.
169///
170/// # Syntax example
171///
172/// ```zypo
173/// fun number_adder(first_num: int, second_num: int) -> int {
174/// --snip--
175/// }
176/// ```
177#[derive(Debug, PartialEq)]
178pub struct Function {
179 /// Function identifier.
180 pub ident: String,
181
182 /// [Parameter]s declared inside of the function signature.
183 pub params: Vec<Parameter>,
184
185 /// The main body inside of the function.
186 pub body: Vec<StatementNode>,
187
188 /// A markdown-compatible [String] that is a documenation comment.
189 ///
190 /// NOTE: This may be bound to a trait in the future to extend to other
191 /// datatypes.
192 pub docs: Option<String>,
193
194 /// Expected return type inside of the function signature.
195 pub return_type: VarType,
196}
197
198/// AST node that contains a parameter (used inside of function declerations or
199/// for the start of [Variable]).
200///
201/// # Syntax example
202///
203/// ```none
204/// my_id: str
205/// ```
206#[derive(Debug, PartialEq)]
207pub struct Parameter {
208 /// Name of the parameter
209 pub ident: String,
210
211 /// Expected parameter type
212 pub ty: VarType,
213}
214
215/// Variable that includes a parameter but extends with a recursive
216/// [ExpressionNode].
217///
218/// # Syntax example
219///
220/// ```zypo
221/// var x: int = 3432;
222/// ```
223#[derive(Debug, PartialEq)]
224pub struct Variable {
225 /// Variable name/identifier.
226 pub ident: String,
227
228 /// The expected type of the varible.
229 pub ty: VarType,
230
231 /// An expression body that can be evaluated (1x [ExpressionNode]).
232 pub body: Box<ExpressionNode>,
233}