}
}
-fn fix_filename(atom_tbl: TabledData<Atom>, filename: &str) -> Result<PathBuf, SessionError> {
- let mut path = PathBuf::from(filename);
-
+fn fix_filename(
+ atom_tbl: TabledData<Atom>,
+ mut path: PathBuf,
+) -> Result<PathBuf, SessionError>
+{
if !path.is_file() {
if path.extension().is_none() {
path.set_extension("pl");
wam: &mut Machine,
stream: ParsingStream<R>,
suppress_warnings: bool,
- listing_src: ClauseName,
+ listing_src: &ListingSource,
) -> Result<ClauseName, SessionError> {
// follow the operation of compile_user_module, but before
// compiling, check that a module is declared in the file. if not,
module.module_decl.name.clone()
} else {
// this impromptu definition (namely, its exports) will be filled out later.
- let module_decl = ModuleDecl { name: listing_src, exports: vec![] };
+ let module_decl = ModuleDecl { name: listing_src.name(), exports: vec![] };
- let mut module = Module::new(module_decl, wam.indices.atom_tbl.clone());
+ let mut module = Module::new(module_decl, wam.indices.atom_tbl.clone(), listing_src.clone());
let module_name = module.module_decl.name.clone();
module.is_impromptu_module = true;
pub(super)
fn load_module_from_file(
wam: &mut Machine,
- filename: &str,
+ path_buf: PathBuf,
suppress_warnings: bool,
) -> Result<ClauseName, SessionError> {
- let path = fix_filename(wam.indices.atom_tbl.clone(), filename)?;
- let filename = clause_name!(path.to_string_lossy().to_string(), wam.indices.atom_tbl);
+ let mut path_buf = fix_filename(wam.indices.atom_tbl.clone(), path_buf)?;
+ let filename = clause_name!(path_buf.to_string_lossy().to_string(), wam.indices.atom_tbl);
- let file_handle = File::open(&path).or_else(|_| {
+ let file_handle = File::open(&path_buf).or_else(|_| {
Err(SessionError::InvalidFileName(filename.clone()))
})?;
- load_module(wam, parsing_stream(file_handle), suppress_warnings, filename)
+ path_buf.pop();
+
+ let listing_src = ListingSource::from_file_and_path(filename, path_buf);
+ load_module(wam, parsing_stream(file_handle), suppress_warnings, &listing_src)
}
pub type PredicateCompileQueue = (Predicate, VecDeque<TopLevel>);
indices.op_dir = module.op_dir.clone();
indices.atom_tbl = module.atom_tbl.clone();
- let mut compiler = ListingCompiler::new(&wam.code_repo, true, module_name.clone());
+ let mut compiler = ListingCompiler::new(&wam.code_repo, true, module.listing_src.clone());
match compile_into_module_impl(wam, &mut compiler, module, src, indices) {
Ok(()) => EvalSession::EntrySuccess,
orig_goal_expansion_lens: (usize, usize),
initialization_goals: (Vec<QueryTerm>, VecDeque<TopLevel>),
suppress_warnings: bool,
- listing_src: ClauseName // a file? a module?
+ listing_src: ListingSource, // a file? a module?
}
fn add_toplevel_code(wam: &mut Machine, code: Code, indices: IndexStore) {
) -> Result<ClauseName, SessionError> {
match LIBRARIES.borrow().get(name.as_str()) {
Some(code) => {
- load_module(wam, parsing_stream(code.as_bytes()),
- suppress_warnings, name.clone())
+ let mut lib_path = current_dir();
+
+ lib_path.pop();
+ lib_path.push("lib");
+
+ let listing_src = ListingSource::from_file_and_path(name, lib_path);
+
+ load_module(
+ wam,
+ parsing_stream(code.as_bytes()),
+ suppress_warnings,
+ &listing_src,
+ )
}
None => Err(SessionError::ModuleNotFound)
}
pub fn new(
code_repo: &CodeRepo,
suppress_warnings: bool,
- listing_src: ClauseName,
+ listing_src: ListingSource,
) -> Self {
ListingCompiler {
non_counted_bt_preds: IndexSet::new(),
}
}
- Ok(self.module = Some(Module::new(module_decl, atom_tbl)))
+ let listing_src = self.listing_src.clone();
+
+ Ok(self.module = Some(Module::new(module_decl, atom_tbl, listing_src)))
} else {
Err(SessionError::from(ParserError::InvalidModuleDecl))
}
}
Declaration::UseModule(ModuleSource::File(filename)) => {
- let name = load_module_from_file(wam, filename.as_str(), true)?;
+ let mut path_buf = self.listing_src.path();
+ path_buf.push(filename.as_str());
+
+ let name = load_module_from_file(wam, path_buf, true)?;
self.use_module(name, &mut wam.code_repo, flags, &mut wam.indices, indices)
}
Declaration::UseQualifiedModule(ModuleSource::File(filename), exports) => {
- let name = load_module_from_file(wam, filename.as_str(), true)?;
+ let mut path_buf = self.listing_src.path();
+ path_buf.push(filename.as_str());
+
+ let name = load_module_from_file(wam, path_buf, true)?;
self.use_qualified_module(
name,
fn print_error(&self, e: &SessionError) {
if let &SessionError::ParserError(ref e) = e {
if let Some((line_num, _col_num)) = e.line_and_col_num() {
- println!("{}:{}: {}", self.listing_src, line_num, e.as_str());
+ println!("{}:{}: {}", self.listing_src.name(), line_num, e.as_str());
}
}
}
if module.is_impromptu_module {
add_module_code(wam, module, module_code, indices);
- let module = wam.indices.take_module(compiler.listing_src.clone()).unwrap();
+ let module = wam.indices.take_module(compiler.listing_src.name()).unwrap();
wam.indices.use_module(&mut wam.code_repo, wam.machine_st.flags, &module)?;
wam.indices.insert_module(module);
if init_goal_code.len() > 0 {
if !wam.run_init_code(init_goal_code) {
println!("Warning: initialization goal for {} failed",
- compiler.listing_src);
+ compiler.listing_src.name());
}
}
if !compiler.suppress_warnings {
issue_singleton_warnings(
- compiler.listing_src.clone(),
+ compiler.listing_src.name(),
results.top_level_terms,
);
}
pub fn compile_special_form<R: Read>(
wam: &mut Machine,
src: ParsingStream<R>,
- listing_src: ClauseName,
+ listing_src: ListingSource,
) -> Result<usize, SessionError> {
let mut indices = default_index_store!(wam.indices.atom_tbl.clone());
setup_indices(wam, clause_name!("builtins"), &mut indices)?;
src: ParsingStream<R>,
indices: IndexStore,
suppress_warnings: bool,
- listing_src: ClauseName,
+ listing_src: ListingSource,
) -> EvalSession {
let mut compiler = ListingCompiler::new(&wam.code_repo, suppress_warnings, listing_src);
wam: &mut Machine,
src: ParsingStream<R>,
suppress_warnings: bool,
- listing_src: ClauseName,
+ listing_src: ListingSource,
) -> EvalSession {
let mut indices = default_index_store!(wam.indices.atom_tbl.clone());
try_eval_session!(setup_indices(wam, clause_name!("builtins"), &mut indices));
use std::io::Read;
use std::mem;
use std::ops::Index;
+use std::path::PathBuf;
use std::rc::Rc;
use std::sync::atomic::AtomicBool;
}
}
+#[inline]
+fn current_dir() -> std::path::PathBuf {
+ let mut path_buf = std::path::PathBuf::from(file!());
+ path_buf.pop();
+ path_buf
+}
+
include!(concat!(env!("OUT_DIR"), "/libraries.rs"));
static TOPLEVEL: &str = include_str!("../toplevel.pl");
impl Machine {
- fn compile_special_forms(&mut self) {
- let verify_attrs_src = clause_name!("verify_attrs.pl");
- let project_attrs_src = clause_name!("project_attributes.pl");
+ fn compile_special_forms(&mut self)
+ {
+ let current_dir = current_dir();
+
+ let verify_attrs_src = ListingSource::from_file_and_path(
+ clause_name!("attributed_variables.pl"),
+ current_dir.clone(),
+ );
match compile_special_form(self, parsing_stream(VERIFY_ATTRS.as_bytes()), verify_attrs_src)
{
panic!("Machine::compile_special_forms() failed at VERIFY_ATTRS"),
}
+ let project_attrs_src = ListingSource::from_file_and_path(
+ clause_name!("project_attributes.pl"),
+ current_dir,
+ );
+
match compile_special_form(self, parsing_stream(PROJECT_ATTRS.as_bytes()), project_attrs_src)
{
Ok(p) => {
fn compile_top_level(&mut self) -> Result<(), SessionError>
{
self.toplevel_idx = self.code_repo.code.len();
- compile_user_module(self, parsing_stream(TOPLEVEL.as_bytes()),
- true, clause_name!("toplevel.pl"));
+
+ let mut current_dir = current_dir();
+ current_dir.pop();
+
+ let top_lvl_src = ListingSource::from_file_and_path(
+ clause_name!("toplevel.pl"),
+ current_dir,
+ );
+
+ compile_user_module(
+ self,
+ parsing_stream(TOPLEVEL.as_bytes()),
+ true,
+ top_lvl_src,
+ );
if let Some(module) = self.indices.take_module(clause_name!("$toplevel")) {
self.indices.use_module(
Err(_) => return,
};
- compile_user_module(self, file_src, true,
- clause_name!("$HOME/.scryerrc"));
+ let rc_src = ListingSource::from_file_and_path(
+ clause_name!(".scryerrc"),
+ path.to_path_buf(),
+ );
+
+ compile_user_module(self, file_src, true, rc_src);
}
}
};
let atom_tbl = wam.indices.atom_tbl.clone();
+ let mut lib_path = current_dir();
+
+ lib_path.pop();
+ lib_path.push("lib");
wam.indices.add_term_and_goal_expansion_indices();
-
+
compile_listing(
&mut wam,
parsing_stream(BUILTINS.as_bytes()),
default_index_store!(atom_tbl.clone()),
true,
- clause_name!("builtins.pl"),
+ ListingSource::from_file_and_path(
+ clause_name!("builtins.pl"),
+ lib_path.clone(),
+ ),
);
wam.compile_special_forms();
compile_user_module(&mut wam, parsing_stream(ERROR.as_bytes()), true,
- clause_name!("error"));
+ ListingSource::from_file_and_path(
+ clause_name!("error"),
+ lib_path.clone(),
+ )
+ );
compile_user_module(&mut wam, parsing_stream(LISTS.as_bytes()), true,
- clause_name!("lists"));
+ ListingSource::from_file_and_path(
+ clause_name!("lists"),
+ lib_path.clone(),
+ ),
+ );
compile_user_module(&mut wam, parsing_stream(NON_ISO.as_bytes()), true,
- clause_name!("non_iso"));
+ ListingSource::from_file_and_path(
+ clause_name!("non_iso"),
+ lib_path.clone(),
+ )
+ );
compile_user_module(&mut wam, parsing_stream(SI.as_bytes()), true,
- clause_name!("si"));
+ ListingSource::from_file_and_path(
+ clause_name!("si"),
+ lib_path.clone(),
+ )
+ );
if wam.compile_top_level().is_err() {
panic!("Loading '$toplevel' module failed");
load_library(self, name, false)
},
ModuleSource::File(name) =>
- load_module_from_file(self, name.as_str(), false)
+ load_module_from_file(self, PathBuf::from(name.as_str()), false)
};
let result = load_result.and_then(|name| {
load_library(self, name, false)
},
ModuleSource::File(name) =>
- load_module_from_file(self, name.as_str(), false)
+ load_module_from_file(self, PathBuf::from(name.as_str()), false)
};
let result = load_result.and_then(|name| {
fn handle_toplevel_command(&mut self, code_ptr: REPLCodePtr, p: LocalCodePtr) {
match code_ptr {
REPLCodePtr::CompileBatch => {
- let user_src = clause_name!("user");
+ let user_src = ListingSource::User;
let src = readline::input_stream();
readline::set_prompt(false);