Skip to main content

mist_api/
transpiler.rs

1/* 1:1 */
2use std::fs;
3/* 2:1 */
4use std::path::PathBuf;
5/* 3:1 */
6use std::process;
7/* 4:1 */
8use std::time;
9/* 5:1 */
10use std::env;
11/* 7:1 */
12use mist_parser::error;
13/* 8:1 */
14use crate::modules;
15/* 10:1 */
16pub fn transpile() -> PathBuf {
17    /* 11:5 */
18    let start = time::Instant::now();
19    /* 13:5 */
20    let root = env::current_dir().expect("Unable to find project root");
21    /* 15:5 */
22    let src: PathBuf = root.join("src");
23    /* 16:5 */
24    let out: PathBuf = root.join(".mist/src");
25    /* 18:5 */
26    let master = modules::master_package(src.join("main.mist"));
27    /* 20:5 */
28    master.transpile(&out);
29    /* 22:5 */
30    let elapsed = start.elapsed();
31    /* 24:5 */
32    println!("\x1b[32m\nTranspile successful\x1b[0m in \x1b[34m{:.2?}\x1b[0m",
33        elapsed);
34    /* 29:5 */
35    root
36}/* 32:1 */
37impl modules::Module {
38    /* 33:5 */
39    fn transpile(self: &Self, parent_dir: &PathBuf) -> String {
40        /* 34:9 */
41        if !self.path.is_dir()&&self.path.extension().and_then(|v| v.to_str())!=Some("mist") {
42            /* 35:13 */
43            let file_name = self.path.file_name().and_then(|s| s.to_str()).expect("Failed to get file name");
44            /* 39:13 */
45            fs::copy(&self.path, parent_dir.join(&file_name)).expect("Failed to copy SideFile");
46            /* 41:13 */
47            return String::new();
48        }
49        /* 44:9 */
50        let dir: PathBuf = self.output_dir(&parent_dir);
51        /* 45:9 */
52        let output_file: PathBuf = self.output_path(&parent_dir);
53        /* 47:9 */
54        let _ = fs::create_dir_all(&dir);
55        /* 49:9 */
56        let mut output: String = String::new();
57        /* 51:9 */
58        for child in &self.children{
59            /* 52:13 */
60            output.push_str(&child.transpile(&dir));
61        }
62        /* 55:9 */
63        if self.path.is_dir() {
64            /* 56:13 */
65            if output.len()==0 {
66                /* 57:17 */
67                return String::new();
68            }
69            /* 60:13 */
70            let res = fs::write(&output_file, output);
71            /* 62:13 */
72            if res.is_err() {
73                /* 63:17 */
74                eprintln!("error: failed to write output {}\n  {}",
75                    output_file.display(),
76                    res.unwrap_err(),);
77                /* 69:17 */
78                process::exit(1);
79            }
80        } else {
81            /* 72:13 */
82            transpile_file(&self.path, &output_file, &output);
83        }
84        /* 75:9 */
85        format!("pub mod {};\n", self.name)
86    }}
87/* 79:1 */
88fn transpile_file(path: &PathBuf, output_file: &PathBuf, mod_decl: &str) -> () {
89    /* 80:5 */
90    let source: String = match fs::read_to_string(path) {        /* 81:9 */
91
92        Ok (s,) => {s}        /* 82:9 */
93
94        Err (e,) => {
95            /* 83:13 */
96            eprintln!("error: failed to read file {}\n  {}", path.display(), e);
97            /* 85:13 */
98            process::exit(1);
99        }
100    };
101    /* 89:5 */
102    let mut gc = mist_codegen::RustCodegen::new();
103    /* 91:5 */
104    let output: String = gc.generate(match mist_parser::parse(&source) {        /* 92:9 */
105
106        Ok (ast,) => {ast}        /* 93:9 */
107
108        Err (e,) => {
109            /* 94:13 */
110            match e {                /* 95:17 */
111
112                error::ParseError::Ast (e,) => {
113                    /* 96:21 */
114                    let start_pos = e.span.start_pos().line_col();
115                    /* 98:21 */
116                    let span = e.span.as_str();
117                    /* 100:21 */
118                    eprintln!("\n{}:{}:{}\n \x1b[31mError\x1b[0m: {}\n\t{}{}\t{}",
119                        path.as_os_str().display(),
120                        start_pos.0,
121                        start_pos.1,
122                        e.error_message,
123                        span,
124                        if span.ends_with("\n") { "" } else { "\n" },
125                        "^".repeat(span.trim().len()),);
126                    /* 111:21 */
127                    process::exit(1);
128                }                /* 114:17 */
129
130                error::ParseError::PreAst (e,) => {
131                    /* 115:21 */
132                    eprintln!("error: parse failed in {}\n{}", path.display(), e);
133                    /* 117:21 */
134                    process::exit(1);
135                }
136            }
137        }
138    });
139    /* 123:5 */
140    let res = fs::write(&output_file, format!("{mod_decl}{output}"));
141    /* 125:5 */
142    if res.is_err() {
143        /* 126:9 */
144        eprintln!("error: failed to write output {}\n  {}",
145            output_file.display(),
146            res.unwrap_err(),);
147        /* 132:9 */
148        process::exit(1);
149    }
150}