diff --git a/src/abstract_tree/enum_definition.rs b/src/abstract_tree/enum_definition.rs index c848a1f..0be3cfc 100644 --- a/src/abstract_tree/enum_definition.rs +++ b/src/abstract_tree/enum_definition.rs @@ -1,4 +1,9 @@ -use super::{AbstractTree, Identifier, Type, WithPosition}; +use crate::{ + context::Context, + error::{RuntimeError, ValidationError}, +}; + +use super::{AbstractTree, Action, Identifier, Type, WithPosition}; #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)] pub struct EnumDefinition { @@ -22,24 +27,17 @@ impl EnumDefinition { } impl AbstractTree for EnumDefinition { - fn expected_type( - &self, - _context: &crate::context::Context, - ) -> Result { - todo!() + fn expected_type(&self, _context: &Context) -> Result { + Ok(Type::None) } - fn validate( - &self, - _context: &crate::context::Context, - ) -> Result<(), crate::error::ValidationError> { - todo!() + fn validate(&self, _context: &Context) -> Result<(), ValidationError> { + Ok(()) } - fn run( - self, - _context: &crate::context::Context, - ) -> Result { - todo!() + fn run(self, context: &Context) -> Result { + context.set_enum_definition(self.name.clone(), self)?; + + Ok(Action::None) } } diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index c0569e0..3b2d9ab 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -25,7 +25,7 @@ impl AbstractTree for Statement { Statement::Assignment(assignment) => assignment.expected_type(_context), Statement::Block(block) => block.expected_type(_context), Statement::Break => Ok(Type::None), - Statement::EnumDefinition(_) => todo!(), + Statement::EnumDefinition(enum_definition) => enum_definition.expected_type(_context), Statement::Expression(expression) => expression.expected_type(_context), Statement::IfElse(if_else) => if_else.expected_type(_context), Statement::Loop(r#loop) => r#loop.expected_type(_context), @@ -38,7 +38,7 @@ impl AbstractTree for Statement { Statement::Assignment(assignment) => assignment.validate(_context), Statement::Block(block) => block.validate(_context), Statement::Break => Ok(()), - Statement::EnumDefinition(_) => todo!(), + Statement::EnumDefinition(enum_definition) => enum_definition.validate(_context), Statement::Expression(expression) => expression.validate(_context), Statement::IfElse(if_else) => if_else.validate(_context), Statement::Loop(r#loop) => r#loop.validate(_context), @@ -51,7 +51,7 @@ impl AbstractTree for Statement { Statement::Assignment(assignment) => assignment.run(_context), Statement::Block(block) => block.run(_context), Statement::Break => Ok(Action::Break), - Statement::EnumDefinition(_) => todo!(), + Statement::EnumDefinition(enum_definition) => enum_definition.run(_context), Statement::Expression(expression) => expression.run(_context), Statement::IfElse(if_else) => if_else.run(_context), Statement::Loop(r#loop) => r#loop.run(_context), diff --git a/src/context.rs b/src/context.rs index 74e3193..27f320b 100644 --- a/src/context.rs +++ b/src/context.rs @@ -4,7 +4,7 @@ use std::{ }; use crate::{ - abstract_tree::{Identifier, Type}, + abstract_tree::{EnumDefinition, Identifier, Type}, error::RwLockPoisonError, value::{BUILT_IN_FUNCTIONS, BUILT_IN_MODULES}, Value, @@ -77,6 +77,7 @@ impl Context { let r#type = match value_data { ValueData::Type(r#type) => r#type.clone(), ValueData::Value(value) => value.r#type(), + ValueData::EnumDefinition(_) => return Ok(None), }; return Ok(Some(r#type.clone())); @@ -117,6 +118,18 @@ impl Context { } } + pub fn set_enum_definition( + &self, + identifier: Identifier, + definition: EnumDefinition, + ) -> Result<(), RwLockPoisonError> { + let mut inner = self.inner.write()?; + + inner.insert(identifier, ValueData::EnumDefinition(definition)); + + Ok(()) + } + pub fn set_type(&self, identifier: Identifier, r#type: Type) -> Result<(), RwLockPoisonError> { self.inner .write()? @@ -138,4 +151,5 @@ impl Context { pub enum ValueData { Type(Type), Value(Value), + EnumDefinition(EnumDefinition), } diff --git a/src/parser.rs b/src/parser.rs index bd62239..0a4f338 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -506,11 +506,11 @@ mod tests { vec![ ( Identifier::new("Foo"), - vec![Type::Custom(Identifier::new("F")).with_position((0, 0))], + vec![Type::Custom(Identifier::new("F")).with_position((62, 63))], ), ( Identifier::new("Bar"), - vec![Type::Custom(Identifier::new("B")).with_position((0, 0))] + vec![Type::Custom(Identifier::new("B")).with_position((90, 91))] ) ] ))