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

View File

@ -5,7 +5,7 @@ use crate::{
errors::CompilerError, errors::CompilerError,
value::ConstrainedValue, value::ConstrainedValue,
GroupType, GroupType,
ImportedPrograms, ImportParser,
}; };
use leo_ast::LeoParser; use leo_ast::LeoParser;
use leo_inputs::LeoInputsParser; use leo_inputs::LeoInputsParser;
@ -26,7 +26,7 @@ pub struct Compiler<F: Field + PrimeField, G: GroupType<F>> {
main_file_path: PathBuf, main_file_path: PathBuf,
program: Program, program: Program,
program_inputs: Inputs, program_inputs: Inputs,
imported_programs: ImportedPrograms, imported_programs: ImportParser,
output: Option<ConstrainedValue<F, G>>, output: Option<ConstrainedValue<F, G>>,
_engine: PhantomData<F>, _engine: PhantomData<F>,
} }
@ -38,7 +38,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> Compiler<F, G> {
main_file_path: PathBuf::new(), main_file_path: PathBuf::new(),
program: Program::new(package_name), program: Program::new(package_name),
program_inputs: Inputs::new(), program_inputs: Inputs::new(),
imported_programs: ImportedPrograms::new(), imported_programs: ImportParser::new(),
output: None, output: None,
_engine: PhantomData, _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 = Program::from(syntax_tree, package_name);
self.program_inputs.set_inputs_size(self.program.expected_inputs.len()); 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); 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(), main_file_path: PathBuf::new(),
program, program,
program_inputs, program_inputs,
imported_programs: ImportedPrograms::new(), imported_programs: ImportParser::new(),
output: None, output: None,
_engine: PhantomData, _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 leo_types::{InputValue, Program};
use snarkos_models::{ use snarkos_models::{
@ -10,7 +10,7 @@ pub fn generate_constraints<F: Field + PrimeField, G: GroupType<F>, CS: Constrai
cs: &mut CS, cs: &mut CS,
program: Program, program: Program,
parameters: Vec<Option<InputValue>>, parameters: Vec<Option<InputValue>>,
imported_programs: &ImportedPrograms, imported_programs: &ImportParser,
) -> Result<ConstrainedValue<F, G>, CompilerError> { ) -> Result<ConstrainedValue<F, G>, CompilerError> {
let mut resolved_program = ConstrainedProgram::new(); let mut resolved_program = ConstrainedProgram::new();
let program_name = program.get_name(); 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>>( pub fn generate_test_constraints<F: Field + PrimeField, G: GroupType<F>>(
cs: &mut TestConstraintSystem<F>, cs: &mut TestConstraintSystem<F>,
program: Program, program: Program,
imported_programs: &ImportedPrograms, imported_programs: &ImportParser,
) -> Result<(), CompilerError> { ) -> Result<(), CompilerError> {
let mut resolved_program = ConstrainedProgram::<F, G>::new(); let mut resolved_program = ConstrainedProgram::<F, G>::new();
let program_name = program.get_name(); let program_name = program.get_name();

View File

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

View File

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

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}; use std::{collections::HashMap, env::current_dir};
/// Parses all relevant import files for a program.
/// Stores compiled program structs.
#[derive(Clone)] #[derive(Clone)]
pub struct ImportedPrograms { pub struct ImportParser {
imports: HashMap<String, Program>, imports: HashMap<String, Program>,
} }
impl ImportedPrograms { impl ImportParser {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
imports: HashMap::new(), 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 // todo: handle conflicting versions for duplicate imports here
let _res = self.imports.insert(file_name, program); let _res = self.imports.insert(file_name, program);
} }
@ -24,7 +26,7 @@ impl ImportedPrograms {
self.imports.get(file_name) 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(); let mut imports = Self::new();
// Find all imports relative to current directory // 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 leo_types::{Package, PackageAccess};
use std::{fs, fs::DirEntry, path::PathBuf}; use std::{fs, fs::DirEntry, path::PathBuf};
@ -7,7 +7,7 @@ static SOURCE_FILE_EXTENSION: &str = ".leo";
static SOURCE_DIRECTORY_NAME: &str = "src/"; static SOURCE_DIRECTORY_NAME: &str = "src/";
static IMPORTS_DIRECTORY_NAME: &str = "imports/"; static IMPORTS_DIRECTORY_NAME: &str = "imports/";
impl ImportedPrograms { impl ImportParser {
pub fn parse_package_access(&mut self, entry: &DirEntry, access: &PackageAccess) -> Result<(), ImportError> { 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 // 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 // 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_ast::LeoParser;
use leo_types::{ImportSymbol, Program, Span}; 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())) 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> { pub fn parse_import_star(&mut self, entry: &DirEntry, span: &Span) -> Result<(), ImportError> {
let path = entry.path(); let path = entry.path();
let is_dir = path.is_dir(); let is_dir = path.is_dir();
@ -72,7 +72,7 @@ impl ImportedPrograms {
.into_string() .into_string()
.unwrap(); // the file exists so these will not fail .unwrap(); // the file exists so these will not fail
self.store(file_name, program); self.insert(file_name, program);
Ok(()) Ok(())
} else { } else {
@ -101,7 +101,7 @@ impl ImportedPrograms {
.into_string() .into_string()
.unwrap(); // the file exists so these will not fail .unwrap(); // the file exists so these will not fail
self.store(file_name, program); self.insert(file_name, program);
Ok(()) Ok(())
} }

View File

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

View File

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

View File

@ -1,3 +1,2 @@
pub mod program; pub mod program;
pub use self::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 { function main() -> u32 {
return bar() return bar()