Skip to content
This repository was archived by the owner on Nov 25, 2019. It is now read-only.

Commit 9e98d0f

Browse files
committed
Faff about with some tests
1 parent a01d87f commit 9e98d0f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

rust/main.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use std::io::{Read, Write};
1+
use std::fs::File;
2+
use std::io::{Read, Write, BufReader};
23

34
fn parse_instructions(code: String) -> Vec<char> {
45
let mut instructions = Vec::new();
@@ -34,6 +35,7 @@ fn evaluate(instructions: Vec<char>) {
3435
let mut tape = vec![0x00];
3536
let mut ins_ptr = 0; // instruction pointer: index in `instructions` of current instruction
3637
let mut mem_ptr = 0; // memory pointer: index in `tape` of current memory cell
38+
let mut input = std::io::stdin().bytes();
3739
while ins_ptr != instructions.len() - 1 {
3840
match instructions[ins_ptr] {
3941
'-' => {
@@ -52,11 +54,8 @@ fn evaluate(instructions: Vec<char>) {
5254
tape[mem_ptr] -= 0x01;
5355
},
5456
'?' => {
55-
let input: Option<u8> = std::io::stdin().bytes()
56-
.next()
57-
.and_then(|r| r.ok())
58-
.map(|b| b as u8);
59-
tape[mem_ptr] = input.unwrap_or(0x00);
57+
let next_from_stdin = input.next().and_then(|r| r.ok()).map(|b| b as u8);
58+
tape[mem_ptr] = next_from_stdin.unwrap_or(0x00);
6059
},
6160
'!' => {
6261
print!("{}", tape[mem_ptr] as char);
@@ -97,8 +96,10 @@ fn evaluate(instructions: Vec<char>) {
9796
}
9897

9998
fn main() {
100-
// Just a test, doesn't seem to be working just yet
101-
let code = "\"?'\"........'\",;;....\";.;.;.;..;...\"-'\";,\";;;.;.;.'\"-'\"--\";;;;.;....;!;,,,!.......!!'\",;.;.--'\";...!---!--!;;;;....'\",-....;'\"-,!;;!...!;!,,,,,,,,!-----!".to_string();
99+
let file = File::open("test/helloworld.qqq").unwrap();
100+
let mut buf_reader = BufReader::new(file);
101+
let mut code = String::new();
102+
buf_reader.read_to_string(&mut code);
102103
let instr = parse_instructions(code);
103104
evaluate(instr);
104105
}

0 commit comments

Comments
 (0)