Begin new std implementation

This commit is contained in:
Jeff 2023-12-31 21:24:46 -05:00
parent 49159d379f
commit 5be7b9b73a
2 changed files with 38 additions and 1 deletions

View File

@ -11,13 +11,14 @@ mod option;
mod output;
mod packages;
mod random;
mod std;
mod r#type;
/// All built-in functions recognized by the interpreter.
///
/// This is the public interface to access built-in functions by iterating over
/// the references it holds.
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 21] = [
pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 22] = [
&assert::Assert,
&assert::AssertEqual,
&collections::Length,
@ -38,6 +39,7 @@ pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 21] = [
&random::RandomBoolean,
&random::RandomFloat,
&random::RandomInteger,
&std::Std,
&r#type::TypeFunction,
];

View File

@ -0,0 +1,35 @@
use std::sync::OnceLock;
use crate::{interpret_with_context, BuiltInFunction, Error, Map, Result, Type, Value};
static STD: OnceLock<Map> = OnceLock::new();
pub struct Std;
impl BuiltInFunction for Std {
fn name(&self) -> &'static str {
"std"
}
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
Error::expect_argument_amount(self, 0, arguments.len())?;
let std_context = STD.get_or_init(|| {
let std_source = "say_hi = () <none> { output(hi) }";
let std_context = Map::new();
interpret_with_context(std_source, std_context.clone()).unwrap();
std_context
});
Ok(Value::Map(std_context.clone()))
}
fn r#type(&self) -> Type {
Type::Function {
parameter_types: vec![],
return_type: Box::new(Type::Map),
}
}
}