diff --git a/Cargo.lock b/Cargo.lock index 2f6f96b..ce5b9d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -30,7 +30,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "brainfuck" -version = "0.4.1" +version = "0.4.2" dependencies = [ "clap", "console", diff --git a/Cargo.toml b/Cargo.toml index 2a52efa..37fee2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "brainfuck" -version = "0.4.1" +version = "0.4.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index ec8e7e7..59c0070 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,13 +10,17 @@ pub mod text; #[derive(Parser, Debug)] #[command(author, version, about, long_about = text::HELP_GENERAL)] struct Cli { - /// The path to the file to be executed. Can be relative or absolute. + /// The path to the file to be executed. Can be relative or absolute #[arg(default_value = None)] path: Option, /// Display help for a particular error #[arg(short, long = "error-help")] error_help: Option, + + /// Reads the program from the standard input + #[arg(short = 'i')] + direct_input: bool, } fn find_matching_bracket(start_index: usize, program: &str) -> usize { @@ -118,18 +122,21 @@ fn iter_error(max_iters: u16) { process::exit(1); } -fn execute(file_path: &str) { - let mut program = ""; - let re = Regex::new(r"[^+-><\[\],.]").unwrap(); - let s; +fn read_file(file_path: &str) -> String { + let mut program = String::from(""); if let Ok(contents) = fs::read_to_string(file_path) { - s = contents.to_owned(); - program = s.as_str(); + program = contents.to_owned(); } else { file_error("The specified file was not found"); } + return program; +} + +fn execute(program: String) { + let re = Regex::new(r"[^+-><\[\],.]").unwrap(); + let check_match = check_brackets_match(&program); if !check_match.0 { syntax_error(check_match.1, &format!("Unmatched bracket '{}'", check_match.2), &program); @@ -228,6 +235,12 @@ fn main() { _ => "Unknown error type" }); } else if let Some(path) = cli.path.as_deref() { - execute(path); + let program = read_file(path); + execute(program); + } else if cli.direct_input { + let term = Term::stdout(); + if let Ok(input) = Term::read_line(&term) { + execute(input); + } } }