Skip to content

Commit

Permalink
Starting some instrumentations.
Browse files Browse the repository at this point in the history
Starts #9
  • Loading branch information
nixpulvis committed Dec 15, 2015
1 parent 6e39428 commit af9e7a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::fmt;
/// cell is **not** 0. This allows for a relatively simple syntax for
/// decrementing iteration. For example `+++[- > operate on cell 2 < ]>.`
/// is the boilerplate for a loop that operates 3 times.
#[derive(Copy, Clone, Debug, PartialEq)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Instruction {
/// Increment the pointer moving it up on the tape.
/// TODO: Document wrapping/error behavior.
Expand Down
27 changes: 23 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@ extern crate rustc_serialize;
extern crate docopt;
extern crate brainfuck;

use std::io;
use std::collections::HashMap;
use docopt::Docopt;
use brainfuck::Program;
use brainfuck::{Interpreter, Program, Instruction};

const USAGE: &'static str = "
Brainfuck
Usage:
brainfuck <file>
brainfuck -e <program>
brainfuck [options] <file>
brainfuck [options] -e <program>
Options:
-i --instrumentation Enable program instrumentation.
";

#[derive(Debug, RustcDecodable)]
struct Args {
arg_program: Option<String>,
arg_file: Option<String>,
flag_instrumentation: bool,
}

fn main() {
Expand All @@ -28,5 +34,18 @@ fn main() {
Args { arg_file: Some(p), .. } => Program::from_file(p).unwrap(),
_ => panic!("Bad args."),
};
brainfuck::eval(program).unwrap();
let mut stdin = io::stdin();
let mut stdout = io::stdout();
let mut interp = Interpreter::new(&mut stdin, &mut stdout);
interp.load(program);
if args.flag_instrumentation {
let mut instruction_map: HashMap<Instruction, usize> = HashMap::new();
interp.run_with_callback(|_, i| {
let counter = instruction_map.entry(*i).or_insert(0);
*counter += 1;
}).unwrap();
println!("{:?}", instruction_map);
} else {
interp.run().unwrap();
}
}

0 comments on commit af9e7a2

Please sign in to comment.