-
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathscript-runner.rs
More file actions
68 lines (61 loc) · 2.16 KB
/
Copy pathscript-runner.rs
File metadata and controls
68 lines (61 loc) · 2.16 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
// EndBASIC
// Copyright 2021 Julio Merino
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//! Complete EndBASIC script executor for programs given as arguments.
//!
//! This example sets up a complete EndBASIC interpreter with the commands and functions that should
//! be allowed in scripts (and not in a REPL).
use futures_lite::future::block_on;
use std::env;
use std::fs;
use std::process;
fn safe_main() -> i32 {
let args: Vec<String> = env::args().collect();
let path = match args.as_slice() {
[_, path] => path,
_ => {
eprintln!("Usage error: expected a file name");
process::exit(1);
}
};
let mut machine = endbasic_std::MachineBuilder::default().build();
let mut input = match fs::File::open(path) {
Ok(file) => file,
Err(e) => {
eprintln!("ERROR: {}", e);
process::exit(1);
}
};
match machine.compile(&mut input) {
Ok(()) => match block_on(machine.exec()) {
Ok(None) => 0,
Ok(Some(code)) => code,
Err(e) => {
eprintln!("ERROR: {}", e);
1
}
},
Err(e) => {
eprintln!("ERROR: {}", e);
1
}
}
}
fn main() {
// We must ensure that all destructors run (in particular, the console's destructor) before
// exiting. `process:exit` doesn't do that for us, so wrap the program's code into a separate
// function so that we can guarantee that the destructors have run here.
process::exit(safe_main());
}