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> {
self.statement.validate(context)?;
let statement_type = self.statement.expected_type(context)?;
if let Some(expected) = &self.r#type {
@ -54,6 +52,8 @@ impl AbstractTree for Assignment {
context.set_type(self.identifier.clone(), statement_type)?;
}
self.statement.validate(context)?;
Ok(())
}

View File

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

View File

@ -6,7 +6,7 @@ use std::{
use crate::{
abstract_tree::{Identifier, Type},
error::RwLockPoisonError,
value::BuiltInFunction,
value::{BuiltInFunction, ValueInner},
Value,
};
@ -37,8 +37,10 @@ impl Context {
let mut new_data = BTreeMap::new();
for (identifier, value_data) in other.inner.read()?.iter() {
if let ValueData::Type(_) = value_data {
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());
}
}
}
@ -49,7 +51,16 @@ impl Context {
let mut new_data = BTreeMap::new();
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))

View File

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