Fix function context error

This commit is contained in:
Jeff 2023-12-22 16:12:41 -05:00
parent 364ce9cb33
commit 8369477346
4 changed files with 15 additions and 15 deletions

View File

@ -117,7 +117,7 @@ impl AbstractTree for FunctionCall {
arguments.push(value); arguments.push(value);
} }
value.as_function()?.call(&arguments, source) value.as_function()?.call(&arguments, source, context)
} }
fn expected_type(&self, context: &Map) -> Result<Type> { fn expected_type(&self, context: &Map) -> Result<Type> {

View File

@ -50,12 +50,12 @@ impl AbstractTree for ValueNode {
} }
} }
let function_context_types = Map::clone_from(context)?; let function_context = Map::clone_from(context)?;
for (parameter_name, parameter_type) in for (parameter_name, parameter_type) in
parameters.iter().zip(parameter_types.iter()) parameters.iter().zip(parameter_types.iter())
{ {
function_context_types.set( function_context.set(
parameter_name.inner().clone(), parameter_name.inner().clone(),
Value::Option(None), Value::Option(None),
Some(parameter_type.clone()), Some(parameter_type.clone()),
@ -67,7 +67,7 @@ impl AbstractTree for ValueNode {
TypeDefinition::from_syntax_node(source, return_type_node, context)?; TypeDefinition::from_syntax_node(source, return_type_node, context)?;
let body_node = child.child(child_count - 1).unwrap(); let body_node = child.child(child_count - 1).unwrap();
let body = Block::from_syntax_node(source, body_node, &function_context_types)?; let body = Block::from_syntax_node(source, body_node, &function_context)?;
let r#type = Type::Function { let r#type = Type::Function {
parameter_types, parameter_types,

View File

@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{AbstractTree, Block, Error, Identifier, Map, Result, Type, Value}; use crate::{AbstractTree, Block, Error, Identifier, Map, Result, Type, Value};
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub struct Function { pub struct Function {
parameters: Vec<Identifier>, parameters: Vec<Identifier>,
body: Block, body: Block,
@ -47,7 +47,7 @@ impl Function {
} }
} }
pub fn call(&self, arguments: &[Value], source: &str) -> Result<Value> { pub fn call(&self, arguments: &[Value], source: &str, outer_context: &Map) -> Result<Value> {
if self.parameters.len() != arguments.len() { if self.parameters.len() != arguments.len() {
return Err(Error::ExpectedFunctionArgumentAmount { return Err(Error::ExpectedFunctionArgumentAmount {
source: "unknown".to_string(), source: "unknown".to_string(),
@ -56,7 +56,7 @@ impl Function {
}); });
} }
let context = Map::new(); let context = Map::clone_from(outer_context)?;
let parameter_argument_pairs = self.parameters.iter().zip(arguments.iter()); let parameter_argument_pairs = self.parameters.iter().zip(arguments.iter());
for (identifier, value) in parameter_argument_pairs { for (identifier, value) in parameter_argument_pairs {
@ -71,14 +71,6 @@ impl Function {
} }
} }
impl PartialEq for Function {
fn eq(&self, other: &Self) -> bool {
self.r#type.eq(&other.r#type)
&& self.parameters.eq(&other.parameters)
&& self.body.eq(&other.body)
}
}
impl Display for Function { impl Display for Function {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!( write!(

View File

@ -56,6 +56,14 @@ impl Map {
Ok(previous) Ok(previous)
} }
pub fn unset_all(&self) -> Result<()> {
for (_key, (value, r#_type)) in self.variables.write()?.iter_mut() {
*value = Value::Option(None);
}
Ok(())
}
pub fn clear(&self) -> Result<()> { pub fn clear(&self) -> Result<()> {
self.variables.write()?.clear(); self.variables.write()?.clear();