Skip to content

Instantly share code, notes, and snippets.

@takoeight0821
Created December 13, 2017 06:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takoeight0821/3a331d2666b5123c684a09096a73fd4a to your computer and use it in GitHub Desktop.
Save takoeight0821/3a331d2666b5123c684a09096a73fd4a to your computer and use it in GitHub Desktop.
RustでBFインタプリタ(なんもわからん)
use std::io::{self, Read, stdin};
use std::collections::HashMap;
#[derive(Debug)]
struct BFM {
program: String,
tape: Vec<i32>,
ip: usize,
dp: usize,
braces: HashMap<usize, usize>,
ret: Vec<usize>,
}
fn parse(src: &String) -> HashMap<usize, usize> {
let mut table = HashMap::new();
let mut stack: Vec<usize> = Vec::new();
for c in src.char_indices() {
match c {
(i, '[') => {
stack.push(i);
}
(i, ']') => {
table.insert(stack.pop().unwrap(), i);
}
(_, _) => {}
}
}
table
}
fn incptr(m: &mut BFM) {
m.dp = m.dp + 1;
if m.tape.len() <= m.dp {
m.tape.push(0);
}
}
fn decptr(m: &mut BFM) {
m.dp = m.dp - 1;
}
fn inc(m: &mut BFM) {
m.tape[m.dp] = m.tape[m.dp] + 1;
}
fn dec(m: &mut BFM) {
m.tape[m.dp] = m.tape[m.dp] - 1;
}
fn getchar(m: &mut BFM) {
let x = stdin().take(1).bytes().nth(0).unwrap();
m.tape[m.dp] = x.unwrap() as i32;
}
fn putchar(m: &BFM) {
print!("{}", m.tape[m.dp] as u8 as char);
}
fn eval(mut m: BFM) {
while m.program.len() > m.ip {
match m.program.chars().nth(m.ip).unwrap() {
'+' => {
inc(&mut m);
m.ip = m.ip + 1;
}
'-' => {
dec(&mut m);
m.ip = m.ip + 1;
}
'>' => {
incptr(&mut m);
m.ip = m.ip + 1;
}
'<' => {
decptr(&mut m);
m.ip = m.ip + 1;
}
'[' => {
m.ret.push(m.ip);
if m.tape[m.dp] == 0 {
m.ip = m.braces[&m.ip] + 1;
} else {
m.ip = m.ip + 1;
}
}
']' => {
if m.tape[m.dp] != 0 {
m.ip = m.ret.pop().unwrap();
} else {
m.ret.pop().unwrap();
m.ip = m.ip + 1;
}
}
'.' => {
putchar(&mut m);
m.ip = m.ip + 1;
}
',' => {
getchar(&mut m);
m.ip = m.ip + 1;
}
_ => {
m.ip = m.ip + 1;
}
}
}
println!("{:?}", m);
}
fn main() {
let mut prog = String::new();
io::stdin().read_to_string(&mut prog).unwrap();
let b = parse(&prog);
let machine = BFM {
program: prog,
tape: vec![0],
ip: 0,
dp: 0,
braces: b,
ret: vec![],
};
eval(machine);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment