From 6ba556ceda1a899983cffde5d73ed20d328fa7d6 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Thu, 4 Feb 2021 14:52:57 -0500 Subject: [PATCH 1/8] fixed imports, but with different syntax that should be logical equivalent --- asg/src/program/mod.rs | 30 ++++++++----- ast/src/imports/import.rs | 13 +++--- ast/src/imports/mod.rs | 6 +++ ast/src/imports/package_access.rs | 14 +++--- ast/src/imports/package_type.rs | 50 +++++++++++++++++++++ ast/src/imports/packages.rs | 63 +++++++++++++++++++++++++++ grammar/src/imports/import.rs | 4 +- grammar/src/imports/mod.rs | 6 +++ grammar/src/imports/package_access.rs | 4 +- grammar/src/imports/package_type.rs | 30 +++++++++++++ grammar/src/imports/packages.rs | 35 +++++++++++++++ grammar/src/leo.pest | 13 ++++-- 12 files changed, 237 insertions(+), 31 deletions(-) create mode 100644 ast/src/imports/package_type.rs create mode 100644 ast/src/imports/packages.rs create mode 100644 grammar/src/imports/package_type.rs create mode 100644 grammar/src/imports/packages.rs diff --git a/asg/src/program/mod.rs b/asg/src/program/mod.rs index 93683d7127..ccc34a532a 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -25,7 +25,7 @@ mod function; pub use function::*; use crate::{AsgConvertError, ImportResolver, InnerScope, Input, Scope}; -use leo_ast::{Identifier, Package, PackageAccess, Span}; +use leo_ast::{Identifier, PackageAccess, PackageType, Span}; use indexmap::IndexMap; use std::{cell::RefCell, sync::Arc}; @@ -74,11 +74,21 @@ enum ImportSymbol { fn resolve_import_package( output: &mut Vec<(Vec, ImportSymbol, Span)>, mut package_segments: Vec, - package: &Package, + package_type: &PackageType, ) { - package_segments.push(package.name.name.clone()); + match package_type { + PackageType::Package(package) => { + package_segments.push(package.name.name.clone()); + resolve_import_package_access(output, package_segments, &package.access); + } + PackageType::Packages(packages) => { + package_segments.push(packages.name.name.clone()); - resolve_import_package_access(output, package_segments, &package.access); + for access in packages.accesses.clone() { + resolve_import_package_access(output, package_segments.clone(), &access); + } + } + } } fn resolve_import_package_access( @@ -91,7 +101,7 @@ fn resolve_import_package_access( output.push((package_segments, ImportSymbol::All, span.clone())); } PackageAccess::SubPackage(subpackage) => { - resolve_import_package(output, package_segments, &*subpackage); + resolve_import_package(output, package_segments, &PackageType::Package(*(*subpackage).clone())); } PackageAccess::Symbol(symbol) => { let span = symbol.symbol.span.clone(); @@ -102,8 +112,8 @@ fn resolve_import_package_access( }; output.push((package_segments, symbol, span)); } - PackageAccess::Multiple(subaccesses) => { - for subaccess in subaccesses.iter() { + PackageAccess::Multiple(packages) => { + for subaccess in packages.accesses.iter() { resolve_import_package_access(output, package_segments.clone(), &subaccess); } } @@ -126,7 +136,7 @@ impl InnerProgram { // Recursively extract imported symbols. let mut imported_symbols: Vec<(Vec, ImportSymbol, Span)> = vec![]; for import in value.imports.iter() { - resolve_import_package(&mut imported_symbols, vec![], &import.package); + resolve_import_package(&mut imported_symbols, vec![], &import.package_type); } // Create package list. @@ -383,11 +393,11 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program { imports: core_programs .iter() .map(|(module, _)| leo_ast::ImportStatement { - package: leo_ast::Package { + package_type: leo_ast::PackageType::Package(leo_ast::Package { name: Identifier::new(module.clone()), access: leo_ast::PackageAccess::Star(Span::default()), span: Default::default(), - }, + }), span: Span::default(), }) .collect(), diff --git a/ast/src/imports/import.rs b/ast/src/imports/import.rs index 614fa5b36d..7dc796a518 100644 --- a/ast/src/imports/import.rs +++ b/ast/src/imports/import.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{Package, Span}; +use crate::{PackageType, Span}; use leo_grammar::imports::Import as GrammarImport; use serde::{Deserialize, Serialize}; @@ -23,7 +23,7 @@ use std::fmt; /// Represents an import statement in a Leo program. #[derive(Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct ImportStatement { - pub package: Package, + pub package_type: PackageType, pub span: Span, } @@ -32,14 +32,17 @@ impl ImportStatement { /// Returns the the package file name of the self import statement. /// pub fn get_file_name(&self) -> &str { - &self.package.name.name + match self.package_type { + PackageType::Package(ref package) => &package.name.name, + PackageType::Packages(ref packages) => &packages.name.name, + } } } impl<'ast> From> for ImportStatement { fn from(import: GrammarImport<'ast>) -> Self { ImportStatement { - package: Package::from(import.package), + package_type: PackageType::from(import.package_type), span: Span::from(import.span), } } @@ -47,7 +50,7 @@ impl<'ast> From> for ImportStatement { impl ImportStatement { fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "import {};", self.package) + write!(f, "import {};", self.package_type) } } diff --git a/ast/src/imports/mod.rs b/ast/src/imports/mod.rs index ba6e6d8eb9..45a6eb3fe0 100644 --- a/ast/src/imports/mod.rs +++ b/ast/src/imports/mod.rs @@ -23,5 +23,11 @@ pub use import_symbol::*; pub mod package; pub use package::*; +pub mod packages; +pub use packages::*; + +pub mod package_type; +pub use package_type::*; + pub mod package_access; pub use package_access::*; diff --git a/ast/src/imports/package_access.rs b/ast/src/imports/package_access.rs index a94b77452e..b329cc6b82 100644 --- a/ast/src/imports/package_access.rs +++ b/ast/src/imports/package_access.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ImportSymbol, Package, Span}; +use crate::{ImportSymbol, Package, Packages, Span}; use leo_grammar::imports::PackageAccess as GrammarPackageAccess; use serde::{Deserialize, Serialize}; @@ -25,7 +25,7 @@ pub enum PackageAccess { Star(Span), SubPackage(Box), Symbol(ImportSymbol), - Multiple(Vec), + Multiple(Packages), } impl<'ast> From> for PackageAccess { @@ -34,9 +34,7 @@ impl<'ast> From> for PackageAccess { GrammarPackageAccess::Star(star) => PackageAccess::Star(Span::from(star.span)), GrammarPackageAccess::SubPackage(package) => PackageAccess::SubPackage(Box::new(Package::from(*package))), GrammarPackageAccess::Symbol(symbol) => PackageAccess::Symbol(ImportSymbol::from(symbol)), - GrammarPackageAccess::Multiple(accesses) => { - PackageAccess::Multiple(accesses.into_iter().map(PackageAccess::from).collect()) - } + GrammarPackageAccess::Multiple(packages) => PackageAccess::Multiple(Packages::from(packages)), } } } @@ -47,11 +45,11 @@ impl PackageAccess { PackageAccess::Star(ref _span) => write!(f, "*"), PackageAccess::SubPackage(ref package) => write!(f, "{}", package), PackageAccess::Symbol(ref symbol) => write!(f, "{}", symbol), - PackageAccess::Multiple(ref accesses) => { + PackageAccess::Multiple(ref packages) => { write!(f, "(")?; - for (i, access) in accesses.iter().enumerate() { + for (i, access) in packages.accesses.iter().enumerate() { write!(f, "{}", access)?; - if i < accesses.len() - 1 { + if i < packages.accesses.len() - 1 { write!(f, ", ")?; } } diff --git a/ast/src/imports/package_type.rs b/ast/src/imports/package_type.rs new file mode 100644 index 0000000000..12e6f49034 --- /dev/null +++ b/ast/src/imports/package_type.rs @@ -0,0 +1,50 @@ +use crate::{Package, Packages}; +use leo_grammar::imports::PackageType as GrammarPackageType; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)] +pub enum PackageType { + Package(Package), + Packages(Packages), +} + +impl<'ast> From> for PackageType { + fn from(package_type: GrammarPackageType<'ast>) -> Self { + match package_type { + GrammarPackageType::Package(package) => PackageType::Package(Package::from(package)), + GrammarPackageType::Packages(packages) => PackageType::Packages(Packages::from(packages)), + } + } +} + +impl PackageType { + fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + PackageType::Package(ref package) => write!(f, "{}", package), + PackageType::Packages(ref packages) => { + write!(f, "(")?; + for (i, access) in packages.accesses.iter().enumerate() { + write!(f, "{}", access)?; + if i < packages.accesses.len() - 1 { + write!(f, ", ")?; + } + } + write!(f, ")") + } + } + } +} + +impl fmt::Debug for PackageType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.format(f) + } +} + +impl fmt::Display for PackageType { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.format(f) + } +} diff --git a/ast/src/imports/packages.rs b/ast/src/imports/packages.rs new file mode 100644 index 0000000000..fab9542716 --- /dev/null +++ b/ast/src/imports/packages.rs @@ -0,0 +1,63 @@ +// Copyright (C) 2019-2021 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 . + +use crate::{common::Identifier, PackageAccess, Span}; +use leo_grammar::imports::Packages as GrammarPackages; + +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)] +pub struct Packages { + pub name: Identifier, + pub accesses: Vec, + pub span: Span, +} + +impl<'ast> From> for Packages { + fn from(packages: GrammarPackages<'ast>) -> Self { + Packages { + name: Identifier::from(packages.name), + accesses: packages.accesses.into_iter().map(PackageAccess::from).collect(), + span: Span::from(packages.span), + } + } +} + +impl Packages { + fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}.(", self.name)?; + for (i, access) in self.accesses.iter().enumerate() { + write!(f, "{}", access)?; + if i < self.accesses.len() - 1 { + write!(f, ", ")?; + } + } + write!(f, ")") + } +} + +impl fmt::Display for Packages { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.format(f) + } +} + +impl fmt::Debug for Packages { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + self.format(f) + } +} diff --git a/grammar/src/imports/import.rs b/grammar/src/imports/import.rs index 37a9fde2ec..f15f1db37e 100644 --- a/grammar/src/imports/import.rs +++ b/grammar/src/imports/import.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ast::Rule, common::LineEnd, imports::Package, SpanDef}; +use crate::{ast::Rule, common::LineEnd, imports::PackageType, SpanDef}; use pest::Span; use pest_ast::FromPest; @@ -23,7 +23,7 @@ use serde::Serialize; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::import))] pub struct Import<'ast> { - pub package: Package<'ast>, + pub package_type: PackageType<'ast>, pub line_end: LineEnd, #[pest_ast(outer())] #[serde(with = "SpanDef")] diff --git a/grammar/src/imports/mod.rs b/grammar/src/imports/mod.rs index d16b54c012..2df37279d8 100644 --- a/grammar/src/imports/mod.rs +++ b/grammar/src/imports/mod.rs @@ -23,6 +23,12 @@ pub use import_symbol::*; pub mod package; pub use package::*; +pub mod packages; +pub use packages::*; + +pub mod package_type; +pub use package_type::*; + pub mod package_access; pub use package_access::*; diff --git a/grammar/src/imports/package_access.rs b/grammar/src/imports/package_access.rs index e22194e231..7d172bd545 100644 --- a/grammar/src/imports/package_access.rs +++ b/grammar/src/imports/package_access.rs @@ -16,7 +16,7 @@ use crate::{ ast::Rule, - imports::{ImportSymbol, Package, Star}, + imports::{ImportSymbol, Package, Packages, Star}, }; use pest_ast::FromPest; @@ -28,5 +28,5 @@ pub enum PackageAccess<'ast> { Star(Star<'ast>), SubPackage(Box>), Symbol(ImportSymbol<'ast>), - Multiple(Vec>), + Multiple(Packages<'ast>), } diff --git a/grammar/src/imports/package_type.rs b/grammar/src/imports/package_type.rs new file mode 100644 index 0000000000..dc6242a7e9 --- /dev/null +++ b/grammar/src/imports/package_type.rs @@ -0,0 +1,30 @@ +// Copyright (C) 2019-2021 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 . + +use crate::{ + ast::Rule, + imports::{Package, Packages}, +}; + +use pest_ast::FromPest; +use serde::Serialize; + +#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] +#[pest_ast(rule(Rule::package_type))] +pub enum PackageType<'ast> { + Package(Package<'ast>), + Packages(Packages<'ast>), +} diff --git a/grammar/src/imports/packages.rs b/grammar/src/imports/packages.rs new file mode 100644 index 0000000000..32003ffb16 --- /dev/null +++ b/grammar/src/imports/packages.rs @@ -0,0 +1,35 @@ +// Copyright (C) 2019-2021 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 . + +use crate::{ + ast::Rule, + imports::{PackageAccess, PackageName}, + SpanDef, +}; + +use pest::Span; +use pest_ast::FromPest; +use serde::Serialize; + +#[derive(Clone, Debug, FromPest, PartialEq, Serialize)] +#[pest_ast(rule(Rule::packages))] +pub struct Packages<'ast> { + pub name: PackageName<'ast>, + pub accesses: Vec>, + #[pest_ast(outer())] + #[serde(with = "SpanDef")] + pub span: Span<'ast>, +} diff --git a/grammar/src/leo.pest b/grammar/src/leo.pest index a5748cedbf..c6aa0596cb 100644 --- a/grammar/src/leo.pest +++ b/grammar/src/leo.pest @@ -448,19 +448,24 @@ input_tuple = _{ "(" ~ (input ~ ("," ~ input)* ~ ","?)? ~ ")"} /// Imports // Declared in imports/import.rs -import = { "import " ~ package ~ LINE_END} +import = { "import " ~ package_type ~ LINE_END} // Declared in imports/package_name.rs package_name = @{ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+ ~ ( "-" ~ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+)* } // Declared in imports/package.rs +package_type = { + packages + | package +} package = { package_name ~ "." ~ package_access } +packages = { package_name ~ "." ~ multiple_package_access } // Declared in imports/package_access package_access = { - multiple_package_access - | star - | package // subpackage + star + | packages // subpackage + | package | import_symbol } From 6831cf402fe4845409108d21d57cc51efec7fc58 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Thu, 4 Feb 2021 15:07:14 -0500 Subject: [PATCH 2/8] put explicit type in perdesen-hash leo program --- examples/pedersen-hash/src/main.leo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/pedersen-hash/src/main.leo b/examples/pedersen-hash/src/main.leo index c1d79bf74f..225a05ad19 100644 --- a/examples/pedersen-hash/src/main.leo +++ b/examples/pedersen-hash/src/main.leo @@ -7,7 +7,7 @@ circuit PedersenHash { } function hash(self, bits: [bool; 256]) -> group { - let mut digest: group = 0; + let mut digest: group = 0group; for i in 0..256 { if bits[i] { digest += self.parameters[i]; From 9d881fd2a4bc1a02409f601a373e4e72064b4423 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Thu, 4 Feb 2021 15:15:36 -0500 Subject: [PATCH 3/8] license fix --- ast/src/imports/package_type.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ast/src/imports/package_type.rs b/ast/src/imports/package_type.rs index 12e6f49034..94cc82d10e 100644 --- a/ast/src/imports/package_type.rs +++ b/ast/src/imports/package_type.rs @@ -1,3 +1,19 @@ +// Copyright (C) 2019-2021 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 . + use crate::{Package, Packages}; use leo_grammar::imports::PackageType as GrammarPackageType; From 59acb1a0227d6143674454e5ce3f39b7f11567c1 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Fri, 5 Feb 2021 11:36:21 -0500 Subject: [PATCH 4/8] unignore test and add test case to thoroughly test --- compiler/tests/import/imports/bar/src/baz.leo | 4 ++++ compiler/tests/import/many_import.leo | 3 ++- compiler/tests/import/mod.rs | 1 - 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/compiler/tests/import/imports/bar/src/baz.leo b/compiler/tests/import/imports/bar/src/baz.leo index 6c1df2d4b9..1bb268a84c 100755 --- a/compiler/tests/import/imports/bar/src/baz.leo +++ b/compiler/tests/import/imports/bar/src/baz.leo @@ -1,3 +1,7 @@ circuit Baz { z: u32 +} + +circuit Bazzar { + a: u32 } \ No newline at end of file diff --git a/compiler/tests/import/many_import.leo b/compiler/tests/import/many_import.leo index 339700fd68..08ae494c4f 100644 --- a/compiler/tests/import/many_import.leo +++ b/compiler/tests/import/many_import.leo @@ -5,7 +5,7 @@ import test-import.( // local import import bar.( // imports directory import Bar, - baz.Baz, + baz.(Baz, Bazzar), bat.bat.Bat, ); @@ -17,6 +17,7 @@ function main() { const bar = Bar { r: 1u32 }; const baz = Baz { z: 1u32 }; + const bazzar = Bazzar { a: 1u32 }; const bat = Bat { t: 1u32 }; const car = Car { c: 1u32 }; diff --git a/compiler/tests/import/mod.rs b/compiler/tests/import/mod.rs index 1a3440b577..0d0e18107b 100644 --- a/compiler/tests/import/mod.rs +++ b/compiler/tests/import/mod.rs @@ -132,7 +132,6 @@ fn test_names_fail_4() { // more complex tests #[test] -#[ignore] fn test_many_import() { set_local_dir(); From 6aea561a5213df0c21045c2b90f419601cfb4d89 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Fri, 5 Feb 2021 11:40:56 -0500 Subject: [PATCH 5/8] forgot to push fix for Multiple enum resolve --- asg/src/program/mod.rs | 4 ++-- grammar/src/leo.pest | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/asg/src/program/mod.rs b/asg/src/program/mod.rs index ccc34a532a..224474a11d 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -83,7 +83,6 @@ fn resolve_import_package( } PackageType::Packages(packages) => { package_segments.push(packages.name.name.clone()); - for access in packages.accesses.clone() { resolve_import_package_access(output, package_segments.clone(), &access); } @@ -93,7 +92,7 @@ fn resolve_import_package( fn resolve_import_package_access( output: &mut Vec<(Vec, ImportSymbol, Span)>, - package_segments: Vec, + mut package_segments: Vec, package: &PackageAccess, ) { match package { @@ -113,6 +112,7 @@ fn resolve_import_package_access( output.push((package_segments, symbol, span)); } PackageAccess::Multiple(packages) => { + package_segments.push(packages.name.name.clone()); for subaccess in packages.accesses.iter() { resolve_import_package_access(output, package_segments.clone(), &subaccess); } diff --git a/grammar/src/leo.pest b/grammar/src/leo.pest index c6aa0596cb..968d6c3dec 100644 --- a/grammar/src/leo.pest +++ b/grammar/src/leo.pest @@ -453,19 +453,21 @@ import = { "import " ~ package_type ~ LINE_END} // Declared in imports/package_name.rs package_name = @{ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+ ~ ( "-" ~ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+)* } -// Declared in imports/package.rs +// Declared in imports/package_type.rs package_type = { packages | package } +// Declared in imports/package.rs package = { package_name ~ "." ~ package_access } +// Declared in imports/packages.rs packages = { package_name ~ "." ~ multiple_package_access } // Declared in imports/package_access package_access = { star - | packages // subpackage - | package + | packages + | package // subpackage | import_symbol } From f7add0fc983f6f63873a4f9aadb0daa487701f53 Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Fri, 5 Feb 2021 15:33:48 -0500 Subject: [PATCH 6/8] re-ignore import test-many in compiler, it breaks pipeline --- compiler/tests/import/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/compiler/tests/import/mod.rs b/compiler/tests/import/mod.rs index 0d0e18107b..1a3440b577 100644 --- a/compiler/tests/import/mod.rs +++ b/compiler/tests/import/mod.rs @@ -132,6 +132,7 @@ fn test_names_fail_4() { // more complex tests #[test] +#[ignore] fn test_many_import() { set_local_dir(); From d211d729d1ebd0974f8b8494071e5e5cfd8d2fec Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Tue, 9 Feb 2021 10:57:37 -0500 Subject: [PATCH 7/8] refactor pacakge_type -> package_or_packages --- asg/src/program/mod.rs | 20 +++++++++------- ast/src/imports/import.rs | 14 +++++------ ast/src/imports/mod.rs | 4 ++-- ...package_type.rs => package_or_packages.rs} | 24 +++++++++---------- grammar/src/imports/import.rs | 4 ++-- grammar/src/imports/mod.rs | 4 ++-- ...package_type.rs => package_or_packages.rs} | 4 ++-- grammar/src/leo.pest | 6 ++--- 8 files changed, 42 insertions(+), 38 deletions(-) rename ast/src/imports/{package_type.rs => package_or_packages.rs} (67%) rename grammar/src/imports/{package_type.rs => package_or_packages.rs} (92%) diff --git a/asg/src/program/mod.rs b/asg/src/program/mod.rs index d70c2cacf8..4de5e81f54 100644 --- a/asg/src/program/mod.rs +++ b/asg/src/program/mod.rs @@ -25,7 +25,7 @@ mod function; pub use function::*; use crate::{AsgConvertError, ImportResolver, InnerScope, Input, Scope}; -use leo_ast::{Identifier, PackageAccess, PackageType, Span}; +use leo_ast::{Identifier, PackageAccess, PackageOrPackages, Span}; use indexmap::IndexMap; use std::{cell::RefCell, sync::Arc}; @@ -74,14 +74,14 @@ enum ImportSymbol { fn resolve_import_package( output: &mut Vec<(Vec, ImportSymbol, Span)>, mut package_segments: Vec, - package_type: &PackageType, + package_or_packages: &PackageOrPackages, ) { - match package_type { - PackageType::Package(package) => { + match package_or_packages { + PackageOrPackages::Package(package) => { package_segments.push(package.name.name.clone()); resolve_import_package_access(output, package_segments, &package.access); } - PackageType::Packages(packages) => { + PackageOrPackages::Packages(packages) => { package_segments.push(packages.name.name.clone()); for access in packages.accesses.clone() { resolve_import_package_access(output, package_segments.clone(), &access); @@ -100,7 +100,11 @@ fn resolve_import_package_access( output.push((package_segments, ImportSymbol::All, span.clone())); } PackageAccess::SubPackage(subpackage) => { - resolve_import_package(output, package_segments, &PackageType::Package(*(*subpackage).clone())); + resolve_import_package( + output, + package_segments, + &PackageOrPackages::Package(*(*subpackage).clone()), + ); } PackageAccess::Symbol(symbol) => { let span = symbol.symbol.span.clone(); @@ -136,7 +140,7 @@ impl InternalProgram { // Recursively extract imported symbols. let mut imported_symbols: Vec<(Vec, ImportSymbol, Span)> = vec![]; for import in program.imports.iter() { - resolve_import_package(&mut imported_symbols, vec![], &import.package_type); + resolve_import_package(&mut imported_symbols, vec![], &import.package_or_packages); } // Create package list. @@ -393,7 +397,7 @@ pub fn reform_ast(program: &Program) -> leo_ast::Program { imports: core_programs .iter() .map(|(module, _)| leo_ast::ImportStatement { - package_type: leo_ast::PackageType::Package(leo_ast::Package { + package_or_packages: leo_ast::PackageOrPackages::Package(leo_ast::Package { name: Identifier::new(module.clone()), access: leo_ast::PackageAccess::Star(Span::default()), span: Default::default(), diff --git a/ast/src/imports/import.rs b/ast/src/imports/import.rs index 7dc796a518..246f61c3b1 100644 --- a/ast/src/imports/import.rs +++ b/ast/src/imports/import.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{PackageType, Span}; +use crate::{PackageOrPackages, Span}; use leo_grammar::imports::Import as GrammarImport; use serde::{Deserialize, Serialize}; @@ -23,7 +23,7 @@ use std::fmt; /// Represents an import statement in a Leo program. #[derive(Clone, Eq, PartialEq, Serialize, Deserialize)] pub struct ImportStatement { - pub package_type: PackageType, + pub package_or_packages: PackageOrPackages, pub span: Span, } @@ -32,9 +32,9 @@ impl ImportStatement { /// Returns the the package file name of the self import statement. /// pub fn get_file_name(&self) -> &str { - match self.package_type { - PackageType::Package(ref package) => &package.name.name, - PackageType::Packages(ref packages) => &packages.name.name, + match self.package_or_packages { + PackageOrPackages::Package(ref package) => &package.name.name, + PackageOrPackages::Packages(ref packages) => &packages.name.name, } } } @@ -42,7 +42,7 @@ impl ImportStatement { impl<'ast> From> for ImportStatement { fn from(import: GrammarImport<'ast>) -> Self { ImportStatement { - package_type: PackageType::from(import.package_type), + package_or_packages: PackageOrPackages::from(import.package_or_packages), span: Span::from(import.span), } } @@ -50,7 +50,7 @@ impl<'ast> From> for ImportStatement { impl ImportStatement { fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "import {};", self.package_type) + write!(f, "import {};", self.package_or_packages) } } diff --git a/ast/src/imports/mod.rs b/ast/src/imports/mod.rs index 45a6eb3fe0..a1b223c861 100644 --- a/ast/src/imports/mod.rs +++ b/ast/src/imports/mod.rs @@ -26,8 +26,8 @@ pub use package::*; pub mod packages; pub use packages::*; -pub mod package_type; -pub use package_type::*; +pub mod package_or_packages; +pub use package_or_packages::*; pub mod package_access; pub use package_access::*; diff --git a/ast/src/imports/package_type.rs b/ast/src/imports/package_or_packages.rs similarity index 67% rename from ast/src/imports/package_type.rs rename to ast/src/imports/package_or_packages.rs index 94cc82d10e..2f1b9bc717 100644 --- a/ast/src/imports/package_type.rs +++ b/ast/src/imports/package_or_packages.rs @@ -15,31 +15,31 @@ // along with the Leo library. If not, see . use crate::{Package, Packages}; -use leo_grammar::imports::PackageType as GrammarPackageType; +use leo_grammar::imports::PackageOrPackages as GrammarPackageOrPackages; use serde::{Deserialize, Serialize}; use std::fmt; #[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)] -pub enum PackageType { +pub enum PackageOrPackages { Package(Package), Packages(Packages), } -impl<'ast> From> for PackageType { - fn from(package_type: GrammarPackageType<'ast>) -> Self { - match package_type { - GrammarPackageType::Package(package) => PackageType::Package(Package::from(package)), - GrammarPackageType::Packages(packages) => PackageType::Packages(Packages::from(packages)), +impl<'ast> From> for PackageOrPackages { + fn from(package_or_packages: GrammarPackageOrPackages<'ast>) -> Self { + match package_or_packages { + GrammarPackageOrPackages::Package(package) => PackageOrPackages::Package(Package::from(package)), + GrammarPackageOrPackages::Packages(packages) => PackageOrPackages::Packages(Packages::from(packages)), } } } -impl PackageType { +impl PackageOrPackages { fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { - PackageType::Package(ref package) => write!(f, "{}", package), - PackageType::Packages(ref packages) => { + PackageOrPackages::Package(ref package) => write!(f, "{}", package), + PackageOrPackages::Packages(ref packages) => { write!(f, "(")?; for (i, access) in packages.accesses.iter().enumerate() { write!(f, "{}", access)?; @@ -53,13 +53,13 @@ impl PackageType { } } -impl fmt::Debug for PackageType { +impl fmt::Debug for PackageOrPackages { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.format(f) } } -impl fmt::Display for PackageType { +impl fmt::Display for PackageOrPackages { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.format(f) } diff --git a/grammar/src/imports/import.rs b/grammar/src/imports/import.rs index f15f1db37e..1b62df80f3 100644 --- a/grammar/src/imports/import.rs +++ b/grammar/src/imports/import.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with the Leo library. If not, see . -use crate::{ast::Rule, common::LineEnd, imports::PackageType, SpanDef}; +use crate::{ast::Rule, common::LineEnd, imports::PackageOrPackages, SpanDef}; use pest::Span; use pest_ast::FromPest; @@ -23,7 +23,7 @@ use serde::Serialize; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] #[pest_ast(rule(Rule::import))] pub struct Import<'ast> { - pub package_type: PackageType<'ast>, + pub package_or_packages: PackageOrPackages<'ast>, pub line_end: LineEnd, #[pest_ast(outer())] #[serde(with = "SpanDef")] diff --git a/grammar/src/imports/mod.rs b/grammar/src/imports/mod.rs index 2df37279d8..7665e520c5 100644 --- a/grammar/src/imports/mod.rs +++ b/grammar/src/imports/mod.rs @@ -26,8 +26,8 @@ pub use package::*; pub mod packages; pub use packages::*; -pub mod package_type; -pub use package_type::*; +pub mod package_or_packages; +pub use package_or_packages::*; pub mod package_access; pub use package_access::*; diff --git a/grammar/src/imports/package_type.rs b/grammar/src/imports/package_or_packages.rs similarity index 92% rename from grammar/src/imports/package_type.rs rename to grammar/src/imports/package_or_packages.rs index dc6242a7e9..3cd0324480 100644 --- a/grammar/src/imports/package_type.rs +++ b/grammar/src/imports/package_or_packages.rs @@ -23,8 +23,8 @@ use pest_ast::FromPest; use serde::Serialize; #[derive(Clone, Debug, FromPest, PartialEq, Serialize)] -#[pest_ast(rule(Rule::package_type))] -pub enum PackageType<'ast> { +#[pest_ast(rule(Rule::package_or_packages))] +pub enum PackageOrPackages<'ast> { Package(Package<'ast>), Packages(Packages<'ast>), } diff --git a/grammar/src/leo.pest b/grammar/src/leo.pest index f215d16f13..5d681c92d3 100644 --- a/grammar/src/leo.pest +++ b/grammar/src/leo.pest @@ -454,13 +454,13 @@ input_tuple = _{ "(" ~ (input ~ ("," ~ input)* ~ ","?)? ~ ")"} /// Imports // Declared in imports/import.rs -import = { "import " ~ package_type ~ LINE_END} +import = { "import " ~ package_or_packages ~ LINE_END} // Declared in imports/package_name.rs package_name = @{ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+ ~ ( "-" ~ (ASCII_ALPHA_LOWER | ASCII_DIGIT)+)* } -// Declared in imports/package_type.rs -package_type = { +// Declared in imports/package_or_packages.rs +package_or_packages = { packages | package } From f02845aa6c60e1ec61ec420f6c10f72656af42de Mon Sep 17 00:00:00 2001 From: gluaxspeed Date: Tue, 9 Feb 2021 11:11:45 -0500 Subject: [PATCH 8/8] forgot to refactor grammar tests --- grammar/tests/imports.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/grammar/tests/imports.rs b/grammar/tests/imports.rs index fb8cdd9a06..8ddd874e3d 100644 --- a/grammar/tests/imports.rs +++ b/grammar/tests/imports.rs @@ -26,7 +26,7 @@ fn test_import_package_rule() { rule: Rule::import, tokens: [ import(0, 11, [ - package_type(7, 10, [ + package_or_packages(7, 10, [ package(7, 10, [ package_name(7, 8, []), package_access(9, 10, [star(9, 10, [])]) @@ -46,7 +46,7 @@ fn test_import_packages_rule() { rule: Rule::import, tokens: [ import(0, 16, [ - package_type(7, 15, [ + package_or_packages(7, 15, [ packages(7, 15, [ package_name(7, 8, []), package_access(10, 11, [ @@ -71,7 +71,7 @@ fn test_complex_import_rule() { rule: Rule::import, tokens: [ import(0, 23, [ - package_type(7, 22, [ + package_or_packages(7, 22, [ packages(7, 22, [ package_name(7, 8, []), package_access(10, 18, [