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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
// Copyright 2024-2025 Golem Cloud
//
// Licensed under the Golem Source License v1.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://license.golem.cloud/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::{Expr, InferredType, MatchArm, Range, VariableId};
use bigdecimal::BigDecimal;
use std::ops::Deref;
pub fn desugar_range_selection(select_from: &Expr, range_expr: &Expr) -> Result<Expr, String> {
let iterable_expr = VariableId::list_comprehension_identifier("__i__");
match range_expr {
Expr::Range { range, .. } => match range {
Range::Range { .. } | Range::RangeInclusive { .. } => Ok(Expr::list_comprehension(
iterable_expr.clone(),
range_expr.clone(),
Expr::select_index(
select_from.clone(),
Expr::identifier_with_variable_id(iterable_expr, None),
),
)
.with_inferred_type(select_from.inferred_type())),
Range::RangeFrom { from, .. } => {
let length_variable = VariableId::local("__size__", 0);
let length_identifier =
Expr::identifier_with_variable_id(length_variable.clone(), None)
.with_inferred_type(from.inferred_type());
let length_minus_one_var = VariableId::local("__index__", 0);
// from's inferred type is used to keep the range from and to be of the same tpe
let length_minus_one_identifier =
Expr::identifier_with_variable_id(length_minus_one_var.clone(), None)
.with_inferred_type(from.inferred_type());
Ok(Expr::expr_block(vec![
// let length = length(select_from)
Expr::let_binding_with_variable_id(
length_variable,
Expr::length(select_from.clone()),
None,
),
// let index = length - 1
Expr::let_binding_with_variable_id(
length_minus_one_var,
Expr::minus(
length_identifier,
Expr::number(BigDecimal::from(1))
.with_inferred_type(from.inferred_type()),
)
.with_inferred_type(from.inferred_type()),
None,
)
.with_inferred_type(from.inferred_type()),
// desugared to
// for __i__ in from..length-1 {
// yield select_from[__i__];
//}
Expr::list_comprehension(
iterable_expr.clone(),
Expr::range(from.deref().clone(), length_minus_one_identifier),
Expr::select_index(
select_from.clone(),
Expr::identifier_with_variable_id(iterable_expr, None),
),
)
.with_inferred_type(select_from.inferred_type()),
]))
}
},
_ => Err("internal error: expression is not a range for any desugar to apply".to_string()),
}
}
pub fn desugar_pattern_match(
pred: &Expr,
match_arms: &[MatchArm],
expr_type: InferredType,
) -> Option<Expr> {
let mut if_else_branches = vec![];
for match_arm in match_arms.iter() {
let if_else_branch = internal::IfThenBranch::from_pred_and_match_arm(match_arm, pred);
if let Some(condition) = if_else_branch {
if_else_branches.push(condition);
}
}
internal::build_expr_from(if_else_branches).map(|expr| expr.merge_inferred_type(expr_type))
}
mod internal {
use crate::call_type::CallType;
use crate::rib_source_span::SourceSpan;
use crate::{ArmPattern, Expr, InferredType, MatchArm, Number, TypeInternal, VariableId};
use bigdecimal::{BigDecimal, FromPrimitive};
pub(crate) fn build_expr_from(if_branches: Vec<IfThenBranch>) -> Option<Expr> {
if let Some(branch) = if_branches.first() {
let mut expr = Expr::cond(
branch.condition.clone(),
branch.body.clone(),
Expr::throw("No match found"),
);
for branch in if_branches.iter().skip(1).rev() {
if let Expr::Cond { rhs, .. } = &mut expr {
let else_copy = *rhs.clone();
*rhs = Box::new(
Expr::cond(branch.condition.clone(), branch.body.clone(), else_copy)
.merge_inferred_type(branch.body.inferred_type()),
);
}
}
Some(expr)
} else {
None
}
}
#[derive(Debug, Clone)]
pub(crate) struct IfThenBranch {
pub(crate) condition: Expr,
pub(crate) body: Expr,
}
impl IfThenBranch {
pub(crate) fn from_pred_and_match_arm(
match_arm: &MatchArm,
pred: &Expr,
) -> Option<IfThenBranch> {
get_conditions(match_arm, pred, None, pred.inferred_type())
}
}
// Match arms are converted to if-else conditions, with the help of instruction called `GetTag`.
// We rely on the actual constructor names, and for that reason we pattern ok, err, option, none
// having a precise control. For example, if it's none, there is no need to check the tag of predicate and
// and arm-literal, as it is a direct boolean expression.
fn get_conditions(
match_arm: &MatchArm,
pred_expr: &Expr,
tag: Option<Expr>,
inferred_type_of_pred: InferredType,
) -> Option<IfThenBranch> {
let arm_pattern = &match_arm.arm_pattern;
let resolution = &match_arm.arm_resolution_expr;
// match x {
// some(some(x)) => "hello"
// }
match arm_pattern {
ArmPattern::Literal(arm_pattern_expr) => {
handle_literal(arm_pattern_expr, pred_expr, resolution, tag)
}
ArmPattern::Constructor(constructor_name, arm_patterns) => hande_constructor(
pred_expr,
constructor_name,
arm_patterns,
resolution,
inferred_type_of_pred,
tag,
),
ArmPattern::TupleConstructor(arm_patterns) => hande_constructor(
pred_expr,
"tuple",
arm_patterns,
resolution,
inferred_type_of_pred,
tag,
),
ArmPattern::ListConstructor(arm_patterns) => hande_constructor(
pred_expr,
"list",
arm_patterns,
resolution,
inferred_type_of_pred,
tag,
),
ArmPattern::RecordConstructor(field_arm_pattern_collection) => {
handle_record_constructor(
pred_expr,
field_arm_pattern_collection,
resolution,
inferred_type_of_pred,
tag,
)
}
ArmPattern::As(name, inner_pattern) => handle_as_pattern(
name,
inner_pattern,
pred_expr,
resolution,
tag,
inferred_type_of_pred,
),
ArmPattern::WildCard => {
let branch = IfThenBranch {
condition: tag.unwrap_or(Expr::boolean(true)),
body: resolution.as_ref().clone(),
};
Some(branch)
}
}
}
fn handle_literal(
arm_pattern_expr: &Expr,
pred_expr: &Expr,
resolution: &Expr,
tag: Option<Expr>,
) -> Option<IfThenBranch> {
match arm_pattern_expr {
Expr::Identifier {
variable_id,
inferred_type,
..
} => {
let assign_var = Expr::let_binding_with_variable_id(
variable_id.clone(),
pred_expr.clone(),
None,
)
.with_inferred_type(inferred_type.clone());
let block = Expr::expr_block(vec![assign_var, resolution.clone()]);
let branch = IfThenBranch {
condition: tag.unwrap_or(Expr::boolean(true)),
body: block,
};
Some(branch)
}
Expr::Call {
call_type: CallType::EnumConstructor(name),
..
} => {
let cond = if let Some(t) = tag {
Expr::and(
t,
Expr::equal_to(Expr::get_tag(pred_expr.clone()), Expr::literal(name)),
)
} else {
Expr::equal_to(Expr::get_tag(pred_expr.clone()), Expr::literal(name))
};
let branch = IfThenBranch {
condition: cond,
body: resolution.clone(),
};
Some(branch)
}
_ => {
let branch = IfThenBranch {
condition: Expr::equal_to(pred_expr.clone(), arm_pattern_expr.clone()),
body: resolution.clone(),
};
Some(branch)
}
}
}
fn handle_record_constructor(
pred_expr: &Expr,
bind_patterns: &[(String, ArmPattern)],
resolution: &Expr,
pred_expr_inferred_type: InferredType,
tag: Option<Expr>,
) -> Option<IfThenBranch> {
match pred_expr_inferred_type.internal_type() {
TypeInternal::Record(field_and_types) => {
// Resolution body is a list of expressions which grows (maybe with some let bindings)
// as we recursively iterate over the bind patterns
// where bind patterns are {name: x, age: _, address : _ } in the case of `match record { {name: x, age: _, address : _ } ) =>`
// These will exist prior to the original resolution of a successful record match.
let mut resolution_body = vec![];
// The conditions keep growing as we recursively iterate over the bind patterns
// and there are multiple conditions (if condition) for each element in the record.
let mut conditions = vec![];
// We assume pred-expr can be queried by field using Expr::select_field, and we pick each element in the bind pattern
// to get the corresponding expr in pred-expr and keep recursively iterating until the record is completed.
// However, there is no resolution body for each of this iteration, so we use an empty expression
// and finally push the original resolution body once we fully build the conditions.
for (field, arm_pattern) in bind_patterns.iter() {
let new_pred = Expr::select_field(pred_expr.clone(), field, None);
let new_pred_type = field_and_types
.iter()
.find(|(f, _)| f == field)
.map(|(_, t)| t.clone())
.unwrap_or(InferredType::unknown());
let branch = get_conditions(
&MatchArm::new(arm_pattern.clone(), Expr::empty_expr()),
&new_pred,
None,
new_pred_type.clone(),
);
if let Some(x) = branch {
conditions.push(x.condition);
resolution_body.push(x.body)
}
}
resolution_body.push(resolution.clone());
let and_cond = Expr::and_combine(conditions);
and_cond.map(|c| IfThenBranch {
condition: {
if let Some(t) = tag {
Expr::and(t, c)
} else {
c
}
},
body: Expr::expr_block(resolution_body),
})
}
_ => None,
}
}
fn hande_constructor(
pred_expr: &Expr,
constructor_name: &str,
bind_patterns: &[ArmPattern],
resolution: &Expr,
pred_expr_inferred_type: InferredType,
tag: Option<Expr>,
) -> Option<IfThenBranch> {
match pred_expr_inferred_type.internal_type() {
TypeInternal::Variant(variant) => {
let arg_pattern_opt = bind_patterns.first();
let inner_type = &variant
.iter()
.find(|(case_name, _)| case_name == constructor_name);
let inner_variant_arg_type = inner_type.and_then(|(_, typ)| typ.clone());
let cond = if let Some(t) = tag {
Expr::and(
t,
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
),
)
} else {
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
)
};
match (arg_pattern_opt, inner_variant_arg_type) {
(None, None) => None,
(Some(pattern), Some(inferred_type)) => get_conditions(
&MatchArm::new(pattern.clone(), resolution.clone()),
&pred_expr.unwrap(),
Some(cond),
inferred_type,
),
_ => None,
}
}
TypeInternal::Option(inner) if constructor_name == "some" => {
let cond = if let Some(t) = tag {
Expr::and(
t,
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
),
)
} else {
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
)
};
match bind_patterns.first() {
Some(pattern) => get_conditions(
&MatchArm::new(pattern.clone(), resolution.clone()),
&pred_expr.unwrap(),
Some(cond),
inner.clone(),
),
_ => None,
}
}
TypeInternal::Option(_) if constructor_name == "none" => {
let cond = if let Some(t) = tag {
Expr::and(
t,
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
),
)
} else {
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
)
};
Some(IfThenBranch {
condition: cond,
body: resolution.clone(),
})
}
TypeInternal::Result { ok, error } => {
let inner_variant_arg_type = if constructor_name == "ok" {
ok
} else if constructor_name == "err" {
error
} else {
return None;
};
let cond = if let Some(t) = tag {
Expr::and(
t,
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
),
)
} else {
Expr::equal_to(
Expr::get_tag(pred_expr.clone()),
Expr::literal(constructor_name),
)
};
match bind_patterns.first() {
Some(pattern) => get_conditions(
&MatchArm::new(pattern.clone(), resolution.clone()),
&pred_expr.unwrap(),
Some(cond),
inner_variant_arg_type
.clone()
.unwrap_or(InferredType::unknown())
.clone(),
),
_ => None,
}
}
TypeInternal::Tuple(inferred_types) => {
// Resolution body is a list of expressions which grows (may be with some let bindings)
// as we recursively iterate over the bind patterns
// where bind patterns are x, _, y in the case of `match tuple_variable { (x, _, y)) =>`
// These will exist prior to the original resolution of a successful tuple match.
let mut resolution_body = vec![];
// The conditions keep growing as we recursively iterate over the bind patterns
// and there are multiple conditions (if condition) for each element in the tuple
let mut conditions = vec![];
// We assume pred-expr is indexed (i.e, tuple is indexed), and we pick each element in the bind pattern
// and get the corresponding expr in pred-expr and keep recursively iterating until the tuple is completed.
// However there is no resolution body for each of this iteration, so we use an empty expression
// and finally push the original resolution body once we fully build the conditions.
for (index, arm_pattern) in bind_patterns.iter().enumerate() {
let new_pred = Expr::select_index(
pred_expr.clone(),
Expr::Number {
number: Number {
value: BigDecimal::from_usize(index).unwrap(),
},
type_annotation: None,
inferred_type: InferredType::u64(),
source_span: SourceSpan::default(),
},
);
let new_pred_type = inferred_types
.get(index)
.cloned()
.unwrap_or(InferredType::unknown());
let branch = get_conditions(
&MatchArm::new(arm_pattern.clone(), Expr::empty_expr()),
&new_pred,
None,
new_pred_type.clone(),
);
if let Some(x) = branch {
conditions.push(x.condition);
resolution_body.push(x.body)
}
}
resolution_body.push(resolution.clone());
let and_cond = Expr::and_combine(conditions);
and_cond.map(|c| IfThenBranch {
condition: {
if let Some(t) = tag {
Expr::and(t, c)
} else {
c
}
},
body: Expr::expr_block(resolution_body),
})
}
TypeInternal::List(inferred_type) => {
// Resolution body is a list of expressions which grows (may be with some let bindings)
// as we recursively iterate over the bind patterns
// where bind patterns are x, _, y in the case of `match list_ { [x, _, y]) =>`
// These will exist prior to the original resolution of a successful list match.
let mut resolution_body = vec![];
// The conditions keep growing as we recursively iterate over the bind patterns
// and there are multiple conditions (if condition) for each element in the list
let mut conditions = vec![];
// We assume pred-expr is indexed (i.e, list is indexed), and we pick each element in the bind pattern
// and get the corresponding expr in pred-expr and keep recursively iterating until the list is completed.
// However there is no resolution body for each of this iteration, so we use an empty expression
// and finally push the original resolution body once we fully build the conditions.
for (index, arm_pattern) in bind_patterns.iter().enumerate() {
let new_pred = Expr::select_index(
pred_expr.clone(),
Expr::Number {
number: Number {
value: BigDecimal::from_usize(index).unwrap(),
},
type_annotation: None,
inferred_type: InferredType::u64(),
source_span: SourceSpan::default(),
},
);
let new_pred_type = inferred_type.clone();
let branch = get_conditions(
&MatchArm::new(arm_pattern.clone(), Expr::empty_expr()),
&new_pred,
None,
new_pred_type.clone(),
);
if let Some(x) = branch {
conditions.push(x.condition);
resolution_body.push(x.body)
}
}
resolution_body.push(resolution.clone());
let and_cond = Expr::and_combine(conditions);
and_cond.map(|c| IfThenBranch {
condition: {
if let Some(t) = tag {
Expr::and(t, c)
} else {
c
}
},
body: Expr::expr_block(resolution_body),
})
}
TypeInternal::Unknown => Some(IfThenBranch {
condition: Expr::boolean(false),
body: resolution.clone(),
}),
_ => None,
}
}
fn handle_as_pattern(
name: &str,
inner_pattern: &ArmPattern,
pred_expr: &Expr,
resolution: &Expr,
tag: Option<Expr>,
pred_expr_inferred_type: InferredType,
) -> Option<IfThenBranch> {
let binding = Expr::let_binding_with_variable_id(
VariableId::global(name.to_string()),
pred_expr.clone(),
None,
)
.with_inferred_type(pred_expr.inferred_type());
let block = Expr::expr_block(vec![binding, resolution.clone()]);
get_conditions(
&MatchArm::new(inner_pattern.clone(), block),
pred_expr,
tag,
pred_expr_inferred_type,
)
}
}
#[cfg(test)]
mod desugar_tests {
use test_r::test;
use super::*;
use crate::compiler::desugar::desugar_tests::expectations::expected_condition_with_identifiers;
use crate::{
ComponentDependency, ComponentDependencyKey, Expr, RibCompiler, RibCompilerConfig,
};
use golem_wasm_ast::analysis::{
AnalysedExport, AnalysedFunction, AnalysedFunctionParameter, AnalysedType, TypeU32, TypeU64,
};
use std::ops::Deref;
use uuid::Uuid;
fn get_test_compiler() -> RibCompiler {
let metadata = vec![
AnalysedExport::Function(AnalysedFunction {
name: "foo".to_string(),
parameters: vec![AnalysedFunctionParameter {
name: "my_parameter".to_string(),
typ: AnalysedType::U64(TypeU64),
}],
result: None,
}),
AnalysedExport::Function(AnalysedFunction {
name: "baz".to_string(),
parameters: vec![AnalysedFunctionParameter {
name: "my_parameter".to_string(),
typ: AnalysedType::U32(TypeU32),
}],
result: None,
}),
];
let component_dependency_key = ComponentDependencyKey {
component_name: "foo".to_string(),
component_id: Uuid::new_v4(),
component_version: 0,
root_package_name: None,
root_package_version: None,
};
RibCompiler::new(RibCompilerConfig::new(
vec![ComponentDependency::new(component_dependency_key, metadata)],
vec![],
vec![],
))
}
#[test]
fn test_desugar_pattern_match_with_identifiers() {
let rib_expr = r#"
let x: option<u64> = some(1);
match x {
some(x) => x,
none => 1u64
}
"#;
let test_compiler = get_test_compiler();
let expr = Expr::from_text(rib_expr).unwrap();
let inferred_expr = test_compiler.infer_types(expr).unwrap();
let desugared_expr = match internal::last_expr(inferred_expr.get_expr()) {
Expr::PatternMatch {
predicate,
match_arms,
..
} => desugar_pattern_match(
predicate.deref(),
&match_arms,
inferred_expr.get_expr().inferred_type(),
)
.unwrap(),
_ => panic!("Expected a match expression"),
};
assert_eq!(desugared_expr, expected_condition_with_identifiers());
}
mod internal {
use crate::Expr;
pub(crate) fn last_expr(expr: &Expr) -> Expr {
match expr {
Expr::ExprBlock { exprs, .. } => exprs.last().unwrap().clone(),
_ => expr.clone(),
}
}
}
mod expectations {
use crate::{Expr, InferredType, TypeName, VariableId};
use bigdecimal::BigDecimal;
pub(crate) fn expected_condition_with_identifiers() -> Expr {
Expr::cond(
Expr::equal_to(
Expr::get_tag(
Expr::identifier_with_variable_id(VariableId::local("x", 0), None)
.with_inferred_type(InferredType::option(InferredType::u64())),
),
Expr::literal("some"),
)
.with_inferred_type(InferredType::bool()),
Expr::expr_block(vec![
Expr::let_binding_with_variable_id(
VariableId::match_identifier("x".to_string(), 1),
Expr::identifier_with_variable_id(VariableId::local("x", 0), None)
.with_inferred_type(InferredType::option(InferredType::u64()))
.unwrap(),
None,
)
.with_inferred_type(InferredType::u64()),
Expr::identifier_with_variable_id(
VariableId::match_identifier("x".to_string(), 1),
None,
)
.with_inferred_type(InferredType::u64()),
])
.with_inferred_type(InferredType::u64()),
Expr::cond(
Expr::equal_to(
Expr::get_tag(
Expr::identifier_with_variable_id(VariableId::local("x", 0), None)
.with_inferred_type(InferredType::option(InferredType::u64())),
),
Expr::literal("none"),
)
.with_inferred_type(InferredType::bool()),
Expr::number_inferred(
BigDecimal::from(1),
Some(TypeName::U64),
InferredType::u64(),
),
Expr::throw("No match found"),
)
.with_inferred_type(InferredType::u64()),
)
.with_inferred_type(InferredType::u64())
}
}
}