add eval_file

This commit is contained in:
b1ek 2023-08-23 00:46:30 +10:00
parent 4d93b7c0d6
commit 5049f1d809
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 60 additions and 0 deletions

60
examples/eval_file.rs Normal file
View File

@ -0,0 +1,60 @@
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));
}