Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::env;
- use std::fs;
- use std::io;
- fn get_char() -> u8 {
- let mut input = String::new();
- io::stdin().read_line(&mut input).expect("Couldn't read the console");
- input.bytes().next().unwrap()
- }
- fn execute(buffer: &[u8]) {
- let mut mem: [u8; 1024] = [0; 1024];
- let mut ip : usize = 0;
- let mut mp : usize = 0;
- let mut loops = Vec::new();
- while ip != buffer.iter().count() {
- match buffer[ip] as char {
- '>' => mp += 1,
- '<' => mp -= 1,
- '+' => mem[mp] += 1,
- '-' => mem[mp] -= 1,
- '.' => print!("{}", mem[mp] as char),
- ',' => mem[mp] = get_char(),
- '[' => loops.push(ip),
- ']' => if mem[mp] != 0 {
- ip = *loops.iter().max().unwrap();
- },
- _ => (),
- }
- ip += 1;
- }
- }
- fn main() {
- let args: Vec<_> = env::args().skip(1).collect();
- if args.iter().count() < 1 {
- println!("No arguments given.");
- return;
- }
- let path = &args[0];
- let buffer = fs::read_to_string(path)
- .expect("Couldn't read file");
- execute(buffer.as_ref());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement