Testing stuff for an upcoming project
1use ecow::EcoString;
2use num_bigint::BigInt;
3
4use crate::lexer::Span;
5
6pub type UntypedModule = Module<()>;
7
8#[derive(Debug)]
9pub struct Module<Type> {
10 pub ast: Vec<Definition<Type>>,
11}
12
13pub type UntypedDefinition = Definition<()>;
14
15#[derive(Debug)]
16pub enum Definition<Type> {
17 Function(Function<Type>),
18 Constant(Constant<Type>),
19 Struct(StructDefinition),
20 Union,
21 Interface,
22 Impl,
23}
24
25pub type UntypedFunction = Function<()>;
26
27#[derive(Debug)]
28pub struct Function<Type> {
29 pub publicity: Publicity,
30 pub name: EcoString,
31 pub parameters: Vec<Parameter<Type>>,
32 pub return_type: Type,
33 pub return_annotation: Option<Annotation>,
34 pub body: Vec<Statement<Type>>,
35 pub location: Span,
36}
37
38pub type UntypedConstant = Constant<()>;
39
40#[derive(Debug)]
41pub struct Constant<Type> {
42 pub publicity: Publicity,
43 pub name: EcoString,
44 pub annotation: Option<Annotation>,
45 pub value: Expression<Type>,
46 pub location: Span,
47}
48
49#[derive(Debug)]
50pub struct StructDefinition {
51 pub publicity: Publicity,
52 pub name: EcoString,
53 pub parameters: Vec<EcoString>,
54 pub fields: Vec<StructField>,
55 pub location: Span,
56}
57
58#[derive(Debug)]
59pub struct StructField {
60 pub publicity: Publicity,
61 pub name: EcoString,
62 pub annotation: Annotation,
63}
64
65#[derive(Debug, Clone, Copy)]
66pub enum Publicity {
67 Public,
68 Private,
69}
70
71pub type UntypedParameter = Parameter<()>;
72
73#[derive(Debug)]
74pub struct Parameter<Type> {
75 pub label: Option<EcoString>,
76 pub name: EcoString,
77 pub annotation: Option<Annotation>,
78 pub type_: Type,
79}
80
81#[derive(Debug)]
82pub enum Annotation {
83 Name {
84 location: Span,
85 name: EcoString,
86 generics: Vec<Self>,
87 },
88 Tuple {
89 location: Span,
90 elements: Vec<Self>,
91 },
92 Pointer {
93 location: Span,
94 mutability: Mutability,
95 underlying: Box<Self>,
96 },
97 Array {
98 location: Span,
99 length: Option<usize>,
100 element: Box<Self>,
101 },
102 List {
103 location: Span,
104 element: Box<Self>,
105 },
106 Map {
107 location: Span,
108 key: Box<Self>,
109 value: Box<Self>,
110 },
111}
112
113impl Annotation {
114 pub fn location(&self) -> Span {
115 match self {
116 Annotation::Name { location, .. }
117 | Annotation::Tuple { location, .. }
118 | Annotation::Pointer { location, .. }
119 | Annotation::Array { location, .. }
120 | Annotation::List { location, .. }
121 | Annotation::Map { location, .. } => *location,
122 }
123 }
124}
125
126#[derive(Debug, Clone, Copy)]
127pub enum Mutability {
128 Immutable,
129 Mutable,
130}
131
132pub type UntypedStatement = Statement<()>;
133
134#[derive(Debug)]
135pub enum Statement<Type> {
136 Expression(Expression<Type>),
137}
138
139pub type UntypedExpression = Expression<()>;
140
141#[derive(Debug)]
142pub enum Expression<Type> {
143 Int {
144 location: Span,
145 value: BigInt,
146 },
147 Bool {
148 location: Span,
149 value: bool,
150 },
151 Float {
152 location: Span,
153 value: f64,
154 },
155 String {
156 location: Span,
157 value: EcoString,
158 },
159 Group {
160 location: Span,
161 inner: Box<Self>,
162 },
163 Tuple {
164 location: Span,
165 elements: Vec<Self>,
166 type_: Type,
167 },
168 Array {
169 location: Span,
170 elements: Vec<Self>,
171 type_: Type,
172 },
173 Map {
174 location: Span,
175 elements: Vec<(Self, Self)>,
176 type_: Type,
177 },
178 Block {
179 location: Span,
180 statements: Vec<Statement<Type>>,
181 },
182 Struct {
183 location: Span,
184 struct_: Box<Self>,
185 fields: Vec<Argument<Self>>,
186 type_: Type,
187 },
188 Variable {
189 location: Span,
190 name: EcoString,
191 type_: Type,
192 },
193 BinaryOperator {
194 location: Span,
195 left: Box<Self>,
196 operator: BinaryOperator,
197 right: Box<Self>,
198 type_: Type,
199 },
200 UnaryOperator {
201 location: Span,
202 value: Box<Self>,
203 operator: UnaryOperator,
204 type_: Type,
205 },
206 Function {
207 location: Span,
208 parameters: Vec<Parameter<Type>>,
209 return_annotation: Option<Annotation>,
210 body: Vec<Statement<Type>>,
211 type_: Type,
212 },
213 Call {
214 location: Span,
215 function: Box<Self>,
216 arguments: Vec<Argument<Self>>,
217 type_: Type,
218 },
219 FieldAccess {
220 location: Span,
221 left: Box<Self>,
222 field: EcoString,
223 type_: Type,
224 },
225 Index {
226 location: Span,
227 left: Box<Self>,
228 index: Box<Self>,
229 type_: Type,
230 },
231 If {
232 location: Span,
233 condition: Box<Self>,
234 body: Vec<Statement<Type>>,
235 else_branch: Option<Box<Self>>,
236 },
237 Match {
238 location: Span,
239 },
240}
241
242impl<Type> Expression<Type> {
243 pub fn location(&self) -> Span {
244 match self {
245 Expression::Int { location, .. }
246 | Expression::Bool { location, .. }
247 | Expression::Float { location, .. }
248 | Expression::String { location, .. }
249 | Expression::Group { location, .. }
250 | Expression::Tuple { location, .. }
251 | Expression::Array { location, .. }
252 | Expression::Map { location, .. }
253 | Expression::Block { location, .. }
254 | Expression::Struct { location, .. }
255 | Expression::Variable { location, .. }
256 | Expression::BinaryOperator { location, .. }
257 | Expression::UnaryOperator { location, .. }
258 | Expression::Function { location, .. }
259 | Expression::Call { location, .. }
260 | Expression::FieldAccess { location, .. }
261 | Expression::Index { location, .. }
262 | Expression::If { location, .. }
263 | Expression::Match { location, .. } => *location,
264 }
265 }
266}
267
268#[derive(Debug, Clone, Copy)]
269pub enum BinaryOperator {
270 Assign,
271 AddAssign,
272 SubAssign,
273 MulAssign,
274 DivAssign,
275 ModAssign,
276
277 LogicalAnd,
278 LogicalOr,
279
280 Less,
281 LessEquals,
282 Greater,
283 GreaterEquals,
284 Equal,
285 NotEqual,
286
287 LeftShift,
288 RightShift,
289 ArithmeticRightShift,
290 BitwiseAnd,
291 BitwiseOr,
292 BitwiseXor,
293
294 Add,
295 Subtract,
296
297 Multiply,
298 Divide,
299 Remainder,
300
301 Power,
302}
303
304#[derive(Debug, Clone, Copy)]
305pub enum UnaryOperator {
306 Increment,
307 Decrement,
308 ErrorPropagation,
309
310 Plus,
311 Minus,
312 LogicalNot,
313 Dereference,
314 Reference,
315 MutableReference,
316 BitwiseNot,
317}
318
319#[derive(Debug)]
320pub struct Argument<Value> {
321 pub label: Option<EcoString>,
322 pub value: Value,
323}