Skip to content

Commit

Permalink
Use a 3-step scope change rather than an Option for the runtime.scope…
Browse files Browse the repository at this point in the history
… object. This creates 3 heap objects, but it's not too bad, and it simplifies later module loading.
  • Loading branch information
Ivorforce committed Jul 18, 2024
1 parent 16dc9f4 commit 76efdbc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
3 changes: 0 additions & 3 deletions src/interpreter/builtins/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ pub fn load(runtime: &mut Runtime) -> RResult<()> {
// -------------------------------------- Monoteny files --------------------------------------
// -------------------------------------- ------ --------------------------------------

runtime.repository.add("core", PathBuf::from("monoteny"));
runtime.get_or_load_module(&module_name("core"))?;

for function in runtime.source.module_by_name[&module_name("core.debug")].explicit_functions(&runtime.source) {
runtime.compile_server.function_inlines.insert(Rc::clone(function), match function.declared_representation.name.as_str() {
"_write_line" => inline_fn_push(OpCode::PRINT),
Expand Down
27 changes: 14 additions & 13 deletions src/interpreter/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub struct Runtime {
pub primitives: Option<HashMap<program::primitives::Type, Rc<Trait>>>,
pub traits: Option<builtins::traits::Traits>,

pub base_scope: Option<Rc<scopes::Scope<'static>>>,
pub base_scope: Rc<scopes::Scope<'static>>,
pub compile_server: CompileServer,
pub vm: VM,

Expand All @@ -44,7 +44,7 @@ impl Runtime {
Metatype: Rc::clone(&Metatype),
primitives: None,
traits: None,
base_scope: None,
base_scope: Rc::new(scopes::Scope::new()), // Temporary empty scope.
compile_server: CompileServer::new(),
vm: VM::new(),
source: Source::new(),
Expand All @@ -60,10 +60,19 @@ impl Runtime {

referencible::add_trait(&mut runtime, &mut builtins_module, None, &Metatype).unwrap();

// Load builtins
runtime.source.module_by_name.insert(builtins_module.name.clone(), builtins_module);
builtins::vm::load(&mut runtime)?;
runtime.base_scope = Rc::new(runtime.make_scope()?);

// Load core
runtime.repository.add("core", PathBuf::from("monoteny"));
runtime.get_or_load_module(&module_name("core"))?;

runtime.base_scope = Some(Rc::new(runtime.make_scope()?));
// Final scope can be loaded.
runtime.base_scope = Rc::new(runtime.make_scope()?);

// Load VM builtins.
builtins::vm::load(&mut runtime)?;

Ok(runtime)
}
Expand Down Expand Up @@ -119,15 +128,7 @@ impl Runtime {

pub fn load_ast_as_module(&mut self, syntax: &ast::Block, name: ModuleName) -> RResult<Box<Module>> {
let mut module = Box::new(Module::new(name));

if let Some(scope) = self.base_scope.as_ref() {
resolver::resolve_file(syntax, &Rc::clone(scope), self, &mut module)?;
}
else {
let scope = self.make_scope()?;
resolver::resolve_file(syntax, &scope, self, &mut module)?;
}

resolver::resolve_file(syntax, &Rc::clone(&self.base_scope), self, &mut module)?;
Ok(module)
}

Expand Down

0 comments on commit 76efdbc

Please sign in to comment.