Extract IO out of instruct
- Attach fail case only once on main
This commit is contained in:
Generated
+7
@@ -149,6 +149,7 @@ dependencies = [
|
|||||||
"anyhow",
|
"anyhow",
|
||||||
"pest",
|
"pest",
|
||||||
"pest_derive",
|
"pest_derive",
|
||||||
|
"string-builder",
|
||||||
"structopt",
|
"structopt",
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -255,6 +256,12 @@ dependencies = [
|
|||||||
"opaque-debug",
|
"opaque-debug",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "string-builder"
|
||||||
|
version = "0.2.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "2bd10a070fb1f2796a288abec42695db4682a82b6f12ffacd60fb8d5ad3a4a12"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "strsim"
|
name = "strsim"
|
||||||
version = "0.8.0"
|
version = "0.8.0"
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ edition = "2018"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.44"
|
anyhow = "1.0.44"
|
||||||
|
string-builder = "0.2.0"
|
||||||
pest = "2.1.3"
|
pest = "2.1.3"
|
||||||
pest_derive = "2.1.0"
|
pest_derive = "2.1.0"
|
||||||
structopt = "0.3.23"
|
structopt = "0.3.23"
|
||||||
|
|||||||
+14
-9
@@ -2,6 +2,7 @@ use std::fs::File;
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
|
use string_builder::Builder;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
extern crate pest;
|
extern crate pest;
|
||||||
@@ -23,19 +24,19 @@ struct Cli {
|
|||||||
output_file: std::path::PathBuf,
|
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() {
|
match statement.as_rule() {
|
||||||
Rule::instruction => {
|
Rule::instruction => {
|
||||||
for nested_statement in statement.into_inner() {
|
for nested_statement in statement.into_inner() {
|
||||||
instruct(nested_statement, out);
|
instruct(nested_statement, out);
|
||||||
}
|
}
|
||||||
out.write(b"\n");
|
out.append("\n");
|
||||||
}
|
}
|
||||||
Rule::operator => {
|
Rule::operator => {
|
||||||
out.write(b"operator ");
|
out.append("operator ");
|
||||||
}
|
}
|
||||||
Rule::operand => {
|
Rule::operand => {
|
||||||
out.write(b"operand ");
|
out.append("operand ");
|
||||||
}
|
}
|
||||||
|
|
||||||
Rule::loopBlock => {
|
Rule::loopBlock => {
|
||||||
@@ -44,13 +45,13 @@ fn instruct(statement: Pair<Rule>, mut out: &File) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Rule::loopBlockStart => {
|
Rule::loopBlockStart => {
|
||||||
out.write(b"loopBlockStart\n");
|
out.append("loopBlockStart\n");
|
||||||
}
|
}
|
||||||
Rule::loopBlockEnd => {
|
Rule::loopBlockEnd => {
|
||||||
out.write(b"loopBlockEnd\n");
|
out.append("loopBlockEnd\n");
|
||||||
}
|
}
|
||||||
Rule::EOI => {
|
Rule::EOI => {
|
||||||
out.write(b"\n");
|
out.append("\n");
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
@@ -62,7 +63,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let content = std::fs::read_to_string(&args.input_file)
|
let content = std::fs::read_to_string(&args.input_file)
|
||||||
.with_context(|| format!("could not read source file {:?}", 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)
|
let parsed_file = MblfParser::parse(Rule::file, &content)
|
||||||
.expect("Parse Error")
|
.expect("Parse Error")
|
||||||
@@ -70,9 +71,13 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
for statement in parsed_file.into_inner() {
|
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()?;
|
out.sync_all()?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user