122 lines
3.1 KiB
Plaintext
122 lines
3.1 KiB
Plaintext
use <std::fs>;
|
|
use <std::path::PathBuf>;
|
|
use <std::process>;
|
|
use <std::time>;
|
|
use <std::env>;
|
|
|
|
use <mist_parser::error>;
|
|
use <crate::modules>;
|
|
|
|
pub PathBuf transpile() {
|
|
var start = time::Instant::now();
|
|
|
|
var root = env::current_dir().expect("Unable to find project root");
|
|
|
|
PathBuf src = root.join("src");
|
|
PathBuf out = root.join(".mist/src");
|
|
|
|
var master = modules::master_package(src.join("main.mist"), out.join("main.rs"));
|
|
|
|
master.transpile(&out);
|
|
|
|
var elapsed = start.elapsed();
|
|
|
|
println!(
|
|
"\x1b[32m\nTranspile successful\x1b[0m in \x1b[34m{:.2?}\x1b[0m",
|
|
elapsed
|
|
);
|
|
|
|
root
|
|
}
|
|
|
|
impl modules::Module {
|
|
String transpile(self*, PathBuf* parent_dir) {
|
|
PathBuf dir = self.output_dir(&parent_dir);
|
|
PathBuf output_file = self.output_path(&parent_dir);
|
|
|
|
var _ = fs::create_dir_all(&dir);
|
|
|
|
String mut output = String::new();
|
|
|
|
for (child : &self.children) {
|
|
output.push_str(&child.transpile(&dir));
|
|
}
|
|
|
|
if (self.path.is_dir()) {
|
|
var res = fs::write(&output_file, output);
|
|
|
|
if (res.is_err()) {
|
|
eprintln!(
|
|
"error: failed to write output {}\n {}",
|
|
output_file.display(),
|
|
res.unwrap_err(),
|
|
);
|
|
|
|
process::exit(1);
|
|
}
|
|
} else {
|
|
transpile_file(&self.path, &output_file, &output);
|
|
}
|
|
|
|
format!("pub mod {};\n", self.name)
|
|
}
|
|
}
|
|
|
|
void transpile_file(PathBuf* path, PathBuf* output_file, str* mod_decl) {
|
|
String source = match (fs::read_to_string(path)) {
|
|
Ok[s] => s;
|
|
Err[e] => {
|
|
eprintln!("error: failed to read file {}\n {}", path.display(), e);
|
|
|
|
process::exit(1);
|
|
}
|
|
};
|
|
|
|
var mut gc = mist_codegen::RustCodegen::new();
|
|
|
|
String output = gc.generate(match (mist_parser::parse(&source)) {
|
|
Ok[ast] => ast;
|
|
Err[e] => {
|
|
match (e) {
|
|
error::ParseError::Ast[e] => {
|
|
var start_pos = e.span.start_pos().line_col();
|
|
|
|
var span = e.span.as_str();
|
|
|
|
eprintln!(
|
|
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}",
|
|
path.as_os_str().display(),
|
|
start_pos.0,
|
|
start_pos.1,
|
|
e.error_message,
|
|
span,
|
|
if span.ends_with("\n") { "" } else { "\n" },
|
|
"^".repeat(span.trim().len()),
|
|
);
|
|
|
|
process::exit(1);
|
|
}
|
|
|
|
error::ParseError::PreAst[e] => {
|
|
eprintln!("error: parse failed in {}\n{}", path.display(), e);
|
|
|
|
process::exit(1);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
var res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
|
|
|
if (res.is_err()) {
|
|
eprintln!(
|
|
"error: failed to write output {}\n {}",
|
|
output_file.display(),
|
|
res.unwrap_err(),
|
|
);
|
|
|
|
process::exit(1);
|
|
}
|
|
}
|
|
|