From 172a6fa8601f1c2103820c17b6413c02f4949c6f Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 16 Feb 2024 10:36:16 -0500 Subject: [PATCH] Remove return statement; Add StatementInner --- src/abstract_tree/block.rs | 23 +----- src/abstract_tree/mod.rs | 6 +- src/abstract_tree/return.rs | 0 src/abstract_tree/statement.rs | 144 +++++++++++++++++++-------------- 4 files changed, 88 insertions(+), 85 deletions(-) delete mode 100644 src/abstract_tree/return.rs diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index 32beb98..f8fb685 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -75,15 +75,8 @@ impl AbstractTree for Block { .find_map_first(|(index, statement)| { let result = statement.run(_source, _context); let is_last_statement = index == statements.len() - 1; - let is_return_statement = if let Statement::Return(_) = statement { - true - } else { - false - }; - if is_return_statement || result.is_err() { - Some(result) - } else if is_last_statement { + if is_last_statement { let get_write_lock = final_result.write(); match get_write_lock { @@ -102,10 +95,6 @@ impl AbstractTree for Block { let mut prev_result = None; for statement in &self.statements { - if let Statement::Return(inner_statement) = statement { - return inner_statement.run(_source, _context); - } - prev_result = Some(statement.run(_source, _context)); } @@ -114,15 +103,7 @@ impl AbstractTree for Block { } fn expected_type(&self, _context: &Context) -> Result { - if let Some(statement) = self.statements.iter().find(|statement| { - if let Statement::Return(_) = statement { - true - } else { - false - } - }) { - statement.expected_type(_context) - } else if let Some(statement) = self.statements.last() { + if let Some(statement) = self.statements.last() { statement.expected_type(_context) } else { Ok(Type::None) diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index 37fe9a3..f17b40c 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -105,11 +105,7 @@ impl AbstractTree for Root { fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> { for statement in &self.statements { - if let Statement::Return(inner_statement) = statement { - return inner_statement.validate(_source, _context); - } else { - statement.validate(_source, _context)?; - } + statement.validate(_source, _context)?; } Ok(()) diff --git a/src/abstract_tree/return.rs b/src/abstract_tree/return.rs deleted file mode 100644 index e69de29..0000000 diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index fbec2f6..2ba1ad7 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -6,58 +6,82 @@ use crate::{ Match, SyntaxNode, Type, TypeDefinition, Value, While, }; +#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] +pub struct Statement { + is_return: bool, + statement_inner: StatementInner, +} + +impl AbstractTree for Statement { + fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result { + todo!() + } + + fn expected_type(&self, context: &Context) -> Result { + todo!() + } + + fn validate(&self, source: &str, context: &Context) -> Result<(), ValidationError> { + todo!() + } + + fn run(&self, source: &str, context: &Context) -> Result { + todo!() + } +} + +impl Format for Statement { + fn format(&self, output: &mut String, indent_level: u8) { + todo!() + } +} + /// Abstract representation of a statement. #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub enum Statement { +pub enum StatementInner { Assignment(Box), Expression(Expression), IfElse(Box), Match(Match), While(Box), Block(Box), - Return(Box), For(Box), IndexAssignment(Box), TypeDefinition(TypeDefinition), } -impl AbstractTree for Statement { +impl AbstractTree for StatementInner { fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result { SyntaxError::expect_syntax_node(source, "statement", node)?; let child = node.child(0).unwrap(); match child.kind() { - "assignment" => Ok(Statement::Assignment(Box::new( + "assignment" => Ok(StatementInner::Assignment(Box::new( Assignment::from_syntax(child, source, context)?, ))), - "expression" => Ok(Statement::Expression(Expression::from_syntax( + "expression" => Ok(StatementInner::Expression(Expression::from_syntax( child, source, context, )?)), - "if_else" => Ok(Statement::IfElse(Box::new(IfElse::from_syntax( + "if_else" => Ok(StatementInner::IfElse(Box::new(IfElse::from_syntax( child, source, context, )?))), - "while" => Ok(Statement::While(Box::new(While::from_syntax( + "while" => Ok(StatementInner::While(Box::new(While::from_syntax( child, source, context, )?))), - "block" => Ok(Statement::Block(Box::new(Block::from_syntax( + "block" => Ok(StatementInner::Block(Box::new(Block::from_syntax( child, source, context, )?))), - "for" => Ok(Statement::For(Box::new(For::from_syntax( + "for" => Ok(StatementInner::For(Box::new(For::from_syntax( child, source, context, )?))), - "index_assignment" => Ok(Statement::IndexAssignment(Box::new( + "index_assignment" => Ok(StatementInner::IndexAssignment(Box::new( IndexAssignment::from_syntax(child, source, context)?, ))), - "match" => Ok(Statement::Match(Match::from_syntax( + "match" => Ok(StatementInner::Match(Match::from_syntax( child, source, context, )?)), - "return" => { - let statement_node = child.child(1).unwrap(); - - Ok(Statement::Return(Box::new(Statement::from_syntax(statement_node, source, context)?))) - }, - "type_definition" => Ok(Statement::TypeDefinition(TypeDefinition::from_syntax( + "type_definition" => Ok(StatementInner::TypeDefinition(TypeDefinition::from_syntax( child, source, context )?)), _ => Err(SyntaxError::UnexpectedSyntaxNode { @@ -72,35 +96,35 @@ impl AbstractTree for Statement { fn expected_type(&self, _context: &Context) -> Result { match self { - Statement::Assignment(assignment) => assignment.expected_type(_context), - Statement::Expression(expression) => expression.expected_type(_context), - Statement::IfElse(if_else) => if_else.expected_type(_context), - Statement::Match(r#match) => r#match.expected_type(_context), - Statement::While(r#while) => r#while.expected_type(_context), - Statement::Block(block) => block.expected_type(_context), - Statement::For(r#for) => r#for.expected_type(_context), - Statement::IndexAssignment(index_assignment) => { + StatementInner::Assignment(assignment) => assignment.expected_type(_context), + StatementInner::Expression(expression) => expression.expected_type(_context), + StatementInner::IfElse(if_else) => if_else.expected_type(_context), + StatementInner::Match(r#match) => r#match.expected_type(_context), + StatementInner::While(r#while) => r#while.expected_type(_context), + StatementInner::Block(block) => block.expected_type(_context), + StatementInner::For(r#for) => r#for.expected_type(_context), + StatementInner::IndexAssignment(index_assignment) => { index_assignment.expected_type(_context) } - Statement::Return(statement) => statement.expected_type(_context), - Statement::TypeDefinition(type_definition) => type_definition.expected_type(_context), + StatementInner::TypeDefinition(type_definition) => { + type_definition.expected_type(_context) + } } } fn validate(&self, _source: &str, _context: &Context) -> Result<(), ValidationError> { match self { - Statement::Assignment(assignment) => assignment.validate(_source, _context), - Statement::Expression(expression) => expression.validate(_source, _context), - Statement::IfElse(if_else) => if_else.validate(_source, _context), - Statement::Match(r#match) => r#match.validate(_source, _context), - Statement::While(r#while) => r#while.validate(_source, _context), - Statement::Block(block) => block.validate(_source, _context), - Statement::For(r#for) => r#for.validate(_source, _context), - Statement::IndexAssignment(index_assignment) => { + StatementInner::Assignment(assignment) => assignment.validate(_source, _context), + StatementInner::Expression(expression) => expression.validate(_source, _context), + StatementInner::IfElse(if_else) => if_else.validate(_source, _context), + StatementInner::Match(r#match) => r#match.validate(_source, _context), + StatementInner::While(r#while) => r#while.validate(_source, _context), + StatementInner::Block(block) => block.validate(_source, _context), + StatementInner::For(r#for) => r#for.validate(_source, _context), + StatementInner::IndexAssignment(index_assignment) => { index_assignment.validate(_source, _context) } - Statement::Return(statement) => statement.validate(_source, _context), - Statement::TypeDefinition(type_definition) => { + StatementInner::TypeDefinition(type_definition) => { type_definition.validate(_source, _context) } } @@ -108,37 +132,39 @@ impl AbstractTree for Statement { fn run(&self, _source: &str, _context: &Context) -> Result { match self { - Statement::Assignment(assignment) => assignment.run(_source, _context), - Statement::Expression(expression) => expression.run(_source, _context), - Statement::IfElse(if_else) => if_else.run(_source, _context), - Statement::Match(r#match) => r#match.run(_source, _context), - Statement::While(r#while) => r#while.run(_source, _context), - Statement::Block(block) => block.run(_source, _context), - Statement::For(r#for) => r#for.run(_source, _context), - Statement::IndexAssignment(index_assignment) => index_assignment.run(_source, _context), - Statement::Return(statement) => statement.run(_source, _context), - Statement::TypeDefinition(type_definition) => type_definition.run(_source, _context), + StatementInner::Assignment(assignment) => assignment.run(_source, _context), + StatementInner::Expression(expression) => expression.run(_source, _context), + StatementInner::IfElse(if_else) => if_else.run(_source, _context), + StatementInner::Match(r#match) => r#match.run(_source, _context), + StatementInner::While(r#while) => r#while.run(_source, _context), + StatementInner::Block(block) => block.run(_source, _context), + StatementInner::For(r#for) => r#for.run(_source, _context), + StatementInner::IndexAssignment(index_assignment) => { + index_assignment.run(_source, _context) + } + StatementInner::TypeDefinition(type_definition) => { + type_definition.run(_source, _context) + } } } } -impl Format for Statement { +impl Format for StatementInner { fn format(&self, output: &mut String, indent_level: u8) { - Statement::indent(output, indent_level); + StatementInner::indent(output, indent_level); match self { - Statement::Assignment(assignment) => assignment.format(output, indent_level), - Statement::Expression(expression) => expression.format(output, indent_level), - Statement::IfElse(if_else) => if_else.format(output, indent_level), - Statement::Match(r#match) => r#match.format(output, indent_level), - Statement::While(r#while) => r#while.format(output, indent_level), - Statement::Block(block) => block.format(output, indent_level), - Statement::For(r#for) => r#for.format(output, indent_level), - Statement::IndexAssignment(index_assignment) => { + StatementInner::Assignment(assignment) => assignment.format(output, indent_level), + StatementInner::Expression(expression) => expression.format(output, indent_level), + StatementInner::IfElse(if_else) => if_else.format(output, indent_level), + StatementInner::Match(r#match) => r#match.format(output, indent_level), + StatementInner::While(r#while) => r#while.format(output, indent_level), + StatementInner::Block(block) => block.format(output, indent_level), + StatementInner::For(r#for) => r#for.format(output, indent_level), + StatementInner::IndexAssignment(index_assignment) => { index_assignment.format(output, indent_level) } - Statement::Return(statement) => statement.format(output, indent_level), - Statement::TypeDefinition(type_definition) => { + StatementInner::TypeDefinition(type_definition) => { type_definition.format(output, indent_level) } }