1
0

Fix inheritance; Add a test

This commit is contained in:
Jeff 2024-07-09 23:01:54 -04:00
parent dd72faf7c8
commit 48f3ccdd58
2 changed files with 58 additions and 7 deletions

View File

@ -13,6 +13,7 @@ use crate::{
error::{PoisonError, ValidationError}, error::{PoisonError, ValidationError},
identifier::Identifier, identifier::Identifier,
standard_library::core_context, standard_library::core_context,
value::ValueInner,
Value, Value,
}; };
@ -55,11 +56,37 @@ impl Context {
if let (Ok(mut self_variables), Ok(other_variables)) = if let (Ok(mut self_variables), Ok(other_variables)) =
(get_self_variables, get_other_variables) (get_self_variables, get_other_variables)
{ {
self_variables.extend(other_variables.iter().map(|(identifier, data)| { self_variables.extend(other_variables.iter().filter_map(
|(identifier, (variable, usage, position))| {
trace!("Inheriting {identifier}"); 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(()) Ok(())

View File

@ -1,5 +1,27 @@
use dust_lang::*; 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] #[test]
fn function_call_with_type_argument() { fn function_call_with_type_argument() {
assert_eq!( assert_eq!(
@ -74,14 +96,16 @@ fn recursion() {
"test", "test",
" "
fib = fn (i: int) -> int { fib = fn (i: int) -> int {
if i <= 1 { if i < 0 {
1 0
} else if i <= 1 {
i
} else { } else {
fib(i - 1) + fib(i - 2) fib(i - 1) + fib(i - 2)
} }
} }
fib(8) fib(4)
" "
), ),
Ok(Some(Value::integer(13))) Ok(Some(Value::integer(13)))