Skip to main content

probl_sema/
lib.rs

1//! The middle of the Probl pipeline: name resolution, lowering to IR and
2//! liveness analysis.
3
4pub mod builtins;
5pub mod coercions;
6pub mod conjugate;
7pub mod data;
8pub mod docs;
9mod draws;
10pub mod effects;
11pub mod ir;
12pub mod liveness;
13pub mod lower;
14pub mod pretty;
15pub mod rewards;
16pub mod symbols;
17
18pub use builtins::{Builtin, Constant};
19pub use liveness::{Liveness, SlotSet, analyze};
20pub use lower::{lower, lower_with_symbols};
21
22use probl_syntax::{Diagnostic, parse_program};
23
24/// The largest source text `compile` accepts, in bytes.
25pub const MAX_SOURCE: usize = 8 * 1024 * 1024;
26
27/// Parse and lower a program, and say where its names are declared and
28/// used. The symbols are there if it parses, even with other errors.
29pub fn compile_with_symbols(src: &str) -> (Option<ir::Program>, Vec<Diagnostic>, Option<symbols::Symbols>) {
30    if src.len() > MAX_SOURCE {
31        let d = Diagnostic::error(
32            probl_syntax::Span::default(),
33            format!("the program is larger than {MAX_SOURCE} bytes"),
34        );
35        return (None, vec![d], None);
36    }
37    let (ast, mut diags) = parse_program(src);
38    if diags.iter().any(Diagnostic::is_error) {
39        return (None, diags, None);
40    }
41    let (program, mut lower_diags, symbols) = lower_with_symbols(&ast, src);
42    diags.append(&mut lower_diags);
43    diags.sort_by_key(|d| d.span.lo);
44    let program = (!diags.iter().any(Diagnostic::is_error)).then_some(program);
45    (program, diags, Some(symbols))
46}
47
48/// Parse and lower a program. The IR is only returned when there are no
49/// errors; warnings are returned either way.
50pub fn compile(src: &str) -> (Option<ir::Program>, Vec<Diagnostic>) {
51    let (program, diags, _) = compile_with_symbols(src);
52    (program, diags)
53}