Extract IO out of instruct

- Attach fail case only once on main
rewrite/grammar-implicit-whitespace
Raymon Zutekouw 3 years ago
parent cbf6f026f6
commit c949adeed9
  1. 7
      Cargo.lock
  2. 1
      Cargo.toml
  3. 23
      src/main.rs

7
Cargo.lock generated

@ -149,6 +149,7 @@ dependencies = [
"anyhow",
"pest",
"pest_derive",
"string-builder",
"structopt",
]
@ -255,6 +256,12 @@ dependencies = [
"opaque-debug",
]
[[package]]
name = "string-builder"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bd10a070fb1f2796a288abec42695db4682a82b6f12ffacd60fb8d5ad3a4a12"
[[package]]
name = "strsim"
version = "0.8.0"

@ -7,6 +7,7 @@ edition = "2018"
[dependencies]
anyhow = "1.0.44"
string-builder = "0.2.0"
pest = "2.1.3"
pest_derive = "2.1.0"
structopt = "0.3.23"

@ -2,6 +2,7 @@ use std::fs::File;
use std::io::Write;
use anyhow::{Context, Result};
use string_builder::Builder;
use structopt::StructOpt;
extern crate pest;
@ -23,19 +24,19 @@ struct Cli {
output_file: std::path::PathBuf,
}
fn instruct(statement: Pair<Rule>, mut out: &File) {
fn instruct(statement: Pair<Rule>, out: &mut Builder) {
match statement.as_rule() {
Rule::instruction => {
for nested_statement in statement.into_inner() {
instruct(nested_statement, out);
}
out.write(b"\n");
out.append("\n");
}
Rule::operator => {
out.write(b"operator ");
out.append("operator ");
}
Rule::operand => {
out.write(b"operand ");
out.append("operand ");
}
Rule::loopBlock => {
@ -44,13 +45,13 @@ fn instruct(statement: Pair<Rule>, mut out: &File) {
}
}
Rule::loopBlockStart => {
out.write(b"loopBlockStart\n");
out.append("loopBlockStart\n");
}
Rule::loopBlockEnd => {
out.write(b"loopBlockEnd\n");
out.append("loopBlockEnd\n");
}
Rule::EOI => {
out.write(b"\n");
out.append("\n");
}
_ => unreachable!(),
}
@ -62,7 +63,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(&args.input_file)
.with_context(|| format!("could not read source file {:?}", args.input_file))?;
let out = File::create(args.output_file)?;
let mut builder = Builder::default();
let parsed_file = MblfParser::parse(Rule::file, &content)
.expect("Parse Error")
@ -70,9 +71,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.unwrap();
for statement in parsed_file.into_inner() {
instruct(statement, &out);
instruct(statement, &mut builder);
}
let bf = builder.string().unwrap();
let mut out = File::create(args.output_file)?;
out.write(bf.as_bytes())?;
out.sync_all()?;
Ok(())

Loading…
Cancel
Save