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
use crate::{AsgConvertError, ConstValue, Expression, ExpressionNode, FromAst, Node, PartialType, Scope, Span, Type};
use std::cell::Cell;
#[derive(Clone)]
pub struct ArrayInitExpression<'a> {
pub parent: Cell<Option<&'a Expression<'a>>>,
pub span: Option<Span>,
pub element: Cell<&'a Expression<'a>>,
pub len: usize,
}
impl<'a> Node for ArrayInitExpression<'a> {
fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
}
impl<'a> ExpressionNode<'a> for ArrayInitExpression<'a> {
fn set_parent(&self, parent: &'a Expression<'a>) {
self.parent.replace(Some(parent));
}
fn get_parent(&self) -> Option<&'a Expression<'a>> {
self.parent.get()
}
fn enforce_parents(&self, expr: &'a Expression<'a>) {
self.element.get().set_parent(expr);
}
fn get_type(&self) -> Option<Type<'a>> {
Some(Type::Array(Box::new(self.element.get().get_type()?), self.len))
}
fn is_mut_ref(&self) -> bool {
false
}
fn const_value(&self) -> Option<ConstValue<'a>> {
let element = self.element.get().const_value()?;
Some(ConstValue::Array(vec![element; self.len]))
}
fn is_consty(&self) -> bool {
self.element.get().is_consty()
}
}
impl<'a> FromAst<'a, leo_ast::ArrayInitExpression> for ArrayInitExpression<'a> {
fn from_ast(
scope: &'a Scope<'a>,
value: &leo_ast::ArrayInitExpression,
expected_type: Option<PartialType<'a>>,
) -> Result<ArrayInitExpression<'a>, AsgConvertError> {
let (mut expected_item, expected_len) = match expected_type {
Some(PartialType::Array(item, dims)) => (item.map(|x| *x), dims),
None => (None, None),
Some(type_) => {
return Err(AsgConvertError::unexpected_type(
&type_.to_string(),
Some("array"),
&value.span,
));
}
};
let dimensions = value
.dimensions
.0
.iter()
.map(|x| {
x.value
.parse::<usize>()
.map_err(|_| AsgConvertError::parse_dimension_error())
})
.collect::<Result<Vec<_>, AsgConvertError>>()?;
let len = *dimensions.get(0).ok_or_else(AsgConvertError::parse_dimension_error)?;
if let Some(expected_len) = expected_len {
if expected_len != len {
return Err(AsgConvertError::unexpected_type(
&*format!("array of length {}", expected_len),
Some(&*format!("array of length {}", len)),
&value.span,
));
}
}
for dimension in (&dimensions[1..]).iter().copied() {
expected_item = match expected_item {
Some(PartialType::Array(item, len)) => {
if let Some(len) = len {
if len != dimension {
return Err(AsgConvertError::unexpected_type(
&*format!("array of length {}", dimension),
Some(&*format!("array of length {}", len)),
&value.span,
));
}
}
item.map(|x| *x)
}
None => None,
Some(type_) => {
return Err(AsgConvertError::unexpected_type(
"array",
Some(&type_.to_string()),
&value.span,
));
}
}
}
let mut element = Some(<&'a Expression<'a>>::from_ast(scope, &*value.element, expected_item)?);
let mut output = None;
for dimension in dimensions.iter().rev().copied() {
output = Some(ArrayInitExpression {
parent: Cell::new(None),
span: Some(value.span.clone()),
element: Cell::new(
output
.map(Expression::ArrayInit)
.map(|expr| &*scope.context.alloc_expression(expr))
.unwrap_or_else(|| element.take().unwrap()),
),
len: dimension,
});
}
Ok(output.unwrap())
}
}
impl<'a> Into<leo_ast::ArrayInitExpression> for &ArrayInitExpression<'a> {
fn into(self) -> leo_ast::ArrayInitExpression {
leo_ast::ArrayInitExpression {
element: Box::new(self.element.get().into()),
dimensions: leo_ast::ArrayDimensions(vec![leo_ast::PositiveNumber {
value: self.len.to_string().into(),
}]),
span: self.span.clone().unwrap_or_default(),
}
}
}