zypo_parser/ast.rs
1/// The main statement enum, allowing for grouping of statements inside of the
2/// AST.
3#[derive(Debug, PartialEq)]
4pub enum StatementNode {
5 Function(Function),
6 WhileLoop(WhileLoop),
7 Variable(Variable),
8}
9
10/// The main expression enum, allowing for grouping of all expressions inside
11/// of the AST.
12#[derive(Debug, PartialEq)]
13pub enum ExpressionNode {
14 /// A binary operation (basic maths) involving 2 recursive
15 /// [ExpressionNode]s.
16 BinOp(Box<ExpressionNode>, BinOp, Box<ExpressionNode>),
17
18 /// A constant forming from [Constant]
19 Constant(Constant),
20}
21
22/// A binary operation type. Having a seperate enum for this avoids repitition
23/// and allows for some optimisations downstream.
24#[derive(Debug, PartialEq)]
25pub enum BinOp {
26 /// Addition. Example: `10+14` is `24`
27 Add,
28
29 /// Subtraction. Example: `5-3` is `2`
30 Sub,
31
32 /// Division. Example: `32/4` is `8`
33 Div,
34
35 /// Multiplication. Example: `5*11` is `55`
36 Mul,
37
38 /// Power of. Example: (`3^3` or `3**3`) is `81`
39 Power,
40
41 /// Modulo. Example: `20 % 2` is `0`
42 Mod,
43}
44
45/// A while loop. This goes down into [WhileLoop] and would look something
46/// like this in Zypo:
47///
48/// ```zypo
49/// while(50 > 49) {
50/// // --snip--
51/// }
52/// ```
53#[derive(Debug, PartialEq)]
54pub struct WhileLoop {
55 pub condition: ExpressionNode,
56 pub body: Vec<StatementNode>,
57}
58
59/// Similar to the [VarType] enum but with defined data in each possible
60/// option.
61#[derive(Debug, PartialEq)]
62pub enum Constant {
63 Int(i32),
64 Str(String),
65 Bool(bool),
66}
67
68/// Variable types for zypo-rs.
69#[derive(Debug, PartialEq)]
70pub enum VarType {
71 /// An integer type (whole number).
72 Int,
73
74 /// A string type (char array/other types). This is not fully worked out yet.
75 Str,
76
77 /// A boolean (true/false).
78 Bool,
79
80 /// A null type, primarily used for functions that do not return anything.
81 Void,
82}
83
84/// A function statement, the basis for the whole language. An example of a
85/// function statement would be the following inside of Zypo itself:
86///
87/// ```zypo
88/// fun number_adder(first_num: int, second_num: int) -> int {
89/// // --snip--
90/// }
91/// ```
92#[derive(Debug, PartialEq)]
93pub struct Function {
94 pub ident: String,
95 pub params: Vec<Parameter>,
96 pub body: Vec<StatementNode>,
97 pub return_type: VarType,
98}
99
100/// A eration of a parameter. This would originally look like `my_id: str`
101/// when being parsed.
102///
103/// This structure is usually used with the [Function] to generate the
104/// parameters.
105///
106/// # Parser input
107///
108/// ```none
109/// my_id: str
110/// ```
111///
112/// # Generated AST
113///
114/// ```none
115/// Parameter { ident: "my_id".to_string(), ty: VarType::Str }
116/// ```
117#[derive(Debug, PartialEq)]
118pub struct Parameter {
119 pub ident: String,
120 pub ty: VarType,
121}
122
123/// Variable that includes a parameter but extends with a recursive
124/// [ExpressionNode].
125///
126/// A variable declaration inside of the language would look like the
127/// following:
128///
129/// ```zypo
130/// var x: int = 3432;
131/// ```
132#[derive(Debug, PartialEq)]
133pub struct Variable {
134 pub ident: String,
135 pub ty: VarType,
136 pub body: Box<ExpressionNode>,
137}