-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
123 lines (107 loc) · 3 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#![warn(clippy::dbg_macro, clippy::print_stdout)]
#![allow(
clippy::unneeded_field_pattern,
clippy::cognitive_complexity,
clippy::option_map_unit_fn,
clippy::map_clone,
clippy::match_bool,
clippy::match_like_matches_macro,
clippy::single_match
)]
#![allow(unstable_name_collisions)]
use std::fs;
use std::io;
use std::io::{Read, Write};
use std::time::Instant;
use crate::err::NiceError;
use crate::utils::Time;
mod anl;
mod ast2ir;
mod cli;
mod collections;
mod emit;
mod err;
mod ir;
mod ir2ast;
mod opt;
mod opt_ast;
mod parse;
mod swc_globals;
mod utils;
#[cfg(test)]
mod tests;
fn main() -> Result<(), NiceError> {
let cli::Options {
verbose,
input,
output,
minify,
optimize: _,
opt_ir,
opt_inline_ssa,
opt_ast,
emit_ir,
} = cli::Options::from_args();
env_logger::Builder::new()
.filter_level(match verbose {
0 => log::LevelFilter::Warn,
1 => log::LevelFilter::Info,
2 => log::LevelFilter::Debug,
_ => log::LevelFilter::Trace,
})
.init();
swc_globals::with(|g| {
let start = Instant::now();
let input_string = if input == "-" {
let mut s = String::new();
io::stdin().read_to_string(&mut s)?;
s
} else {
fs::read_to_string(input)?
};
log::info!("Done reading @ {}", Time(start.elapsed()));
let (ast, files) = parse::parse(g, input_string)?;
log::info!("Done parsing @ {}", Time(start.elapsed()));
let ir = ast2ir::convert(g, ast);
log::info!("Done ast2ir @ {}", Time(start.elapsed()));
let ir = if opt_ir {
let ir = opt::run_passes(g, ir);
log::info!("Done optimization @ {}", Time(start.elapsed()));
ir
} else {
ir
};
let output_string = if emit_ir {
let ppr = ir::print(g, &ir);
log::info!("Done printing @ {}", Time(start.elapsed()));
ppr
} else {
let ast = ir2ast::convert(
g,
ir,
ir2ast::Opt {
inline: opt_inline_ssa,
minify,
},
);
log::info!("Done ir2ast @ {}", Time(start.elapsed()));
let ast = if opt_ast {
let ast = opt_ast::run(g, ast, opt_ast::Opt { minify });
log::info!("Done ast optimization @ {}", Time(start.elapsed()));
ast
} else {
ast
};
let js = emit::emit(g, ast, files, emit::Opt { minify })?;
log::info!("Done emitting @ {}", Time(start.elapsed()));
js
};
if output == "-" {
io::stdout().write_all(output_string.as_bytes())?;
} else {
fs::write(output, output_string)?;
}
log::info!("Done writing @ {}", Time(start.elapsed()));
Ok(())
})
}