use std::{ env, fs::File, io::{self, Read, Write}, path::Path, process, }; use ano_jit_bfi_rs as lib; fn input() -> u8 { let mut buffer = [0u8]; { // We lock the stdin mutex inside this block // To allow running this function recursively // Because the mutex will then be unlocked let stdin = std::io::stdin(); let handle = stdin.lock(); let mut limited_handle = handle.take(1); if limited_handle.read(&mut buffer).is_err() { process::exit(0); } } if buffer[0] == '\r' as u8 { // Handle "\r", usually coming from "\r\n" on Windows return input(); } else { return buffer[0]; } } fn output(c: u8) { let stdout = io::stdout(); let mut handle = stdout.lock(); if handle.write(&[c]).is_err() { print!("{}", c as char); } handle.flush().ok(); } fn should_stop(_ticks: u128) -> bool { false } fn main() { let args: Vec = env::args().collect(); if args.len() != 2 { println!("Another brainfuck interpreter, written in Rust"); println!("It is optimized for mandelbrot, Game of Life"); println!("\nUsage:\n {} ", args[0]); process::exit(0); } let path = Path::new(&args[1]); let display = path.display(); let mut file = match File::open(&path) { Err(why) => panic!("Could not open {}: {}", display, why), Ok(file) => file, }; let mut s = String::new(); match file.read_to_string(&mut s) { Err(why) => panic!("Could not read {}: {}", display, why), Ok(_) => {} } let code = lib::parse(s, true); let ticks = lib::execute(&code, input, output, should_stop); println!("\nProgram executed in {} ticks.", ticks); }