From 2062ec80a73a2a3e000bfe180b50415f86ba4156 Mon Sep 17 00:00:00 2001 From: Luke Peterson Date: Mon, 23 Oct 2023 01:15:48 -0600 Subject: [PATCH] Changing init.metta to run in the runner's top-level Space, rather than as its own MeTTa module --- lib/src/metta/runner/mod.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/src/metta/runner/mod.rs b/lib/src/metta/runner/mod.rs index 0aa0e2001..b84ba89f6 100644 --- a/lib/src/metta/runner/mod.rs +++ b/lib/src/metta/runner/mod.rs @@ -6,6 +6,7 @@ use super::space::*; use super::text::{Tokenizer, SExprParser}; use super::types::validate_atom; +use core::borrow::Borrow; use std::rc::Rc; use std::path::{Path, PathBuf}; use std::collections::HashMap; @@ -113,7 +114,7 @@ impl Metta { //Run the `init.metta` file if let Some(init_meta_file) = metta.0.environment.initialization_metta_file_path() { - metta.load_module(init_meta_file.into()).unwrap(); + metta.run_from_file(init_meta_file).unwrap(); } metta } @@ -241,6 +242,14 @@ impl Metta { self.0.settings.borrow().get(key.into()).map(|a| a.to_string()) } + /// Runs the MeTTa code in the specified file + pub fn run_from_file>(&self, program_path: P) -> Result>, String> { + let path = program_path.borrow(); + let program = std::fs::read_to_string(path) + .map_err(|err| format!("Could not read file, path: {}, error: {}", path.display(), err))?; + self.run(SExprParser::new(program.as_str())) + } + pub fn run<'p, 'a: 'p>(&'a self, parser: SExprParser<'p>) -> Result>, String> { let state = RunnerState::new_with_parser(self, parser); state.run_to_completion()