Skip to content

Commit

Permalink
Updated sarge
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyllingene committed Sep 17, 2023
1 parent b5e39c4 commit e787f86
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
6 changes: 3 additions & 3 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brim"
version = "3.0.0"
version = "3.0.1"
edition = "2021"

description = "An optimizing brain* interpreter."
Expand All @@ -12,7 +12,7 @@ keywords = ["cli", "command-line", "interpreter"]
categories = ["command-line-interface"]

[dependencies]
sarge = "3.0.1"
sarge = "4.0.2"

[features]
default = []
Expand Down
26 changes: 24 additions & 2 deletions src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{
cmp::Ordering,
fmt::Display,
io::{stdin, Read},
process::exit,
io::{stdin, Read, Stdout, Write},
process::exit, fs::File,
};

#[cfg(debug_assertions)]
Expand Down Expand Up @@ -116,3 +116,25 @@ impl Drop for ReadIter {
*STDIN_ITER_EXISTS.lock().unwrap() = false;
}
}

/// A wrapper around a Write-able object.
pub enum WriteWrapper {
Stdout(Stdout),
File(File),
}

impl Write for WriteWrapper {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
match self {
WriteWrapper::Stdout(s) => s.write(buf),
WriteWrapper::File(f) => f.write(buf),
}
}

fn flush(&mut self) -> std::io::Result<()> {
match self {
WriteWrapper::Stdout(s) => s.flush(),
WriteWrapper::File(f) => f.flush(),
}
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use token::parse;
/// Since [`Token`] is `Copy`, this takes a slice instead of a `Vec`. It's
/// other arguments, `stdin` and `stdout`, are also generic enough to provide
/// for many use cases.
pub fn interpret(code: &[Token], mut stdin: impl Iterator<Item = u8>, stdout: impl Write) {
pub fn interpret(code: &[Token], stdin: &mut impl Iterator<Item = u8>, stdout: &mut impl Write) {
let mut stdout = BufWriter::new(stdout);

let mut tape = [0u8; 30000];
Expand Down
48 changes: 25 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,51 @@ use std::{
use sarge::prelude::*;

use brim::{
helper::{err, warn, ReadIter},
helper::{err, warn, ReadIter, WriteWrapper},
interpret, parse,
token::optimize,
};

fn main() {
let mut parser = ArgumentParser::new();
parser.add(arg!(flag, both, 'h', "help"));
parser.add(arg!(str, both, 'i', "input"));
parser.add(arg!(str, both, 'o', "output"));
let parser = ArgumentParser::new();
let help = parser.add(tag::both('h', "help"));
let stdin_arg = parser.add::<String>(tag::both('i', "input"));
let stdout_arg = parser.add::<String>(tag::both('o', "output"));

let files = parser
.parse()
.unwrap_or_else(|e| err("failed to parse arguments", e));

if get_flag!(parser, both, 'h', "help") || files.is_empty() {
if help.get() == Ok(true) || files.is_empty() {
warn(include_str!("usage.txt"));

return;
}

let mut stdin = if let Ok(i) = stdin_arg.get() {
let file =
File::open(i).unwrap_or_else(|e| err("failed to open input file", e));

ReadIter::new(Box::new(file))
} else {
ReadIter::stdin()
};

let mut stdout = if let Ok(o) = stdout_arg.get() {
let file = File::create(o)
.unwrap_or_else(|e| err("failed to open output file", e));

WriteWrapper::File(file)
} else {
WriteWrapper::Stdout(stdout())
};

for filename in files {
let input = fs::read_to_string(filename).unwrap_or_else(|e| err("failed", e));

let toks = parse(&input);
let toks = optimize(&toks);

let stdin = if let Some(out) = get_arg!(parser, both, 'i', "input").unwrap().val.clone() {
let file =
File::open(out.get_str()).unwrap_or_else(|e| err("failed to open input file", e));

ReadIter::new(Box::new(file))
} else {
ReadIter::stdin()
};

if let Some(out) = get_arg!(parser, both, 'o', "output").unwrap().val.clone() {
let file = File::create(out.get_str())
.unwrap_or_else(|e| err("failed to open output file", e));

interpret(&toks, stdin, file);
} else {
interpret(&toks, stdin, stdout());
}
interpret(&toks, &mut stdin, &mut stdout);
}
}

0 comments on commit e787f86

Please sign in to comment.