diff --git a/src/ast/workspace/mod.rs b/src/ast/workspace/mod.rs index 09083b3..43ef669 100644 --- a/src/ast/workspace/mod.rs +++ b/src/ast/workspace/mod.rs @@ -54,6 +54,7 @@ impl<'a> AstWorkspace<'a> { settings.push(Settings { adept_version: AdeptVersion::CURRENT, debug_skip_merging_helper_exprs: false, + imported_namespaces: vec![], }), Self::DEFAULT_SETTINGS_ID.0 ); @@ -114,6 +115,7 @@ impl<'a> AstWorkspace<'a> { pub struct Settings { pub adept_version: AdeptVersion, pub debug_skip_merging_helper_exprs: bool, + pub imported_namespaces: Vec>, } #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] diff --git a/src/interpreter/syscall_handler.rs b/src/interpreter/syscall_handler.rs index c452f95..c6f64a5 100644 --- a/src/interpreter/syscall_handler.rs +++ b/src/interpreter/syscall_handler.rs @@ -35,6 +35,7 @@ pub struct BuildSystemSyscallHandler { pub link_filenames: HashSet, pub link_frameworks: HashSet, pub debug_skip_merging_helper_exprs: bool, + pub imported_namespaces: Vec>, } fn read_cstring(memory: &Memory, value: &Value) -> String { @@ -66,6 +67,13 @@ impl SyscallHandler for BuildSystemSyscallHandler { println!("{}", read_cstring(memory, &args[0])); Value::Literal(ir::Literal::Void) } + ir::InterpreterSyscallKind::BuildAddProject => { + assert_eq!(args.len(), 2); + let name = read_cstring(memory, &args[0]); + let kind = ProjectKind::from_u64(args[1].as_u64().unwrap()).unwrap(); + self.projects.push(Project { name, kind }); + Value::Literal(ir::Literal::Void) + } ir::InterpreterSyscallKind::BuildLinkFilename => { assert_eq!(args.len(), 1); self.link_filenames.insert(read_cstring(memory, &args[0])); @@ -110,11 +118,10 @@ impl SyscallHandler for BuildSystemSyscallHandler { Value::Literal(ir::Literal::Void) } - ir::InterpreterSyscallKind::BuildAddProject => { - assert_eq!(args.len(), 2); - let name = read_cstring(memory, &args[0]); - let kind = ProjectKind::from_u64(args[1].as_u64().unwrap()).unwrap(); - self.projects.push(Project { name, kind }); + ir::InterpreterSyscallKind::ImportNamespace => { + assert_eq!(args.len(), 1); + self.imported_namespaces + .push(read_cstring(memory, &args[0]).into_boxed_str()); Value::Literal(ir::Literal::Void) } } diff --git a/src/interpreter_env/mod.rs b/src/interpreter_env/mod.rs index ba02aef..4fce2f5 100644 --- a/src/interpreter_env/mod.rs +++ b/src/interpreter_env/mod.rs @@ -145,6 +145,12 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) { InterpreterSyscallKind::Experimental, )); + file.functions.push(thin_cstring_function( + "importNamespace", + "namespace", + InterpreterSyscallKind::ImportNamespace, + )); + file.functions.push(Function { name: "project".into(), parameters: Parameters { diff --git a/src/ir/mod.rs b/src/ir/mod.rs index 9ecd2e7..0dd7f26 100644 --- a/src/ir/mod.rs +++ b/src/ir/mod.rs @@ -145,6 +145,7 @@ pub enum InterpreterSyscallKind { BuildLinkFilename, BuildLinkFrameworkName, Experimental, + ImportNamespace, } #[derive(Clone, Debug)] diff --git a/src/pragma_section/run.rs b/src/pragma_section/run.rs index 004fab0..8b82e2f 100644 --- a/src/pragma_section/run.rs +++ b/src/pragma_section/run.rs @@ -110,6 +110,7 @@ impl PragmaSection { Ok(Settings { adept_version, debug_skip_merging_helper_exprs: user_settings.debug_skip_merging_helper_exprs, + imported_namespaces: user_settings.imported_namespaces, }) } } diff --git a/src/resolve/function_search_ctx.rs b/src/resolve/function_search_ctx.rs index 1b3259a..8f2e13f 100644 --- a/src/resolve/function_search_ctx.rs +++ b/src/resolve/function_search_ctx.rs @@ -17,11 +17,10 @@ pub enum FindFunctionError { } impl FunctionSearchCtx { - pub fn new() -> Self { + pub fn new(imported_namespaces: Vec>) -> Self { Self { available: Default::default(), - // TODO: Make this value user-specified - imported_namespaces: vec!["io".to_string().into_boxed_str()], + imported_namespaces, } } diff --git a/src/resolve/mod.rs b/src/resolve/mod.rs index 0d42afd..fc6bb55 100644 --- a/src/resolve/mod.rs +++ b/src/resolve/mod.rs @@ -281,9 +281,21 @@ pub fn resolve<'a>( ctx.jobs .push_back(Job::Regular(*real_file_id, function_i, function_ref)); - let function_search_context = ctx - .function_search_ctxs - .get_or_insert_with(file_id, || FunctionSearchCtx::new()); + let imported_namespaces = + if let Some(settings) = file.settings.map(|id| &ast_workspace.settings[id.0]) { + Some(&settings.imported_namespaces) + } else { + None + }; + + let function_search_context = + ctx.function_search_ctxs.get_or_insert_with(file_id, || { + FunctionSearchCtx::new( + imported_namespaces + .map(|namespaces| namespaces.clone()) + .unwrap_or_else(|| vec![]), + ) + }); function_search_context .available