From d46a592f87818467fff8c069c7df28840da43b02 Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 19 Mar 2024 19:46:41 -0400 Subject: [PATCH] Implement simple struct error --- src/abstract_tree/structure_definition.rs | 4 +++- src/abstract_tree/value_node.rs | 8 ++++++++ tests/structs.rs | 19 ++++++++++++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/abstract_tree/structure_definition.rs b/src/abstract_tree/structure_definition.rs index a192c88..ab7090b 100644 --- a/src/abstract_tree/structure_definition.rs +++ b/src/abstract_tree/structure_definition.rs @@ -22,7 +22,9 @@ impl AbstractTree for StructureDefinition { Ok(Type::None) } - fn validate(&self, _context: &Context) -> Result<(), ValidationError> { + fn validate(&self, context: &Context) -> Result<(), ValidationError> { + context.set_type(self.name.clone(), Type::Named(self.name.clone()))?; + Ok(()) } diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 2bb5d79..8567d9e 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -114,6 +114,14 @@ impl AbstractTree for ValueNode { })?; } + if let ValueNode::Structure { name, fields } = self { + let r#type = if let Some(r#type) = context.get_type(name)? { + r#type + } else { + return Err(ValidationError::TypeNotFound(name.clone())); + }; + } + Ok(()) } diff --git a/tests/structs.rs b/tests/structs.rs index 67a6c1f..db98905 100644 --- a/tests/structs.rs +++ b/tests/structs.rs @@ -1,4 +1,4 @@ -use dust_lang::{abstract_tree::Identifier, *}; +use dust_lang::{abstract_tree::Identifier, error::Error, *}; #[test] fn simple_structure() { @@ -57,3 +57,20 @@ fn nested_structure() { ))) ) } + +#[test] +fn undefined_struct() { + assert_eq!( + interpret( + " + Foo { + bar = 42 + } + " + ), + Err(vec![Error::Validation { + error: error::ValidationError::TypeNotFound(Identifier::new("Foo")), + position: (17, 82).into() + }]) + ) +}