adjust the generation of the LIBRARIES map

pre-genrate constants instaed of driectly genrating the literal for the insert
This commit is contained in:
Bennet Bleßmann
2024-07-06 03:44:53 +02:00
parent 52fa51853e
commit d2d451b182
2 changed files with 61 additions and 20 deletions

View File

@@ -11,34 +11,45 @@ use std::io::Write;
use std::path::Path; use std::path::Path;
use std::process::{Command, Stdio}; 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() { let entries = match current_dir.read_dir() {
Ok(entries) => entries, Ok(entries) => entries,
Err(_) => return, Err(_) => return constants,
}; };
for entry in entries.filter_map(Result::ok).map(|e| e.path()) { for entry in entries.filter_map(Result::ok).map(|e| e.path()) {
if entry.is_dir() { if entry.is_dir() {
if let Some(file_name) = entry.file_name() { if let Some(file_name) = entry.file_name() {
let new_prefix = prefix.to_owned() + file_name.to_str().unwrap() + "/"; let file_name = file_name.to_str().unwrap();
find_prolog_files(libraries, &new_prefix, &entry); 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() { } else if entry.is_file() {
let ext = std::ffi::OsStr::new("pl"); let ext = std::ffi::OsStr::new("pl");
if entry.extension() == Some(ext) { if entry.extension() == Some(ext) {
let contain = String::from_utf8(fs::read(&entry).unwrap()).unwrap(); let contain = String::from_utf8(fs::read(&entry).unwrap()).unwrap();
let name = entry.file_stem().unwrap().to_str().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!( writeln!(libraries, "const {const_name}: &str = {contain:?};").unwrap();
" m.insert(\"{}\",\n{:?});\n",
prefix.to_owned() + name,
contain
);
libraries.write_all(line.as_bytes()).unwrap(); constants.push((lib_name, const_name));
} }
} }
} }
constants
} }
fn main() { fn main() {
@@ -58,16 +69,42 @@ fn main() {
let mut libraries = File::create(dest_path).unwrap(); let mut libraries = File::create(dest_path).unwrap();
let lib_path = Path::new("src/lib"); let lib_path = Path::new("src/lib");
libraries writeln!(
.write_all( libraries,
b"ref_thread_local::ref_thread_local! { "\
pub(crate) static managed LIBRARIES: IndexMap<&'static str, &'static str> = { use indexmap::IndexMap;\
let mut m = IndexMap::new();\n", "
) )
.unwrap(); .unwrap();
find_prolog_files(&mut libraries, "", lib_path); let constants = find_prolog_files(&mut libraries, "", "LIB", lib_path);
libraries.write_all(b"\n m\n };\n}\n").unwrap();
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();
}
writeln!(
libraries,
"
m
}};
}}"
)
.unwrap();
let instructions_path = Path::new(&out_dir).join("instructions.rs"); let instructions_path = Path::new(&out_dir).join("instructions.rs");
let mut instructions_file = File::create(&instructions_path).unwrap(); let mut instructions_file = File::create(&instructions_path).unwrap();

View File

@@ -117,7 +117,11 @@ fn current_dir() -> PathBuf {
} }
} }
mod libraries {
include!(concat!(env!("OUT_DIR"), "/libraries.rs")); include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
}
pub(crate) use libraries::LIBRARIES;
pub static BREAK_FROM_DISPATCH_LOOP_LOC: usize = 0; pub static BREAK_FROM_DISPATCH_LOOP_LOC: usize = 0;
pub static INSTALL_VERIFY_ATTR_INTERRUPT: usize = 1; pub static INSTALL_VERIFY_ATTR_INTERRUPT: usize = 1;
@@ -492,7 +496,7 @@ impl Machine {
bootstrapping_compile( bootstrapping_compile(
Stream::from_static_string( Stream::from_static_string(
LIBRARIES.borrow()["ops_and_meta_predicates"], libraries::LIBRARIES.borrow()["ops_and_meta_predicates"],
&mut wam.machine_st.arena, &mut wam.machine_st.arena,
), ),
&mut wam, &mut wam,