diff --git a/PROJECT/dep/dep/foo.zok b/PROJECT/dep/dep/foo.zok new file mode 100644 index 00000000..f4a6f92e --- /dev/null +++ b/PROJECT/dep/dep/foo.zok @@ -0,0 +1,2 @@ +def foo() -> (field): + return true \ No newline at end of file diff --git a/PROJECT/dep/foo.zok b/PROJECT/dep/foo.zok new file mode 100644 index 00000000..0e8910f0 --- /dev/null +++ b/PROJECT/dep/foo.zok @@ -0,0 +1,4 @@ +from "./dep/foo.zok" import foo as bar + +def foo() -> (field): + return 42 \ No newline at end of file diff --git a/PROJECT/main.zok b/PROJECT/main.zok new file mode 100644 index 00000000..2c412c84 --- /dev/null +++ b/PROJECT/main.zok @@ -0,0 +1,4 @@ +from "./dep/foo.zok" import foo + +def main() -> (field): + return foo() \ No newline at end of file diff --git a/zokrates_cli/src/bin.rs b/zokrates_cli/src/bin.rs index 06150c04..e2bc7fbd 100644 --- a/zokrates_cli/src/bin.rs +++ b/zokrates_cli/src/bin.rs @@ -272,8 +272,6 @@ fn cli() -> Result<(), String> { let path = PathBuf::from(sub_matches.value_of("input").unwrap()); let location = path - .parent() - .unwrap() .to_path_buf() .into_os_string() .into_string() diff --git a/zokrates_core/src/compile.rs b/zokrates_core/src/compile.rs index f01f3e80..cf98ca13 100644 --- a/zokrates_core/src/compile.rs +++ b/zokrates_core/src/compile.rs @@ -138,9 +138,11 @@ impl fmt::Display for CompileErrorInner { pub type Resolve<'a, E> = &'a dyn Fn(String, String) -> Result<(String, String), E>; +type FilePath = String; + pub fn compile>( source: String, - location: String, + location: FilePath, resolve_option: Option>, ) -> Result, CompileErrors> { let arena = Arena::new(); @@ -183,7 +185,7 @@ pub fn compile>( pub fn compile_program<'ast, T: Field, E: Into>( source: &'ast str, - location: String, + location: FilePath, resolve_option: Option>, arena: &'ast Arena, ) -> Result, CompileErrors> { @@ -207,7 +209,7 @@ pub fn compile_program<'ast, T: Field, E: Into>( pub fn compile_module<'ast, T: Field, E: Into>( source: &'ast str, - location: String, + location: FilePath, resolve_option: Option>, modules: &mut HashMap>, arena: &'ast Arena, diff --git a/zokrates_core/src/imports.rs b/zokrates_core/src/imports.rs index b5b3548c..8764702b 100644 --- a/zokrates_core/src/imports.rs +++ b/zokrates_core/src/imports.rs @@ -134,6 +134,9 @@ impl Importer { let mut symbols: Vec<_> = vec![]; for import in destination.imports { + + println!("Apply import {} to {}", import.value.source, location); + let pos = import.pos(); let import = import.value; let alias = import.alias; @@ -174,7 +177,7 @@ impl Importer { // to resolve imports, we need a resolver match resolve_option { Some(resolve) => match resolve(location.clone(), import.source.to_string()) { - Ok((source, location)) => { + Ok((source, new_location)) => { let source = arena.alloc(source); // generate an alias from the imported path if none was given explicitely @@ -192,11 +195,15 @@ impl Importer { .unwrap(), ); + println!("compile module {}", new_location); + let compiled = - compile_module(source, location, resolve_option, modules, &arena) + compile_module(source, new_location.clone(), resolve_option, modules, &arena) .map_err(|e| e.with_context(import.source.to_string()))?; - modules.insert(import.source.to_string(), compiled); + println!("Add module {} {}", new_location, import.source); + + assert!(modules.insert(new_location.clone(), compiled).is_none()); symbols.push( SymbolDeclaration { @@ -204,7 +211,7 @@ impl Importer { symbol: Symbol::There( SymbolImport::with_id_in_module( import.symbol.unwrap_or("main"), - import.source.clone(), + new_location, ) .start_end(pos.0, pos.1), ), diff --git a/zokrates_core/src/semantics.rs b/zokrates_core/src/semantics.rs index 325efe23..ab5f33e1 100644 --- a/zokrates_core/src/semantics.rs +++ b/zokrates_core/src/semantics.rs @@ -89,6 +89,7 @@ impl SymbolUnifier { impl<'ast, T: Field> State<'ast, T> { fn new(modules: Modules<'ast, T>) -> Self { + println!("{:#?}", modules); State { modules, typed_modules: HashMap::new(), diff --git a/zokrates_fs_resolver/src/lib.rs b/zokrates_fs_resolver/src/lib.rs index bf1f32a5..29f392a6 100644 --- a/zokrates_fs_resolver/src/lib.rs +++ b/zokrates_fs_resolver/src/lib.rs @@ -14,12 +14,15 @@ pub fn resolve<'a>( current_location: CurrentLocation, import_location: ImportLocation<'a>, ) -> Result<(SourceCode, CurrentLocation), io::Error> { + + println!("get file {} {}", current_location, import_location); + let source = Path::new(&import_location); // paths starting with `./` or `../` are interpreted relative to the current file // other paths `abc/def` are interpreted relative to $ZOKRATES_HOME let base = match source.components().next() { - Some(Component::CurDir) | Some(Component::ParentDir) => PathBuf::from(current_location), + Some(Component::CurDir) | Some(Component::ParentDir) => PathBuf::from(current_location).parent().unwrap().into(), _ => PathBuf::from( std::env::var(ZOKRATES_HOME).expect("$ZOKRATES_HOME is not set, please set it"), ), @@ -40,9 +43,7 @@ pub fn resolve<'a>( } fn generate_next_location<'a>(path: &'a PathBuf) -> Result { - path.parent() - .ok_or(io::Error::new(io::ErrorKind::Other, "Invalid path")) - .map(|v| v.to_path_buf().into_os_string().into_string().unwrap()) + Ok(path.to_path_buf().into_os_string().into_string().unwrap()) } #[cfg(test)]