fix multiple import bug and update tests

This commit is contained in:
collin 2020-07-02 05:48:43 -07:00
parent c30a50c5dc
commit a76a51bcd7
5 changed files with 44 additions and 51 deletions

View File

@ -13,26 +13,22 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
// get imported program name from import
// get imported symbols from from import
let imported_symbols = ImportedSymbols::from(import);
let program_name = imported_symbols.name.clone();
// get imported program from hashmap
let program = imported_programs
.get(&program_name)
.ok_or(ImportError::unknown_package(import.package.name.clone()))?;
for (package, symbol) in imported_symbols.symbols {
// get imported program from hashmap
let program = imported_programs
.get(&package)
.ok_or(ImportError::unknown_package(import.package.name.clone()))?;
// resolve imported program's import statements
program
.imports
.iter()
.map(|import| self.store_import(program_name.clone(), import, imported_programs))
.collect::<Result<Vec<()>, ImportError>>()?;
// resolve imported program's import statements
program
.imports
.iter()
.map(|import| self.store_import(package.clone(), import, imported_programs))
.collect::<Result<Vec<()>, ImportError>>()?;
// store imported symbols in constrained program
imported_symbols
.symbols
.iter()
.map(|symbol| self.store_symbol(scope.clone(), program_name.clone(), symbol, program))
.collect::<Result<Vec<()>, ImportError>>()?;
self.store_symbol(scope.clone(), package, &symbol, program)?;
}
Ok(())
}

View File

@ -2,38 +2,37 @@ use leo_types::{Import, ImportSymbol, Package, PackageAccess};
#[derive(Debug)]
pub(crate) struct ImportedSymbols {
pub name: String,
pub symbols: Vec<ImportSymbol>,
pub symbols: Vec<(String, ImportSymbol)>,
}
impl ImportedSymbols {
fn new(name: String) -> Self {
Self { name, symbols: vec![] }
fn new() -> Self {
Self { symbols: vec![] }
}
pub(crate) fn from(import: &Import) -> Self {
let mut symbols = Self::new(import.package.name.name.clone());
let mut symbols = Self::new();
symbols.from_package_access(&import.package.access);
symbols.from_package(&import.package);
symbols
}
fn from_package(&mut self, package: &Package) {
self.name = package.name.name.clone();
self.from_package_access(&package.access);
self.from_package_access(package.name.name.clone(), &package.access);
}
fn from_package_access(&mut self, access: &PackageAccess) {
fn from_package_access(&mut self, package: String, access: &PackageAccess) {
match access {
PackageAccess::SubPackage(package) => self.from_package(package),
PackageAccess::Star(span) => {
let star = ImportSymbol::star(span);
self.symbols.push(star);
self.symbols.push((package, star));
}
PackageAccess::Symbol(symbol) => self.symbols.push(symbol.clone()),
PackageAccess::Multiple(packages) => packages.iter().for_each(|access| self.from_package_access(access)),
PackageAccess::Symbol(symbol) => self.symbols.push((package, symbol.clone())),
PackageAccess::Multiple(packages) => packages
.iter()
.for_each(|access| self.from_package_access(package.clone(), access)),
}
}
}

View File

@ -57,7 +57,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
program_name.clone(),
Box::new(ConstrainedValue::Function(None, function.clone())),
),
None => return Err(ImportError::unknown_symbol(symbol.to_owned(), scope)),
None => return Err(ImportError::unknown_symbol(symbol.to_owned(), program_name)),
}
}
};

View File

@ -12,12 +12,12 @@ import bar.( // imports directory import
import car.Car; // imports directory import
function main() -> u32 {
// const point = Point { x: 1u32, y: 1u32 };
// const foo = foo();
const point = Point { x: 1u32, y: 1u32 };
const foo = foo();
const bar = Bar { r: 1u32 };
const bat = Bat { t: 1u32 };
const baz = Baz { z: 1u32 };
const bat = Bat { t: 1u32 };
const car = Car { c: 1u32 };

View File

@ -1,4 +1,4 @@
use crate::{get_error, integers::u32::output_one, parse_program};
use crate::{integers::u32::output_one, parse_program};
use std::env::{current_dir, set_current_dir};
@ -16,55 +16,53 @@ fn set_local_dir() {
#[test]
#[ignore]
fn test_basic() {
set_local_dir();
let bytes = include_bytes!("basic.leo");
let program = parse_program(bytes).unwrap();
set_local_dir();
output_one(program);
}
#[test]
#[ignore]
fn test_multiple() {
set_local_dir();
let bytes = include_bytes!("multiple.leo");
let program = parse_program(bytes).unwrap();
set_local_dir();
output_one(program);
}
#[test]
#[ignore]
fn test_star() {
set_local_dir();
let bytes = include_bytes!("star.leo");
let program = parse_program(bytes).unwrap();
set_local_dir();
output_one(program);
}
#[test]
#[ignore]
fn test_star_fail() {
let bytes = include_bytes!("star_fail.leo");
let program = parse_program(bytes).unwrap();
set_local_dir();
let _err = get_error(program);
let bytes = include_bytes!("star_fail.leo");
assert!(parse_program(bytes).is_err());
}
#[test]
#[ignore]
fn test_alias() {
set_local_dir();
let bytes = include_bytes!("alias.leo");
let program = parse_program(bytes).unwrap();
set_local_dir();
output_one(program);
}
@ -72,21 +70,21 @@ fn test_alias() {
#[test]
#[ignore]
fn test_many_import() {
set_local_dir();
let bytes = include_bytes!("many_import.leo");
let program = parse_program(bytes).unwrap();
set_local_dir();
output_one(program);
}
#[test]
#[ignore]
fn test_many_import_star() {
set_local_dir();
let bytes = include_bytes!("many_import_star.leo");
let program = parse_program(bytes).unwrap();
set_local_dir();
output_one(program);
}