From a8f840a305f1446af865558a0713b9d62b95ee86 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sat, 23 Mar 2024 08:47:57 -0400 Subject: [PATCH] Continue with standard library --- dust-lang/src/abstract_tree/map_index.rs | 2 ++ dust-lang/src/context.rs | 35 ++++++++++++++++++++++-- dust-lang/src/lib.rs | 15 ++-------- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/dust-lang/src/abstract_tree/map_index.rs b/dust-lang/src/abstract_tree/map_index.rs index ba31f5a..5a08c7e 100644 --- a/dust-lang/src/abstract_tree/map_index.rs +++ b/dust-lang/src/abstract_tree/map_index.rs @@ -111,6 +111,8 @@ impl AbstractNode for MapIndex { if let (ValueInner::Map(map), Expression::Identifier(identifier)) = (collection.inner().as_ref(), &self.right.node) { + println!("{map:?} {identifier}"); + let action = map .get(identifier) .map(|value| Action::Return(value.clone())) diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index 56b7a16..24d0726 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -1,14 +1,39 @@ use std::{ collections::BTreeMap, - sync::{Arc, RwLock, RwLockReadGuard}, + sync::{Arc, OnceLock, RwLock, RwLockReadGuard}, }; use crate::{ abstract_tree::{Identifier, Type}, + built_in_functions::BUILT_IN_FUNCTIONS, error::{RwLockPoisonError, ValidationError}, - Value, + Interpreter, Value, }; +static STD_CONTEXT: OnceLock = OnceLock::new(); + +pub fn std_context<'a>() -> &'a Context { + STD_CONTEXT.get_or_init(|| { + let mut data = BTreeMap::new(); + + for function in BUILT_IN_FUNCTIONS { + data.insert( + Identifier::new(function.name()), + ValueData::Value(function.as_value()), + ); + } + + let context = Context::with_data(data); + let mut interpreter = Interpreter::new(context.clone()); + + interpreter.run(include_str!("../../std/io.ds")).unwrap(); + + context.remove(&Identifier::new("write_line")).unwrap(); + + context + }) +} + #[derive(Clone, Debug)] pub struct Context { inner: Arc>>, @@ -99,6 +124,12 @@ impl Context { Ok(()) } + + pub fn remove(&self, identifier: &Identifier) -> Result, RwLockPoisonError> { + let removed = self.inner.write()?.remove(identifier); + + Ok(removed) + } } #[derive(Clone, Debug, PartialEq)] diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index e67d8ac..5e4f197 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -6,26 +6,15 @@ pub mod lexer; pub mod parser; pub mod value; -use abstract_tree::Identifier; -use built_in_functions::BUILT_IN_FUNCTIONS; -use context::Context; +use context::{std_context, Context}; use error::Error; use lexer::lex; use parser::parse; pub use value::Value; pub fn interpret(source: &str) -> Result, Vec> { - let context = Context::new(); + let mut interpreter = Interpreter::new(std_context().clone()); - for function in BUILT_IN_FUNCTIONS { - context - .set_value(Identifier::new(function.name()), function.as_value()) - .unwrap(); - } - - let mut interpreter = Interpreter::new(context); - - interpreter.run(include_str!("../../std/io.ds"))?; interpreter.run(source) }