1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::collections::HashMap;
use std::path;
use arret_syntax::datum::DataStr;
use arret_syntax::span::Span;
use crate::hir::error::{Error, ErrorKind, Result};
use crate::rfi;
use crate::source::SourceFile;
use crate::CompileCtx;
pub struct PackagePath {
arret_base: Box<path::Path>,
native_rust_base: Box<path::Path>,
target_rust_base: Box<path::Path>,
}
pub struct PackagePaths {
paths: HashMap<Box<str>, PackagePath>,
}
impl PackagePaths {
pub fn empty() -> PackagePaths {
PackagePaths {
paths: HashMap::new(),
}
}
pub fn with_stdlib(arret_root_dir: &path::Path, target_triple: Option<&str>) -> PackagePaths {
let mut pp = PackagePaths::empty();
let native_rust_base = arret_root_dir.join("target");
let target_rust_base = if let Some(target_triple) = target_triple {
native_rust_base.join(target_triple)
} else {
native_rust_base.clone()
};
let stdlib_path = PackagePath {
arret_base: arret_root_dir.join("stdlib/arret").into(),
native_rust_base: native_rust_base.into(),
target_rust_base: target_rust_base.into(),
};
pp.add_package("stdlib", stdlib_path);
pp
}
pub fn test_paths(target_triple: Option<&str>) -> PackagePaths {
let parent_path = path::Path::new("..");
Self::with_stdlib(parent_path, target_triple)
}
pub fn add_package(&mut self, package_name: &str, path: PackagePath) {
self.paths.insert(package_name.into(), path);
}
}
#[derive(PartialEq, Eq, Hash, Clone)]
pub struct ModuleName {
package_name: DataStr,
path: Vec<DataStr>,
terminal_name: DataStr,
}
#[derive(Debug)]
pub enum LoadedModule {
Source(SourceFile),
Rust(rfi::Library),
}
impl ModuleName {
pub fn new(package_name: DataStr, path: Vec<DataStr>, terminal_name: DataStr) -> ModuleName {
ModuleName {
package_name,
path,
terminal_name,
}
}
pub fn is_rfi(&self) -> bool {
self.path.is_empty() && self.terminal_name.as_ref() == "rust"
}
pub fn terminal_name(&self) -> &DataStr {
&self.terminal_name
}
}
pub fn load_module_by_name(
ccx: &CompileCtx,
span: Span,
module_name: &ModuleName,
) -> Result<LoadedModule> {
let package_path = if let Some(package_path) = ccx
.package_paths()
.paths
.get(module_name.package_name.as_ref())
{
package_path
} else {
return Err(Error::new(span, ErrorKind::PackageNotFound));
};
if module_name.is_rfi() {
ccx.rfi_loader()
.load(
span,
ccx.source_loader(),
&package_path.native_rust_base,
&package_path.target_rust_base,
&module_name.package_name,
)
.map(LoadedModule::Rust)
} else {
let mut path_buf = path::PathBuf::new();
path_buf.push(&package_path.arret_base);
for path_component in &module_name.path {
path_buf.push(path_component.as_ref());
}
path_buf.push(format!("{}.arret", module_name.terminal_name));
let path = path_buf.as_path();
let source_file = ccx
.source_loader()
.load_path(path)
.map_err(|err| Error::from_module_io(span, path, &err))?;
Ok(LoadedModule::Source(source_file))
}
}
#[cfg(test)]
mod test {
use super::*;
use crate::source::EMPTY_SPAN;
fn load_stdlib_module(name: &'static str) -> Result<LoadedModule> {
let ccx = CompileCtx::new(PackagePaths::test_paths(None), true);
let module_name = ModuleName::new("stdlib".into(), vec![], name.into());
load_module_by_name(&ccx, EMPTY_SPAN, &module_name)
}
#[test]
fn load_stdlib_base() {
let loaded_module = load_stdlib_module("base").unwrap();
if let LoadedModule::Source(data) = loaded_module {
assert!(!data.parsed().unwrap().is_empty());
} else {
panic!("Did not get source module; got {:?}", loaded_module);
}
}
#[test]
fn load_stdlib_rust() {
let loaded_module = load_stdlib_module("rust").expect(
"unable to load stdlib library; you may need to `cargo build` before running tests",
);
if let LoadedModule::Rust(rfi_module) = loaded_module {
assert!(!rfi_module.exported_funs.is_empty());
} else {
panic!("Did not get Rust module; got {:?}", loaded_module);
}
}
#[test]
fn load_stdlib_missing() {
let err = load_stdlib_module("notamodule").unwrap_err();
if let ErrorKind::ModuleNotFound(_) = err.kind() {
} else {
panic!("Unexpected error kind: {:?}", err.kind())
}
}
}