-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.rs
More file actions
180 lines (166 loc) Β· 5.91 KB
/
main.rs
File metadata and controls
180 lines (166 loc) Β· 5.91 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#![warn(clippy::pedantic)]
//! # Inference Compiler CLI
//!
//! Command line interface for the Inference toolchain.
//!
//! 1. Parse (`--parse`) β build the typed AST.
//! 2. Analyze (`--analyze`) β perform type / semantic inference and validation.
//! 3. Codegen (`--codegen`) β emit WebAssembly, and optionally translate to V (`-o`).
//!
//! At least one of the phase flags must be supplied; the phases that are requested will be
//! executed in the canonical order even if specified out of order on the command line.
//!
//! Output artifacts are written to an `out/` directory relative to the current working directory.
//! When `-o` is passed together with `--codegen` the produced WASM module is further translated
//! into V source and saved as `out/out.v`.
//!
//! ## Exit codes
//! * 0 β success.
//! * 1 β usage / IO / phase failure.
//!
//! ## Example
//! ```bash
//! infc examples/hello.inf --codegen -o
//! ```
//!
//! ## Tests
//! Integration tests exercise flag validation and the happy path compilation pipeline.
mod parser;
use clap::Parser;
use inference::{analyze, codegen, parse, wasm_to_v};
use parser::Cli;
use std::{
fs,
path::PathBuf,
process::{self},
};
/// Entry point for the CLI executable.
///
/// Responsibilities:
/// * Parse flags.
/// * Validate that the input path exists and at least one phase is selected.
/// * Run requested phases (parse -> analyze -> codegen).
/// * Optionally translate emitted WASM into V source when `-o` is set.
///
/// On any failure a diagnostic is printed to stderr and the process exits with code `1`.
fn main() {
let args = Cli::parse();
if !args.path.exists() {
eprintln!("Error: path not found");
process::exit(1);
}
let output_path = PathBuf::from("out");
let need_parse = args.parse;
let need_analyze = args.analyze;
let need_codegen = args.codegen;
if !(need_parse || need_analyze || need_codegen) {
eprintln!("Error: at least one of --parse, --analyze, or --codegen must be specified");
process::exit(1);
}
let source_code = fs::read_to_string(&args.path).expect("Error reading source file");
let mut t_ast = None;
if need_codegen || need_analyze || need_parse {
match parse(source_code.as_str()) {
Ok(ast) => {
println!("Parsed: {}", args.path.display());
t_ast = Some(ast);
}
Err(e) => {
eprintln!("Parse error: {e}");
process::exit(1);
}
}
}
let Some(t_ast) = t_ast else {
unreachable!("Phase validation guarantees parse ran when required");
};
if need_codegen || need_analyze {
if let Err(e) = analyze(&t_ast) {
eprintln!("Analysis failed: {e}");
process::exit(1);
}
println!("Analyzed: {}", args.path.display());
}
if need_codegen {
let wasm = match codegen(&t_ast) {
Ok(w) => w,
Err(e) => {
eprintln!("Codegen failed: {e}");
process::exit(1);
}
};
println!("WASM generated");
let source_fname = args
.path
.file_stem()
.unwrap_or_else(|| std::ffi::OsStr::new("module"))
.to_str()
.unwrap();
if args.generate_wasm_output {
let wasm_file_path = output_path.join(format!("{source_fname}.wasm"));
if let Err(e) = fs::create_dir_all(&output_path) {
eprintln!("Failed to create output directory: {e}");
process::exit(1);
}
if let Err(e) = fs::write(&wasm_file_path, &wasm) {
eprintln!("Failed to write WASM file: {e}");
process::exit(1);
}
println!("WASM generated at: {}", wasm_file_path.to_string_lossy());
}
if args.generate_v_output {
match wasm_to_v(source_fname, &wasm) {
Ok(v_output) => {
let v_file_path = output_path.join(format!("{source_fname}.v"));
if let Err(e) = fs::create_dir_all(&output_path) {
eprintln!("Failed to create output directory: {e}");
process::exit(1);
}
if let Err(e) = fs::write(&v_file_path, v_output) {
eprintln!("Failed to write V file: {e}");
process::exit(1);
}
println!("V generated at: {}", v_file_path.to_string_lossy());
}
Err(e) => {
eprintln!("WASM->V translation failed: {e}");
process::exit(1);
}
}
}
}
process::exit(0);
}
#[cfg(test)]
mod test {
// #[test]
// fn test_wasm_to_coq() {
// if std::env::var("GITHUB_ACTIONS").is_ok() {
// eprintln!("Skipping test on GitHub Actions");
// return;
// }
// let path = get_test_data_path().join("wasm").join("comments.0.wasm");
// let absolute_path = path.canonicalize().unwrap();
// let bytes = std::fs::read(absolute_path).unwrap();
// let mod_name = String::from("index");
// let coq = inference_wasm_coq_translator::wasm_parser::translate_bytes(
// &mod_name,
// bytes.as_slice(),
// );
// assert!(coq.is_ok());
// let coq_file_path = get_out_path().join("test_wasm_to_coq.v");
// std::fs::write(coq_file_path, coq.unwrap()).unwrap();
// }
#[allow(dead_code)]
pub(crate) fn get_test_data_path() -> std::path::PathBuf {
let current_dir = std::env::current_dir().unwrap();
current_dir
.parent() // inference
.unwrap()
.join("test_data")
}
#[allow(dead_code)]
fn get_out_path() -> std::path::PathBuf {
get_test_data_path().parent().unwrap().join("out")
}
}