Compare commits
2 Commits
dc29847cd3
...
3facaa10a2
Author | SHA1 | Date |
---|---|---|
b1ek | 3facaa10a2 | |
b1ek | dce22d5719 |
|
@ -7,10 +7,6 @@ fn main() {
|
|||
)
|
||||
);
|
||||
if eval.is_err() {
|
||||
let err = eval.unwrap_err();
|
||||
if err == "Stdin closed" {
|
||||
return
|
||||
}
|
||||
eprintln!("Program failed: {}", err);
|
||||
eprintln!("Program failed: \n{}", eval.unwrap_err());
|
||||
}
|
||||
}
|
81
src/lib.rs
81
src/lib.rs
|
@ -1,6 +1,68 @@
|
|||
#![forbid(unsafe_code)]
|
||||
|
||||
use std::io::{stdout, Write, Read, stdin};
|
||||
use std::{io::{stdout, Write, Read, stdin}, fmt::format};
|
||||
|
||||
/**
|
||||
* 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> {
|
||||
|
||||
|
@ -57,7 +119,7 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
|
|||
if char == ',' {
|
||||
let byte = stdin().bytes().next();
|
||||
if byte.is_none() {
|
||||
return Err("Stdin closed".into());
|
||||
continue
|
||||
}
|
||||
let byte = byte.unwrap();
|
||||
if byte.is_err() {
|
||||
|
@ -94,6 +156,21 @@ pub fn eval_mem(bf_str: &&str, mem: &mut [u8], pointer: &mut usize) -> Result<()
|
|||
}
|
||||
|
||||
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 pointer = 0 as usize;
|
||||
|
||||
|
|
Loading…
Reference in New Issue