Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 90 additions & 21 deletions crates/steel-core/src/compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ use crate::{
parser::parser::Sources,
};

use std::{borrow::Cow, iter::Iterator};
use std::{
borrow::Cow,
iter::Iterator,
sync::{Arc, Mutex},
};
use std::{
collections::{HashMap, HashSet},
path::PathBuf,
Expand Down Expand Up @@ -315,7 +319,12 @@ pub struct Compiler {
pub(crate) macro_env: FxHashMap<InternedString, SteelMacro>,
pub(crate) module_manager: ModuleManager,
opt_level: OptLevel,
pub(crate) kernel: Option<Kernel>,

// Note: This is... a bit nasty, and most like we need
// the kernel to actually be cheaply cloneable since
// we need to move the kernel down into the proper environments.
pub(crate) kernel: Option<Arc<Mutex<Kernel>>>,

memoization_table: MemoizationTable,
mangled_identifiers: FxHashSet<InternedString>,
// Try this out?
Expand All @@ -337,6 +346,8 @@ pub struct Compiler {
// want to have the compiler share everything with the runtime.
sources: Sources,
builtin_modules: ModuleContainer,

pub(crate) read_table: steel_parser::lexer::ReadTable,
}

pub struct SerializableCompiler {
Expand Down Expand Up @@ -413,6 +424,7 @@ impl Compiler {
search_dirs: Vec::new(),
sources,
builtin_modules,
read_table: std::sync::Arc::new(std::sync::Mutex::new(HashMap::default())),
}
}

Expand All @@ -431,7 +443,7 @@ impl Compiler {
macro_env,
module_manager,
opt_level: OptLevel::Three,
kernel: Some(kernel),
kernel: Some(Arc::new(Mutex::new(kernel))),
memoization_table: MemoizationTable::new(),
mangled_identifiers: FxHashSet::default(),
lifted_kernel_environments: HashMap::new(),
Expand All @@ -441,6 +453,7 @@ impl Compiler {
search_dirs: Vec::new(),
sources,
builtin_modules,
read_table: std::sync::Arc::new(std::sync::Mutex::new(HashMap::default())),
}
}

Expand Down Expand Up @@ -516,15 +529,35 @@ impl Compiler {

let id = self.sources.add_source(expr_str.clone(), path.clone());

// let mut read_table = HashMap::new();

// Pass in the compiler as well to this function? Do something like that?
// read_table.insert(
// '◊',
// Box::new(
// |lexer: &mut steel_parser::lexer::Lexer, c: char| -> ExprKind {
// println!("Hello world!");

// ExprKind::atom("foo")
// },
// ) as _,
// );

let mut read_table = self.read_table.lock().unwrap();

// Could fail here
let parsed: std::result::Result<Vec<ExprKind>, ParseError> = path
.as_ref()
.map(|p| Parser::new_from_source(expr_str.as_ref(), p.clone(), Some(id)))
.unwrap_or_else(|| Parser::new(expr_str.as_ref(), Some(id)))
.without_lowering()
.with_read_table(Some(&mut read_table))
.map(|x| x.and_then(lower_macro_and_require_definitions))
.collect();

// Otherwise we can't continue on here
drop(read_table);

#[cfg(feature = "profiling")]
if log::log_enabled!(target: "pipeline_time", log::Level::Debug) {
log::debug!(target: "pipeline_time", "Parsing Time: {:?}", now.elapsed());
Expand Down Expand Up @@ -612,7 +645,10 @@ impl Compiler {
self.module_manager.add_module(
path,
&mut self.macro_env,
&mut self.kernel,
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
sources,
builtin_modules,
)
Expand All @@ -630,7 +666,10 @@ impl Compiler {
// #[cfg(feature = "modules")]
return self.module_manager.compile_main(
&mut self.macro_env,
&mut self.kernel,
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
&mut self.sources,
exprs,
path,
Expand Down Expand Up @@ -694,13 +733,19 @@ impl Compiler {

if let Some(kernel) = self.kernel.as_mut() {
// Label anything at the top as well - top level
kernel.load_syntax_transformers(&mut expanded_statements, "top-level".to_string())?;
kernel
.lock()
.unwrap()
.load_syntax_transformers(&mut expanded_statements, "top-level".to_string())?;
}

for expr in expanded_statements.iter_mut() {
expand_kernel_in_env(
expr,
self.kernel.as_mut(),
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
self.builtin_modules.clone(),
"top-level",
)?;
Expand All @@ -711,7 +756,10 @@ impl Compiler {
for expr in expanded_statements.iter_mut() {
expand_kernel_in_env(
expr,
self.kernel.as_mut(),
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
self.builtin_modules.clone(),
"top-level",
)?;
Expand All @@ -738,7 +786,10 @@ impl Compiler {
for (module, lifted_env) in &mut self.lifted_kernel_environments {
let changed = expand_kernel_in_env_with_change(
expr,
self.kernel.as_mut(),
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
self.builtin_modules.clone(),
&module,
)?;
Expand All @@ -750,7 +801,10 @@ impl Compiler {

expand_kernel_in_env(
expr,
self.kernel.as_mut(),
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
self.builtin_modules.clone(),
"top-level",
)?;
Expand Down Expand Up @@ -889,13 +943,19 @@ impl Compiler {

if let Some(kernel) = self.kernel.as_mut() {
// Label anything at the top as well - top level
kernel.load_syntax_transformers(&mut expanded_statements, "top-level".to_string())?;
kernel
.lock()
.unwrap()
.load_syntax_transformers(&mut expanded_statements, "top-level".to_string())?;
}

for expr in expanded_statements.iter_mut() {
expand_kernel_in_env(
expr,
self.kernel.as_mut(),
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
self.builtin_modules.clone(),
"top-level",
)?;
Expand All @@ -918,7 +978,10 @@ impl Compiler {
for (module, lifted_env) in &mut self.lifted_kernel_environments {
let changed = expand_kernel_in_env_with_change(
expr,
self.kernel.as_mut(),
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
self.builtin_modules.clone(),
&module,
)?;
Expand All @@ -930,7 +993,10 @@ impl Compiler {

expand_kernel_in_env(
expr,
self.kernel.as_mut(),
self.kernel
.as_mut()
.map(|x| x.lock().unwrap())
.as_deref_mut(),
self.builtin_modules.clone(),
"top-level",
)?;
Expand Down Expand Up @@ -1189,22 +1255,25 @@ impl Compiler {
#[cfg(feature = "profiling")]
let opt_time = Instant::now();

let mut maybe_kernel = None;

if use_kernel {
if let Some(kernel) = self.kernel.as_mut() {
kernel.load_program_for_comptime(constants.clone(), &mut expanded_statements)?;
kernel
.lock()
.unwrap()
.load_program_for_comptime(constants.clone(), &mut expanded_statements)?;
}
}

let mut kernel = self.kernel.as_mut().map(|x| x.lock().unwrap());

let mut manager = ConstantEvaluatorManager::new(
&mut self.memoization_table,
constants.clone(),
self.opt_level,
if use_kernel {
&mut self.kernel
kernel.as_deref_mut()
} else {
&mut maybe_kernel
None
},
);

Expand All @@ -1228,9 +1297,9 @@ impl Compiler {
constants,
self.opt_level,
if use_kernel {
&mut self.kernel
kernel.as_deref_mut()
} else {
&mut maybe_kernel
None
},
)
.run(expanded_statements)?;
Expand Down
Loading