Skip to content

Commit 6070e31

Browse files
committed
Added ability to import namespaces
1 parent 7be20b7 commit 6070e31

File tree

7 files changed

+39
-11
lines changed

7 files changed

+39
-11
lines changed

Diff for: src/ast/workspace/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ impl<'a> AstWorkspace<'a> {
5454
settings.push(Settings {
5555
adept_version: AdeptVersion::CURRENT,
5656
debug_skip_merging_helper_exprs: false,
57+
imported_namespaces: vec![],
5758
}),
5859
Self::DEFAULT_SETTINGS_ID.0
5960
);
@@ -114,6 +115,7 @@ impl<'a> AstWorkspace<'a> {
114115
pub struct Settings {
115116
pub adept_version: AdeptVersion,
116117
pub debug_skip_merging_helper_exprs: bool,
118+
pub imported_namespaces: Vec<Box<str>>,
117119
}
118120

119121
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]

Diff for: src/interpreter/syscall_handler.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub struct BuildSystemSyscallHandler {
3535
pub link_filenames: HashSet<String>,
3636
pub link_frameworks: HashSet<String>,
3737
pub debug_skip_merging_helper_exprs: bool,
38+
pub imported_namespaces: Vec<Box<str>>,
3839
}
3940

4041
fn read_cstring(memory: &Memory, value: &Value) -> String {
@@ -66,6 +67,13 @@ impl SyscallHandler for BuildSystemSyscallHandler {
6667
println!("{}", read_cstring(memory, &args[0]));
6768
Value::Literal(ir::Literal::Void)
6869
}
70+
ir::InterpreterSyscallKind::BuildAddProject => {
71+
assert_eq!(args.len(), 2);
72+
let name = read_cstring(memory, &args[0]);
73+
let kind = ProjectKind::from_u64(args[1].as_u64().unwrap()).unwrap();
74+
self.projects.push(Project { name, kind });
75+
Value::Literal(ir::Literal::Void)
76+
}
6977
ir::InterpreterSyscallKind::BuildLinkFilename => {
7078
assert_eq!(args.len(), 1);
7179
self.link_filenames.insert(read_cstring(memory, &args[0]));
@@ -110,11 +118,10 @@ impl SyscallHandler for BuildSystemSyscallHandler {
110118

111119
Value::Literal(ir::Literal::Void)
112120
}
113-
ir::InterpreterSyscallKind::BuildAddProject => {
114-
assert_eq!(args.len(), 2);
115-
let name = read_cstring(memory, &args[0]);
116-
let kind = ProjectKind::from_u64(args[1].as_u64().unwrap()).unwrap();
117-
self.projects.push(Project { name, kind });
121+
ir::InterpreterSyscallKind::ImportNamespace => {
122+
assert_eq!(args.len(), 1);
123+
self.imported_namespaces
124+
.push(read_cstring(memory, &args[0]).into_boxed_str());
118125
Value::Literal(ir::Literal::Void)
119126
}
120127
}

Diff for: src/interpreter_env/mod.rs

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@ pub fn setup_build_system_interpreter_symbols(file: &mut AstFile) {
145145
InterpreterSyscallKind::Experimental,
146146
));
147147

148+
file.functions.push(thin_cstring_function(
149+
"importNamespace",
150+
"namespace",
151+
InterpreterSyscallKind::ImportNamespace,
152+
));
153+
148154
file.functions.push(Function {
149155
name: "project".into(),
150156
parameters: Parameters {

Diff for: src/ir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub enum InterpreterSyscallKind {
145145
BuildLinkFilename,
146146
BuildLinkFrameworkName,
147147
Experimental,
148+
ImportNamespace,
148149
}
149150

150151
#[derive(Clone, Debug)]

Diff for: src/pragma_section/run.rs

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ impl PragmaSection {
110110
Ok(Settings {
111111
adept_version,
112112
debug_skip_merging_helper_exprs: user_settings.debug_skip_merging_helper_exprs,
113+
imported_namespaces: user_settings.imported_namespaces,
113114
})
114115
}
115116
}

Diff for: src/resolve/function_search_ctx.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,10 @@ pub enum FindFunctionError {
1717
}
1818

1919
impl FunctionSearchCtx {
20-
pub fn new() -> Self {
20+
pub fn new(imported_namespaces: Vec<Box<str>>) -> Self {
2121
Self {
2222
available: Default::default(),
23-
// TODO: Make this value user-specified
24-
imported_namespaces: vec!["io".to_string().into_boxed_str()],
23+
imported_namespaces,
2524
}
2625
}
2726

Diff for: src/resolve/mod.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,21 @@ pub fn resolve<'a>(
281281
ctx.jobs
282282
.push_back(Job::Regular(*real_file_id, function_i, function_ref));
283283

284-
let function_search_context = ctx
285-
.function_search_ctxs
286-
.get_or_insert_with(file_id, || FunctionSearchCtx::new());
284+
let imported_namespaces =
285+
if let Some(settings) = file.settings.map(|id| &ast_workspace.settings[id.0]) {
286+
Some(&settings.imported_namespaces)
287+
} else {
288+
None
289+
};
290+
291+
let function_search_context =
292+
ctx.function_search_ctxs.get_or_insert_with(file_id, || {
293+
FunctionSearchCtx::new(
294+
imported_namespaces
295+
.map(|namespaces| namespaces.clone())
296+
.unwrap_or_else(|| vec![]),
297+
)
298+
});
287299

288300
function_search_context
289301
.available

0 commit comments

Comments
 (0)