From 97319d28b2373c1656a309d920d5d107a6fb4408 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 14 Feb 2024 22:46:14 -0500 Subject: [PATCH] Implement custom types; Add test --- src/abstract_tree/type.rs | 4 +++- tests/enums.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/abstract_tree/type.rs b/src/abstract_tree/type.rs index c8a68c8..88672e2 100644 --- a/src/abstract_tree/type.rs +++ b/src/abstract_tree/type.rs @@ -136,6 +136,7 @@ impl AbstractTree for Type { let type_node = node.child(0).unwrap(); let r#type = match type_node.kind() { + "identifier" => Type::Custom(Identifier::from_syntax(type_node, _source, _context)?), "[" => { let item_type_node = node.child(1).unwrap(); let item_type = Type::from_syntax(item_type_node, _source, _context)?; @@ -185,7 +186,8 @@ impl AbstractTree for Type { } _ => { return Err(SyntaxError::UnexpectedSyntaxNode { - expected: "any, bool, float, int, num, str, option, (, [ or {".to_string(), + expected: "any, bool, float, int, num, str, option, custom type, (, [ or {" + .to_string(), actual: type_node.kind().to_string(), location: type_node.start_position(), relevant_source: _source[type_node.byte_range()].to_string(), diff --git a/tests/enums.rs b/tests/enums.rs index 046c1a1..7b7b800 100644 --- a/tests/enums.rs +++ b/tests/enums.rs @@ -22,3 +22,34 @@ fn simple_enum() { ))) ); } + +#[test] +fn nested_enum() { + let result = interpret( + " + enum Fizzbuzz { + Fizz, + Buzz, + } + enum Foobar { + Foo, + Bar(Fizzbuzz), + } + + new Foobar:Bar(new Fizzbuzz:Fizz) + ", + ); + + assert_eq!( + result, + Ok(Value::Enum(EnumInstance::new( + "Foobar".to_string(), + "Bar".to_string(), + Value::Enum(EnumInstance::new( + "Fizzbuzz".to_string(), + "Fizz".to_string(), + Value::none() + )) + ))) + ); +}