Continue with standard library
This commit is contained in:
parent
9bb4e1b944
commit
a8f840a305
@ -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()))
|
||||
|
@ -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<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)]
|
||||
pub struct Context {
|
||||
inner: Arc<RwLock<BTreeMap<Identifier, ValueData>>>,
|
||||
@ -99,6 +124,12 @@ impl Context {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn remove(&self, identifier: &Identifier) -> Result<Option<ValueData>, RwLockPoisonError> {
|
||||
let removed = self.inner.write()?.remove(identifier);
|
||||
|
||||
Ok(removed)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
|
@ -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<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)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user