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
use crate::{
Environment,
builtin::{DEFAULT_KSL_PATH, FILE_EXT, KSL_PATH_ENV, MODULE_NAME_ENV, MODULE_PATH_ENV},
eval::{apply::eval_apply, eval},
expand_tilde,
init_environment,
value::Value,
};
pub(crate) fn builtin(args: &[Value], env: &Environment) -> Option<(Value, Environment)> {
let mut local_environment = Environment::new();
if let [Value::Identity(name), path_string] = &args[..] {
if env.contains_key(name) {
eprintln!(
concat!(
"Error[ksl::builtin::load]: ",
"Symbol `{}` cannot be rebound."
),
name
);
None
} else {
match eval_apply(path_string, env) {
Some((Value::String(path), _)) => {
let path_with_ext = format!("{}.{}", path, FILE_EXT);
let real_path = if let Ok(true) = std::fs::exists(&path_with_ext) {
path_with_ext
} else {
let ksl_path = match std::env::var(KSL_PATH_ENV) {
Ok(p) => p,
Err(_) => String::from(DEFAULT_KSL_PATH),
};
if let Some(ksl_path) = expand_tilde(ksl_path) {
format!("{}/{}", ksl_path, path_with_ext)
} else {
return None;
}
};
if let Some(Value::List(module_paths)) = env.get(MODULE_PATH_ENV) {
if module_paths.contains(&Value::String(real_path.clone())) {
eprintln!(concat!(
"Error[ksl::builtin::load]: ",
"Module has already been loaded."
));
None
} else {
let mut local_env = init_environment();
let mut currnet_paths = module_paths.clone();
currnet_paths.push(Value::String(real_path.clone()));
let _ = local_env.insert(
String::from(MODULE_PATH_ENV),
Value::List(currnet_paths.clone()),
);
let _ = local_environment.insert(
String::from(MODULE_PATH_ENV),
Value::List(currnet_paths),
);
match std::fs::File::options()
.read(true)
.open(&real_path)
.and_then(|f| {
let mut buffer = String::new();
std::io::Read::read_to_string(
&mut std::io::BufReader::new(f),
&mut buffer,
)
.map(|_| buffer)
}) {
Ok(load_file) => match eval(&load_file, &local_env) {
Some((_, mut module_env)) => {
if let Some(Value::String(module_name)) =
module_env.remove(MODULE_NAME_ENV)
{
let _ = local_environment.insert(
name.clone(),
Value::Module(module_name, module_env),
);
Some((Value::Unit, local_environment))
} else {
eprintln!(
concat!(
"Error[ksl::builtin::load]: ",
"`{}` is not a module."
),
real_path
);
None
}
}
None => {
eprintln!(
concat!(
"Error[ksl::builtin::load]: ",
"Failed to load module `{}`."
),
name
);
None
}
},
Err(_) => {
eprintln!(
concat!(
"Error[ksl::builtin::load]: ",
"Unable to read contents of file at `{}`."
),
real_path
);
None
}
}
}
} else {
eprintln!(concat!(
"Error[ksl::builtin::load]: ",
"Uninitialized environment."
));
None
}
}
Some((e, _)) => {
eprintln!(
concat!(
"Error[ksl::builtin::load]: ",
"Expected a string, but got `{:?}`."
),
e
);
None
}
None => {
eprintln!(
concat!(
"Error[ksl::builtin::load]: ",
"Cannot evaluate expression {:?}."
),
path_string
);
None
}
}
}
} else {
eprintln!(
concat!(
"Error[ksl::builtin::load]: ",
"Only accepts a symbol ",
"and a file path without suffix as arguments, ",
"but got {:?}.",
),
args
);
None
}
}