Compare commits

..

No commits in common. "3facaa10a24755f6e868a10952991ee9d3c6a6a3" and "dc29847cd3b37fd234d32f81f088a2e8f0031aff" have entirely different histories.

2 changed files with 7 additions and 80 deletions

View File

@ -7,6 +7,10 @@ fn main() {
) )
); );
if eval.is_err() { if eval.is_err() {
eprintln!("Program failed: \n{}", eval.unwrap_err()); let err = eval.unwrap_err();
if err == "Stdin closed" {
return
}
eprintln!("Program failed: {}", err);
} }
} }

View File

@ -1,68 +1,6 @@
#![forbid(unsafe_code)] #![forbid(unsafe_code)]
use std::{io::{stdout, Write, Read, stdin}, fmt::format}; use std::io::{stdout, Write, Read, stdin};
/**
* Check code for syntax errors
*/
pub fn lint_code(code: &&str) -> Result<(), Vec<String>> {
let mut loop_stack: Vec<usize> = vec![];
let mut pos = 0 as usize;
let mut pointer = 0;
let chars = code.chars().collect::<Vec<char>>();
let size = code.len();
let mut errors = vec![];
loop {
if pos == size {
break
}
let char = chars[pos];
pos += 1;
if char == '<' {
if pointer == 0 {
errors.push(format!("ERR: Reducing pointer to less than 0 is not allowed ({})", pos));
continue
}
pointer -= 1;
}
if char == '>' {
if pointer == 29999 {
errors.push(format!("ERR: Increasing pointer to more than 29999 is not allowed ({})", pos));
continue
}
pointer += 1;
}
if char == '[' {
loop_stack.push(pos);
continue
}
if char == ']' {
if loop_stack.len() == 0 {
errors.push(format!("ERR: Unmatched ']' ({})", pos));
continue
}
loop_stack.pop();
}
}
if loop_stack.len() != 0 {
for lup in loop_stack.iter() {
errors.push(format!("Unmatched '[' ({})", *lup));
}
}
if errors.len() != 0 {
return Err(errors)
}
Ok(())
}
pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<(), String> { pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<(), String> {
@ -119,7 +57,7 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
if char == ',' { if char == ',' {
let byte = stdin().bytes().next(); let byte = stdin().bytes().next();
if byte.is_none() { if byte.is_none() {
continue return Err("Stdin closed".into());
} }
let byte = byte.unwrap(); let byte = byte.unwrap();
if byte.is_err() { if byte.is_err() {
@ -156,21 +94,6 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
} }
pub fn eval(bf_str: &&str) -> Result<[u8; 30000], String> { pub fn eval(bf_str: &&str) -> Result<[u8; 30000], String> {
// first check the code
let lint = lint_code(bf_str);
if lint.is_err() {
return Err(
format!(
"Code linting failed: \n\n{}",
lint.unwrap_err().join("\n")
)
)
}
// then actually run it
let mut memory: [u8; 30000] = core::array::from_fn(|_| 0); let mut memory: [u8; 30000] = core::array::from_fn(|_| 0);
let mut pointer = 0 as usize; let mut pointer = 0 as usize;