Loads the program given by `path` from storage and executes it on the `machine`.
(
machine: &mut Machine,
console: Rc<RefCell<dyn Console>>,
storage: Rc<RefCell<Storage>>,
program: Rc<RefCell<dyn Program>>,
path: &str,
will_run_repl: bool,
)
| 142 | |
| 143 | /// Loads the program given by `path` from storage and executes it on the `machine`. |
| 144 | pub async fn run_from_storage_path( |
| 145 | machine: &mut Machine, |
| 146 | console: Rc<RefCell<dyn Console>>, |
| 147 | storage: Rc<RefCell<Storage>>, |
| 148 | program: Rc<RefCell<dyn Program>>, |
| 149 | path: &str, |
| 150 | will_run_repl: bool, |
| 151 | ) -> io::Result<i32> { |
| 152 | let path = storage.borrow().make_canonical_with_extension(path, "bas")?; |
| 153 | |
| 154 | console.borrow_mut().print(&format!("Loading {}...", path))?; |
| 155 | let content = storage.borrow().get(&path).await?; |
| 156 | let content = match String::from_utf8(content) { |
| 157 | Ok(text) => text, |
| 158 | Err(e) => { |
| 159 | let mut console = console.borrow_mut(); |
| 160 | console.print(&format!("Invalid program to run '{}': {}", path, e))?; |
| 161 | return Ok(1); |
| 162 | } |
| 163 | }; |
| 164 | program.borrow_mut().load(Some(&path), &content); |
| 165 | |
| 166 | console.borrow_mut().print("Starting...")?; |
| 167 | console.borrow_mut().print("")?; |
| 168 | |
| 169 | if let Err(e) = machine.compile(&mut "RUN".as_bytes()) { |
| 170 | let mut console = console.borrow_mut(); |
| 171 | console.print(&format!("**** ERROR: {} ****", e))?; |
| 172 | return Ok(1); |
| 173 | } |
| 174 | |
| 175 | let result = machine.exec().await; |
| 176 | |
| 177 | let mut console = console.borrow_mut(); |
| 178 | |
| 179 | console.print("")?; |
| 180 | let code = match result { |
| 181 | Ok(None) => { |
| 182 | console.print("**** Program exited due to EOF ****")?; |
| 183 | 0 |
| 184 | } |
| 185 | Ok(Some(code)) => { |
| 186 | console.print(&format!("**** Program exited with code {} ****", code))?; |
| 187 | code |
| 188 | } |
| 189 | Err(StdError::Break) => { |
| 190 | console.print("**** Program stopped due to BREAK ****")?; |
| 191 | 130 |
| 192 | } |
| 193 | Err(e) => { |
| 194 | console.print(&format!("**** ERROR: {} ****", e))?; |
| 195 | 1 |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | if will_run_repl { |
| 200 | console.print("")?; |
| 201 | refill_and_print( |