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