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
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use std::{
cell::RefCell,
sync::{Arc, Weak},
};
#[derive(Debug)]
pub struct TupleInitExpression {
pub parent: RefCell<Option<Weak<Expression>>>,
pub span: Option<Span>,
pub elements: Vec<Arc<Expression>>,
}
impl Node for TupleInitExpression {
fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
}
impl ExpressionNode for TupleInitExpression {
fn set_parent(&self, parent: Weak<Expression>) {
self.parent.replace(Some(parent));
}
fn get_parent(&self) -> Option<Arc<Expression>> {
self.parent.borrow().as_ref().map(Weak::upgrade).flatten()
}
fn enforce_parents(&self, expr: &Arc<Expression>) {
self.elements.iter().for_each(|element| {
element.set_parent(Arc::downgrade(expr));
})
}
fn get_type(&self) -> Option<Type> {
let mut output = vec![];
for element in self.elements.iter() {
output.push(element.get_type()?);
}
Some(Type::Tuple(output))
}
fn is_mut_ref(&self) -> bool {
false
}
fn const_value(&self) -> Option<ConstValue> {
let mut consts = vec![];
for element in self.elements.iter() {
if let Some(const_value) = element.const_value() {
consts.push(const_value);
} else {
return None;
}
}
Some(ConstValue::Tuple(consts))
}
fn is_consty(&self) -> bool {
self.elements.iter().all(|x| x.is_consty())
}
}
impl FromAst<leo_ast::TupleInitExpression> for TupleInitExpression {
fn from_ast(
scope: &Scope,
value: &leo_ast::TupleInitExpression,
expected_type: Option<PartialType>,
) -> Result<TupleInitExpression, AsgConvertError> {
let tuple_types = match expected_type {
Some(PartialType::Tuple(sub_types)) => Some(sub_types),
None => None,
x => {
return Err(AsgConvertError::unexpected_type(
"tuple",
x.map(|x| x.to_string()).as_deref(),
&value.span,
));
}
};
if let Some(tuple_types) = tuple_types.as_ref() {
if tuple_types.len() != value.elements.len() {
return Err(AsgConvertError::unexpected_type(
&*format!("tuple of length {}", tuple_types.len()),
Some(&*format!("tuple of length {}", value.elements.len())),
&value.span,
));
}
}
let elements = value
.elements
.iter()
.enumerate()
.map(|(i, e)| {
Arc::<Expression>::from_ast(
scope,
e,
tuple_types.as_ref().map(|x| x.get(i)).flatten().cloned().flatten(),
)
})
.collect::<Result<Vec<_>, AsgConvertError>>()?;
Ok(TupleInitExpression {
parent: RefCell::new(None),
span: Some(value.span.clone()),
elements,
})
}
}
impl Into<leo_ast::TupleInitExpression> for &TupleInitExpression {
fn into(self) -> leo_ast::TupleInitExpression {
leo_ast::TupleInitExpression {
elements: self.elements.iter().map(|e| e.as_ref().into()).collect(),
span: self.span.clone().unwrap_or_default(),
}
}
}