From d2d451b1828d2bd51856e5e9d881d87a2e20eedc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bennet=20Ble=C3=9Fmann?= Date: Sat, 6 Jul 2024 03:44:53 +0200 Subject: [PATCH] adjust the generation of the LIBRARIES map pre-genrate constants instaed of driectly genrating the literal for the insert --- build/main.rs | 71 +++++++++++++++++++++++++++++++++++----------- src/machine/mod.rs | 10 +++++-- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/build/main.rs b/build/main.rs index 9bcbac15..bd254c93 100644 --- a/build/main.rs +++ b/build/main.rs @@ -11,34 +11,45 @@ use std::io::Write; use std::path::Path; use std::process::{Command, Stdio}; -fn find_prolog_files(libraries: &mut File, prefix: &str, current_dir: &Path) { +fn find_prolog_files( + libraries: &mut File, + path_prefix: &str, + const_prefix: &str, + current_dir: &Path, +) -> Vec<(String, String)> { + let mut constants = vec![]; + let entries = match current_dir.read_dir() { Ok(entries) => entries, - Err(_) => return, + Err(_) => return constants, }; for entry in entries.filter_map(Result::ok).map(|e| e.path()) { if entry.is_dir() { if let Some(file_name) = entry.file_name() { - let new_prefix = prefix.to_owned() + file_name.to_str().unwrap() + "/"; - find_prolog_files(libraries, &new_prefix, &entry); + let file_name = file_name.to_str().unwrap(); + let new_path_prefix = format!("{path_prefix}{file_name}/"); + let new_const_prefix = format!("{const_prefix}_{}", file_name.to_uppercase()); + let new_consts = + find_prolog_files(libraries, &new_path_prefix, &new_const_prefix, &entry); + constants.extend(new_consts); } } else if entry.is_file() { let ext = std::ffi::OsStr::new("pl"); if entry.extension() == Some(ext) { let contain = String::from_utf8(fs::read(&entry).unwrap()).unwrap(); let name = entry.file_stem().unwrap().to_str().unwrap(); + let lib_name = format!("{path_prefix}{name}"); + let const_name = format!("{const_prefix}_{}", name.to_uppercase()); - let line = format!( - " m.insert(\"{}\",\n{:?});\n", - prefix.to_owned() + name, - contain - ); + writeln!(libraries, "const {const_name}: &str = {contain:?};").unwrap(); - libraries.write_all(line.as_bytes()).unwrap(); + constants.push((lib_name, const_name)); } } } + + constants } fn main() { @@ -58,16 +69,42 @@ fn main() { let mut libraries = File::create(dest_path).unwrap(); let lib_path = Path::new("src/lib"); - libraries - .write_all( - b"ref_thread_local::ref_thread_local! { - pub(crate) static managed LIBRARIES: IndexMap<&'static str, &'static str> = { - let mut m = IndexMap::new();\n", + writeln!( + libraries, + "\ +use indexmap::IndexMap;\ + " + ) + .unwrap(); + + let constants = find_prolog_files(&mut libraries, "", "LIB", lib_path); + + writeln!( + libraries, + "\ +ref_thread_local::ref_thread_local! {{ + pub(crate) static managed LIBRARIES: IndexMap<&'static str, &'static str> = {{ + let mut m = IndexMap::new();" + ) + .unwrap(); + + for (name, constant) in constants { + writeln!( + libraries, + "\ + m.insert(\"{name}\",{constant});" ) .unwrap(); + } - find_prolog_files(&mut libraries, "", lib_path); - libraries.write_all(b"\n m\n };\n}\n").unwrap(); + writeln!( + libraries, + " + m + }}; +}}" + ) + .unwrap(); let instructions_path = Path::new(&out_dir).join("instructions.rs"); let mut instructions_file = File::create(&instructions_path).unwrap(); diff --git a/src/machine/mod.rs b/src/machine/mod.rs index eee60b8e..d3d22953 100644 --- a/src/machine/mod.rs +++ b/src/machine/mod.rs @@ -111,13 +111,17 @@ impl LoadContext { #[inline] fn current_dir() -> PathBuf { if !cfg!(miri) { - env::current_dir().unwrap_or(PathBuf::from("./")) + env::current_dir().unwrap_or(PathBuf::from("./")) } else { PathBuf::from("./") } } -include!(concat!(env!("OUT_DIR"), "/libraries.rs")); +mod libraries { + include!(concat!(env!("OUT_DIR"), "/libraries.rs")); +} + +pub(crate) use libraries::LIBRARIES; pub static BREAK_FROM_DISPATCH_LOOP_LOC: usize = 0; pub static INSTALL_VERIFY_ATTR_INTERRUPT: usize = 1; @@ -492,7 +496,7 @@ impl Machine { bootstrapping_compile( Stream::from_static_string( - LIBRARIES.borrow()["ops_and_meta_predicates"], + libraries::LIBRARIES.borrow()["ops_and_meta_predicates"], &mut wam.machine_st.arena, ), &mut wam, -- 2.54.0