Skip to content

Commit

Permalink
Added ability to import namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacShelton committed Sep 21, 2024
1 parent 7be20b7 commit 6070e31
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 11 deletions.
2 changes: 2 additions & 0 deletions src/ast/workspace/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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<Box<str>>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
17 changes: 12 additions & 5 deletions src/interpreter/syscall_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct BuildSystemSyscallHandler {
pub link_filenames: HashSet<String>,
pub link_frameworks: HashSet<String>,
pub debug_skip_merging_helper_exprs: bool,
pub imported_namespaces: Vec<Box<str>>,
}

fn read_cstring(memory: &Memory, value: &Value) -> String {
Expand Down Expand Up @@ -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]));
Expand Down Expand Up @@ -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)
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/interpreter_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub enum InterpreterSyscallKind {
BuildLinkFilename,
BuildLinkFrameworkName,
Experimental,
ImportNamespace,
}

#[derive(Clone, Debug)]
Expand Down
1 change: 1 addition & 0 deletions src/pragma_section/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
}
}
5 changes: 2 additions & 3 deletions src/resolve/function_search_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ pub enum FindFunctionError {
}

impl FunctionSearchCtx {
pub fn new() -> Self {
pub fn new(imported_namespaces: Vec<Box<str>>) -> Self {
Self {
available: Default::default(),
// TODO: Make this value user-specified
imported_namespaces: vec!["io".to_string().into_boxed_str()],
imported_namespaces,
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6070e31

Please sign in to comment.