-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapper.rs
752 lines (728 loc) · 35.5 KB
/
mapper.rs
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
use std::iter::FromIterator;
use crate::frontend::ast::*;
use crate::frontend::error::{FrontendError, FrontendErrorKind};
use crate::frontend::typechecker::typechecker::TypeChecker;
use crate::frontend::typechecker::util::ToTypeEnv;
use crate::meta::{GetLocation, GetType, LocationMeta, TypeMeta};
use crate::util::env::Env;
use crate::util::mapper::AstMapper;
pub type TypeCheckResult<AstT> = Result<AstT, Vec<FrontendError<LocationMeta>>>;
impl AstMapper<LocationMeta, TypeMeta, FrontendError<LocationMeta>> for TypeChecker<'_> {
fn map_var_reference(&mut self, r: &Reference<LocationMeta>) -> TypeCheckResult<Reference<TypeMeta>> {
let loc = r.get_meta();
let typecheck_result = match &r.item {
ReferenceKind::Ident { ident } => {
let var_t = self.get_variable(ident, loc)?;
// if the variable actually refers to current class member, mark it explicitly
// this is necessary for compiler backend to assign correct operation later
if self.is_class_variable(ident) {
// we use unwrap on current class because is_class_variable implies it exists
let typed_self_reference = ReferenceKind::TypedObject {
obj: String::from("self"),
cls: self.get_current_class().unwrap().item.get_key().clone(),
field: ident.clone()
};
Ok((typed_self_reference, var_t.clone()))
} else {
Ok((ReferenceKind::Ident { ident: ident.clone() }, var_t.clone()))
}
}
ReferenceKind::Object { obj, field } => {
let var_t = self.get_variable(obj, loc)?;
if let Type::Array { item_t } = var_t {
// manually check field name and convert ReferenceKind to ArrayLen
if field == "length" {
Ok((ReferenceKind::ArrayLen { ident: obj.clone() }, Type::Int))
} else {
let kind = FrontendErrorKind::EnvError {
message: format!("Invalid instance variable for array: {}", field)
};
Err(vec![FrontendError::new(kind, loc.clone())])
}
} else {
// interpret var_t as object and try to get the `field` instance variable
let cls = self.get_class(var_t, loc)?;
let field_t = self.get_instance_variable(cls, field, loc)?;
if self.is_class_variable(obj) {
// map reference to TypedMemberObject, backend needs more than just name of the class
let typed_self_reference = ReferenceKind::TypedMemberObject {
self_cls: self.get_current_class().unwrap().item.get_key().clone(),
obj: obj.clone(),
obj_class: cls.item.get_key().clone(),
field: field.clone(),
};
Ok((typed_self_reference, field_t.clone()))
} else {
// map reference to TypedObject, backend needs to know the name of the class
let mapped_ref = ReferenceKind::TypedObject {
obj: obj.clone(),
cls: cls.item.get_key().clone(),
field: field.clone()
};
Ok((mapped_ref, field_t.clone()))
}
}
}
ReferenceKind::ObjectSelf { field } => {
if let Some(cls) = self.get_current_class() {
let field_t = self.get_instance_variable(cls, field, loc)?;
let typed_self_reference = ReferenceKind::TypedObject {
obj: String::from("self"),
cls: cls.item.get_key().clone(),
field: field.clone()
};
Ok((typed_self_reference, field_t.clone()))
} else {
let kind = FrontendErrorKind::EnvError {
message: String::from("No object in the current context")
};
Err(vec![FrontendError::new(kind, loc.clone())])
}
}
ReferenceKind::Array { arr, idx } => {
// TODO: Refactor to 2 simpler methods (like class & member)
let var_t = self.get_variable(arr, loc)?;
if let Type::Array { item_t } = var_t {
let item_t = *item_t.clone();
let mapped_expr = self.map_expression(idx)?;
let mapped_t = &mapped_expr.get_meta().t;
if *mapped_t == Type::Int {
Ok((ReferenceKind::Array { arr: arr.clone(), idx: Box::new(mapped_expr) }, item_t))
} else {
let kind = FrontendErrorKind::TypeError {
expected: Type::Int,
actual: mapped_t.clone(),
};
Err(vec![FrontendError::new(kind, loc.clone())])
}
} else {
let kind = FrontendErrorKind::TypeError {
expected: Type::Array { item_t: Box::new(Type::Any) },
actual: var_t.clone(),
};
Err(vec![FrontendError::new(kind, loc.clone())])
}
}
_ => unreachable!()
};
match typecheck_result {
Ok((kind, t)) => {
let mapped: Reference<TypeMeta> = Reference::new(kind, TypeMeta { t });
Ok(mapped)
}
Err(err_vec) => Err(err_vec),
}
}
fn map_func_reference(&mut self, r: &Reference<LocationMeta>) -> TypeCheckResult<Reference<TypeMeta>> {
let loc = r.get_meta();
let typecheck_result = match &r.item {
ReferenceKind::Ident { ident } => {
let func_t = self.get_func(ident, loc)?;
Ok((ReferenceKind::Ident { ident: ident.clone() }, func_t.clone()))
}
ReferenceKind::Object { obj, field } => {
let var_t = self.get_variable(obj, loc)?;
let cls = self.get_class(var_t, loc)?;
let method_t = self.get_method(cls, field, loc)?;
// similarly to the variable case, if we refer to member function of a class member
// we need to provide additional information about self class for the compiler
if self.is_class_variable(obj) {
let typed_self_reference = ReferenceKind::TypedMemberObject {
self_cls: self.get_current_class().unwrap().item.get_key().clone(),
obj: obj.clone(),
obj_class: cls.item.get_key().clone(),
field: field.clone(),
};
Ok((typed_self_reference, method_t.clone()))
} else {
let typed_reference = ReferenceKind::TypedObject {
obj: obj.clone(),
cls: cls.item.get_key().clone(),
field: field.clone()
};
Ok((typed_reference, method_t.clone()))
}
}
ReferenceKind::ObjectSelf { field } => {
if let Some(cls) = self.get_current_class() {
let method_t = self.get_method(cls, field, loc)?;
let typed_self_reference = ReferenceKind::TypedObject {
obj: String::from("self"),
cls: cls.item.get_key().clone(),
field: field.clone()
};
Ok((typed_self_reference, method_t.clone()))
} else {
let kind = FrontendErrorKind::EnvError {
message: String::from("No object in the current context")
};
Err(vec![FrontendError::new(kind, loc.clone())])
}
}
r => {
let kind = FrontendErrorKind::ArgumentError {
message: format!("Expected function or method, got: {:?}", r)
};
Err(vec![FrontendError::new(kind, loc.clone())])
}
};
// TODO: Refactor to separate function (or better: From trait)
match typecheck_result {
Ok((kind, t)) => {
let mapped: Reference<TypeMeta> = Reference::new(kind, TypeMeta { t });
Ok(mapped)
}
Err(err_vec) => Err(err_vec),
}
}
fn map_block(&mut self, block: &Block<LocationMeta>) -> TypeCheckResult<Block<TypeMeta>> {
let mut mapped_stmts = Vec::new();
let mut errors = Vec::new();
for block_stmt in block.item.stmts.iter() {
match self.map_statement(&block_stmt) {
Ok(mapped_stmt) => {
mapped_stmts.push(Box::new(mapped_stmt));
}
Err(mut v) => {
errors.append(&mut v);
}
}
}
if errors.is_empty() {
// return type is always determined by last statement thanks to the BlockOrganizer
let return_t = match mapped_stmts.last() {
Some(stmt) => stmt.get_meta().clone(),
None => TypeMeta { t: Type::Void },
};
let item = BlockItem::<TypeMeta> { stmts: mapped_stmts };
Ok(Block::new(item, return_t.clone()))
} else {
Err(errors)
}
}
fn map_expression(&mut self, expr: &Expression<LocationMeta>) -> TypeCheckResult<Expression<TypeMeta>> {
let typecheck_result = match &expr.item {
ExpressionKind::LitInt { val } => {
Ok((ExpressionKind::LitInt { val: val.clone() }, Type::Int))
}
ExpressionKind::LitBool { val } => {
Ok((ExpressionKind::LitBool { val: val.clone() }, Type::Bool))
}
ExpressionKind::LitStr { val } => {
Ok((ExpressionKind::LitStr { val: val.clone() }, Type::Str))
}
ExpressionKind::LitNull => {
Ok((ExpressionKind::LitNull, Type::Null))
}
ExpressionKind::App { r, args } => {
let mapped_r = self.map_func_reference(&r)?;
match &mapped_r.get_meta().t {
Type::Function { args: exp_args, ret } => {
let mut mapped_args = Vec::new();
let mut errors: Vec<FrontendError<LocationMeta>> = Vec::new();
if exp_args.len() != args.len() {
let kind = FrontendErrorKind::ArgumentError {
message: format!(
"Incorrect argument count, expected {} got {}",
exp_args.len(),
args.len()
)
};
errors.push(FrontendError::new(kind, r.get_location()));
} else {
for (expected_arg_type, arg_expr) in exp_args.iter().zip(args.iter()) {
match self.map_expression(&arg_expr) {
Ok(mapped_arg) => {
let assignment_check = self.check_assignment(
&expected_arg_type,
&mapped_arg.get_meta().t,
);
if let Err(kind) = assignment_check {
errors.push(FrontendError::new(kind, arg_expr.get_location()));
} else {
mapped_args.push(Box::new(mapped_arg));
}
}
Err(mut err_vec) => {
errors.append(&mut err_vec);
}
}
}
}
if errors.is_empty() {
let t = *ret.clone();
Ok((ExpressionKind::App { r: mapped_r, args: mapped_args }, t))
} else {
Err(errors)
}
}
t => {
let kind = FrontendErrorKind::TypeError {
expected: Type::Function { args: vec![], ret: Box::new(Type::Any) },
actual: t.clone(),
};
Err(vec![FrontendError::new(kind, r.get_location())])
}
}
}
ExpressionKind::Unary { op, arg } => {
let op_t = match op {
UnaryOperator::Neg => Type::Int,
UnaryOperator::Not => Type::Bool,
};
let mapped_arg = self.map_expression(&arg)?;
let t = mapped_arg.get_type();
if t == op_t {
Ok((ExpressionKind::Unary { op: op.clone(), arg: Box::new(mapped_arg) }, t))
} else {
let kind = FrontendErrorKind::TypeError {
expected: op_t,
actual: mapped_arg.get_type(),
};
Err(vec![FrontendError::new(kind, arg.get_location())])
}
}
ExpressionKind::Binary { left, op, right } => {
// TODO: Collect errors from both sides before terminating
let mapped_l = self.map_expression(&left)?;
let mapped_r = self.map_expression(&right)?;
if mapped_l.get_meta() == mapped_r.get_meta() {
let left_t = mapped_l.get_type();
let op_result_t = match op {
BinaryOperator::Equal | BinaryOperator::NotEqual => {
Option::Some(Type::Bool)
}
BinaryOperator::Plus => {
if left_t == Type::Str || left_t == Type::Int {
Option::Some(left_t.clone())
} else {
Option::None
}
}
BinaryOperator::And | BinaryOperator::Or => {
if left_t == Type::Bool {
Option::Some(Type::Bool)
} else {
Option::None
}
}
BinaryOperator::Greater
| BinaryOperator::GreaterEqual
| BinaryOperator::LessEqual
| BinaryOperator::Less => {
if left_t == Type::Int {
Option::Some(Type::Bool)
} else {
Option::None
}
}
_ => {
if left_t == Type::Int {
Option::Some(Type::Int)
} else {
Option::None
}
}
};
if let Some(result_t) = op_result_t {
let mapped_expr = ExpressionKind::Binary {
left: Box::new(mapped_l),
op: op.clone(),
right: Box::new(mapped_r),
};
Ok((mapped_expr, result_t))
} else {
let kind = FrontendErrorKind::ArgumentError {
message: format!(
"Invalid argument type {:?} for operator {:?}",
mapped_l.get_type(),
op
)
};
Err(vec![FrontendError::new(kind, left.get_location())])
}
} else {
let kind = FrontendErrorKind::TypeError {
expected: mapped_l.get_type(),
actual: mapped_r.get_type(),
};
Err(vec![FrontendError::new(kind, right.get_location())])
}
}
ExpressionKind::InitDefault { t } => {
Ok((ExpressionKind::InitDefault { t: t.clone() }, t.clone()))
}
ExpressionKind::InitArr { t, size } => {
let mapped_size = self.map_expression(&size)?;
if mapped_size.get_meta().t == Type::Int {
// in this scope, t is always an array (by parser definition)
// + the type that we return will always be checked by the caller
Ok((ExpressionKind::InitArr { t: t.clone(), size: Box::new(mapped_size) }, t.clone()))
} else {
let kind = FrontendErrorKind::TypeError {
expected: Type::Int,
actual: mapped_size.get_type(),
};
Err(vec![FrontendError::new(kind, size.get_location())])
}
}
ExpressionKind::Reference { r } => {
let mapped_ref = self.map_var_reference(r)?;
let t = mapped_ref.get_type();
Ok((ExpressionKind::Reference { r: mapped_ref }, t))
}
ExpressionKind::Cast { t, expr } => {
let mapped_expr = self.map_expression(&expr)?;
if mapped_expr.get_meta().t == Type::Null {
let kind = ExpressionKind::Cast { t: t.clone(), expr: Box::new(mapped_expr) };
Ok((kind, t.clone()))
} else {
// for now we only allow the null type to be casted
let kind = FrontendErrorKind::TypeError {
expected: Type::Null,
actual: mapped_expr.get_type(),
};
Err(vec![FrontendError::new(kind, expr.get_location())])
}
}
ExpressionKind::Error => {
unreachable!()
}
};
match typecheck_result {
Ok((kind, t)) => {
let mapped: Expression<TypeMeta> = Expression::new(kind, TypeMeta { t });
Ok(mapped)
}
Err(err_vec) => Err(err_vec),
}
}
fn map_statement(&mut self, stmt: &Statement<LocationMeta>) -> TypeCheckResult<Statement<TypeMeta>> {
match &stmt.item {
StatementKind::Block { block } => {
let mut typechecker = self.with_nested_env(Env::new());
let mapped_block = typechecker.map_block(&block)?;
let meta = mapped_block.get_meta().clone();
let kind = StatementKind::Block { block: mapped_block };
Ok(Statement::new(kind, meta))
}
StatementKind::Empty => {
Ok(Statement::new(StatementKind::Empty, TypeMeta { t: Type::Void }))
}
StatementKind::Decl { items, t } => {
let mut errors = Vec::new();
let mut mapped_declitems = Vec::new();
for declitem in items.iter() {
// TODO: Refactor to separate function: map declitem?
match &declitem.item {
DeclItemKind::NoInit { ident } => {
// check for duplicate variable declaration
if self.local_decl.contains(ident) {
let err = FrontendErrorKind::EnvError {
message: format!("Duplicated declaration of {}", ident)
};
errors.push(FrontendError::new(err, declitem.get_location()));
} else {
// define the variable
self.local_env.insert(ident.clone(), t.clone());
self.local_decl.insert(ident.clone());
let kind = DeclItemKind::NoInit { ident: ident.clone() };
mapped_declitems.push(DeclItem::new(kind, TypeMeta { t: t.clone() }))
}
}
DeclItemKind::Init { ident, val } => {
let loc = val.get_location();
// check for duplicate variable declaration
if self.local_decl.contains(ident) {
let err = FrontendErrorKind::EnvError {
message: format!("Duplicated declaration of {}", ident)
};
errors.push(FrontendError::new(err, declitem.get_location()));
continue;
}
// check expression and define the variable
match self.map_expression(&val) {
Ok(mapped_expr) => {
let expr_t = &mapped_expr.get_meta().t;
match self.check_assignment(&t, expr_t) {
Ok(_) => {
self.local_env.insert(ident.clone(), t.clone());
let kind = DeclItemKind::Init {
ident: ident.clone(),
val: Box::new(mapped_expr.clone()),
};
mapped_declitems.push(DeclItem::new(
kind,
TypeMeta { t: t.clone() },
));
}
Err(kind) => {
errors.push(FrontendError::new(kind, loc));
}
}
}
Err(mut v) => {
errors.append(&mut v);
}
}
}
};
}
if errors.is_empty() {
let kind = StatementKind::Decl { t: t.clone(), items: mapped_declitems };
Ok(Statement::new(kind, TypeMeta { t: Type::Void }))
} else {
Err(errors)
}
}
StatementKind::Ass { r, expr } => {
let expr_loc = expr.get_location();
// TODO: Collect errors from both expression and the reference before failing
let mapped_expr = self.map_expression(&expr)?;
let mapped_ref = self.map_var_reference(&r)?;
let ref_t = &mapped_ref.get_meta().t;
let expr_t = &mapped_expr.get_meta().t;
match self.check_assignment(&ref_t, &expr_t) {
Ok(_) => {
// assignment is not an expression, doesn't have a return value
let kind = StatementKind::Ass { r: mapped_ref, expr: Box::new(mapped_expr) };
let meta = TypeMeta { t: Type::Void };
Ok(Statement::new(kind, meta))
}
Err(kind) => {
Err(vec![FrontendError::new(kind, expr_loc)])
}
}
}
StatementKind::Mut { r, op } => {
let mapped_ref = self.map_var_reference(r)?;
let target_t = &mapped_ref.get_meta().t;
// ++ and -- expressions can only be performed on integer types
match target_t {
Type::Int => {
// ++ and -- are not expressions, they don't have a return value
let kind = StatementKind::Mut { r: mapped_ref, op: op.clone() };
let meta = TypeMeta { t: Type::Void };
Ok(Statement::new(kind, meta))
}
t => {
let kind = FrontendErrorKind::TypeError {
expected: Type::Int,
actual: t.clone(),
};
Err(vec![FrontendError::new(kind, r.get_location())])
}
}
}
StatementKind::Return { expr } => {
match expr {
None => {
let kind = StatementKind::Return { expr: Option::None };
let meta = TypeMeta { t: Type::Void };
Ok(Statement::new(kind, meta))
}
Some(expr) => {
let mapped_expr = self.map_expression(&expr)?;
let t = mapped_expr.get_type();
let kind = StatementKind::Return { expr: Some(Box::new(mapped_expr)) };
let meta = TypeMeta { t };
Ok(Statement::new(kind, meta))
}
}
}
StatementKind::Cond { expr, stmt } => {
// TODO: Collect errors from both expression and statement before failing
let mapped_expr = self.map_expression(&expr)?;
match &mapped_expr.get_meta().t {
Type::Bool => {
let mapped_stmt = self.map_statement(&stmt)?;
let t = mapped_stmt.get_type();
let kind = StatementKind::Cond {
expr: Box::new(mapped_expr),
stmt: Box::new(mapped_stmt),
};
let meta = TypeMeta { t };
Ok(Statement::new(kind, meta))
}
t => {
let kind = FrontendErrorKind::TypeError {
expected: Type::Bool,
actual: t.clone(),
};
Err(vec![FrontendError::new(kind, expr.get_location())])
}
}
}
StatementKind::CondElse { expr, stmt_true, stmt_false } => {
let mapped_expr = self.map_expression(&expr)?;
match &mapped_expr.get_meta().t {
Type::Bool => {
// TODO: Collect errors from both statements before failing
let mapped_true = self.map_statement(&stmt_true)?;
let mapped_false = self.map_statement(&stmt_false)?;
let true_t = &mapped_true.get_meta().t;
let false_t = &mapped_false.get_meta().t;
match self.get_types_lca(&true_t, &false_t) {
Some(lca_t) => {
let kind = StatementKind::CondElse {
expr: Box::new(mapped_expr),
stmt_true: Box::new(mapped_true),
stmt_false: Box::new(mapped_false),
};
let meta = TypeMeta { t: lca_t };
Ok(Statement::new(kind, meta))
}
None => {
// technically, if this is not the last statement in block we could
// allow retuning from one branch and not returning from other branch
// ie: if (cond) { return 10; } else {} return 20;
// but this is not a good practice and we want to encourage either:
// int i; if (cond) { i = 10; } else {i = 20;} return i;
// or:
// if (cond) {return 10;} else {return 20;}
// so I decided to report it as a typing error
let kind = FrontendErrorKind::TypeError {
expected: true_t.clone(),
actual: false_t.clone(),
};
Err(vec![FrontendError::new(kind, stmt_false.get_location())])
}
}
}
t => {
let kind = FrontendErrorKind::TypeError {
expected: Type::Bool,
actual: t.clone(),
};
Err(vec![FrontendError::new(kind, expr.get_location())])
}
}
}
StatementKind::While { expr, stmt } => {
let mapped_expr = self.map_expression(&expr)?;
let mut typechecker = self.with_nested_env(Env::new());
let mapped_stmt = typechecker.map_statement(&stmt)?;
let kind = StatementKind::While {
expr: Box::new(mapped_expr),
stmt: Box::new(mapped_stmt),
};
Ok(Statement::new(kind, TypeMeta { t: Type::Void }))
}
StatementKind::For { t, ident, arr, stmt } => {
let mapped_arr = self.map_expression(&arr)?;
let arr_t = &mapped_arr.get_meta().t;
// check if arr is an array
match arr_t {
Type::Array { item_t } => {
// check array item type
match self.check_assignment(&t, &item_t) {
Ok(_) => {
// check loop statement with nested environemnt
let mut loop_env = Env::new();
loop_env.insert(ident.clone(), t.clone());
let mut typechecker = self.with_nested_env(loop_env);
let mapped_stmt = typechecker.map_statement(&stmt)?;
let kind = StatementKind::For {
t: t.clone(),
ident: ident.clone(),
arr: Box::new(mapped_arr),
stmt: Box::new(mapped_stmt),
};
Ok(Statement::new(kind, TypeMeta { t: Type::Void }))
}
Err(kind) => {
Err(vec![FrontendError::new(kind, arr.get_location())])
}
}
}
invalid_arr_t => {
let kind = FrontendErrorKind::TypeError {
expected: Type::Array { item_t: Box::new(t.clone()) },
actual: invalid_arr_t.clone(),
};
Err(vec![FrontendError::new(kind, arr.get_location())])
}
}
}
StatementKind::Expr { expr } => {
let mapped_expr = self.map_expression(&expr)?;
let kind = StatementKind::Expr { expr: Box::new(mapped_expr) };
Ok(Statement::new(kind, TypeMeta { t: Type::Void }))
}
StatementKind::Error => {
unreachable!()
}
}
}
fn map_class(&mut self, class: &Class<LocationMeta>) -> TypeCheckResult<Class<TypeMeta>> {
let mut typechecker = self.with_class(class);
// variables are mapped by swapping meta, no errors can happen here
let mut mapped_vars: Vec<_> = class.item.vars
.values()
.map(|cv| {
let meta = TypeMeta { t: cv.item.t.clone() };
ClassVar::new(cv.item.clone(), meta)
})
.collect();
// every method needs to be checked for type correctness
let (mapped_methods, errors): (Vec<_>, Vec<_>) = class.item.methods
.values()
.map(|func| {
typechecker.map_function(&func)
})
.partition(Result::is_ok);
let mut mapped_methods: Vec<Function<TypeMeta>> = mapped_methods
.into_iter()
.map(Result::unwrap)
.collect();
let errors: Vec<FrontendError<LocationMeta>> = errors
.into_iter()
.map(Result::unwrap_err)
.flatten()
.collect();
if errors.is_empty() {
if let Ok(mut item) = ClassItem::new(
class.item.get_key().clone(),
&mut mapped_vars,
&mut mapped_methods,
) {
if let Some(parent) = &class.item.parent {
item = item.with_parent(parent);
}
Ok(Class::new(item, TypeMeta { t: Type::Class { ident: class.item.get_key().clone() } }))
} else {
// because we only transformed metadata in envs, we know creation cannot fail
unreachable!()
}
} else {
Err(errors)
}
}
fn map_function(&mut self, function: &Function<LocationMeta>) -> TypeCheckResult<Function<TypeMeta>> {
let mapped_args: Vec<Arg<TypeMeta>> = function.item.args.iter()
.map(|arg| {
Arg::new(arg.item.clone(), TypeMeta { t: arg.item.t.clone() })
})
.collect();
let mut typechecker = self.with_nested_env(function.to_type_env());
let mapped_block = typechecker.map_block(&function.item.block)?;
match self.check_assignment(&function.item.ret, &mapped_block.get_meta().t) {
Ok(_) => {
if let Ok(item) = FunctionItem::new(
function.item.ret.clone(),
function.item.ident.clone(),
mapped_args,
mapped_block,
) {
Ok(Function::new(item.clone(), TypeMeta { t: item.get_type() }))
} else {
// because we only transformed metadata in envs, we know creation cannot fail
unreachable!()
}
}
Err(kind) => {
Err(vec![FrontendError::new(kind, function.get_location())])
}
}
}
}