use crate::types::Effect;
use std::path::PathBuf;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum OptimizationLevel {
O0,
O1,
O2,
#[default]
O3,
}
#[derive(Debug, Clone)]
pub struct ExternalBuiltin {
pub seq_name: String,
pub symbol: String,
pub effect: Option<Effect>,
}
impl ExternalBuiltin {
fn validate_symbol(symbol: &str) -> Result<(), String> {
if symbol.is_empty() {
return Err("Symbol name cannot be empty".to_string());
}
for c in symbol.chars() {
if !c.is_alphanumeric() && c != '_' && c != '.' {
return Err(format!(
"Invalid character '{}' in symbol '{}'. \
Symbols may only contain alphanumeric characters, underscores, and periods.",
c, symbol
));
}
}
Ok(())
}
#[deprecated(
since = "2.0.0",
note = "Use with_effect instead - effects are now required"
)]
pub fn new(seq_name: impl Into<String>, symbol: impl Into<String>) -> Self {
let symbol = symbol.into();
Self::validate_symbol(&symbol).expect("Invalid symbol name");
ExternalBuiltin {
seq_name: seq_name.into(),
symbol,
effect: None,
}
}
pub fn with_effect(
seq_name: impl Into<String>,
symbol: impl Into<String>,
effect: Effect,
) -> Self {
let symbol = symbol.into();
Self::validate_symbol(&symbol).expect("Invalid symbol name");
ExternalBuiltin {
seq_name: seq_name.into(),
symbol,
effect: Some(effect),
}
}
}
#[derive(Debug, Clone, Default)]
pub struct CompilerConfig {
pub external_builtins: Vec<ExternalBuiltin>,
pub library_paths: Vec<String>,
pub libraries: Vec<String>,
pub ffi_manifest_paths: Vec<PathBuf>,
pub pure_inline_test: bool,
pub optimization_level: OptimizationLevel,
pub instrument: bool,
}
impl CompilerConfig {
pub fn new() -> Self {
CompilerConfig::default()
}
pub fn with_builtin(mut self, builtin: ExternalBuiltin) -> Self {
self.external_builtins.push(builtin);
self
}
pub fn with_builtins(mut self, builtins: impl IntoIterator<Item = ExternalBuiltin>) -> Self {
self.external_builtins.extend(builtins);
self
}
pub fn with_library_path(mut self, path: impl Into<String>) -> Self {
self.library_paths.push(path.into());
self
}
pub fn with_library(mut self, lib: impl Into<String>) -> Self {
self.libraries.push(lib.into());
self
}
pub fn with_ffi_manifest(mut self, path: impl Into<PathBuf>) -> Self {
self.ffi_manifest_paths.push(path.into());
self
}
pub fn with_ffi_manifests(mut self, paths: impl IntoIterator<Item = PathBuf>) -> Self {
self.ffi_manifest_paths.extend(paths);
self
}
pub fn with_optimization_level(mut self, level: OptimizationLevel) -> Self {
self.optimization_level = level;
self
}
pub fn external_names(&self) -> Vec<&str> {
self.external_builtins
.iter()
.map(|b| b.seq_name.as_str())
.collect()
}
}
#[cfg(test)]
mod tests;