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
// Copyright (C) 2019-2020 Aleo Systems Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{ArrayDimensions, GroupValue};
use leo_input::{
    errors::InputParserError,
    expressions::{ArrayInitializerExpression, ArrayInlineExpression, Expression, TupleExpression},
    types::{ArrayType, DataType, IntegerType, TupleType, Type},
    values::{Address, AddressValue, BooleanValue, FieldValue, GroupValue as InputGroupValue, NumberValue, Value},
};
use pest::Span;

use std::fmt;

#[derive(Clone, PartialEq, Eq)]
pub enum InputValue {
    Address(String),
    Boolean(bool),
    Field(String),
    Group(GroupValue),
    Integer(IntegerType, String),
    Array(Vec<InputValue>),
    Tuple(Vec<InputValue>),
}

impl InputValue {
    fn from_address(address: Address) -> Self {
        InputValue::Address(address.value)
    }

    fn from_address_value(value: AddressValue) -> Self {
        match value {
            AddressValue::Explicit(address) => Self::from_address(address.address),
            AddressValue::Implicit(address) => Self::from_address(address),
        }
    }

    fn from_boolean(boolean: BooleanValue) -> Result<Self, InputParserError> {
        let boolean = boolean.value.parse::<bool>()?;
        Ok(InputValue::Boolean(boolean))
    }

    fn from_number(integer_type: IntegerType, number: String) -> Self {
        InputValue::Integer(integer_type, number)
    }

    fn from_group(group: InputGroupValue) -> Self {
        InputValue::Group(GroupValue::from(group))
    }

    fn from_field(field: FieldValue) -> Self {
        InputValue::Field(field.number.to_string())
    }

    fn from_implicit(data_type: DataType, implicit: NumberValue) -> Result<Self, InputParserError> {
        match data_type {
            DataType::Address(_) => Err(InputParserError::implicit_type(data_type, implicit)),
            DataType::Boolean(_) => Err(InputParserError::implicit_type(data_type, implicit)),
            DataType::Integer(integer_type) => Ok(InputValue::from_number(integer_type, implicit.to_string())),
            DataType::Group(_) => Err(InputParserError::implicit_group(implicit)),
            DataType::Field(_) => Ok(InputValue::Field(implicit.to_string())),
        }
    }

    fn from_value(data_type: DataType, value: Value) -> Result<Self, InputParserError> {
        match (data_type, value) {
            (DataType::Address(_), Value::Address(address)) => Ok(InputValue::from_address_value(address)),
            (DataType::Boolean(_), Value::Boolean(boolean)) => InputValue::from_boolean(boolean),
            (DataType::Integer(integer_type), Value::Integer(integer)) => {
                Ok(InputValue::from_number(integer_type, integer.to_string()))
            }
            (DataType::Group(_), Value::Group(group)) => Ok(InputValue::from_group(group)),
            (DataType::Field(_), Value::Field(field)) => Ok(InputValue::from_field(field)),
            (data_type, Value::Implicit(implicit)) => InputValue::from_implicit(data_type, implicit),
            (data_type, value) => Err(InputParserError::data_type_mismatch(data_type, value)),
        }
    }

    pub(crate) fn from_expression(type_: Type, expression: Expression) -> Result<Self, InputParserError> {
        match (type_, expression) {
            (Type::Basic(data_type), Expression::Value(value)) => InputValue::from_value(data_type, value),
            (Type::Array(array_type), Expression::ArrayInline(inline)) => {
                InputValue::from_array_inline(array_type, inline)
            }
            (Type::Array(array_type), Expression::ArrayInitializer(initializer)) => {
                InputValue::from_array_initializer(array_type, initializer)
            }
            (Type::Tuple(tuple_type), Expression::Tuple(tuple)) => InputValue::from_tuple(tuple_type, tuple),
            (type_, expression) => Err(InputParserError::expression_type_mismatch(type_, expression)),
        }
    }

    ///
    /// Returns a new `InputValue` from the given `ArrayType` and `ArrayInlineExpression`.
    ///
    pub(crate) fn from_array_inline(
        mut array_type: ArrayType,
        inline: ArrayInlineExpression,
    ) -> Result<Self, InputParserError> {
        // Create a new `ArrayDimensions` type from the input array_type dimensions.
        let array_dimensions_type = ArrayDimensions::from(array_type.dimensions.clone());

        // Convert the array dimensions to usize.
        let array_dimensions = parse_array_dimensions(array_dimensions_type, array_type.span.clone())?;

        // Return an error if the outer array dimension does not equal the number of array elements.
        if array_dimensions[0] != inline.expressions.len() {
            return Err(InputParserError::array_inline_length(array_dimensions[0], inline));
        }

        array_type.dimensions = array_type.dimensions.next_dimension();

        let inner_array_type = if array_dimensions.len() == 1 {
            // This is a single array
            *array_type.type_
        } else {
            // This is a multi-dimensional array
            Type::Array(array_type)
        };

        let mut elements = Vec::with_capacity(inline.expressions.len());
        for expression in inline.expressions.into_iter() {
            let element = InputValue::from_expression(inner_array_type.clone(), expression)?;

            elements.push(element)
        }

        Ok(InputValue::Array(elements))
    }

    pub(crate) fn from_array_initializer(
        array_type: ArrayType,
        initializer: ArrayInitializerExpression,
    ) -> Result<Self, InputParserError> {
        let array_dimensions_type = ArrayDimensions::from(initializer.dimensions.clone());
        let array_dimensions = parse_array_dimensions(array_dimensions_type, array_type.span.clone())?;

        if array_dimensions.len() > 1 {
            // The expression is an array initializer with tuple syntax
            Self::from_array_initializer_tuple(array_type, initializer, array_dimensions)
        } else {
            // The expression is an array initializer with nested syntax
            Self::from_array_initializer_nested(array_type, initializer, array_dimensions)
        }
    }

    pub(crate) fn from_array_initializer_tuple(
        array_type: ArrayType,
        initializer: ArrayInitializerExpression,
        initializer_dimensions: Vec<usize>,
    ) -> Result<Self, InputParserError> {
        let (array_dimensions, array_element_type) = fetch_nested_array_type_dimensions(array_type.clone(), vec![])?;

        // Return an error if the dimensions of the array are incorrect.
        if array_dimensions.ne(&initializer_dimensions) {
            return Err(InputParserError::array_init_length(
                array_dimensions,
                initializer_dimensions,
                initializer.span,
            ));
        }

        let value = InputValue::from_expression(array_element_type, *initializer.expression.clone())?;
        let mut elements = vec![];

        // Build the elements of the array using the `vec!` macro
        for (i, dimension) in initializer_dimensions.into_iter().enumerate() {
            if i == 0 {
                elements = vec![value.clone(); dimension];
            } else {
                let element = InputValue::Array(elements.clone());

                elements = vec![element; dimension];
            }
        }

        Ok(InputValue::Array(elements))
    }

    pub(crate) fn from_array_initializer_nested(
        mut array_type: ArrayType,
        initializer: ArrayInitializerExpression,
        initializer_dimensions: Vec<usize>,
    ) -> Result<Self, InputParserError> {
        // Create a new `ArrayDimensions` type from the input array_type dimensions.
        let array_dimensions_type = ArrayDimensions::from(array_type.dimensions.clone());

        // Convert the array dimensions to usize.
        let array_dimensions = parse_array_dimensions(array_dimensions_type, array_type.span.clone())?;

        let current_array_dimension = array_dimensions[0];
        let current_initializer_dimension = initializer_dimensions[0];

        // Return an error if the outer array dimension does not equal the number of array elements.
        if current_array_dimension.ne(&current_initializer_dimension) {
            return Err(InputParserError::array_init_length(
                array_dimensions,
                initializer_dimensions,
                initializer.span,
            ));
        }

        array_type.dimensions = array_type.dimensions.next_dimension();

        let inner_array_type = if array_dimensions.len() == 1 {
            // This is the innermost dimension
            *array_type.type_
        } else {
            // This is an outer dimension of a multi-dimensional array
            Type::Array(array_type)
        };

        // Evaluate the array initializer
        let element = InputValue::from_expression(inner_array_type.clone(), *initializer.expression)?;
        let elements = vec![element; current_initializer_dimension];

        Ok(InputValue::Array(elements))
    }

    pub(crate) fn from_tuple(tuple_type: TupleType, tuple: TupleExpression) -> Result<Self, InputParserError> {
        let num_types = tuple_type.types_.len();
        let num_values = tuple.expressions.len();

        if num_types != num_values {
            return Err(InputParserError::tuple_length(
                num_types,
                num_values,
                tuple_type.span.clone(),
            ));
        }

        let mut values = Vec::with_capacity(tuple_type.types_.len());
        for (type_, value) in tuple_type.types_.into_iter().zip(tuple.expressions.into_iter()) {
            let value = InputValue::from_expression(type_, value)?;

            values.push(value)
        }

        Ok(InputValue::Tuple(values))
    }
}

///
/// Returns a new vector of usize values from an [`ArrayDimensions`] type.
///
/// Attempts to parse each dimension in the array from a `String` to a `usize` value. If parsing
/// is successful, the `usize` value is appended to the return vector. If parsing fails, an error
/// is returned.
///
fn parse_array_dimensions(array_dimensions_type: ArrayDimensions, span: Span) -> Result<Vec<usize>, InputParserError> {
    // Convert the array dimensions to usize.
    let mut array_dimensions = Vec::with_capacity(array_dimensions_type.0.len());

    for dimension in array_dimensions_type.0 {
        // Convert the dimension to a string.
        let dimension_string = dimension.to_string();

        // Convert the string to usize.
        let dimension_usize = match dimension_string.parse::<usize>() {
            Ok(dimension_usize) => dimension_usize,
            Err(_) => return Err(InputParserError::array_index(dimension_string, span.clone())),
        };

        // Collect dimension usize values.
        array_dimensions.push(dimension_usize);
    }

    Ok(array_dimensions)
}

///
/// Recursively fetch all dimensions from the array type.
///
fn fetch_nested_array_type_dimensions(
    array_type: ArrayType,
    mut array_dimensions: Vec<usize>,
) -> Result<(Vec<usize>, Type), InputParserError> {
    // Create a new `ArrayDimensions` type from the input array_type dimensions.
    let array_dimensions_type = ArrayDimensions::from(array_type.dimensions.clone());

    // Convert the array dimensions to usize.
    let mut current_dimension = parse_array_dimensions(array_dimensions_type, array_type.span.clone())?;

    array_dimensions.append(&mut current_dimension);

    match *array_type.type_ {
        Type::Array(next_array_type) => fetch_nested_array_type_dimensions(next_array_type, array_dimensions),
        type_ => Ok((array_dimensions, type_)),
    }
}

impl fmt::Display for InputValue {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            InputValue::Address(ref address) => write!(f, "{}", address),
            InputValue::Boolean(ref boolean) => write!(f, "{}", boolean),
            InputValue::Group(ref group) => write!(f, "{}", group),
            InputValue::Field(ref field) => write!(f, "{}", field),
            InputValue::Integer(ref type_, ref number) => write!(f, "{}{:?}", number, type_),
            InputValue::Array(ref array) => {
                let values = array.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(", ");

                write!(f, "array [{}]", values)
            }
            InputValue::Tuple(ref tuple) => {
                let values = tuple.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(", ");

                write!(f, "({})", values)
            }
        }
    }
}