Pass function tests

This commit is contained in:
Jeff 2024-03-11 14:49:44 -04:00
parent cabbf8821f
commit 764ea0550d
4 changed files with 19 additions and 8 deletions

View File

@ -42,8 +42,6 @@ impl AbstractTree for Assignment {
} }
fn validate(&self, context: &Context) -> Result<(), ValidationError> { fn validate(&self, context: &Context) -> Result<(), ValidationError> {
self.statement.validate(context)?;
let statement_type = self.statement.expected_type(context)?; let statement_type = self.statement.expected_type(context)?;
if let Some(expected) = &self.r#type { if let Some(expected) = &self.r#type {
@ -54,6 +52,8 @@ impl AbstractTree for Assignment {
context.set_type(self.identifier.clone(), statement_type)?; context.set_type(self.identifier.clone(), statement_type)?;
} }
self.statement.validate(context)?;
Ok(()) Ok(())
} }

View File

@ -84,6 +84,8 @@ impl AbstractTree for ValueNode {
function_context.set_type(identifier.clone(), r#type.clone())?; function_context.set_type(identifier.clone(), r#type.clone())?;
} }
body.validate(&function_context)?;
let actual_return_type = body.expected_type(&function_context)?; let actual_return_type = body.expected_type(&function_context)?;
return_type.check(&actual_return_type)?; return_type.check(&actual_return_type)?;

View File

@ -6,7 +6,7 @@ use std::{
use crate::{ use crate::{
abstract_tree::{Identifier, Type}, abstract_tree::{Identifier, Type},
error::RwLockPoisonError, error::RwLockPoisonError,
value::BuiltInFunction, value::{BuiltInFunction, ValueInner},
Value, Value,
}; };
@ -37,8 +37,10 @@ impl Context {
let mut new_data = BTreeMap::new(); let mut new_data = BTreeMap::new();
for (identifier, value_data) in other.inner.read()?.iter() { for (identifier, value_data) in other.inner.read()?.iter() {
if let ValueData::Type(_) = value_data { if let ValueData::Type(r#type) = value_data {
new_data.insert(identifier.clone(), value_data.clone()); if let Type::Function { .. } = r#type {
new_data.insert(identifier.clone(), value_data.clone());
}
} }
} }
@ -49,7 +51,16 @@ impl Context {
let mut new_data = BTreeMap::new(); let mut new_data = BTreeMap::new();
for (identifier, value_data) in other.inner.read()?.iter() { for (identifier, value_data) in other.inner.read()?.iter() {
new_data.insert(identifier.clone(), value_data.clone()); if let ValueData::Type(r#type) = value_data {
if let Type::Function { .. } = r#type {
new_data.insert(identifier.clone(), value_data.clone());
}
}
if let ValueData::Value(value) = value_data {
if let ValueInner::Function { .. } = value.inner().as_ref() {
new_data.insert(identifier.clone(), value_data.clone());
}
}
} }
Ok(Self::with_data(new_data)) Ok(Self::with_data(new_data))

View File

@ -94,8 +94,6 @@ fn function_context_captures_functions() {
#[test] #[test]
fn recursion() { fn recursion() {
env_logger::builder().is_test(true).try_init().unwrap();
assert_eq!( assert_eq!(
interpret( interpret(
" "