support nested array type in leo programs

This commit is contained in:
collin 2020-09-03 10:02:59 -07:00
parent 2620ae7bc2
commit de3564b98d
9 changed files with 36 additions and 57 deletions

View File

@ -197,15 +197,7 @@ type_self = { "Self" }
type_circuit = { identifier }
// Declared in types/array_type.rs
type_array = { "[" ~ array_element ~ ";" ~ array_dimensions ~ "]" }
// Declared in types/array_element.rs
array_element = {
type_self
| type_tuple
| type_data
| type_circuit
}
type_array = { "[" ~ type_ ~ ";" ~ array_dimensions ~ "]" }
// Declared in types/array_dimensions.rs
array_dimensions = {

View File

@ -1,29 +0,0 @@
// 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::{ast::Rule, types::*};
use pest_ast::FromPest;
use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::array_element))]
pub enum ArrayElement<'ast> {
Basic(DataType),
Tuple(TupleType<'ast>),
Circuit(CircuitType<'ast>),
SelfType(SelfType<'ast>),
}

View File

@ -16,7 +16,7 @@
use crate::{
ast::Rule,
types::{ArrayDimensions, ArrayElement},
types::{ArrayDimensions, Type},
SpanDef,
};
@ -27,7 +27,7 @@ use serde::Serialize;
#[derive(Clone, Debug, FromPest, PartialEq, Serialize)]
#[pest_ast(rule(Rule::type_array))]
pub struct ArrayType<'ast> {
pub type_: ArrayElement<'ast>,
pub type_: Box<Type<'ast>>,
pub dimensions: ArrayDimensions<'ast>,
#[pest_ast(outer())]
#[serde(with = "SpanDef")]

View File

@ -20,9 +20,6 @@ pub use address_type::*;
pub mod array_dimensions;
pub use array_dimensions::*;
pub mod array_element;
pub use array_element::*;
pub mod array_type;
pub use array_type::*;

View File

@ -66,6 +66,22 @@ fn test_type_fail() {
assert!(syntax_error);
}
#[test]
fn test_type_tuple() {
let program_bytes = include_bytes!("type_tuple.leo");
let program = parse_program(program_bytes).unwrap();
assert_satisfied(program);
}
#[test]
fn test_type_nested() {
let program_bytes = include_bytes!("type_nested.leo");
let program = parse_program(program_bytes).unwrap();
assert_satisfied(program);
}
#[test]
fn test_inline() {
let program_bytes = include_bytes!("inline.leo");

View File

@ -1,3 +1,3 @@
let main() {
function main() {
let a: [u8; -2] = [0u32; 2];
}

View File

@ -0,0 +1,7 @@
function main() {
let a: [[u8; 3]; 2] = [[0; 3]; 2];
let b: [[u8; 3]; 2] = [0; (2, 3)];
console.assert(a == b);
}

View File

@ -0,0 +1,7 @@
function main() {
let a: [u8; (2, 3)] = [[0; 3]; 2];
let b: [u8; (2, 3)] = [0; (2, 3)];
console.assert(a == b);
}

View File

@ -15,7 +15,7 @@
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
use crate::{Expression, Identifier, IntegerType};
use leo_ast::types::{ArrayElement, ArrayType, CircuitType, DataType, TupleType, Type as AstType};
use leo_ast::types::{ArrayType, CircuitType, DataType, TupleType, Type as AstType};
use leo_input::types::{
ArrayElement as InputArrayElement,
ArrayType as InputArrayType,
@ -102,24 +102,13 @@ impl From<DataType> for Type {
impl<'ast> From<ArrayType<'ast>> for Type {
fn from(array_type: ArrayType<'ast>) -> Self {
let element_type = Box::new(Type::from(array_type.type_));
let element_type = Box::new(Type::from(*array_type.type_));
let dimensions = Expression::get_array_dimensions(array_type.dimensions);
Type::Array(element_type, dimensions)
}
}
impl<'ast> From<ArrayElement<'ast>> for Type {
fn from(element: ArrayElement<'ast>) -> Self {
match element {
ArrayElement::Basic(type_) => Type::from(type_),
ArrayElement::Tuple(type_) => Type::from(type_),
ArrayElement::Circuit(type_) => Type::from(type_),
ArrayElement::SelfType(_type) => Type::SelfType,
}
}
}
impl<'ast> From<TupleType<'ast>> for Type {
fn from(tuple_type: TupleType<'ast>) -> Self {
let types = tuple_type.types.into_iter().map(|type_| Type::from(type_)).collect();