prune_lang/cli/
pipeline.rs1use super::args::CliArgs;
2use super::diagnostic::{DiagLevel, Diagnostic};
3use super::*;
4use crate::cli::replay::ReplayWriter;
5use crate::{interp, logic, syntax, tych};
6
7pub struct PipeIO {
8 pub output: Box<dyn Write>,
9 pub stat: Box<dyn Write>,
10 pub prog: Box<dyn Write>,
11}
12
13impl PipeIO {
14 pub fn empty() -> PipeIO {
15 PipeIO {
16 output: Box::new(io::empty()),
17 stat: Box::new(io::empty()),
18 prog: Box::new(io::empty()),
19 }
20 }
21}
22
23pub struct Pipeline<'arg> {
24 pub args: &'arg CliArgs,
25 pub diags: Vec<Diagnostic>,
26}
27
28impl<'arg> Pipeline<'arg> {
29 pub fn new(args: &'arg CliArgs) -> Pipeline<'arg> {
30 Pipeline {
31 args,
32 diags: Vec::new(),
33 }
34 }
35
36 fn emit_diags<D: Into<Diagnostic>>(&mut self, diags: Vec<D>) -> bool {
37 let mut flag = false;
38 for diag in diags.into_iter() {
39 let diag = diag.into();
40 if diag.level == DiagLevel::Error
41 || (self.args.warn_as_err && diag.level == DiagLevel::Warn)
42 {
43 flag = true;
44 }
45 self.diags.push(diag);
46 }
47 flag
48 }
49
50 pub fn run_pipline(
51 &mut self,
52 src: &str,
53 pipe_io: &mut PipeIO,
54 ) -> Result<Vec<usize>, io::Error> {
55 let mut prog = self.parse_program(src)?;
56
57 self.rename_pass(&mut prog)?;
58
59 self.check_pass(&mut prog)?;
60
61 let prog = self.compile_pass(&prog);
62
63 writeln!(pipe_io.prog, "{prog}").unwrap();
64
65 let res = self.run_backend(&prog, pipe_io);
66 Ok(res)
67 }
68
69 pub fn parse_program(&mut self, src: &str) -> Result<syntax::ast::Program, io::Error> {
70 let (prog, errs) = syntax::parser::parse_program(src);
71 if self.emit_diags(errs) {
72 return Err(io::Error::other("failed to parse program!"));
73 }
74 Ok(prog)
75 }
76
77 pub fn rename_pass(&mut self, prog: &mut syntax::ast::Program) -> Result<(), io::Error> {
78 let errs = tych::rename::rename_pass(prog);
79 if self.emit_diags(errs) {
80 return Err(io::Error::other("failed in binding analysis pass!"));
81 }
82 Ok(())
83 }
84
85 pub fn check_pass(&mut self, prog: &mut syntax::ast::Program) -> Result<(), io::Error> {
86 let errs = tych::check::check_pass(prog);
87 if self.emit_diags(errs) {
88 return Err(io::Error::other("failed in type checking pass!"));
89 }
90 Ok(())
91 }
92
93 pub fn compile_pass(&mut self, prog: &syntax::ast::Program) -> logic::ast::Program {
94 let mut prog = logic::compile::compile_pass(prog);
95
96 logic::elaborate::elaborate_pass(&mut prog);
97
98 prog
99 }
100
101 pub fn run_backend(&self, prog: &logic::ast::Program, pipe_io: &mut PipeIO) -> Vec<usize> {
102 let mut res_vec = Vec::new();
103
104 let mut runner = interp::runner::RunnerState::new(prog, pipe_io, self.args);
105
106 for query_decl in &prog.querys {
107 for param in &query_decl.params {
108 runner.config_set_param(param);
109 }
110 let res = runner.run_iddfs_loop(query_decl.entry);
111 res_vec.push(res);
112 }
113 res_vec
114 }
115}
116
117fn create_dump_dir(src_path: &PathBuf) -> Result<PathBuf, io::Error> {
118 use std::fs;
119
120 if src_path.extension().and_then(|ext| ext.to_str()) != Some("pr") {
121 return Err(io::Error::new(
122 io::ErrorKind::InvalidInput,
123 format!("source file extension is not \".pr\"!: {src_path:?}"),
124 ));
125 }
126
127 if !src_path.exists() {
128 return Err(io::Error::new(
129 io::ErrorKind::NotFound,
130 format!("file \"{src_path:?}\" doesn't exist!"),
131 ));
132 }
133
134 if !src_path.is_file() {
135 return Err(io::Error::new(
136 io::ErrorKind::InvalidInput,
137 format!("path \"{src_path:?}\" exists, but it is not a file!"),
138 ));
139 }
140
141 let file_stem = src_path.file_stem().unwrap().to_os_string();
142
143 let mut dir_path = src_path.clone();
144 dir_path.pop();
145 dir_path.push(file_stem);
146
147 if !dir_path.exists() {
148 fs::create_dir(&dir_path)?;
149 } else if !dir_path.is_dir() {
150 return Err(io::Error::new(
151 io::ErrorKind::AlreadyExists,
152 format!("path \"{dir_path:?}\" exist, but it is not a directory!"),
153 ));
154 }
155
156 Ok(dir_path)
157}
158
159pub fn run_pipline(args: &CliArgs) -> Result<Vec<usize>, io::Error> {
160 let src_path = PathBuf::from(&args.input);
161
162 let mut pipe_io = PipeIO::empty();
163 if args.dump_file {
164 let dir_path = create_dump_dir(&src_path)?;
165
166 let output = File::create(dir_path.join("output.txt"))?;
167 if args.show_output {
168 pipe_io.output = Box::new(ReplayWriter::replay_stdout(output));
169 } else {
170 pipe_io.output = Box::new(output);
171 }
172
173 let stat = File::create(dir_path.join("stat.txt"))?;
174 if args.show_stat {
175 pipe_io.stat = Box::new(ReplayWriter::replay_stdout(stat));
176 } else {
177 pipe_io.stat = Box::new(stat);
178 }
179
180 let prog = File::create(dir_path.join("prog.txt"))?;
181 if args.show_prog {
182 pipe_io.prog = Box::new(ReplayWriter::replay_stdout(prog));
183 } else {
184 pipe_io.prog = Box::new(prog);
185 }
186 } else {
187 if args.show_output {
188 pipe_io.output = Box::new(io::stdout());
189 }
190
191 if args.show_stat {
192 pipe_io.stat = Box::new(io::stdout());
193 }
194
195 if args.show_prog {
196 pipe_io.prog = Box::new(io::stdout());
197 }
198 }
199
200 let src = std::fs::read_to_string(src_path)?;
201 let mut pipe = Pipeline::new(args);
202 match pipe.run_pipline(&src, &mut pipe_io) {
203 Ok(res) => {
204 for diag in pipe.diags.into_iter() {
205 eprintln!("{}", diag.report(&src, args.verbosity));
206 }
207 Ok(res)
208 }
209 Err(err) => {
210 for diag in pipe.diags.into_iter() {
211 eprintln!("{}", diag.report(&src, args.verbosity));
212 }
213 Err(err)
214 }
215 }
216}
217
218pub fn run_cli_pipeline() -> Result<Vec<usize>, io::Error> {
219 let args = args::parse_cli_args();
220 let res = pipeline::run_pipline(&args)?;
221 Ok(res)
222}
223
224pub fn run_test_pipeline(prog_name: PathBuf) -> Result<Vec<usize>, io::Error> {
225 let args = args::get_test_cli_args(prog_name);
226 let res = pipeline::run_pipline(&args)?;
227 Ok(res)
228}
229
230pub fn run_bench_pipeline(
231 prog_name: PathBuf,
232 heuristic: args::Heuristic,
233 depth_limit: usize,
234) -> Result<Vec<usize>, io::Error> {
235 let args = args::get_bench_cli_args(prog_name, heuristic, depth_limit);
236 let res = pipeline::run_pipline(&args)?;
237 Ok(res)
238}