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
#![allow(clippy::await_holding_refcell_ref)]
#![allow(clippy::collapsible_else_if)]
#![warn(anonymous_parameters, bad_style, missing_docs)]
#![warn(unused, unused_extern_crates, unused_import_braces, unused_qualifications)]
#![warn(unsafe_code)]
use endbasic_core::exec::{Machine, StopReason};
use endbasic_std::console::{self, Console};
use endbasic_std::program::{continue_if_modified, Program};
use endbasic_std::storage::Storage;
use std::cell::RefCell;
use std::io;
use std::rc::Rc;
pub mod demos;
pub mod editor;
pub fn print_welcome(console: Rc<RefCell<dyn Console>>) -> io::Result<()> {
let mut console = console.borrow_mut();
console.print("")?;
console.print(&format!(" Welcome to EndBASIC {}.", env!("CARGO_PKG_VERSION")))?;
console.print("")?;
console.print(" Type HELP for interactive usage information.")?;
console.print(" For a guided tour, type: LOAD \"DEMOS:/TOUR.BAS\": RUN")?;
console.print("")?;
Ok(())
}
pub async fn try_load_autoexec(
machine: &mut Machine,
console: Rc<RefCell<dyn Console>>,
storage: Rc<RefCell<Storage>>,
) -> io::Result<()> {
let code = match storage.borrow().get("AUTOEXEC.BAS").await {
Ok(code) => code,
Err(e) if e.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(e) => {
return console
.borrow_mut()
.print(&format!("AUTOEXEC.BAS exists but cannot be read: {}", e));
}
};
console.borrow_mut().print("Loading AUTOEXEC.BAS...")?;
match machine.exec(&mut code.as_bytes()).await {
Ok(_) => Ok(()),
Err(e) => {
console.borrow_mut().print(&format!("AUTOEXEC.BAS failed: {}", e))?;
Ok(())
}
}
}
pub async fn run_repl_loop(
machine: &mut Machine,
console: Rc<RefCell<dyn Console>>,
program: Rc<RefCell<dyn Program>>,
) -> io::Result<i32> {
let mut stop_reason = StopReason::Eof;
let mut history = vec![];
while stop_reason == StopReason::Eof {
let line = {
let mut console = console.borrow_mut();
if console.is_interactive() {
console.print("Ready")?;
}
console::read_line(&mut *console, "", "", Some(&mut history)).await
};
match line {
Ok(line) => match machine.exec(&mut line.as_bytes()).await {
Ok(reason) => stop_reason = reason,
Err(e) => {
let mut console = console.borrow_mut();
console.print(format!("ERROR: {}", e).as_str())?;
}
},
Err(e) => {
if e.kind() == io::ErrorKind::Interrupted {
let mut console = console.borrow_mut();
console.print("Interrupted by CTRL-C")?;
stop_reason = StopReason::Exited(1);
} else if e.kind() == io::ErrorKind::UnexpectedEof {
let mut console = console.borrow_mut();
console.print("End of input by CTRL-D")?;
stop_reason = StopReason::Exited(0);
} else {
stop_reason = StopReason::Exited(1);
}
}
}
#[allow(clippy::collapsible_if)]
if stop_reason != StopReason::Eof {
if !continue_if_modified(&*program.borrow(), &mut *console.borrow_mut()).await? {
console.borrow_mut().print("Exit aborted; resuming REPL loop.")?;
stop_reason = StopReason::Eof;
}
}
}
Ok(stop_reason.as_exit_code())
}
#[cfg(test)]
mod tests {
use super::*;
use endbasic_std::testutils::Tester;
use futures_lite::future::block_on;
#[test]
fn test_autoexec_ok() {
let autoexec = "PRINT \"hello\": global_var = 3: CD \"MEMORY:/\"";
let mut tester = Tester::default().write_file("AUTOEXEC.BAS", autoexec);
let (console, storage) = (tester.get_console(), tester.get_storage());
block_on(try_load_autoexec(tester.get_machine(), console, storage)).unwrap();
tester
.run("")
.expect_var("global_var", 3)
.expect_prints(["Loading AUTOEXEC.BAS...", "hello"])
.expect_file("MEMORY:/AUTOEXEC.BAS", autoexec)
.check();
}
#[test]
fn test_autoexec_error_is_ignored() {
let autoexec = "a = 1: b = undef: c = 2";
let mut tester = Tester::default().write_file("AUTOEXEC.BAS", autoexec);
let (console, storage) = (tester.get_console(), tester.get_storage());
block_on(try_load_autoexec(tester.get_machine(), console, storage)).unwrap();
tester
.run("after = 5")
.expect_var("a", 1)
.expect_var("after", 5)
.expect_prints([
"Loading AUTOEXEC.BAS...",
"AUTOEXEC.BAS failed: Undefined variable undef",
])
.expect_file("MEMORY:/AUTOEXEC.BAS", autoexec)
.check();
}
#[test]
fn test_autoexec_name_is_case_sensitive() {
let mut tester = Tester::default()
.write_file("AUTOEXEC.BAS", "a = 1")
.write_file("autoexec.bas", "a = 2");
let (console, storage) = (tester.get_console(), tester.get_storage());
block_on(try_load_autoexec(tester.get_machine(), console, storage)).unwrap();
tester
.run("")
.expect_var("a", 1)
.expect_prints(["Loading AUTOEXEC.BAS..."])
.expect_file("MEMORY:/AUTOEXEC.BAS", "a = 1")
.expect_file("MEMORY:/autoexec.bas", "a = 2")
.check();
}
#[test]
fn test_autoexec_missing() {
let mut tester = Tester::default();
let (console, storage) = (tester.get_console(), tester.get_storage());
block_on(try_load_autoexec(tester.get_machine(), console, storage)).unwrap();
tester.run("").check();
}
}