Skip to content

Commit

Permalink
feat: Allow an AnalyzerConfig to be passed when compiling the standar…
Browse files Browse the repository at this point in the history
…d library
  • Loading branch information
Markus Westerlind committed May 20, 2022
1 parent f9d68cf commit a24a944
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
9 changes: 7 additions & 2 deletions libflux/flux-core/src/bin/analyze_query_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ fn main() -> Result<()> {

let connection = rusqlite::Connection::open(&app.database)?;

let config = semantic::AnalyzerConfig {
features: vec![semantic::Feature::LabelPolymorphism],
};

let stdlib_path = PathBuf::from("../stdlib");
let (prelude, imports, _sem_pkgs) = semantic::bootstrap::infer_stdlib_dir(&stdlib_path)?;
let (prelude, imports, _sem_pkgs) =
semantic::bootstrap::infer_stdlib_dir(&stdlib_path, config.clone())?;

let mut analyzer = Analyzer::new((&prelude).into(), &imports, Default::default());
let mut analyzer = Analyzer::new((&prelude).into(), &imports, config);
for source in connection
.prepare("SELECT source FROM query limit 100")?
.query_map([], |row| row.get(0))?
Expand Down
17 changes: 12 additions & 5 deletions libflux/flux-core/src/semantic/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use crate::{
types::{
MonoType, PolyType, PolyTypeHashMap, Record, RecordLabel, SemanticMap, Tvar, TvarKinds,
},
Analyzer, PackageExports,
Analyzer, AnalyzerConfig, PackageExports,
},
};

Expand All @@ -43,10 +43,16 @@ pub type SemanticPackageMap = SemanticMap<String, Package>;
/// Infers the Flux standard library given the path to the source code.
/// The prelude and the imports are returned.
#[allow(clippy::type_complexity)]
pub fn infer_stdlib_dir(path: &Path) -> Result<(PackageExports, Packages, SemanticPackageMap)> {
pub fn infer_stdlib_dir(
path: &Path,
config: AnalyzerConfig,
) -> Result<(PackageExports, Packages, SemanticPackageMap)> {
let ast_packages = parse_dir(path)?;

let mut infer_state = InferState::default();
let mut infer_state = InferState {
config,
..InferState::default()
};
let prelude = infer_state.infer_pre(&ast_packages)?;
infer_state.infer_std(&ast_packages, &prelude)?;

Expand Down Expand Up @@ -160,6 +166,7 @@ struct InferState {
// types available for import
imports: Packages,
sem_pkg_map: SemanticPackageMap,
config: AnalyzerConfig,
}

impl InferState {
Expand Down Expand Up @@ -247,7 +254,7 @@ impl InferState {
.ok_or_else(|| anyhow!(r#"package import "{}" not found"#, name))?;

let env = Environment::new(prelude.into());
let mut analyzer = Analyzer::new_with_defaults(env, &mut self.imports);
let mut analyzer = Analyzer::new(env, &mut self.imports, self.config.clone());
let (exports, sem_pkg) = analyzer.analyze_ast(file).map_err(|mut err| {
if err.error.source.is_none() {
err.error.source = file.base.location.source.clone();
Expand Down Expand Up @@ -353,7 +360,7 @@ pub fn stdlib(dir: &Path) -> Result<(PackageExports, FileSystemImporter<StdFS>)>

/// Compiles the stdlib found at the srcdir into the outdir.
pub fn compile_stdlib(srcdir: &Path, outdir: &Path) -> Result<()> {
let (_, imports, mut sem_pkgs) = infer_stdlib_dir(srcdir)?;
let (_, imports, mut sem_pkgs) = infer_stdlib_dir(srcdir, AnalyzerConfig::default())?;
// Write each file as compiled module
for (path, exports) in &imports {
if let Some(code) = sem_pkgs.remove(path) {
Expand Down
2 changes: 1 addition & 1 deletion libflux/flux-core/src/semantic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ impl std::str::FromStr for Feature {
}

/// A set of configuration options for the behavior of an Analyzer.
#[derive(Default, Debug)]
#[derive(Clone, Default, Debug)]
pub struct AnalyzerConfig {
/// Features used in the flux compiler
pub features: Vec<Feature>,
Expand Down

0 comments on commit a24a944

Please sign in to comment.