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
186
187
188
189
190
191
192
193
194
195
196
use super::Evaluator;
use crate::ast::source::*;
use crate::evaluate::Value;
use crate::error::{NbclError, Result};
use crate::ast::resolved::ResolvedNode;
use crate::parser::NbclParser;
use crate::parser::Rule;
use pest::Parser;
use std::fs;
use std::io::ErrorKind;
impl Evaluator {
pub(crate) fn handle_import(
&mut self,
imp: ImportDef,
root_nodes: &mut Vec<ResolvedNode>
) -> Result<()> {
match imp.def {
ImportDefType::Module(path_str, alias) => {
let target_path = match &self.mod_resolver {
Some(r) => r.find_target(&path_str),
None => {
return Err(NbclError::Runtime {
message: "module resolver is not registered".into(),
hint: Some("Looks like the developer messed with the module resolver... You'd have to stick with a singular crate for now.".to_string()),
span: Some(imp.span),
})
}
}?;
// Avoiding circular imports
if self.loaded_files.contains(&target_path) {
return Ok(());
}
// Read and Parse
let source = fs::read_to_string(&target_path).map_err(|e| {
let (msg, hint) = match e.kind() {
ErrorKind::NotFound => {
let msg = format!("module not found: '{}'", target_path.display());
let hint = "Ensure that the module exists and try adjusting the path."
.to_string();
(msg, Some(hint))
}
ErrorKind::PermissionDenied => {
let msg = format!(
"permission denied reading module: '{}'",
target_path.display()
);
let hint = "Set proper file permissions".to_string();
(msg, Some(hint))
}
_ => {
let msg =
format!("failed to read module '{}': {}", target_path.display(), e);
(msg, None)
}
};
NbclError::IO { message: msg, hint, path: target_path.clone() }
})?;
let mut tokens = NbclParser::parse(Rule::file, &source)?;
let file_pair = tokens.next().ok_or_else(|| NbclError::Ast {
message: "empty file".into(),
hint: None,
span: None,
})?;
let ast = crate::builder::build_file(file_pair)?;
let mut global_var = Vec::new();
for item in ast.items.clone() {
match item {
TopLevelItem::Import(imp) => self.handle_import(imp.clone(), root_nodes)?,
TopLevelItem::FnDef(mut f) => {
f.name = format!("{}.{}", alias, f.name);
self.registry.register_function(f);
}
TopLevelItem::ComponentDef(mut c) => {
c.name = format!("{}.{}", alias, c.name);
self.registry.register_component(c);
}
TopLevelItem::Stmt(Stmt::Global(name, _, expr)) => {
// We evaluate globals now so they are available in Loop 2
let val = self.eval_expr(&expr)?;
global_var.push((name.clone(), val));
}
_ => {}
}
}
if !global_var.is_empty() {
self.registry.globals.insert(alias, Value::Map(global_var));
}
for item in ast.items {
match item {
TopLevelItem::Node(invocation) => {
let nodes = self.resolve_node(invocation)?;
root_nodes.extend(nodes);
}
TopLevelItem::Stmt(stmt) => {
self.execute_stmt(stmt)?;
}
_ => {} // Rest are already handled
}
}
self.loaded_files.insert(target_path);
Ok(())
}
ImportDefType::Library(lib_name, lib_item) => {
let maybe_library =
self.registry.libraries.iter().find(|&lib| lib.name == lib_name);
let library = match maybe_library {
Some(lib) => lib,
None => {
let library_names = self
.registry
.libraries
.iter()
.map(|lib| lib.name.clone())
.collect::<Vec<String>>();
let suggestion =
crate::utils::find_best_match(&lib_name, library_names.iter());
let hint = suggestion.map(|s| {
format!(
"Library \"{}\" doesn't exist. Did you mean \"{}\"?",
&lib_name, s
)
});
return Err(NbclError::Runtime {
message: "library not found".into(),
hint,
span: Some(imp.span),
});
}
};
let maybe_lib_item = library.items.iter().find(|&i| i.name == lib_item);
let item = match maybe_lib_item {
Some(i) => i,
None => {
let item_names = library
.items
.iter()
.map(|item| item.name.clone())
.collect::<Vec<String>>();
let suggestion =
crate::utils::find_best_match(&lib_item, item_names.iter());
let hint = suggestion.map(|s| {
format!(
"Library item \"{}\" doesn't exist. Did you mean \"{}\"?",
&lib_item, s
)
});
return Err(NbclError::Runtime {
message: format!("library item '{}' not found", &lib_item),
hint,
span: Some(imp.span),
});
}
};
for (fn_name, schema) in item.native_functions.clone() {
let new_name = format!("{}.{}", &lib_item, fn_name);
self.registry.native_functions.insert(new_name, schema);
}
let mut global_var = Vec::new();
for (name, var) in item.globals.clone() {
global_var.push((name, var))
}
if !global_var.is_empty() {
self.registry.globals.insert(lib_item, Value::Map(global_var));
}
Ok(())
}
}
}
}