/
transpile.rs
66 lines (50 loc) 路 1.55 KB
/
transpile.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::{io, path::Path};
use brainease_runtime::io_handler::DefaultIoHandler;
use brainease_runtime::runtime::Runtime;
use brainease_transpiler::as_string::AsString;
use brainease_transpiler::parser;
use clap::Parser;
use crate::run::start_runtime;
use crate::util;
#[derive(Parser, Debug)]
#[clap(
name = "transpile",
about = "Transpile Brainease source code to Brainfuck"
)]
pub struct TranspileOpts {
/// The bf source file to transpile
#[clap(short = 'f', long = "file")]
main: String,
/// The output file to write
#[clap(short = 'o', long = "output")]
output: Option<String>,
#[clap(short = 'r', long = "run", help = "Run the transpiled code")]
run_after: bool,
#[clap(
long = "memory",
help = "The memory length when -r is used",
default_value = "3000"
)]
memory_length: usize,
}
pub fn run(opts: &TranspileOpts) -> io::Result<()> {
let main = Path::new(&opts.main);
let main_content = util::read_file(main)?;
log::trace!("Transpiling {}", main.display());
let transpiled = parser::parse_bf(&main_content);
log::trace!("{:#?}", transpiled);
let output_name = &opts.output.clone().unwrap_or_else(|| {
if opts.main.ends_with(".bf") {
opts.main.replace(".bf", ".brain")
} else {
format!("{}.brain", opts.main)
}
});
let string_lines = AsString::all(&transpiled, 0);
util::write_file(Path::new(output_name), string_lines)?;
if opts.run_after {
let mut runtime = Runtime::new(transpiled, opts.memory_length, DefaultIoHandler {});
start_runtime(&mut runtime)?;
}
Ok(())
}