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
|
//! Nodes are parts of an expression, but aren't expressions themselves.
//! Example: A BinaryExpression is made out of two expressions and an operator node,
//! because an operator can't be an expression by itself.
//!
//! Nodes can also contain nested expressions.
//! This module also contains the actual expression structs for this reason,
//! although this might be changed later?
use crate::lex::token::{Location, Token, TokenKind::*};
use super::{expression::Expression, statement::Statement};
#[derive(Debug, Clone)]
pub struct BinaryOperatorNode {
pub at: Location,
pub kind: BinaryOperatorKind,
}
#[derive(Debug, Clone, Copy)]
pub enum BinaryOperatorKind {
Plus,
Minus,
Star,
Slash,
Eq,
Neq,
Gt,
Gte,
Lt,
Lte,
And,
Or,
Assign,
ConstAssign,
Dot,
}
impl BinaryOperatorNode {
pub fn from_token(token: Token) -> Self {
use BinaryOperatorKind as Kind;
let kind = match token.kind {
OpPlus => Kind::Plus,
OpMinus => Kind::Minus,
OpStar => Kind::Star,
OpSlash => Kind::Slash,
OpEq => Kind::Eq,
OpNeq => Kind::Neq,
OpLt => Kind::Lt,
OpGt => Kind::Gt,
OpLte => Kind::Lte,
OpGte => Kind::Gte,
OpAnd => Kind::And,
OpOr => Kind::Or,
Assign => Kind::Assign,
ConstAssign => Kind::ConstAssign,
Dot => Kind::Dot,
_ => panic!("Can't create binary operator from '{:?}'.", token.kind),
};
Self {
at: token.location,
kind,
}
}
}
#[derive(Debug, Clone)]
pub struct UnaryOperatorNode {
pub at: Location,
pub kind: UnaryOperatorKind,
}
#[derive(Debug, Clone, Copy)]
pub enum UnaryOperatorKind {
Minus,
Not,
}
impl UnaryOperatorNode {
pub fn from_token(token: Token) -> Self {
use UnaryOperatorKind as Kind;
let kind = match token.kind {
OpMinus => Kind::Minus,
OpNot => Kind::Not,
_ => panic!("Can't create unary operator from '{:?}'.", token.kind),
};
Self {
at: token.location,
kind,
}
}
}
#[derive(Debug, Clone)]
pub struct SimpleLiteralNode {
pub at: Location,
pub kind: SimpleLiteralKind,
}
#[derive(Debug, Clone)]
pub enum SimpleLiteralKind {
Int(u32),
Float(f32),
Bool(bool),
}
impl SimpleLiteralNode {
pub fn from_token(token: Token) -> Self {
use SimpleLiteralKind as Kind;
let kind = match token.kind {
Int(int) => Kind::Int(int),
Float(float) => Kind::Float(float),
KeywordTrue => Kind::Bool(true),
KeywordFalse => Kind::Bool(false),
_ => panic!("Can't create literal from '{:?}'.", token.kind),
};
Self {
at: token.location,
kind,
}
}
}
pub type Identifier = String;
// If the constraint is None the type will have to be inferred
// during analysis.
#[derive(Debug, Clone)]
pub struct TypedIdentifierNode {
// TODO: Add locations to other parts, not just the start of the identifier.
// i.e. make an IdentifierNode or something like that.
pub at: Location,
pub identifier: Identifier,
pub type_constraint: Option<Identifier>,
}
#[derive(Debug, Clone)]
pub struct CallExpression {
// TODO: Ditto.
pub at: Location,
pub called: Expression,
pub arguments: Vec<Expression>,
}
#[derive(Debug, Clone)]
pub struct ArrayAccessExpression {
pub at: Location,
pub array: Expression,
pub index: Expression,
}
#[derive(Debug, Clone)]
pub struct MemberAccessExpression {
// TODO: Ditto.
pub at: Location,
pub object: Expression,
pub member_name: Identifier,
}
#[derive(Debug, Clone)]
pub struct StrExpression {
pub at: Location,
pub parts: Vec<StrPartNode>,
}
#[derive(Debug, Clone)]
pub struct StrPartNode {
pub at: Location,
pub kind: StrPartKind,
}
#[derive(Debug, Clone)]
pub enum StrPartKind {
Literal(String),
Embed(Expression),
}
#[derive(Debug, Clone)]
pub struct FnExpression {
pub at: Location,
pub header: FnHeaderNode,
pub body: BlockExpression,
}
#[derive(Debug, Clone)]
pub struct FnHeaderNode {
// TODO: ditto...
pub at: Location,
pub has_self_receiver: bool,
pub parameters: Vec<TypedIdentifierNode>,
pub return_type: Option<Identifier>,
}
#[derive(Debug, Clone)]
pub struct ArrayExpression {
pub at: Location,
pub elements: Vec<Expression>,
}
#[derive(Debug, Clone)]
pub struct IfExpression {
pub at: Location,
pub conditionals: Vec<ConditionalBlockNode>,
pub else_block: Option<BlockExpression>,
}
#[derive(Debug, Clone)]
pub struct LoopExpression {
pub at: Location,
pub condition: Option<Expression>,
pub body: BlockExpression,
}
#[derive(Debug, Clone)]
pub struct BlockExpression {
pub at: Location,
pub statements: Vec<Statement>,
pub tail_expression: Option<Expression>,
}
#[derive(Debug, Clone)]
pub struct ConditionalBlockNode {
pub at: Location,
pub condition: Expression,
pub block: BlockExpression,
}
|