204 lines
5.4 KiB
Plaintext
204 lines
5.4 KiB
Plaintext
use std::fs;
|
|
use std::path::Path;
|
|
use std::path::PathBuf;
|
|
use std::process;
|
|
use std::time;
|
|
use std::env;
|
|
|
|
use mist_parser::error;
|
|
use crate::modules;
|
|
use mist_codegen::GetRust;
|
|
|
|
use serde::Deserialize;
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct MistConfig {
|
|
packages Vec<PathBuf>,
|
|
}
|
|
impl MistConfig {
|
|
pub fn is_package(*self, path *Path) bool {
|
|
for (pkg : &self.packages) {
|
|
if (path.ends_with(pkg)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
false
|
|
}
|
|
}
|
|
|
|
pub fn transpile() PathBuf {
|
|
let start = time::Instant::now();
|
|
|
|
let root = env::current_dir().expect("Unable to find project root");
|
|
|
|
let toml_path = root.join("Mist.toml");
|
|
let src = root.join("src");
|
|
let out = root.join(".mist/src");
|
|
|
|
let toml_content = fs::read_to_string(&toml_path)
|
|
.unwrap_or_else(fn(_) -> panic!("Could not read file at {:?}", toml_path));
|
|
|
|
let mut config MistConfig = toml::from_str(&toml_content)
|
|
.unwrap_or_else(fn(_) -> panic!("Failed to parse Mist.toml from {:?}", toml_path));
|
|
|
|
for (pkg : &mut config.packages) {
|
|
*pkg = src.join(&*pkg);
|
|
}
|
|
|
|
let master = modules::master_package(
|
|
config.packages.get(0)
|
|
.unwrap_or_else(fn() -> panic!("Mist.toml packages must contain at least one element, eg, 'src/main.mist'"))
|
|
.to_path_buf()
|
|
);
|
|
|
|
master.transpile(&out, &config);
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
println!(
|
|
"\x1b[32m\nTranspile successful\x1b[0m in \x1b[34m{:.2?}\x1b[0m",
|
|
elapsed
|
|
);
|
|
|
|
root
|
|
}
|
|
|
|
impl modules::Module {
|
|
fn transpile(*self, parent_dir *PathBuf, config *MistConfig) String {
|
|
if (!self.path.is_dir() && self.path.extension().and_then(fn(v) -> v.to_str()) != Some("mist")) {
|
|
let file_name = self.path.file_name()
|
|
.and_then(fn(s) -> s.to_str())
|
|
.expect("Failed to get file name");
|
|
|
|
fs::copy(&self.path, parent_dir.join(&file_name)).expect("Failed to copy SideFile");
|
|
|
|
return String::new();
|
|
}
|
|
|
|
let dir = self.output_dir(&parent_dir);
|
|
let output_file = self.output_path(&parent_dir);
|
|
|
|
let _ = fs::create_dir_all(&dir);
|
|
|
|
let mut output = String::new();
|
|
|
|
for (child : &self.children) {
|
|
output.push_str(&child.transpile(&dir, config));
|
|
}
|
|
|
|
if (self.path.is_dir()) {
|
|
if (output.len() == 0) {
|
|
return String::new();
|
|
}
|
|
|
|
let 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);
|
|
}
|
|
|
|
format!("pub mod {};\n", self.name)
|
|
} else {
|
|
transpile_file(&self.path, output_file, &output)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn transpile_file(path *PathBuf, mut output_file PathBuf, mod_decl *str) String {
|
|
let 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);
|
|
}
|
|
};
|
|
|
|
let mut gc = mist_codegen::RustCodegen::new();
|
|
|
|
let parsed = match (mist_parser::parse(&source)) {
|
|
Ok[ast] => ast;
|
|
Err[e] => {
|
|
match (e) {
|
|
error::ParseError::Ast[e] => {
|
|
let start_pos = e.span.start_pos().line_col();
|
|
|
|
let 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);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
if (let [Some[[vis, name]], ast] = parsed) {
|
|
let output = gc.generate(ast);
|
|
|
|
if (let Some[ext] = output_file.extension().map(fn(e) -> e.to_owned())) {
|
|
let mut new_name = std::ffi::OsString::from(&name.0);
|
|
|
|
new_name.push(".");
|
|
new_name.push(ext);
|
|
|
|
output_file.set_file_name(new_name);
|
|
} else {
|
|
output_file.set_file_name(&name.0);
|
|
}
|
|
|
|
let 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);
|
|
}
|
|
|
|
format!("{}mod {};", vis.get_rust(), name.get_rust())
|
|
} else {
|
|
let output = gc.generate(parsed.1);
|
|
|
|
let 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);
|
|
}
|
|
|
|
String::new()
|
|
}
|
|
}
|
|
|