Advertisement
cos8o

bf.rs

Aug 10th, 2019
433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 1.18 KB | None | 0 0
  1. use std::env;
  2. use std::fs;
  3. use std::io;
  4.  
  5. fn get_char() -> u8 {
  6.     let mut input = String::new();
  7.  
  8.     io::stdin().read_line(&mut input).expect("Couldn't read the console");
  9.  
  10.     input.bytes().next().unwrap()
  11. }
  12.  
  13. fn execute(buffer: &[u8]) {
  14.     let mut mem: [u8; 1024] = [0; 1024];
  15.     let mut ip : usize = 0;
  16.     let mut mp : usize = 0;
  17.     let mut loops = Vec::new();
  18.  
  19.     while ip != buffer.iter().count() {
  20.         match buffer[ip] as char {
  21.             '>' => mp += 1,
  22.             '<' => mp -= 1,
  23.             '+' => mem[mp] += 1,
  24.             '-' => mem[mp] -= 1,
  25.             '.' => print!("{}", mem[mp] as char),
  26.             ',' => mem[mp] = get_char(),
  27.             '[' => loops.push(ip),
  28.             ']' => if mem[mp] != 0 {
  29.                 ip = *loops.iter().max().unwrap();
  30.             },
  31.             _ => (),
  32.         }
  33.  
  34.         ip += 1;
  35.     }
  36.  
  37. }
  38.  
  39. fn main() {
  40.     let args: Vec<_> = env::args().skip(1).collect();
  41.  
  42.     if args.iter().count() < 1 {
  43.         println!("No arguments given.");
  44.         return;
  45.     }
  46.  
  47.     let path = &args[0];
  48.  
  49.     let buffer = fs::read_to_string(path)
  50.         .expect("Couldn't read file");
  51.  
  52.     execute(buffer.as_ref());
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement