From e486413acacf746daf6be54e2ed8b9bc508d7e3d Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 31 Jan 2024 20:52:34 -0500 Subject: [PATCH] Fix type setting bugs; Rename function --- src/abstract_tree/assignment.rs | 25 ++++++------ src/abstract_tree/assignment_operator.rs | 2 +- src/abstract_tree/block.rs | 6 +-- src/abstract_tree/built_in_value.rs | 2 +- src/abstract_tree/command.rs | 2 +- src/abstract_tree/expression.rs | 20 +++++----- src/abstract_tree/for.rs | 24 ++++++++++-- src/abstract_tree/function_call.rs | 2 +- src/abstract_tree/function_expression.rs | 12 +++--- src/abstract_tree/function_node.rs | 4 +- src/abstract_tree/identifier.rs | 2 +- src/abstract_tree/if_else.rs | 10 ++--- src/abstract_tree/index.rs | 8 ++-- src/abstract_tree/index_assignment.rs | 6 +-- src/abstract_tree/index_expression.rs | 10 ++--- src/abstract_tree/logic.rs | 6 +-- src/abstract_tree/logic_operator.rs | 2 +- src/abstract_tree/match.rs | 10 ++--- src/abstract_tree/math.rs | 6 +-- src/abstract_tree/math_operator.rs | 2 +- src/abstract_tree/mod.rs | 8 ++-- src/abstract_tree/new.rs | 2 +- src/abstract_tree/statement.rs | 20 +++++----- src/abstract_tree/type.rs | 2 +- src/abstract_tree/type_specification.rs | 2 +- src/abstract_tree/value_node.rs | 4 +- src/abstract_tree/while.rs | 6 +-- src/abstract_tree/yield.rs | 4 +- src/error/mod.rs | 12 +++--- src/error/runtime_error.rs | 49 +----------------------- src/error/syntax_error.rs | 10 ++++- src/error/validation_error.rs | 10 ++++- src/interpret.rs | 2 +- 33 files changed, 141 insertions(+), 151 deletions(-) diff --git a/src/abstract_tree/assignment.rs b/src/abstract_tree/assignment.rs index 4dddfa1..184dd57 100644 --- a/src/abstract_tree/assignment.rs +++ b/src/abstract_tree/assignment.rs @@ -43,16 +43,6 @@ impl AbstractTree for Assignment { let statement_node = syntax_node.child(child_count - 1).unwrap(); let statement = Statement::from_syntax(statement_node, source, context)?; - // if let AssignmentOperator::Equal = operator { - // let r#type = if let Some(definition) = &type_specification { - // definition.inner().clone() - // } else { - // statement.expected_type(context)? - // }; - - // context.set_type(identifier.inner().clone(), r#type)?; - // } - Ok(Assignment { identifier, type_specification, @@ -62,7 +52,18 @@ impl AbstractTree for Assignment { }) } - fn check_type(&self, source: &str, context: &Map) -> Result<(), ValidationError> { + fn validate(&self, source: &str, context: &Map) -> Result<(), ValidationError> { + if let AssignmentOperator::Equal = self.operator { + let key = self.identifier.inner().clone(); + let r#type = if let Some(definition) = &self.type_specification { + definition.inner().clone() + } else { + self.statement.expected_type(context)? + }; + + context.set_type(key, r#type)?; + } + if let Some(type_specification) = &self.type_specification { match self.operator { AssignmentOperator::Equal => { @@ -123,7 +124,7 @@ impl AbstractTree for Assignment { } } - self.statement.check_type(source, context)?; + self.statement.validate(source, context)?; Ok(()) } diff --git a/src/abstract_tree/assignment_operator.rs b/src/abstract_tree/assignment_operator.rs index 8b996d0..2963509 100644 --- a/src/abstract_tree/assignment_operator.rs +++ b/src/abstract_tree/assignment_operator.rs @@ -47,7 +47,7 @@ impl AbstractTree for AssignmentOperator { Ok(Type::None) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { Ok(()) } } diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index 25c9ff5..aaeeeca 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -52,12 +52,12 @@ impl AbstractTree for Block { }) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { for statement in &self.statements { if let Statement::Return(inner_statement) = statement { - return inner_statement.check_type(_source, _context); + return inner_statement.validate(_source, _context); } else { - statement.check_type(_source, _context)?; + statement.validate(_source, _context)?; } } diff --git a/src/abstract_tree/built_in_value.rs b/src/abstract_tree/built_in_value.rs index 8e56203..c3abf97 100644 --- a/src/abstract_tree/built_in_value.rs +++ b/src/abstract_tree/built_in_value.rs @@ -195,7 +195,7 @@ impl AbstractTree for BuiltInValue { Ok(self.r#type()) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { Ok(()) } diff --git a/src/abstract_tree/command.rs b/src/abstract_tree/command.rs index 2adfd99..4aed217 100644 --- a/src/abstract_tree/command.rs +++ b/src/abstract_tree/command.rs @@ -51,7 +51,7 @@ impl AbstractTree for Command { Ok(Type::String) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { todo!() } diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index aaf43b5..ed8fe48 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -78,17 +78,17 @@ impl AbstractTree for Expression { } } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { match self { - Expression::Value(value_node) => value_node.check_type(_source, _context), - Expression::Identifier(identifier) => identifier.check_type(_source, _context), - Expression::Math(math) => math.check_type(_source, _context), - Expression::Logic(logic) => logic.check_type(_source, _context), - Expression::FunctionCall(function_call) => function_call.check_type(_source, _context), - Expression::Index(index) => index.check_type(_source, _context), - Expression::Yield(r#yield) => r#yield.check_type(_source, _context), - Expression::New(new) => new.check_type(_source, _context), - Expression::Command(command) => command.check_type(_source, _context), + Expression::Value(value_node) => value_node.validate(_source, _context), + Expression::Identifier(identifier) => identifier.validate(_source, _context), + Expression::Math(math) => math.validate(_source, _context), + Expression::Logic(logic) => logic.validate(_source, _context), + Expression::FunctionCall(function_call) => function_call.validate(_source, _context), + Expression::Index(index) => index.validate(_source, _context), + Expression::Yield(r#yield) => r#yield.validate(_source, _context), + Expression::New(new) => new.validate(_source, _context), + Expression::Command(command) => command.validate(_source, _context), } } diff --git a/src/abstract_tree/for.rs b/src/abstract_tree/for.rs index 22c250d..659533c 100644 --- a/src/abstract_tree/for.rs +++ b/src/abstract_tree/for.rs @@ -3,7 +3,8 @@ use serde::{Deserialize, Serialize}; use crate::{ error::{RuntimeError, SyntaxError, ValidationError}, - AbstractTree, Block, Expression, Format, Identifier, Map, SyntaxNode, Type, Value, + AbstractTree, Block, Expression, Format, Identifier, Map, SourcePosition, SyntaxNode, Type, + Value, }; /// Abstract representation of a for loop statement. @@ -13,6 +14,7 @@ pub struct For { item_id: Identifier, collection: Expression, block: Block, + source_position: SourcePosition, } impl AbstractTree for For { @@ -47,6 +49,7 @@ impl AbstractTree for For { item_id: identifier, collection: expression, block: item, + source_position: SourcePosition::from(node.range()), }) } @@ -54,8 +57,23 @@ impl AbstractTree for For { Ok(Type::None) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.block.check_type(_source, _context) + fn validate(&self, _source: &str, context: &Map) -> Result<(), ValidationError> { + let collection_type = self.collection.expected_type(context)?; + let item_type = if let Type::List(item_type) = collection_type { + item_type.as_ref().clone() + } else if let Type::Range = collection_type { + Type::Integer + } else { + return Err(ValidationError::TypeCheck { + expected: Type::Collection, + actual: collection_type, + position: self.source_position, + }); + }; + let key = self.item_id.inner().clone(); + + context.set_type(key, item_type)?; + self.block.validate(_source, context) } fn run(&self, source: &str, context: &Map) -> Result { diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index 5c5d9c4..89615bf 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -85,7 +85,7 @@ impl AbstractTree for FunctionCall { } } - fn check_type(&self, _source: &str, context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, context: &Map) -> Result<(), ValidationError> { let function_expression_type = self.function_expression.expected_type(context)?; let parameter_types = match function_expression_type { diff --git a/src/abstract_tree/function_expression.rs b/src/abstract_tree/function_expression.rs index 65a3e79..7a8def8 100644 --- a/src/abstract_tree/function_expression.rs +++ b/src/abstract_tree/function_expression.rs @@ -62,15 +62,15 @@ impl AbstractTree for FunctionExpression { } } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { match self { - FunctionExpression::Identifier(identifier) => identifier.check_type(_source, _context), + FunctionExpression::Identifier(identifier) => identifier.validate(_source, _context), FunctionExpression::FunctionCall(function_call) => { - function_call.check_type(_source, _context) + function_call.validate(_source, _context) } - FunctionExpression::Value(value_node) => value_node.check_type(_source, _context), - FunctionExpression::Index(index) => index.check_type(_source, _context), - FunctionExpression::Yield(r#yield) => r#yield.check_type(_source, _context), + FunctionExpression::Value(value_node) => value_node.validate(_source, _context), + FunctionExpression::Index(index) => index.validate(_source, _context), + FunctionExpression::Yield(r#yield) => r#yield.validate(_source, _context), } } diff --git a/src/abstract_tree/function_node.rs b/src/abstract_tree/function_node.rs index a69a1ff..45619f5 100644 --- a/src/abstract_tree/function_node.rs +++ b/src/abstract_tree/function_node.rs @@ -146,7 +146,7 @@ impl AbstractTree for FunctionNode { Ok(self.r#type().clone()) } - fn check_type(&self, source: &str, context: &Map) -> Result<(), ValidationError> { + fn validate(&self, source: &str, context: &Map) -> Result<(), ValidationError> { let function_context = Map::new(); for (key, (_value, r#type)) in context.variables()?.iter() { @@ -174,7 +174,7 @@ impl AbstractTree for FunctionNode { }); } - self.body.check_type(source, &function_context)?; + self.body.validate(source, &function_context)?; Ok(()) } else { diff --git a/src/abstract_tree/identifier.rs b/src/abstract_tree/identifier.rs index 9c1eb35..415e132 100644 --- a/src/abstract_tree/identifier.rs +++ b/src/abstract_tree/identifier.rs @@ -39,7 +39,7 @@ impl AbstractTree for Identifier { Ok(Identifier(text.to_string())) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { Ok(()) } diff --git a/src/abstract_tree/if_else.rs b/src/abstract_tree/if_else.rs index 4c991f3..92617a2 100644 --- a/src/abstract_tree/if_else.rs +++ b/src/abstract_tree/if_else.rs @@ -63,9 +63,9 @@ impl AbstractTree for IfElse { self.if_block.expected_type(context) } - fn check_type(&self, _source: &str, context: &Map) -> Result<(), ValidationError> { - self.if_expression.check_type(_source, context)?; - self.if_block.check_type(_source, context)?; + fn validate(&self, _source: &str, context: &Map) -> Result<(), ValidationError> { + self.if_expression.validate(_source, context)?; + self.if_block.validate(_source, context)?; let expected = self.if_block.expected_type(context)?; let else_ifs = self @@ -74,8 +74,8 @@ impl AbstractTree for IfElse { .zip(self.else_if_blocks.iter()); for (expression, block) in else_ifs { - expression.check_type(_source, context)?; - block.check_type(_source, context)?; + expression.validate(_source, context)?; + block.validate(_source, context)?; let actual = block.expected_type(context)?; diff --git a/src/abstract_tree/index.rs b/src/abstract_tree/index.rs index 8872338..e17d801 100644 --- a/src/abstract_tree/index.rs +++ b/src/abstract_tree/index.rs @@ -54,12 +54,12 @@ impl AbstractTree for Index { } } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.collection.check_type(_source, _context)?; - self.index.check_type(_source, _context)?; + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + self.collection.validate(_source, _context)?; + self.index.validate(_source, _context)?; if let Some(index_end) = &self.index_end { - index_end.check_type(_source, _context)?; + index_end.validate(_source, _context)?; } Ok(()) diff --git a/src/abstract_tree/index_assignment.rs b/src/abstract_tree/index_assignment.rs index 022d96b..bc0636a 100644 --- a/src/abstract_tree/index_assignment.rs +++ b/src/abstract_tree/index_assignment.rs @@ -37,9 +37,9 @@ impl AbstractTree for IndexAssignment { Ok(Type::None) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.index.check_type(_source, _context)?; - self.statement.check_type(_source, _context) + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + self.index.validate(_source, _context)?; + self.statement.validate(_source, _context) } fn run(&self, source: &str, context: &Map) -> Result { diff --git a/src/abstract_tree/index_expression.rs b/src/abstract_tree/index_expression.rs index 8a9b775..235396f 100644 --- a/src/abstract_tree/index_expression.rs +++ b/src/abstract_tree/index_expression.rs @@ -58,13 +58,13 @@ impl AbstractTree for IndexExpression { } } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { match self { - IndexExpression::Value(value_node) => value_node.check_type(_source, _context), - IndexExpression::Identifier(identifier) => identifier.check_type(_source, _context), - IndexExpression::Index(index) => index.check_type(_source, _context), + IndexExpression::Value(value_node) => value_node.validate(_source, _context), + IndexExpression::Identifier(identifier) => identifier.validate(_source, _context), + IndexExpression::Index(index) => index.validate(_source, _context), IndexExpression::FunctionCall(function_call) => { - function_call.check_type(_source, _context) + function_call.validate(_source, _context) } } } diff --git a/src/abstract_tree/logic.rs b/src/abstract_tree/logic.rs index 8ea9db5..13b1b60 100644 --- a/src/abstract_tree/logic.rs +++ b/src/abstract_tree/logic.rs @@ -44,9 +44,9 @@ impl AbstractTree for Logic { Ok(Type::Boolean) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.left.check_type(_source, _context)?; - self.right.check_type(_source, _context) + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + self.left.validate(_source, _context)?; + self.right.validate(_source, _context) } fn run(&self, source: &str, context: &Map) -> Result { diff --git a/src/abstract_tree/logic_operator.rs b/src/abstract_tree/logic_operator.rs index 21c671e..7092216 100644 --- a/src/abstract_tree/logic_operator.rs +++ b/src/abstract_tree/logic_operator.rs @@ -48,7 +48,7 @@ impl AbstractTree for LogicOperator { Ok(Type::None) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { Ok(()) } diff --git a/src/abstract_tree/match.rs b/src/abstract_tree/match.rs index c6490dc..c0d66d0 100644 --- a/src/abstract_tree/match.rs +++ b/src/abstract_tree/match.rs @@ -65,16 +65,16 @@ impl AbstractTree for Match { first_statement.expected_type(context) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.matcher.check_type(_source, _context)?; + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + self.matcher.validate(_source, _context)?; for (expression, statement) in &self.options { - expression.check_type(_source, _context)?; - statement.check_type(_source, _context)?; + expression.validate(_source, _context)?; + statement.validate(_source, _context)?; } if let Some(statement) = &self.fallback { - statement.check_type(_source, _context)?; + statement.validate(_source, _context)?; } Ok(()) diff --git a/src/abstract_tree/math.rs b/src/abstract_tree/math.rs index 3dc477e..4a09d7c 100644 --- a/src/abstract_tree/math.rs +++ b/src/abstract_tree/math.rs @@ -40,9 +40,9 @@ impl AbstractTree for Math { self.left.expected_type(context) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.left.check_type(_source, _context)?; - self.right.check_type(_source, _context) + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + self.left.validate(_source, _context)?; + self.right.validate(_source, _context) } fn run(&self, source: &str, context: &Map) -> Result { diff --git a/src/abstract_tree/math_operator.rs b/src/abstract_tree/math_operator.rs index 3a65036..4db7fe9 100644 --- a/src/abstract_tree/math_operator.rs +++ b/src/abstract_tree/math_operator.rs @@ -40,7 +40,7 @@ impl AbstractTree for MathOperator { Ok(Type::None) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { Ok(()) } diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index c53e314..1d2dd56 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -97,12 +97,12 @@ impl AbstractTree for Root { Ok(Root { statements }) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { for statement in &self.statements { if let Statement::Return(inner_statement) = statement { - return inner_statement.check_type(_source, _context); + return inner_statement.validate(_source, _context); } else { - statement.check_type(_source, _context)?; + statement.validate(_source, _context)?; } } @@ -160,7 +160,7 @@ pub trait AbstractTree: Sized + Format { fn expected_type(&self, context: &Map) -> Result; /// Verify the type integrity of the node. Returns a validation error if the tree is invalid. - fn check_type(&self, source: &str, context: &Map) -> Result<(), ValidationError>; + fn validate(&self, source: &str, context: &Map) -> Result<(), ValidationError>; /// Execute this node's logic and return a value. Returns a runtime error if the node cannot /// resolve to a value. diff --git a/src/abstract_tree/new.rs b/src/abstract_tree/new.rs index a517d66..fef1529 100644 --- a/src/abstract_tree/new.rs +++ b/src/abstract_tree/new.rs @@ -29,7 +29,7 @@ impl AbstractTree for New { todo!() } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { todo!() } diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index b0612f9..917ea8e 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -80,19 +80,19 @@ impl AbstractTree for Statement { } } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { match self { - Statement::Assignment(assignment) => assignment.check_type(_source, _context), - Statement::Expression(expression) => expression.check_type(_source, _context), - Statement::IfElse(if_else) => if_else.check_type(_source, _context), - Statement::Match(r#match) => r#match.check_type(_source, _context), - Statement::While(r#while) => r#while.check_type(_source, _context), - Statement::Block(block) => block.check_type(_source, _context), - Statement::For(r#for) => r#for.check_type(_source, _context), + 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) => { - index_assignment.check_type(_source, _context) + index_assignment.validate(_source, _context) } - Statement::Return(statement) => statement.check_type(_source, _context), + Statement::Return(statement) => statement.validate(_source, _context), } } diff --git a/src/abstract_tree/type.rs b/src/abstract_tree/type.rs index 13b01e6..9c24425 100644 --- a/src/abstract_tree/type.rs +++ b/src/abstract_tree/type.rs @@ -196,7 +196,7 @@ impl AbstractTree for Type { Ok(Type::None) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { Ok(()) } diff --git a/src/abstract_tree/type_specification.rs b/src/abstract_tree/type_specification.rs index a158b84..471c7ee 100644 --- a/src/abstract_tree/type_specification.rs +++ b/src/abstract_tree/type_specification.rs @@ -38,7 +38,7 @@ impl AbstractTree for TypeSpecification { self.r#type.expected_type(context) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { Ok(()) } diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 0ed962f..23f3890 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -234,11 +234,11 @@ impl AbstractTree for ValueNode { Ok(r#type) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { match self { ValueNode::Function(function) => { if let Function::ContextDefined(function_node) = function { - function_node.check_type(_source, _context)?; + function_node.validate(_source, _context)?; } } _ => {} diff --git a/src/abstract_tree/while.rs b/src/abstract_tree/while.rs index f373573..d6a82ea 100644 --- a/src/abstract_tree/while.rs +++ b/src/abstract_tree/while.rs @@ -31,9 +31,9 @@ impl AbstractTree for While { self.block.expected_type(context) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.expression.check_type(_source, _context)?; - self.block.check_type(_source, _context) + fn validate(&self, _source: &str, context: &Map) -> Result<(), ValidationError> { + self.expression.validate(_source, context)?; + self.block.validate(_source, context) } fn run(&self, source: &str, context: &Map) -> Result { diff --git a/src/abstract_tree/yield.rs b/src/abstract_tree/yield.rs index a8e718b..be496f1 100644 --- a/src/abstract_tree/yield.rs +++ b/src/abstract_tree/yield.rs @@ -47,8 +47,8 @@ impl AbstractTree for Yield { self.call.expected_type(context) } - fn check_type(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { - self.call.check_type(_source, _context) + fn validate(&self, _source: &str, _context: &Map) -> Result<(), ValidationError> { + self.call.validate(_source, _context) } fn run(&self, source: &str, context: &Map) -> Result { diff --git a/src/error/mod.rs b/src/error/mod.rs index 13fbcba..bca67be 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -61,15 +61,15 @@ impl fmt::Debug for Error { } impl fmt::Display for Error { - fn fmt(&self, _f: &mut Formatter) -> fmt::Result { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { use Error::*; match self { - Syntax(_) => todo!(), - Validation(_) => todo!(), - Runtime(_) => todo!(), - ParserCancelled => todo!(), - Language(_) => todo!(), + Syntax(error) => write!(f, "Syntax error: {error}"), + Validation(error) => write!(f, "Validation error: {error}"), + Runtime(error) => write!(f, "Runtime error: {error}"), + ParserCancelled => write!(f, "Parsing was cancelled because the parser took too long."), + Language(error) => write!(f, "Parser failed to load language grammar."), } } } diff --git a/src/error/runtime_error.rs b/src/error/runtime_error.rs index a813c0f..77c87e5 100644 --- a/src/error/runtime_error.rs +++ b/src/error/runtime_error.rs @@ -10,7 +10,7 @@ use crate::Value; use super::rw_lock_error::RwLockError; -#[derive(PartialEq)] +#[derive(Debug, PartialEq)] pub enum RuntimeError { Csv(String), @@ -176,53 +176,8 @@ impl From for RuntimeError { } impl Display for RuntimeError { - fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result { - use RuntimeError::*; - - match self { - VariableIdentifierNotFound(_) => todo!(), - RwLock(_) => todo!(), - Csv(_) => todo!(), - Io(_) => todo!(), - Reqwest(_) => todo!(), - Json(_) => todo!(), - SystemTime(_) => todo!(), - Toml(_) => todo!(), - Utf8(_) => todo!(), - ParseFloat(_) => todo!(), - ExpectedBuiltInFunctionArgumentAmount { - function_name: _, - expected: _, - actual: _, - } => todo!(), - ExpectedString { actual: _ } => todo!(), - ExpectedInteger { actual: _ } => todo!(), - ExpectedFloat { actual: _ } => todo!(), - ExpectedNumber { actual: _ } => todo!(), - ExpectedNumberOrString { actual: _ } => todo!(), - ExpectedBoolean { actual: _ } => todo!(), - ExpectedList { actual: _ } => todo!(), - ExpectedMinLengthList { - minimum_len: _, - actual_len: _, - } => todo!(), - ExpectedFixedLenList { - expected_len: _, - actual: _, - } => todo!(), - ExpectedNone { actual: _ } => todo!(), - ExpectedMap { actual: _ } => todo!(), - ExpectedTable { actual: _ } => todo!(), - ExpectedFunction { actual: _ } => todo!(), - ExpectedOption { actual: _ } => todo!(), - ExpectedCollection { actual: _ } => todo!(), - } - } -} - -impl Debug for RuntimeError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{self}") + write!(f, "{self:?}") } } diff --git a/src/error/syntax_error.rs b/src/error/syntax_error.rs index a0a7df8..544f57f 100644 --- a/src/error/syntax_error.rs +++ b/src/error/syntax_error.rs @@ -1,3 +1,5 @@ +use std::fmt::{self, Display, Formatter}; + use serde::{Deserialize, Serialize}; use tree_sitter::{Node as SyntaxNode, Point}; @@ -5,7 +7,7 @@ use crate::SourcePosition; use super::rw_lock_error::RwLockError; -#[derive(Clone, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum SyntaxError { /// Invalid user input. InvalidSource { @@ -57,3 +59,9 @@ impl From for SyntaxError { SyntaxError::RwLock(error) } } + +impl Display for SyntaxError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{self:?}") + } +} diff --git a/src/error/validation_error.rs b/src/error/validation_error.rs index 576fdc2..1c5fe46 100644 --- a/src/error/validation_error.rs +++ b/src/error/validation_error.rs @@ -1,10 +1,12 @@ +use std::fmt::{self, Display, Formatter}; + use serde::{Deserialize, Serialize}; use crate::{Identifier, SourcePosition, Type, Value}; use super::rw_lock_error::RwLockError; -#[derive(Clone, PartialEq, Serialize, Deserialize)] +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] pub enum ValidationError { /// The 'assert' macro did not resolve successfully. AssertEqualFailed { @@ -80,3 +82,9 @@ impl From for ValidationError { ValidationError::RwLock(RwLockError) } } + +impl Display for ValidationError { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{self:?}") + } +} diff --git a/src/interpret.rs b/src/interpret.rs index 4faf40e..8a7ad2f 100644 --- a/src/interpret.rs +++ b/src/interpret.rs @@ -139,7 +139,7 @@ impl Interpreter { let abstract_tree = Root::from_syntax(syntax_tree.root_node(), source, &self.context)?; - abstract_tree.check_type(source, &self.context)?; + abstract_tree.validate(source, &self.context)?; Ok(abstract_tree) }