bf_rust/examples/eval_file.rs

60 lines
1.2 KiB
Rust

use std::{process::exit, env, fs};
use brainrust::*;
fn dump_mem(memory: &[u8]) -> String {
let mut out: String = "Memory dump:\n{ ".into();
let mut i = 0;
for byte in memory.iter() {
if *byte == 0 && i > 50 { break }
out += format!("{}, ", byte).as_str();
i += 1;
}
// remove the last ", "
out.pop();
out.pop();
out += " }";
out
}
fn main() {
// read file
let file = env::args().collect::<Vec<String>>();
let file = file.get(1);
if file.is_none() {
eprintln!("Please specify file as 1st argument");
exit(-1);
}
let file = file.unwrap();
let file = fs::read_to_string(file).unwrap();
// done reading file; actual shit here
let mut memory: [u8; 30000] = core::array::from_fn(|_| 0);
let mut pointer = 0 as usize;
let eval_res = eval_mem(&file.as_str(), &mut memory, &mut pointer);
// print error if there is any
if eval_res.is_err() {
let err = eval_res.unwrap_err();
eprintln!("Program failed: {}", err.error);
println!("{}", dump_mem(&memory));
println!("Pointer is {}", pointer);
exit(-1);
}
// dump memory (for dev purposes)
// println!("{}", dump_mem(&memory));
}