1
0

Remove return statement; Add StatementInner

This commit is contained in:
Jeff 2024-02-16 10:36:16 -05:00
parent 51869f04b6
commit 172a6fa860
4 changed files with 88 additions and 85 deletions

View File

@ -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<Type, ValidationError> {
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)

View File

@ -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(())

View File

@ -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<Self, SyntaxError> {
todo!()
}
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError> {
todo!()
}
fn validate(&self, source: &str, context: &Context) -> Result<(), ValidationError> {
todo!()
}
fn run(&self, source: &str, context: &Context) -> Result<Value, RuntimeError> {
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<Assignment>),
Expression(Expression),
IfElse(Box<IfElse>),
Match(Match),
While(Box<While>),
Block(Box<Block>),
Return(Box<Statement>),
For(Box<For>),
IndexAssignment(Box<IndexAssignment>),
TypeDefinition(TypeDefinition),
}
impl AbstractTree for Statement {
impl AbstractTree for StatementInner {
fn from_syntax(node: SyntaxNode, source: &str, context: &Context) -> Result<Self, SyntaxError> {
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<Type, ValidationError> {
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<Value, RuntimeError> {
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)
}
}