diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index 59150c7..f7004ee 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -13,6 +13,7 @@ use crate::{ error::{PoisonError, ValidationError}, identifier::Identifier, standard_library::core_context, + value::ValueInner, Value, }; @@ -55,11 +56,37 @@ impl Context { if let (Ok(mut self_variables), Ok(other_variables)) = (get_self_variables, get_other_variables) { - self_variables.extend(other_variables.iter().map(|(identifier, data)| { - trace!("Inheriting {identifier}"); + self_variables.extend(other_variables.iter().filter_map( + |(identifier, (variable, usage, position))| { + trace!("Inheriting {identifier}"); - (identifier.clone(), data.clone()) - })); + if let VariableData::Type(r#type) = variable { + match r#type { + Type::Enum { .. } | Type::Function { .. } | Type::Structure { .. } => { + return Some(( + identifier.clone(), + (variable.clone(), usage.clone(), *position), + )) + } + _ => {} + } + } + + if let VariableData::Value(value) = variable { + match value.inner().as_ref() { + ValueInner::BuiltInFunction(_) | ValueInner::Function(_) => { + return Some(( + identifier.clone(), + (variable.clone(), usage.clone(), *position), + )) + } + _ => {} + } + } + + None + }, + )); } Ok(()) diff --git a/dust-lang/tests/functions.rs b/dust-lang/tests/functions.rs index 930f6a7..82da220 100644 --- a/dust-lang/tests/functions.rs +++ b/dust-lang/tests/functions.rs @@ -1,5 +1,27 @@ use dust_lang::*; +#[test] +fn function_scope() { + assert_eq!( + interpret( + "test", + " + x = 2 + + foo = fn () -> int { + x = 42 + x + } + + x = 1 + + foo() + " + ), + Ok(Some(Value::integer(42))) + ); +} + #[test] fn function_call_with_type_argument() { assert_eq!( @@ -74,14 +96,16 @@ fn recursion() { "test", " fib = fn (i: int) -> int { - if i <= 1 { - 1 + if i < 0 { + 0 + } else if i <= 1 { + i } else { fib(i - 1) + fib(i - 2) } } - fib(8) + fib(4) " ), Ok(Some(Value::integer(13)))