Fix built-in function bug

This commit is contained in:
Jeff 2024-06-24 02:01:08 -04:00
parent ff5c4972eb
commit fbcb28ce24
2 changed files with 27 additions and 13 deletions

View File

@ -191,9 +191,11 @@ where
fn evaluate( fn evaluate(
self, self,
_: &Context, context: &Context,
manage_memory: bool, manage_memory: bool,
) -> Result<Option<Evaluation>, RuntimeError> { ) -> Result<Option<Evaluation>, RuntimeError> {
self.context.set_parent(context.clone())?;
self.function.call(&self.context, manage_memory) self.function.call(&self.context, manage_memory)
} }
@ -350,7 +352,7 @@ impl FunctionLogic for WriteLine {
) { ) {
( (
None::<array::IntoIter<Identifier, 0>>, None::<array::IntoIter<Identifier, 0>>,
Some([(Identifier::new("message"), Type::String)].into_iter()), Some([(Identifier::new("output"), Type::Any)].into_iter()),
) )
} }
@ -358,18 +360,24 @@ impl FunctionLogic for WriteLine {
Ok(None) Ok(None)
} }
fn call(self, context: &Context, _: bool) -> Result<Option<Evaluation>, RuntimeError> { fn call(
let message = context.get_value(&Identifier::new("message"))?; self,
context: &Context,
if let Some(message) = message { manage_memory: bool,
println!("{message}"); ) -> Result<Option<Evaluation>, RuntimeError> {
let position = self.0.position();
Ok(None) let evaluation = self.0.evaluate(context, manage_memory)?;
let value = if let Some(Evaluation::Return(value)) = evaluation {
value
} else { } else {
Err(RuntimeError::ValidationFailure( return Err(RuntimeError::ValidationFailure(
ValidationError::BuiltInFunctionFailure(self.0.position()), ValidationError::ExpectedExpression(position),
)) ));
} };
println!("{value}");
Ok(None)
} }
} }

View File

@ -37,6 +37,12 @@ impl Context {
Context::new(Some(self.clone())) Context::new(Some(self.clone()))
} }
pub fn set_parent(&self, parent: Context) -> Result<(), PoisonError> {
self.data.write()?.parent = Some(parent);
Ok(())
}
pub fn contains(&self, identifier: &Identifier) -> Result<bool, PoisonError> { pub fn contains(&self, identifier: &Identifier) -> Result<bool, PoisonError> {
log::trace!("Checking that {identifier} exists."); log::trace!("Checking that {identifier} exists.");