use absolute paths
This commit is contained in:
parent
b990a12982
commit
0f7594d8ff
8 changed files with 32 additions and 13 deletions
2
PROJECT/dep/dep/foo.zok
Normal file
2
PROJECT/dep/dep/foo.zok
Normal file
|
@ -0,0 +1,2 @@
|
|||
def foo() -> (field):
|
||||
return true
|
4
PROJECT/dep/foo.zok
Normal file
4
PROJECT/dep/foo.zok
Normal file
|
@ -0,0 +1,4 @@
|
|||
from "./dep/foo.zok" import foo as bar
|
||||
|
||||
def foo() -> (field):
|
||||
return 42
|
4
PROJECT/main.zok
Normal file
4
PROJECT/main.zok
Normal file
|
@ -0,0 +1,4 @@
|
|||
from "./dep/foo.zok" import foo
|
||||
|
||||
def main() -> (field):
|
||||
return foo()
|
|
@ -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()
|
||||
|
|
|
@ -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<T: Field, E: Into<imports::Error>>(
|
||||
source: String,
|
||||
location: String,
|
||||
location: FilePath,
|
||||
resolve_option: Option<Resolve<E>>,
|
||||
) -> Result<CompilationArtifacts<T>, CompileErrors> {
|
||||
let arena = Arena::new();
|
||||
|
@ -183,7 +185,7 @@ pub fn compile<T: Field, E: Into<imports::Error>>(
|
|||
|
||||
pub fn compile_program<'ast, T: Field, E: Into<imports::Error>>(
|
||||
source: &'ast str,
|
||||
location: String,
|
||||
location: FilePath,
|
||||
resolve_option: Option<Resolve<E>>,
|
||||
arena: &'ast Arena<String>,
|
||||
) -> Result<Program<'ast, T>, CompileErrors> {
|
||||
|
@ -207,7 +209,7 @@ pub fn compile_program<'ast, T: Field, E: Into<imports::Error>>(
|
|||
|
||||
pub fn compile_module<'ast, T: Field, E: Into<imports::Error>>(
|
||||
source: &'ast str,
|
||||
location: String,
|
||||
location: FilePath,
|
||||
resolve_option: Option<Resolve<E>>,
|
||||
modules: &mut HashMap<ModuleId, Module<'ast, T>>,
|
||||
arena: &'ast Arena<String>,
|
||||
|
|
|
@ -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),
|
||||
),
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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<String, io::Error> {
|
||||
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)]
|
||||
|
|
Loading…
Reference in a new issue