Skip to content

Commit

Permalink
Changing init.metta to run in the runner's top-level Space, rather th…
Browse files Browse the repository at this point in the history
…an as its own MeTTa module
  • Loading branch information
luketpeterson committed Oct 23, 2023
1 parent fad8565 commit 2062ec8
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/src/metta/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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<P: Borrow<Path>>(&self, program_path: P) -> Result<Vec<Vec<Atom>>, 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<Vec<Vec<Atom>>, String> {
let state = RunnerState::new_with_parser(self, parser);
state.run_to_completion()
Expand Down

0 comments on commit 2062ec8

Please sign in to comment.