small changes

This commit is contained in:
nora 2021-05-03 16:08:35 +02:00
parent 329c33f3cb
commit 5cc058267b
6 changed files with 14 additions and 15 deletions

View file

@ -50,7 +50,7 @@ fn parse(chars: Vec<char>, direct_print: bool) -> Vec<Statement> {
}
}
return loop_stack.pop().unwrap();
loop_stack.pop().unwrap()
}
#[cfg(test)]

View file

@ -37,7 +37,7 @@ impl From<Statement> for ExStatement {
Statement::In => ExStatement::In,
Statement::Out => ExStatement::Out,
Statement::Loop(v) => ExStatement::Loop(
v.into_iter().map(|s| ExStatement::from(s)).collect()
v.into_iter().map(ExStatement::from).collect()
),
Statement::DOut => ExStatement::DOut
}
@ -66,20 +66,19 @@ impl Error for BfErr {}
pub fn run(pgm: &str, direct_print: bool) -> Result<String, BfErr> {
let pgm = minify(pgm);
if pgm.len() < 1 { return Err(BfErr::new("no program found")); };
if pgm.is_empty() { return Err(BfErr::new("no program found")); };
let pgm = parse(pgm.chars().collect(), direct_print);
let pgm = optimize(&pgm);
let out = interpret(&pgm);
Ok(out)
}
fn optimize(code: &Vec<Statement>) -> Vec<ExStatement> {
fn optimize(code: &[Statement]) -> Vec<ExStatement> {
let code = o_set_null(code);
let code = o_repeat(code);
code
o_repeat(code)
}
fn o_set_null(code: &Vec<Statement>) -> Vec<ExStatement> {
fn o_set_null(code: &[Statement]) -> Vec<ExStatement> {
code.iter().map(|s| {
match s {
Statement::Loop(v) => {
@ -119,7 +118,7 @@ fn o_repeat(code: Vec<ExStatement>) -> Vec<ExStatement> {
result
}
fn interpret(pgm: &Vec<ExStatement>) -> String {
fn interpret(pgm: &[ExStatement]) -> String {
let mut out = String::new();
let mut pointer: usize = 0;
let mut mem: [u8; MEM_SIZE] = [0; MEM_SIZE];
@ -145,7 +144,7 @@ fn execute(statement: &ExStatement, mem: &mut Memory, pointer: &mut usize, out:
}
ExStatement::In => {
let mut in_buffer = [0, 1];
stdin().read(&mut in_buffer).unwrap();
stdin().read_exact(&mut in_buffer).unwrap();
mem[*pointer] = in_buffer[0] as u8;
}
ExStatement::Loop(vec) => {

View file

@ -12,11 +12,10 @@ use crate::interpreter::{MEM_SIZE, Memory, minify, parse, Statement};
pub fn run(pgm: &str) -> String {
let pgm = minify(pgm);
let pgm = parse(pgm.chars().collect(), false);
let out = interpret(&pgm);
out
interpret(&pgm)
}
fn interpret(pgm: &Vec<Statement>) -> String {
fn interpret(pgm: &[Statement]) -> String {
let mut out = String::new();
let mut pointer: usize = 0;
let mut mem: [u8; MEM_SIZE] = [0; MEM_SIZE];
@ -37,7 +36,7 @@ fn execute(statement: &Statement, mem: &mut Memory, pointer: &mut usize, out: &m
Statement::Out => out.push(mem[*pointer] as u8 as char),
Statement::In => {
let mut in_buffer = [0, 1];
stdin().read(&mut in_buffer).unwrap();
stdin().read_exact(&mut in_buffer).unwrap();
mem[*pointer] = in_buffer[0] as u8;
}
Statement::Loop(vec) => {

View file

@ -27,7 +27,7 @@ fn interpret(pgm: Vec<char>) -> String {
'-' => mem[pointer] = mem[pointer].wrapping_sub(1),
'.' => out.push(mem[pointer] as u8 as char),
',' => {
stdin().read(&mut in_buffer).unwrap();
stdin().read_exact(&mut in_buffer).unwrap();
mem[pointer] = in_buffer[0] as u8;
}
'[' => {

View file

@ -1,11 +1,12 @@
mod interpreter;
mod repl;
use std::{env, fs};
use std::time::SystemTime;
fn main() {
let path = env::args().skip(1).next();
let path = env::args().nth(1);
let path = match path {
Some(p) => p,
None => {

0
bfi-rust/src/repl/mod.rs Normal file
View file