Add test; Fix struct type resolution
This commit is contained in:
parent
21fea2b43f
commit
d076a329d2
@ -1,7 +1,5 @@
|
||||
use std::{cmp::Ordering, collections::BTreeMap, ops::Range};
|
||||
|
||||
use chumsky::container::Container;
|
||||
|
||||
use crate::{
|
||||
context::Context,
|
||||
error::{RuntimeError, ValidationError},
|
||||
|
@ -177,6 +177,7 @@ impl Error {
|
||||
ValidationError::ExpectedValue(_) => todo!(),
|
||||
ValidationError::PropertyNotFound { .. } => todo!(),
|
||||
ValidationError::WrongArguments { .. } => todo!(),
|
||||
ValidationError::TypeNotFound(_) => todo!(),
|
||||
}
|
||||
}
|
||||
|
||||
@ -279,6 +280,7 @@ pub enum ValidationError {
|
||||
actual: Vec<Type>,
|
||||
},
|
||||
VariableNotFound(Identifier),
|
||||
TypeNotFound(Identifier),
|
||||
PropertyNotFound {
|
||||
identifier: Identifier,
|
||||
position: SourcePosition,
|
||||
|
33
src/value.rs
33
src/value.rs
@ -15,7 +15,7 @@ use stanza::{
|
||||
};
|
||||
|
||||
use crate::{
|
||||
abstract_tree::{AbstractTree, Action, Block, Expression, Identifier, Type, WithPosition},
|
||||
abstract_tree::{AbstractTree, Action, Block, Identifier, Type, WithPosition},
|
||||
context::Context,
|
||||
error::{RuntimeError, ValidationError},
|
||||
};
|
||||
@ -106,22 +106,11 @@ impl Value {
|
||||
},
|
||||
Function::BuiltIn(built_in_function) => built_in_function.r#type(),
|
||||
},
|
||||
ValueInner::Structure {
|
||||
name,
|
||||
fields: expressions,
|
||||
} => {
|
||||
let mut fields = Vec::with_capacity(expressions.len());
|
||||
|
||||
for (identifier, value) in expressions {
|
||||
fields.push((
|
||||
identifier.clone(),
|
||||
value.r#type(context)?.with_position((0, 0)),
|
||||
));
|
||||
}
|
||||
|
||||
Type::Structure {
|
||||
name: name.clone(),
|
||||
fields,
|
||||
ValueInner::Structure { name, .. } => {
|
||||
if let Some(r#type) = context.get_type(name)? {
|
||||
r#type
|
||||
} else {
|
||||
return Err(ValidationError::TypeNotFound(name.clone()));
|
||||
}
|
||||
}
|
||||
};
|
||||
@ -200,7 +189,15 @@ impl Display for Value {
|
||||
ValueInner::Function(Function::BuiltIn(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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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())),
|
||||
]
|
||||
)))
|
||||
)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user