fix import module

This commit is contained in:
collin 2020-07-07 21:05:03 -07:00
parent 5557f08435
commit 965bdc4d6a
19 changed files with 62 additions and 52 deletions

View File

@ -321,9 +321,9 @@ package = { identifier ~ "." ~ package_access }
// Declared in imports/package_access
package_access = {
star
multiple_package_access
| star
| package // subpackage
| multiple_package_access
| import_symbol
}
@ -333,7 +333,7 @@ multiple_package_access = _{ "(" ~ NEWLINE* ~ package_access ~ ("," ~ NEWLINE* ~
star = {"*"}
// Declared in imports/import_symbol.rs
import_symbol = { identifier ~ (" as " ~ identifier)? }
import_symbol = { identifier ~ ("as " ~ identifier)? }
/// Utilities

View File

@ -5,7 +5,7 @@ use crate::{
errors::CompilerError,
value::ConstrainedValue,
GroupType,
ImportedPrograms,
ImportParser,
};
use leo_ast::LeoParser;
use leo_inputs::LeoInputsParser;
@ -26,7 +26,7 @@ pub struct Compiler<F: Field + PrimeField, G: GroupType<F>> {
main_file_path: PathBuf,
program: Program,
program_inputs: Inputs,
imported_programs: ImportedPrograms,
imported_programs: ImportParser,
output: Option<ConstrainedValue<F, G>>,
_engine: PhantomData<F>,
}
@ -38,7 +38,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
main_file_path: PathBuf::new(),
program: Program::new(package_name),
program_inputs: Inputs::new(),
imported_programs: ImportedPrograms::new(),
imported_programs: ImportParser::new(),
output: None,
_engine: PhantomData,
}
@ -108,7 +108,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
self.program = Program::from(syntax_tree, package_name);
self.program_inputs.set_inputs_size(self.program.expected_inputs.len());
self.imported_programs = ImportedPrograms::from_program(&self.program)?;
self.imported_programs = ImportParser::parse(&self.program)?;
log::debug!("Program parsing complete\n{:#?}", self.program);
@ -139,7 +139,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
main_file_path: PathBuf::new(),
program,
program_inputs,
imported_programs: ImportedPrograms::new(),
imported_programs: ImportParser::new(),
output: None,
_engine: PhantomData,
})

View File

@ -1,4 +1,4 @@
use crate::{errors::CompilerError, new_scope, ConstrainedProgram, ConstrainedValue, GroupType, ImportedPrograms};
use crate::{errors::CompilerError, new_scope, ConstrainedProgram, ConstrainedValue, GroupType, ImportParser};
use leo_types::{InputValue, Program};
use snarkos_models::{
@ -10,7 +10,7 @@ pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: Constrai
cs: &mut CS,
program: Program,
parameters: Vec<Option<InputValue>>,
imported_programs: &ImportedPrograms,
imported_programs: &ImportParser,
) -> Result<ConstrainedValue<F, G>, CompilerError> {
let mut resolved_program = ConstrainedProgram::new();
let program_name = program.get_name();
@ -34,7 +34,7 @@ pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: Constrai
pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
cs: &mut TestConstraintSystem<F>,
program: Program,
imported_programs: &ImportedPrograms,
imported_programs: &ImportParser,
) -> Result<(), CompilerError> {
let mut resolved_program = ConstrainedProgram::<F, G>::new();
let program_name = program.get_name();

View File

@ -1,4 +1,4 @@
//! Module containing methods to enforce constraints in an Leo program
pub mod generate_constraints;
pub use self::generate_constraints::*;
pub mod constraints;
pub use self::constraints::*;

View File

@ -3,7 +3,7 @@ use crate::{
program::{new_scope, ConstrainedProgram},
value::ConstrainedValue,
GroupType,
ImportedPrograms,
ImportParser,
};
use leo_types::Program;
@ -13,7 +13,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
pub(crate) fn store_definitions(
&mut self,
program: Program,
imported_programs: &ImportedPrograms,
imported_programs: &ImportParser,
) -> Result<(), ImportError> {
let program_name = program.name.clone();

View File

@ -1,8 +1,2 @@
pub mod import;
pub use self::import::*;
pub mod imported_symbols;
pub mod definitions;
pub mod symbol;
pub use self::definitions::*;

View File

@ -0,0 +1,9 @@
//! Imports are split up into two parts: parsing and storing
/// The import parser creates a hashmap of import program names -> import program structs
pub mod parser;
pub use self::parser::*;
/// The import store brings an imported symbol into the main program from an import program struct
pub mod store;
pub use self::store::*;

View File

@ -3,19 +3,21 @@ use leo_types::Program;
use std::{collections::HashMap, env::current_dir};
/// Parses all relevant import files for a program.
/// Stores compiled program structs.
#[derive(Clone)]
pub struct ImportedPrograms {
pub struct ImportParser {
imports: HashMap<String, Program>,
}
impl ImportedPrograms {
impl ImportParser {
pub fn new() -> Self {
Self {
imports: HashMap::new(),
}
}
pub(crate) fn store(&mut self, file_name: String, program: Program) {
pub(crate) fn insert(&mut self, file_name: String, program: Program) {
// todo: handle conflicting versions for duplicate imports here
let _res = self.imports.insert(file_name, program);
}
@ -24,7 +26,7 @@ impl ImportedPrograms {
self.imports.get(file_name)
}
pub fn from_program(program: &Program) -> Result<Self, ImportError> {
pub fn parse(program: &Program) -> Result<Self, ImportError> {
let mut imports = Self::new();
// Find all imports relative to current directory

View File

@ -0,0 +1,10 @@
//! The import parser creates a hashmap of import program names -> import program structs
pub mod parse_symbol;
pub use self::parse_symbol::*;
pub mod import_parser;
pub use self::import_parser::*;
pub mod parse_package;
pub use self::parse_package::*;

View File

@ -1,4 +1,4 @@
use crate::{errors::constraints::ImportError, ImportedPrograms};
use crate::{errors::constraints::ImportError, ImportParser};
use leo_types::{Package, PackageAccess};
use std::{fs, fs::DirEntry, path::PathBuf};
@ -7,7 +7,7 @@ static SOURCE_FILE_EXTENSION: &str = ".leo";
static SOURCE_DIRECTORY_NAME: &str = "src/";
static IMPORTS_DIRECTORY_NAME: &str = "imports/";
impl ImportedPrograms {
impl ImportParser {
pub fn parse_package_access(&mut self, entry: &DirEntry, access: &PackageAccess) -> Result<(), ImportError> {
// bring one or more import symbols into scope for the current constrained program
// we will recursively traverse sub packages here until we find the desired symbol

View File

@ -1,4 +1,4 @@
use crate::{errors::constraints::ImportError, ImportedPrograms};
use crate::{errors::constraints::ImportError, ImportParser};
use leo_ast::LeoParser;
use leo_types::{ImportSymbol, Program, Span};
@ -38,7 +38,7 @@ fn parse_import_file(entry: &DirEntry, span: &Span) -> Result<Program, ImportErr
Ok(Program::from(syntax_tree, file_name.clone()))
}
impl ImportedPrograms {
impl ImportParser {
pub fn parse_import_star(&mut self, entry: &DirEntry, span: &Span) -> Result<(), ImportError> {
let path = entry.path();
let is_dir = path.is_dir();
@ -72,7 +72,7 @@ impl ImportedPrograms {
.into_string()
.unwrap(); // the file exists so these will not fail
self.store(file_name, program);
self.insert(file_name, program);
Ok(())
} else {
@ -101,7 +101,7 @@ impl ImportedPrograms {
.into_string()
.unwrap(); // the file exists so these will not fail
self.store(file_name, program);
self.insert(file_name, program);
Ok(())
}

View File

@ -1,10 +1,4 @@
use crate::{
definitions::imported_symbols::ImportedSymbols,
errors::ImportError,
ConstrainedProgram,
GroupType,
ImportedPrograms,
};
use crate::{errors::ImportError, imported_symbols::ImportedSymbols, ConstrainedProgram, GroupType, ImportParser};
use leo_types::Import;
use snarkos_models::curves::{Field, PrimeField};
@ -14,7 +8,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
&mut self,
scope: String,
import: &Import,
imported_programs: &ImportedPrograms,
imported_programs: &ImportParser,
) -> Result<(), ImportError> {
// get imported program name from import
// get imported symbols from from import

View File

@ -1,5 +1,6 @@
use leo_types::{Import, ImportSymbol, Package, PackageAccess};
/// Stores the the package file name and imported symbol from an import statement
#[derive(Debug)]
pub(crate) struct ImportedSymbols {
pub symbols: Vec<(String, ImportSymbol)>,

View File

@ -0,0 +1,9 @@
//! The import store brings an imported symbol into the main program from an imported compiled program
pub mod import;
pub use self::import::*;
pub mod imported_symbols;
pub mod symbol;
pub use self::symbol::*;

View File

@ -1,8 +0,0 @@
pub mod imported_programs;
pub use self::imported_programs::*;
pub mod import_symbol;
pub use self::import_symbol::*;
pub mod package;
pub use self::package::*;

View File

@ -18,8 +18,8 @@ pub use self::expression::*;
pub mod function;
pub use self::function::*;
pub mod imports;
pub use self::imports::*;
pub mod import;
pub use self::import::*;
pub mod program;
pub use self::program::*;

View File

@ -1,3 +1,2 @@
pub mod program;
pub use self::program::*;

View File

@ -1,4 +1,4 @@
import test_import.foo as bar;
import test_import.foo as bar;
function main() -> u32 {
return bar()