Skip to content

Commit

Permalink
Add execution from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
Spartan2909 committed Nov 12, 2022
1 parent 4e3ce6c commit 5c087b3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 21 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// Display help for a particular error
#[arg(short, long = "error-help")]
error_help: Option<String>,

/// Reads the program from the standard input
#[arg(short = 'i')]
direct_input: bool,
}

fn find_matching_bracket(start_index: usize, program: &str) -> usize {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit 5c087b3

Please sign in to comment.