mist-api 0.3.0-alpha.0

The Mist programming language module system
Documentation
/* 1:1 */
use std::fs;
/* 2:1 */
use std::path::PathBuf;
/* 3:1 */
use std::process;
/* 4:1 */
use std::time;
/* 5:1 */
use std::env;
/* 7:1 */
use mist_parser::error;
/* 8:1 */
use crate::modules;
/* 10:1 */
pub fn transpile() -> PathBuf {
    /* 11:5 */
    let start = time::Instant::now();
    /* 13:5 */
    let root = env::current_dir().expect("Unable to find project root");
    /* 15:5 */
    let src: PathBuf = root.join("src");
    /* 16:5 */
    let out: PathBuf = root.join(".mist/src");
    /* 18:5 */
    let master = modules::master_package(src.join("main.mist"));
    /* 20:5 */
    master.transpile(&out);
    /* 22:5 */
    let elapsed = start.elapsed();
    /* 24:5 */
    println!("\x1b[32m\nTranspile successful\x1b[0m in \x1b[34m{:.2?}\x1b[0m",
        elapsed);
    /* 29:5 */
    root
}/* 32:1 */
impl modules::Module {
    /* 33:5 */
    fn transpile(self: &Self, parent_dir: &PathBuf) -> String {
        /* 34:9 */
        if !self.path.is_dir()&&self.path.extension().and_then(|v| v.to_str())!=Some("mist") {
            /* 35:13 */
            let file_name = self.path.file_name().and_then(|s| s.to_str()).expect("Failed to get file name");
            /* 39:13 */
            fs::copy(&self.path, parent_dir.join(&file_name)).expect("Failed to copy SideFile");
            /* 41:13 */
            return String::new();
        }
        /* 44:9 */
        let dir: PathBuf = self.output_dir(&parent_dir);
        /* 45:9 */
        let output_file: PathBuf = self.output_path(&parent_dir);
        /* 47:9 */
        let _ = fs::create_dir_all(&dir);
        /* 49:9 */
        let mut output: String = String::new();
        /* 51:9 */
        for child in &self.children{
            /* 52:13 */
            output.push_str(&child.transpile(&dir));
        }
        /* 55:9 */
        if self.path.is_dir() {
            /* 56:13 */
            if output.len()==0 {
                /* 57:17 */
                return String::new();
            }
            /* 60:13 */
            let res = fs::write(&output_file, output);
            /* 62:13 */
            if res.is_err() {
                /* 63:17 */
                eprintln!("error: failed to write output {}\n  {}",
                    output_file.display(),
                    res.unwrap_err(),);
                /* 69:17 */
                process::exit(1);
            }
        } else {
            /* 72:13 */
            transpile_file(&self.path, &output_file, &output);
        }
        /* 75:9 */
        format!("pub mod {};\n", self.name)
    }}
/* 79:1 */
fn transpile_file(path: &PathBuf, output_file: &PathBuf, mod_decl: &str) -> () {
    /* 80:5 */
    let source: String = match fs::read_to_string(path) {        /* 81:9 */

        Ok (s,) => {s}        /* 82:9 */

        Err (e,) => {
            /* 83:13 */
            eprintln!("error: failed to read file {}\n  {}", path.display(), e);
            /* 85:13 */
            process::exit(1);
        }
    };
    /* 89:5 */
    let mut gc = mist_codegen::RustCodegen::new();
    /* 91:5 */
    let output: String = gc.generate(match mist_parser::parse(&source) {        /* 92:9 */

        Ok (ast,) => {ast}        /* 93:9 */

        Err (e,) => {
            /* 94:13 */
            match e {                /* 95:17 */

                error::ParseError::Ast (e,) => {
                    /* 96:21 */
                    let start_pos = e.span.start_pos().line_col();
                    /* 98:21 */
                    let span = e.span.as_str();
                    /* 100:21 */
                    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()),);
                    /* 111:21 */
                    process::exit(1);
                }                /* 114:17 */

                error::ParseError::PreAst (e,) => {
                    /* 115:21 */
                    eprintln!("error: parse failed in {}\n{}", path.display(), e);
                    /* 117:21 */
                    process::exit(1);
                }
            }
        }
    });
    /* 123:5 */
    let res = fs::write(&output_file, format!("{mod_decl}{output}"));
    /* 125:5 */
    if res.is_err() {
        /* 126:9 */
        eprintln!("error: failed to write output {}\n  {}",
            output_file.display(),
            res.unwrap_err(),);
        /* 132:9 */
        process::exit(1);
    }
}