-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.rs
More file actions
250 lines (218 loc) · 7.35 KB
/
main.rs
File metadata and controls
250 lines (218 loc) · 7.35 KB
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
mod cli;
mod repl;
#[cfg(test)]
mod tests;
use std::ffi::OsStr;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use clap::{parser::ValueSource, ArgMatches};
use feint_driver::{Driver, DriverErrKind, DriverResult};
use repl::Repl;
/// Interpret a file if one is specified. Otherwise, run the REPL.
fn main() -> ExitCode {
env_logger::init();
let app = cli::build_cli();
let matches = app.get_matches();
let max_call_depth = *matches.get_one("max_call_depth").unwrap();
let debug = *matches.get_one::<bool>("debug").unwrap();
let max_call_depth = match matches.value_source("max_call_depth") {
Some(ValueSource::DefaultValue) => {
if cfg!(debug_assertions) {
256
} else {
1024
}
}
_ => max_call_depth,
};
let return_code = match matches.subcommand() {
Some(("run", matches)) => handle_run(matches, max_call_depth, debug),
Some(("test", matches)) => handle_test(matches, max_call_depth, debug),
None => handle_run(&matches, max_call_depth, debug),
Some((name, _)) => {
unreachable!("Subcommand not defined: {}", name);
}
};
ExitCode::from(return_code)
}
/// Subcommand: run
fn handle_run(matches: &ArgMatches, max_call_depth: usize, debug: bool) -> u8 {
let file_name = matches.get_one::<String>("FILE_NAME");
let code = matches.get_one::<String>("code");
let dis = *matches.get_one::<bool>("dis").unwrap();
let history_path = matches.get_one::<String>("history_path");
let save_repl_history = !matches.get_one::<bool>("no_history").unwrap();
let mut argv: Vec<String> = matches
.get_many::<String>("argv")
.unwrap_or_default()
.map(|v| v.to_string())
.collect();
// When running code via -c, the file_name is actually the first
// arg in argv.
if code.is_some() {
if let Some(arg) = file_name {
argv.insert(0, arg.to_owned());
}
}
// When running the REPL, use incremental mode. This keeps certain
// errors from being printed in cases where more input might fix the
// error.
let incremental = !(code.is_some() || file_name.is_some());
let mut driver = Driver::new(max_call_depth, argv, incremental, dis, debug);
if let Err(err) = driver.bootstrap() {
return handle_driver_result(Err(err));
}
let result = if let Some(code) = code {
driver.execute_text(code)
} else if let Some(file_name) = file_name {
if file_name == "-" {
driver.execute_stdin()
} else if let Some(path) = get_script_file_path(file_name) {
driver.execute_file(path.as_path())
} else {
driver.execute_module_as_script(file_name)
}
} else {
let history_path = create_repl_history_file(&save_repl_history, history_path);
driver.install_sigint_handler();
let mut repl = Repl::new(history_path, driver);
repl.run()
};
handle_driver_result(result)
}
/// Subcommand: test
fn handle_test(matches: &ArgMatches, max_call_depth: usize, debug: bool) -> u8 {
let argv: Vec<String> = matches
.get_many::<String>("argv")
.unwrap_or_default()
.map(|v| v.to_string())
.collect();
let mut driver = Driver::new(max_call_depth, argv, false, false, debug);
if let Err(err) = driver.bootstrap() {
return handle_driver_result(Err(err));
}
handle_driver_result(driver.execute_module_as_script("std.test"))
}
// Utilities -----------------------------------------------------------
/// Get script file path from `name`.
///
/// If `name` refers to an existing file path _or_ is absolute _or_ has
/// a `.fi` extension, return `Path` for `name`.
///
/// If `name` is "main", unconditionally return `Path` for main script
/// at `./src/main.fi`. If there's no main script, this will cause an
/// error.
///
/// Otherwise, try to find a script in `./scripts`. If this fails,
/// return `None`.
fn get_script_file_path(name: &String) -> Option<PathBuf> {
let path = Path::new(name);
let path = path.to_path_buf();
if path.is_file()
|| path.is_absolute()
|| path.extension() == Some(OsStr::new("fi"))
{
Some(path)
} else if name == "main" {
// NOTE: main can only refer to src/main.fi and not a script
let main_path = Path::new("./src").join("main.fi");
Some(main_path)
} else {
let mut script_path = Path::new("./scripts").join(name);
script_path.set_extension("fi");
if script_path.is_file() {
Some(script_path)
} else {
None
}
}
}
/// Convert REPL history path from CLI to a `PathBuf`, if possible.
fn create_repl_history_file(cond: &bool, path: Option<&String>) -> Option<PathBuf> {
if !cond {
return None;
}
let default = String::from("repl-history");
let path = str_to_path_buf(path, Some(&default));
path.as_ref()?;
let path = path.unwrap();
if path.is_file() {
return Some(path);
}
if path.is_dir() {
eprintln!("WARNING: REPL history path is a directory: {}", path.display());
eprintln!("WARNING: REPL history will not be saved");
eprintln!();
return None;
}
if let Some(parent) = path.parent() {
if let Err(err) = fs::create_dir_all(parent) {
eprintln!(
"WARNING: Could not create REPL history directory: {}",
parent.display()
);
eprintln!("WARNING: {err}");
eprintln!("WARNING: REPL history will not be saved");
eprintln!();
None
} else {
eprintln!("INFO: Created REPL history directory: {}", parent.display());
eprintln!();
Some(path)
}
} else {
Some(path)
}
}
fn handle_driver_result(result: DriverResult) -> u8 {
result.unwrap_or_else(|err| {
if let Some(code) = err.exit_code() {
code
} else {
if matches!(
&err.kind,
DriverErrKind::Bootstrap(_)
| DriverErrKind::CouldNotReadSourceFile(_)
| DriverErrKind::ModuleDirNotFound(_)
| DriverErrKind::ModuleNotFound(_)
| DriverErrKind::ReplErr(_)
) {
eprintln!("{err}");
}
255
}
})
}
/// Get path for str, expanding leading ~ to user home directory. The
/// default path is used when the input path is None, "", or the home
/// directory isn't found.
fn str_to_path_buf(path: Option<&String>, default: Option<&String>) -> Option<PathBuf> {
let home = dirs::home_dir();
let default = if let Some(default) = default {
str_to_path_buf(Some(default), None)
} else {
None
};
if let Some(path) = path {
if path.is_empty() {
default
} else if path == "~" {
if let Some(home) = home {
Some(home)
} else {
default
}
} else if path.starts_with('~') {
if let Some(home) = home {
Some(home.join(&path[2..]))
} else {
default
}
} else {
Some(Path::new(path).to_path_buf())
}
} else {
default
}
}