-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
128 lines (123 loc) · 3.9 KB
/
main.rs
File metadata and controls
128 lines (123 loc) · 3.9 KB
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
124
125
126
127
128
mod ast;
mod compiler;
mod interpreter;
mod monomorphize;
mod parser;
mod source;
mod type_checker;
use colored::*;
use std::env;
use std::io;
use std::path::{Path, PathBuf};
use std::time::Instant;
enum CommandKind {
Interpret,
Compile { output: Option<PathBuf> },
}
/// Parses the command line into a high-level command and source path.
fn parse_cli(args: &[String]) -> Result<(CommandKind, &str), String> {
match args.len() {
2 => Ok((CommandKind::Interpret, &args[1])),
_ => match args[1].as_str() {
"interpret" => {
if args.len() != 3 {
Err("Usage: skunk interpret <file_path>".to_string())
} else {
Ok((CommandKind::Interpret, &args[2]))
}
}
"compile" => {
if args.len() < 3 || args.len() > 4 {
Err("Usage: skunk compile <file_path> [output_path]".to_string())
} else {
Ok((
CommandKind::Compile {
output: args.get(3).map(PathBuf::from),
},
&args[2],
))
}
}
_ => Err(
"Usage: skunk <file_path>\n skunk interpret <file_path>\n skunk compile <file_path> [output_path]"
.to_string(),
),
},
}
}
/// Chooses the default binary path for `skunk compile` when the caller does not
/// provide one explicitly.
fn default_output_path(source_path: &Path) -> PathBuf {
let stem = source_path
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or("out");
source_path.with_file_name(stem)
}
fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
let type_checker_enabled: bool = true;
if args.len() < 2 {
eprintln!("Usage: skunk <file_path>");
std::process::exit(1);
}
let (command, file_path) = match parse_cli(&args) {
Ok(parsed) => parsed,
Err(err) => {
eprintln!("{}", err.red());
std::process::exit(1);
}
};
let node = match source::load_program(Path::new(file_path)) {
Ok(node) => node,
Err(err) => {
eprintln!("Error: {}", err.red());
std::process::exit(1);
}
};
let node = match monomorphize::prepare_program(&node) {
Ok(node) => node,
Err(err) => {
eprintln!("Error: {}", err.red());
std::process::exit(1);
}
};
if type_checker_enabled {
match type_checker::check(&node) {
Ok(_) => (),
Err(e) => {
eprintln!("Error: {}", e.red());
std::process::exit(1);
}
};
}
match command {
CommandKind::Interpret => {
let now = Instant::now();
let _result = interpreter::evaluate(&node);
let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed);
}
CommandKind::Compile { output } => {
let source_path = Path::new(file_path);
let output_path = output.unwrap_or_else(|| default_output_path(source_path));
let now = Instant::now();
match compiler::compile_to_executable(&node, source_path, &output_path) {
Ok(artifact) => {
let elapsed = now.elapsed();
println!(
"Compiled {} -> {}",
artifact.llvm_ir_path.display(),
artifact.binary_path.display()
);
println!("Elapsed: {:.2?}", elapsed);
}
Err(err) => {
eprintln!("Compile error: {}", err.red());
std::process::exit(1);
}
}
}
}
Ok(())
}