304 lines
8.8 KiB
Plaintext
304 lines
8.8 KiB
Plaintext
pub module transpiler;
|
|
|
|
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)]
|
|
#[serde(default)]
|
|
pub struct MistConfig {
|
|
pub package PathBuf,
|
|
pub packages Vec<PathBuf>,
|
|
pub main_mods String
|
|
}
|
|
|
|
impl MistConfig {
|
|
pub fn bool should_ignore(*self, path *Path) {
|
|
if path == self.package {
|
|
return true;
|
|
}
|
|
|
|
for pkg in &self.packages {
|
|
if path.ends_with(pkg) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
false
|
|
}
|
|
}
|
|
impl Default for MistConfig {
|
|
fn Self default() {
|
|
Self {
|
|
package: PathBuf::from("main.mist"),
|
|
packages: Vec::new(),
|
|
main_mods: String::new(),
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn PathBuf transpile() {
|
|
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 in &mut config.packages {
|
|
*pkg = src.join(&*pkg);
|
|
}
|
|
|
|
config.package = src.join(&*config.package);
|
|
|
|
let master = modules::Module::new(
|
|
config.package.clone(),
|
|
&config
|
|
);
|
|
|
|
master.transpile(&out, &mut config);
|
|
|
|
let elapsed = start.elapsed();
|
|
|
|
println!(
|
|
"\x1b[32m\nTranspile successful\x1b[0m in \x1b[34m{:.2?}\x1b[0m",
|
|
elapsed
|
|
);
|
|
|
|
root
|
|
}
|
|
|
|
impl modules::Module {
|
|
fn (bool, String) transpile(*self, parent_dir *PathBuf, config *mut MistConfig) {
|
|
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 (true, String::new());
|
|
}
|
|
|
|
let dir = self.output_dir(parent_dir);
|
|
|
|
let _ = fs::create_dir_all(&dir);
|
|
|
|
let mut output = String::new();
|
|
|
|
let mut child_changed = false;
|
|
|
|
for child in &self.children {
|
|
if !config.should_ignore(&child.path) {
|
|
let out = child.transpile(&dir, config);
|
|
if out.0 {
|
|
child_changed = true;
|
|
}
|
|
|
|
output.push_str(&out.1);
|
|
}
|
|
}
|
|
|
|
if self.path == config.package {
|
|
config.main_mods = output.clone();
|
|
} else if config.should_ignore(&self.path) {
|
|
output.push_str(&config.main_mods)
|
|
}
|
|
|
|
self.write_transpile(output, parent_dir, child_changed)
|
|
}
|
|
|
|
fn (bool, String) write_transpile(*self, output String, parent_dir *PathBuf, child_changed bool) {
|
|
if self.path.is_dir() {
|
|
let output_file = self.output_path(parent_dir, &None);
|
|
|
|
if output.len() == 0 {
|
|
return (true, 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);
|
|
}
|
|
|
|
(false, format!("pub mod {};\n", self.name))
|
|
} else {
|
|
let source = match fs::read_to_string(&self.path) {
|
|
Ok(s) => s;
|
|
Err(e) => {
|
|
eprintln!("error: failed to read file {}\n {}", self.path.display(), e);
|
|
|
|
process::exit(1);
|
|
}
|
|
};
|
|
|
|
let curr_mod_decl = mist_parser::parse_module(&source).expect("Failed parsing module");
|
|
|
|
let output_file = self.output_path(parent_dir, &curr_mod_decl);
|
|
|
|
let should_change = is_source_newer(&self.path, &output_file).expect("Failed to cache");
|
|
|
|
if should_change || child_changed {
|
|
transpile_file(&self.path, &source, output_file, &output, &curr_mod_decl);
|
|
}
|
|
|
|
(should_change, if let Some[[vis, name]] = &curr_mod_decl {
|
|
format!("{}mod {};\n", vis.get_rust(), name.get_rust())
|
|
} else {
|
|
String::new()
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
fn std::io::Result<bool> is_source_newer(source *Path, output *Path) {
|
|
if !output.exists() {
|
|
return Ok(true);
|
|
}
|
|
|
|
let source_time = fs::metadata(source)?.modified()?;
|
|
let output_time = fs::metadata(output)?.modified()?;
|
|
|
|
Ok(source_time > output_time)
|
|
}
|
|
|
|
pub enum TranspileError<'a> {
|
|
Parse(error::ParseError<'a, Vec<mist_parser::ast::TopLevel>>),
|
|
Semantic(Vec<mist_parser::semantics::SemanticError>)
|
|
}
|
|
|
|
pub fn Result<(PathBuf, mist_codegen::RustCodegen, String), TranspileError<'a>>
|
|
transpile_code<'a>(path *PathBuf, source *'a str, mut output_file PathBuf, mod_decl *str, curr_mod_decl *Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>) {
|
|
|
|
if let Some[[_, name]] = &curr_mod_decl {
|
|
if output_file.file_name().map(fn(v) -> v.to_str()).unwrap_or_default() != Some("mod.rs") {
|
|
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 mut gc = mist_codegen::RustCodegen::new(path.clone());
|
|
|
|
let parsed = mist_parser::parse(source).map_err(TranspileError::Parse)?;
|
|
|
|
for i in &parsed {
|
|
let _ = mist_parser::semantics::check_class_semantics(i).map_err(TranspileError::Semantic)?;
|
|
}
|
|
|
|
let output = gc.generate(parsed);
|
|
|
|
gc.mapping.shift_rust(mod_decl.lines().count() as isize, 0);
|
|
|
|
Ok((output_file, gc, output))
|
|
}
|
|
|
|
pub fn void transpile_file(path *PathBuf, source *str, output_file PathBuf, mod_decl *str, curr_mod_decl *Option<(mist_parser::ast::Visibility, mist_parser::ast::Identifier)>) {
|
|
let (output_file, gc, output) = match transpile_code(path, source, output_file, mod_decl, curr_mod_decl) {
|
|
Ok(output) => output;
|
|
Err(e) => {
|
|
decode_err(&e, path);
|
|
process::exit(1);
|
|
}
|
|
};
|
|
|
|
let res = fs::write(&output_file, format!("{mod_decl}{output}"));
|
|
|
|
let output_file_map = output_file.with_extension("map.json");
|
|
|
|
let res_map = fs::write(&output_file_map, serde_json::to_string(&gc.mapping).unwrap());
|
|
|
|
if res_map.is_err() {
|
|
eprintln!(
|
|
"error: failed to write mapping output {}\n {}",
|
|
output_file_map.display(),
|
|
res.as_ref().unwrap_err(),
|
|
);
|
|
}
|
|
|
|
if res.is_err() {
|
|
eprintln!(
|
|
"error: failed to write output {}\n {}",
|
|
output_file.display(),
|
|
res.as_ref().unwrap_err(),
|
|
);
|
|
}
|
|
|
|
if res_map.is_err() || res.is_err() {
|
|
process::exit(1);
|
|
}
|
|
}
|
|
|
|
pub fn void decode_err<'a>(err *TranspileError<'a>, path *PathBuf) {
|
|
let curr_dir = std::env::current_dir().unwrap();
|
|
|
|
match err {
|
|
TranspileError::Semantic(e_list) => {
|
|
for e in e_list {
|
|
eprintln!(
|
|
"\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}",
|
|
path.strip_prefix(&curr_dir).unwrap_or_else(|_| path).as_os_str().display(),
|
|
e.line,
|
|
e.column,
|
|
e.error_message,
|
|
);
|
|
}
|
|
}
|
|
|
|
TranspileError::Parse(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.strip_prefix(curr_dir).unwrap_or_else(|_| 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()),
|
|
);
|
|
}
|
|
|
|
error::ParseError::PreAst(e) => {
|
|
eprintln!("error: parse failed in {}\n{}", path.strip_prefix(curr_dir).unwrap_or_else(|_| path).display(), e);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|