1
0

Add test; Fix struct type resolution

This commit is contained in:
Jeff 2024-03-19 19:33:02 -04:00
parent 21fea2b43f
commit d076a329d2
4 changed files with 46 additions and 20 deletions

View File

@ -1,7 +1,5 @@
use std::{cmp::Ordering, collections::BTreeMap, ops::Range}; use std::{cmp::Ordering, collections::BTreeMap, ops::Range};
use chumsky::container::Container;
use crate::{ use crate::{
context::Context, context::Context,
error::{RuntimeError, ValidationError}, error::{RuntimeError, ValidationError},

View File

@ -177,6 +177,7 @@ impl Error {
ValidationError::ExpectedValue(_) => todo!(), ValidationError::ExpectedValue(_) => todo!(),
ValidationError::PropertyNotFound { .. } => todo!(), ValidationError::PropertyNotFound { .. } => todo!(),
ValidationError::WrongArguments { .. } => todo!(), ValidationError::WrongArguments { .. } => todo!(),
ValidationError::TypeNotFound(_) => todo!(),
} }
} }
@ -279,6 +280,7 @@ pub enum ValidationError {
actual: Vec<Type>, actual: Vec<Type>,
}, },
VariableNotFound(Identifier), VariableNotFound(Identifier),
TypeNotFound(Identifier),
PropertyNotFound { PropertyNotFound {
identifier: Identifier, identifier: Identifier,
position: SourcePosition, position: SourcePosition,

View File

@ -15,7 +15,7 @@ use stanza::{
}; };
use crate::{ use crate::{
abstract_tree::{AbstractTree, Action, Block, Expression, Identifier, Type, WithPosition}, abstract_tree::{AbstractTree, Action, Block, Identifier, Type, WithPosition},
context::Context, context::Context,
error::{RuntimeError, ValidationError}, error::{RuntimeError, ValidationError},
}; };
@ -106,22 +106,11 @@ impl Value {
}, },
Function::BuiltIn(built_in_function) => built_in_function.r#type(), Function::BuiltIn(built_in_function) => built_in_function.r#type(),
}, },
ValueInner::Structure { ValueInner::Structure { name, .. } => {
name, if let Some(r#type) = context.get_type(name)? {
fields: expressions, r#type
} => { } else {
let mut fields = Vec::with_capacity(expressions.len()); return Err(ValidationError::TypeNotFound(name.clone()));
for (identifier, value) in expressions {
fields.push((
identifier.clone(),
value.r#type(context)?.with_position((0, 0)),
));
}
Type::Structure {
name: name.clone(),
fields,
} }
} }
}; };
@ -200,7 +189,15 @@ impl Display for Value {
ValueInner::Function(Function::BuiltIn(built_in_function)) => { ValueInner::Function(Function::BuiltIn(built_in_function)) => {
write!(f, "{built_in_function}") write!(f, "{built_in_function}")
} }
ValueInner::Structure { name, fields } => todo!(), ValueInner::Structure { name, fields } => {
let mut table = create_table();
for (identifier, value) in fields {
table = table.with_row([identifier.as_str(), &value.to_string()]);
}
write!(f, "{name}\n{}", Console::default().render(&table))
}
} }
} }
} }

View File

@ -25,3 +25,32 @@ fn simple_structure() {
))) )))
) )
} }
#[test]
fn nested_structure() {
assert_eq!(
interpret(
"
struct Bar {
baz : int
}
struct Foo {
bar : Bar
}
Foo {
bar = Baz {
baz = 42
}
}
"
),
Ok(Some(Value::structure(
Identifier::new("Foo"),
vec![
(Identifier::new("bar"), Value::integer(42)),
(Identifier::new("baz"), Value::string("hiya".to_string())),
]
)))
)
}