From c0254e8a94583d916f15e1f137d565f1d5822b44 Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 11 Aug 2024 19:18:13 -0400 Subject: [PATCH] Refactor to use type checking --- dust-lang/src/abstract_tree.rs | 1 - dust-lang/src/analyzer.rs | 190 +++++++---------------------- dust-lang/src/built_in_function.rs | 1 - dust-lang/src/type.rs | 10 +- dust-lang/src/value.rs | 1 - dust-lang/src/vm.rs | 44 +------ 6 files changed, 52 insertions(+), 195 deletions(-) diff --git a/dust-lang/src/abstract_tree.rs b/dust-lang/src/abstract_tree.rs index d1b576d..e1da7eb 100644 --- a/dust-lang/src/abstract_tree.rs +++ b/dust-lang/src/abstract_tree.rs @@ -136,7 +136,6 @@ impl Statement { let item_type = nodes.first().unwrap().inner.expected_type(context)?; Some(Type::List { - length: nodes.len(), item_type: Box::new(item_type), }) } diff --git a/dust-lang/src/analyzer.rs b/dust-lang/src/analyzer.rs index 85d1a01..109aceb 100644 --- a/dust-lang/src/analyzer.rs +++ b/dust-lang/src/analyzer.rs @@ -358,38 +358,14 @@ impl<'a> Analyzer<'a> { #[derive(Clone, Debug, PartialEq)] pub enum AnalyzerError { - ExpectedBoolean { - actual: Node, - position: Span, - }, - ExpectedFloat { - actual: Node, - position: (usize, usize), - }, - ExpectedFunction { - actual: Node, - position: Span, - }, ExpectedIdentifier { actual: Node, position: Span, }, - ExpectedInteger { + ExpectedBoolean { actual: Node, position: Span, }, - ExpectedNumber { - actual: Node, - position: Span, - }, - ExpectedNumberOrString { - actual: Node, - position: Span, - }, - ExpectedString { - actual: Node, - position: (usize, usize), - }, ExpectedValue { actual: Node, }, @@ -418,14 +394,8 @@ impl AnalyzerError { pub fn position(&self) -> Span { match self { AnalyzerError::ExpectedBoolean { position, .. } => *position, - AnalyzerError::ExpectedFloat { position, .. } => *position, - AnalyzerError::ExpectedFunction { position, .. } => *position, AnalyzerError::ExpectedIdentifier { position, .. } => *position, AnalyzerError::ExpectedValue { actual } => actual.position, - AnalyzerError::ExpectedInteger { position, .. } => *position, - AnalyzerError::ExpectedNumber { position, .. } => *position, - AnalyzerError::ExpectedNumberOrString { position, .. } => *position, - AnalyzerError::ExpectedString { position, .. } => *position, AnalyzerError::ExpectedValueArgumentCount { position, .. } => *position, AnalyzerError::TypeConflict { actual_statement, .. @@ -445,27 +415,9 @@ impl Display for AnalyzerError { AnalyzerError::ExpectedBoolean { actual, .. } => { write!(f, "Expected boolean, found {}", actual) } - AnalyzerError::ExpectedFunction { actual, .. } => { - write!(f, "Expected function, found {}", actual) - } - AnalyzerError::ExpectedFloat { actual, .. } => { - write!(f, "Expected float, found {}", actual) - } AnalyzerError::ExpectedIdentifier { actual, .. } => { write!(f, "Expected identifier, found {}", actual) } - AnalyzerError::ExpectedInteger { actual, .. } => { - write!(f, "Expected integer, found {}", actual) - } - AnalyzerError::ExpectedNumber { actual, .. } => { - write!(f, "Expected integer or float, found {}", actual) - } - AnalyzerError::ExpectedNumberOrString { actual, .. } => { - write!(f, "Expected integer, float, or string, found {}", actual) - } - AnalyzerError::ExpectedString { actual, .. } => { - write!(f, "Expected string, found {}", actual) - } AnalyzerError::ExpectedValue { actual, .. } => { write!(f, "Expected value, found {}", actual) } @@ -498,26 +450,10 @@ impl Display for AnalyzerError { #[cfg(test)] mod tests { - use crate::{BuiltInFunction, Identifier, Value}; + use crate::{Identifier, Value}; use super::*; - #[test] - fn is_even_wrong_type() { - let source = "is_even('hello')"; - - assert_eq!( - analyze(source), - Err(DustError::AnalyzerError { - analyzer_error: AnalyzerError::ExpectedInteger { - actual: Node::new(Statement::Constant(Value::string("hello")), (1, 1)), - position: (1, 1) - }, - source - }) - ); - } - #[test] fn length_no_arguments() { let source = "length()"; @@ -554,107 +490,73 @@ mod tests { #[test] fn integer_plus_boolean() { - let abstract_tree = AbstractSyntaxTree { - nodes: [Node::new( - Statement::BinaryOperation { - left: Box::new(Node::new(Statement::Constant(Value::integer(1)), (0, 1))), - operator: Node::new(BinaryOperator::Add, (1, 2)), - right: Box::new(Node::new(Statement::Constant(Value::boolean(true)), (3, 4))), - }, - (0, 2), - )] - .into(), - }; - let mut context = Context::new(); - let mut analyzer = Analyzer::new(&abstract_tree, &mut context); + let source = "42 + true"; assert_eq!( - analyzer.analyze(), - Err(AnalyzerError::ExpectedInteger { - actual: Node::new(Statement::Constant(Value::boolean(true)), (3, 4)), - position: (3, 4) + analyze(source), + Err(DustError::AnalyzerError { + analyzer_error: AnalyzerError::TypeConflict { + actual_statement: Node::new(Statement::Constant(Value::boolean(true)), (5, 9)), + actual_type: Type::Boolean, + expected: Type::Integer, + }, + source }) ) } #[test] fn is_even_expects_number() { - let abstract_tree = AbstractSyntaxTree { - nodes: [Node::new( - Statement::PropertyAccess( - Box::new(Node::new(Statement::Constant(Value::boolean(true)), (0, 1))), - Box::new(Node::new( - Statement::BuiltInFunctionCall { - function: BuiltInFunction::IsEven, - type_arguments: None, - value_arguments: None, - }, - (1, 2), - )), - ), - (0, 2), - )] - .into(), - }; - let mut context = Context::new(); - let mut analyzer = Analyzer::new(&abstract_tree, &mut context); + let source = "is_even('hello')"; assert_eq!( - analyzer.analyze(), - Err(AnalyzerError::ExpectedNumber { - actual: Node::new(Statement::Constant(Value::boolean(true)), (0, 1)), - position: (0, 1) + analyze(source), + Err(DustError::AnalyzerError { + analyzer_error: AnalyzerError::TypeConflict { + actual_statement: Node::new( + Statement::Constant(Value::string("hello")), + (8, 15) + ), + actual_type: Type::String, + expected: Type::Number, + }, + source }) - ) + ); } + #[test] fn is_odd_expects_number() { - let abstract_tree = AbstractSyntaxTree { - nodes: [Node::new( - Statement::PropertyAccess( - Box::new(Node::new(Statement::Constant(Value::boolean(true)), (0, 1))), - Box::new(Node::new( - Statement::BuiltInFunctionCall { - function: BuiltInFunction::IsOdd, - type_arguments: None, - value_arguments: None, - }, - (1, 2), - )), - ), - (0, 2), - )] - .into(), - }; - let mut context = Context::new(); - let mut analyzer = Analyzer::new(&abstract_tree, &mut context); + let source = "is_odd('hello')"; assert_eq!( - analyzer.analyze(), - Err(AnalyzerError::ExpectedNumber { - actual: Node::new(Statement::Constant(Value::boolean(true)), (0, 1)), - position: (0, 1) + analyze(source), + Err(DustError::AnalyzerError { + analyzer_error: AnalyzerError::TypeConflict { + actual_statement: Node::new( + Statement::Constant(Value::string("hello")), + (7, 14) + ), + actual_type: Type::String, + expected: Type::Number, + }, + source }) - ) + ); } #[test] fn undefined_variable() { - let abstract_tree = AbstractSyntaxTree { - nodes: [Node::new( - Statement::Identifier(Identifier::new("x")), - (0, 1), - )] - .into(), - }; - let mut context = Context::new(); - let mut analyzer = Analyzer::new(&abstract_tree, &mut context); + let source = "foo"; assert_eq!( - analyzer.analyze(), - Err(AnalyzerError::UndefinedVariable { - identifier: Node::new(Statement::Identifier(Identifier::new("x")), (0, 1)) + analyze(source), + Err(DustError::AnalyzerError { + analyzer_error: AnalyzerError::UndefinedVariable { + identifier: Node::new(Statement::Identifier(Identifier::new("foo")), (0, 3)), + }, + source }) - ) + ); } } diff --git a/dust-lang/src/built_in_function.rs b/dust-lang/src/built_in_function.rs index f973fb8..92e4210 100644 --- a/dust-lang/src/built_in_function.rs +++ b/dust-lang/src/built_in_function.rs @@ -49,7 +49,6 @@ impl BuiltInFunction { "value".into(), Type::List { item_type: Box::new(Type::Any), - length: 1, }, )] } diff --git a/dust-lang/src/type.rs b/dust-lang/src/type.rs index e6f3900..33201fe 100644 --- a/dust-lang/src/type.rs +++ b/dust-lang/src/type.rs @@ -48,7 +48,6 @@ pub enum Type { }, Integer, List { - length: usize, item_type: Box, }, Map(BTreeMap), @@ -138,15 +137,13 @@ impl Type { } ( Type::List { - length: left_length, item_type: left_type, }, Type::List { - length: right_length, item_type: right_type, }, ) => { - if left_length != right_length || left_type != right_type { + if left_type.check(right_type).is_err() { return Err(TypeConflict { actual: other.clone(), expected: self.clone(), @@ -249,7 +246,7 @@ impl Display for Type { } } Type::Integer => write!(f, "int"), - Type::List { length, item_type } => write!(f, "[{length}; {}]", item_type), + Type::List { item_type } => write!(f, "[{item_type}]"), Type::Map(map) => { write!(f, "{{ ")?; @@ -316,11 +313,9 @@ mod tests { assert_eq!(Type::Integer.check(&Type::Integer), Ok(())); assert_eq!( Type::List { - length: 4, item_type: Box::new(Type::Boolean), } .check(&Type::List { - length: 4, item_type: Box::new(Type::Boolean), }), Ok(()) @@ -362,7 +357,6 @@ mod tests { Type::Float, Type::Integer, Type::List { - length: 10, item_type: Box::new(Type::Integer), }, Type::Map(BTreeMap::new()), diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index 6e1b491..f1c9c17 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -640,7 +640,6 @@ impl ValueInner { let item_type = values.first().unwrap().r#type(context); Type::List { - length: values.len(), item_type: Box::new(item_type), } } diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index d4d0224..570a070 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -464,42 +464,6 @@ impl Vm { } } - if let ( - value, - Statement::BuiltInFunctionCall { - function, - type_arguments: _, - value_arguments: value_argument_nodes, - }, - ) = (left_value, right.inner) - { - let mut value_arguments = Vec::new(); - - value_arguments.push(value); - - if let Some(value_nodes) = value_argument_nodes { - for node in value_nodes { - let position = node.position; - let value = if let Some(value) = self.run_node(node, context)? { - value - } else { - return Err(VmError::ExpectedValue { position }); - }; - - value_arguments.push(value); - } - } - - let function_call_return = function.call(None, Some(value_arguments)).map_err( - |built_in_function_error| VmError::BuiltInFunctionError { - error: built_in_function_error, - position: right_span, - }, - )?; - - return Ok(function_call_return); - } - Err(VmError::ExpectedIdentifierOrInteger { position: right_span, }) @@ -648,7 +612,7 @@ mod tests { #[test] fn to_string() { - let input = "42.to_string()"; + let input = "to_string(42)"; assert_eq!( run(input, &mut Context::new()), @@ -824,7 +788,7 @@ mod tests { #[test] fn is_even() { - let input = "42.is_even()"; + let input = "is_even(42)"; assert_eq!( run(input, &mut Context::new()), @@ -834,7 +798,7 @@ mod tests { #[test] fn is_odd() { - let input = "42.is_odd()"; + let input = "is_odd(42)"; assert_eq!( run(input, &mut Context::new()), @@ -844,7 +808,7 @@ mod tests { #[test] fn length() { - let input = "[1, 2, 3].length()"; + let input = "length([1, 2, 3])"; assert_eq!(run(input, &mut Context::new()), Ok(Some(Value::integer(3)))); }