1
0

Continue with standard library

This commit is contained in:
Jeff 2024-03-23 08:47:57 -04:00
parent 9bb4e1b944
commit a8f840a305
3 changed files with 37 additions and 15 deletions

View File

@ -111,6 +111,8 @@ impl AbstractNode for MapIndex {
if let (ValueInner::Map(map), Expression::Identifier(identifier)) = if let (ValueInner::Map(map), Expression::Identifier(identifier)) =
(collection.inner().as_ref(), &self.right.node) (collection.inner().as_ref(), &self.right.node)
{ {
println!("{map:?} {identifier}");
let action = map let action = map
.get(identifier) .get(identifier)
.map(|value| Action::Return(value.clone())) .map(|value| Action::Return(value.clone()))

View File

@ -1,14 +1,39 @@
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
sync::{Arc, RwLock, RwLockReadGuard}, sync::{Arc, OnceLock, RwLock, RwLockReadGuard},
}; };
use crate::{ use crate::{
abstract_tree::{Identifier, Type}, abstract_tree::{Identifier, Type},
built_in_functions::BUILT_IN_FUNCTIONS,
error::{RwLockPoisonError, ValidationError}, error::{RwLockPoisonError, ValidationError},
Value, Interpreter, Value,
}; };
static STD_CONTEXT: OnceLock<Context> = 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)] #[derive(Clone, Debug)]
pub struct Context { pub struct Context {
inner: Arc<RwLock<BTreeMap<Identifier, ValueData>>>, inner: Arc<RwLock<BTreeMap<Identifier, ValueData>>>,
@ -99,6 +124,12 @@ impl Context {
Ok(()) Ok(())
} }
pub fn remove(&self, identifier: &Identifier) -> Result<Option<ValueData>, RwLockPoisonError> {
let removed = self.inner.write()?.remove(identifier);
Ok(removed)
}
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]

View File

@ -6,26 +6,15 @@ pub mod lexer;
pub mod parser; pub mod parser;
pub mod value; pub mod value;
use abstract_tree::Identifier; use context::{std_context, Context};
use built_in_functions::BUILT_IN_FUNCTIONS;
use context::Context;
use error::Error; use error::Error;
use lexer::lex; use lexer::lex;
use parser::parse; use parser::parse;
pub use value::Value; pub use value::Value;
pub fn interpret(source: &str) -> Result<Option<Value>, Vec<Error>> { pub fn interpret(source: &str) -> Result<Option<Value>, Vec<Error>> {
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) interpreter.run(source)
} }