From ad409b69f3785df02d5aee7554503df022f3eb5c Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 12 Jul 2024 10:20:52 -0400 Subject: [PATCH] Run clippy and clean up everything --- dust-lang/src/abstract_tree/as.rs | 6 +- dust-lang/src/abstract_tree/assignment.rs | 2 +- dust-lang/src/abstract_tree/async_block.rs | 2 +- .../src/abstract_tree/built_in_function.rs | 2 +- .../src/abstract_tree/enum_declaration.rs | 6 +- dust-lang/src/abstract_tree/function_call.rs | 24 ++++-- dust-lang/src/abstract_tree/logic.rs | 8 +- dust-lang/src/abstract_tree/map_index.rs | 7 +- dust-lang/src/abstract_tree/math.rs | 33 ++++----- .../src/abstract_tree/structure_definition.rs | 4 +- dust-lang/src/abstract_tree/type.rs | 8 +- dust-lang/src/abstract_tree/type_alias.rs | 2 +- .../src/abstract_tree/type_constructor.rs | 6 +- dust-lang/src/abstract_tree/use.rs | 7 +- dust-lang/src/abstract_tree/value_node.rs | 29 +++----- dust-lang/src/abstract_tree/while.rs | 9 +-- dust-lang/src/context.rs | 14 +++- dust-lang/src/identifier.rs | 2 +- dust-lang/src/lexer.rs | 13 ++-- dust-lang/src/lib.rs | 14 ++-- dust-lang/src/parser/mod.rs | 73 +++++++------------ dust-lang/src/value.rs | 31 ++++++-- 22 files changed, 150 insertions(+), 152 deletions(-) diff --git a/dust-lang/src/abstract_tree/as.rs b/dust-lang/src/abstract_tree/as.rs index 537ed93..d3d8e9c 100644 --- a/dust-lang/src/abstract_tree/as.rs +++ b/dust-lang/src/abstract_tree/as.rs @@ -62,7 +62,7 @@ impl AbstractNode for As { ValidationError::ExpectedValueStatement(expression_position), )); }; - let r#type = self.constructor.construct(&context)?; + let r#type = self.constructor.construct(context)?; let (from_value, to_type): (&ValueInner, Type) = (value.inner().borrow(), r#type); let converted = match (from_value, to_type) { @@ -74,9 +74,7 @@ impl AbstractNode for As { } fn expected_type(&self, context: &Context) -> Result, ValidationError> { - self.constructor - .construct(&context) - .map(|r#type| Some(r#type)) + self.constructor.construct(context).map(Some) } } diff --git a/dust-lang/src/abstract_tree/assignment.rs b/dust-lang/src/abstract_tree/assignment.rs index 75a7c92..f472b31 100644 --- a/dust-lang/src/abstract_tree/assignment.rs +++ b/dust-lang/src/abstract_tree/assignment.rs @@ -52,7 +52,7 @@ impl AbstractNode for Assignment { scope: SourcePosition, ) -> Result<(), ValidationError> { let r#type = if let Some(constructor) = &self.constructor { - constructor.construct(&context)? + constructor.construct(context)? } else if let Some(r#type) = self.statement.expected_type(context)? { r#type } else { diff --git a/dust-lang/src/abstract_tree/async_block.rs b/dust-lang/src/abstract_tree/async_block.rs index d3dcc39..8872c64 100644 --- a/dust-lang/src/abstract_tree/async_block.rs +++ b/dust-lang/src/abstract_tree/async_block.rs @@ -48,7 +48,7 @@ impl AbstractNode for AsyncBlock { let statement_count = self.statements.len(); let error_option = self.statements.into_par_iter().enumerate().find_map_any( |(index, statement)| -> Option { - let result = statement.evaluate(&_context, false, scope); + let result = statement.evaluate(_context, false, scope); if let Err(error) = result { return Some(error); diff --git a/dust-lang/src/abstract_tree/built_in_function.rs b/dust-lang/src/abstract_tree/built_in_function.rs index 91b2f9e..d69b202 100644 --- a/dust-lang/src/abstract_tree/built_in_function.rs +++ b/dust-lang/src/abstract_tree/built_in_function.rs @@ -300,6 +300,6 @@ impl FunctionLogic for JsonParse { Ok(value) } - parse_value(&input, target_type).map(|value| Some(value)) + parse_value(input, target_type).map(Some) } } diff --git a/dust-lang/src/abstract_tree/enum_declaration.rs b/dust-lang/src/abstract_tree/enum_declaration.rs index 9e4f43d..01f7084 100644 --- a/dust-lang/src/abstract_tree/enum_declaration.rs +++ b/dust-lang/src/abstract_tree/enum_declaration.rs @@ -36,7 +36,7 @@ impl AbstractNode for EnumDeclaration { &self, context: &Context, _: bool, - scope: SourcePosition, + _scope: SourcePosition, ) -> Result<(), ValidationError> { let EnumDeclaration { name, @@ -60,7 +60,7 @@ impl AbstractNode for EnumDeclaration { let mut types = Vec::with_capacity(content.len()); for constructor in content { - let r#type = constructor.construct(&context)?; + let r#type = constructor.construct(context)?; types.push(r#type); } @@ -95,7 +95,7 @@ impl AbstractNode for EnumDeclaration { self, _: &Context, _: bool, - scope: SourcePosition, + _scope: SourcePosition, ) -> Result, RuntimeError> { Ok(None) } diff --git a/dust-lang/src/abstract_tree/function_call.rs b/dust-lang/src/abstract_tree/function_call.rs index ccfd944..5506b16 100644 --- a/dust-lang/src/abstract_tree/function_call.rs +++ b/dust-lang/src/abstract_tree/function_call.rs @@ -73,7 +73,7 @@ impl AbstractNode for FunctionCall { }); } - for (identifier, constructor) in parameters.into_iter().zip(arguments.into_iter()) { + for (identifier, constructor) in parameters.into_iter().zip(arguments.iter()) { let r#type = constructor.construct(context)?; context.set_type(identifier, r#type, self.function_expression.position())?; @@ -82,9 +82,7 @@ impl AbstractNode for FunctionCall { match (value_parameters, &self.value_arguments) { (Some(parameters), Some(arguments)) => { - for ((identifier, _), expression) in - parameters.iter().zip(arguments.into_iter()) - { + for ((identifier, _), expression) in parameters.iter().zip(arguments.iter()) { let r#type = if let Some(r#type) = expression.expected_type(context)? { r#type } else { @@ -241,7 +239,7 @@ impl AbstractNode for FunctionCall { return function .call(context, manage_memory) - .map(|option| option.map(|value| Evaluation::Return(value))); + .map(|option| option.map(Evaluation::Return)); } Err(RuntimeError::ValidationFailure( @@ -277,7 +275,7 @@ impl AbstractNode for FunctionCall { }) = return_type.clone().map(|r#box| *r#box) { if let (Some(parameters), Some(arguments)) = (type_parameters, &self.type_arguments) { - for (identifier, constructor) in parameters.into_iter().zip(arguments.into_iter()) { + for (identifier, constructor) in parameters.into_iter().zip(arguments.iter()) { if identifier == return_identifier { let r#type = constructor.construct(context)?; @@ -347,6 +345,18 @@ impl PartialOrd for FunctionCall { impl Ord for FunctionCall { fn cmp(&self, other: &Self) -> Ordering { - todo!() + let expression_cmp = self.function_expression.cmp(&other.function_expression); + + if expression_cmp.is_eq() { + let type_arg_cmp = self.type_arguments.cmp(&other.type_arguments); + + if type_arg_cmp.is_eq() { + self.value_arguments.cmp(&other.value_arguments) + } else { + type_arg_cmp + } + } else { + expression_cmp + } } } diff --git a/dust-lang/src/abstract_tree/logic.rs b/dust-lang/src/abstract_tree/logic.rs index 1db1391..c103d25 100644 --- a/dust-lang/src/abstract_tree/logic.rs +++ b/dust-lang/src/abstract_tree/logic.rs @@ -126,7 +126,7 @@ impl AbstractNode for Logic { ) -> Result, RuntimeError> { let run_and_expect_value = |expression: Expression| -> Result { let expression_position = expression.position(); - let evaluation = expression.evaluate(&mut context.clone(), _manage_memory, scope)?; + let evaluation = expression.evaluate(context, _manage_memory, scope)?; let value = if let Some(Evaluation::Return(value)) = evaluation { value } else { @@ -140,7 +140,7 @@ impl AbstractNode for Logic { let run_and_expect_boolean = |expression: Expression| -> Result { let expression_position = expression.position(); - let evaluation = expression.evaluate(&mut context.clone(), _manage_memory, scope)?; + let evaluation = expression.evaluate(context, _manage_memory, scope)?; let value = if let Some(Evaluation::Return(value)) = evaluation { value } else { @@ -152,12 +152,12 @@ impl AbstractNode for Logic { if let ValueInner::Boolean(boolean) = value.inner().as_ref() { Ok(*boolean) } else { - return Err(RuntimeError::ValidationFailure( + Err(RuntimeError::ValidationFailure( ValidationError::ExpectedBoolean { actual: value.r#type(context)?, position: expression_position, }, - )); + )) } }; diff --git a/dust-lang/src/abstract_tree/map_index.rs b/dust-lang/src/abstract_tree/map_index.rs index ca99b98..1c0d3ac 100644 --- a/dust-lang/src/abstract_tree/map_index.rs +++ b/dust-lang/src/abstract_tree/map_index.rs @@ -81,10 +81,7 @@ impl AbstractNode for MapIndex { if let (ValueInner::Map(map), Expression::Identifier(index)) = (collection.inner().as_ref(), self.index) { - let eval_option = map - .get(&index.node) - .cloned() - .map(|value| Evaluation::Return(value)); + let eval_option = map.get(&index.node).cloned().map(Evaluation::Return); Ok(eval_option) } else { @@ -133,7 +130,7 @@ impl AbstractNode for MapIndex { for (property, constructor_option, expression) in properties { if property == &index.node { return if let Some(constructor) = constructor_option { - let r#type = constructor.clone().construct(&context)?; + let r#type = constructor.clone().construct(context)?; Ok(Some(r#type)) } else { diff --git a/dust-lang/src/abstract_tree/math.rs b/dust-lang/src/abstract_tree/math.rs index 7033024..94b7e45 100644 --- a/dust-lang/src/abstract_tree/math.rs +++ b/dust-lang/src/abstract_tree/math.rs @@ -25,7 +25,7 @@ impl AbstractNode for Math { &self, context: &Context, _manage_memory: bool, - scope: SourcePosition, + _scope: SourcePosition, ) -> Result<(), ValidationError> { match self { Math::Add(left, right) => { @@ -90,20 +90,19 @@ impl AbstractNode for Math { _manage_memory: bool, scope: SourcePosition, ) -> Result, RuntimeError> { - let run_and_expect_value = |position: SourcePosition, - expression: Expression| - -> Result { - let evaluation = expression.evaluate(&mut _context.clone(), _manage_memory, scope)?; - let value = if let Some(Evaluation::Return(value)) = evaluation { - value - } else { - return Err(RuntimeError::ValidationFailure( - ValidationError::ExpectedValueStatement(position), - )); - }; + let run_and_expect_value = + |position: SourcePosition, expression: Expression| -> Result { + let evaluation = expression.evaluate(_context, _manage_memory, scope)?; + let value = if let Some(Evaluation::Return(value)) = evaluation { + value + } else { + return Err(RuntimeError::ValidationFailure( + ValidationError::ExpectedValueStatement(position), + )); + }; - Ok(value) - }; + Ok(value) + }; let value = match self { Math::Add(left, right) => { @@ -220,7 +219,7 @@ impl AbstractNode for Math { } (ValueInner::Integer(_) | ValueInner::Float(_), _) => { return Err(RuntimeError::ValidationFailure( - ValidationError::ExpectedIntegerOrFloat(right_position).into(), + ValidationError::ExpectedIntegerOrFloat(right_position), )) } _ => { @@ -259,7 +258,7 @@ impl AbstractNode for Math { } (ValueInner::Integer(_) | ValueInner::Float(_), _) => { return Err(RuntimeError::ValidationFailure( - ValidationError::ExpectedIntegerOrFloat(right_position).into(), + ValidationError::ExpectedIntegerOrFloat(right_position), )) } _ => { @@ -298,7 +297,7 @@ impl AbstractNode for Math { } (ValueInner::Integer(_) | ValueInner::Float(_), _) => { return Err(RuntimeError::ValidationFailure( - ValidationError::ExpectedIntegerOrFloat(right_position).into(), + ValidationError::ExpectedIntegerOrFloat(right_position), )) } _ => { diff --git a/dust-lang/src/abstract_tree/structure_definition.rs b/dust-lang/src/abstract_tree/structure_definition.rs index 7927bc8..5840445 100644 --- a/dust-lang/src/abstract_tree/structure_definition.rs +++ b/dust-lang/src/abstract_tree/structure_definition.rs @@ -32,7 +32,7 @@ impl AbstractNode for StructureDefinition { let mut fields = Vec::with_capacity(self.fields.len()); for (identifier, constructor) in &self.fields { - let r#type = constructor.construct(&context)?; + let r#type = constructor.construct(context)?; fields.push((identifier.clone(), r#type)); } @@ -56,7 +56,7 @@ impl AbstractNode for StructureDefinition { let mut fields = Vec::with_capacity(self.fields.len()); for (identifier, constructor) in self.fields { - let r#type = constructor.construct(&context)?; + let r#type = constructor.construct(context)?; fields.push((identifier, r#type)); } diff --git a/dust-lang/src/abstract_tree/type.rs b/dust-lang/src/abstract_tree/type.rs index 65c479c..00fea89 100644 --- a/dust-lang/src/abstract_tree/type.rs +++ b/dust-lang/src/abstract_tree/type.rs @@ -63,7 +63,7 @@ impl Type { }, ) => match (left, right) { (Some(left), Some(right)) => { - if left.check(&right).is_ok() { + if left.check(right).is_ok() { return Ok(()); } } @@ -81,7 +81,7 @@ impl Type { } } (Type::ListOf(left), Type::ListOf(right)) => { - if left.check(&right).is_ok() { + if left.check(right).is_ok() { return Ok(()); } } @@ -143,7 +143,7 @@ impl Type { }, Type::ListOf(left_type), ) => { - if right_type.check(&left_type).is_err() { + if right_type.check(left_type).is_err() { return Err(TypeConflict { actual: other.clone(), expected: self.clone(), @@ -247,7 +247,7 @@ impl Display for Type { Type::Map(map) => { write!(f, "{{ ")?; - for (index, (key, r#type)) in map.into_iter().enumerate() { + for (index, (key, r#type)) in map.iter().enumerate() { write!(f, "{key}: {type}")?; if index != map.len() - 1 { diff --git a/dust-lang/src/abstract_tree/type_alias.rs b/dust-lang/src/abstract_tree/type_alias.rs index 7ec05be..c1e4ba8 100644 --- a/dust-lang/src/abstract_tree/type_alias.rs +++ b/dust-lang/src/abstract_tree/type_alias.rs @@ -32,7 +32,7 @@ impl AbstractNode for TypeAlias { _: bool, scope: SourcePosition, ) -> Result<(), ValidationError> { - let r#type = self.constructor.construct(&context)?; + let r#type = self.constructor.construct(context)?; context.set_type(self.identifier.node.clone(), r#type, scope)?; diff --git a/dust-lang/src/abstract_tree/type_constructor.rs b/dust-lang/src/abstract_tree/type_constructor.rs index 1f31e2e..b0a9979 100644 --- a/dust-lang/src/abstract_tree/type_constructor.rs +++ b/dust-lang/src/abstract_tree/type_constructor.rs @@ -86,7 +86,7 @@ impl TypeConstructor { let type_parameters = declared_type_parameters.as_ref().map(|identifiers| { identifiers - .into_iter() + .iter() .map(|identifier| identifier.node.clone()) .collect() }); @@ -95,7 +95,7 @@ impl TypeConstructor { let mut parameters = Vec::with_capacity(declared_value_parameters.len()); for (identifier, constructor) in declared_value_parameters { - let r#type = constructor.construct(&context)?; + let r#type = constructor.construct(context)?; parameters.push((identifier.node.clone(), r#type)); } @@ -127,7 +127,7 @@ impl TypeConstructor { } } TypeConstructor::ListOf(item_type) => { - let item_type = item_type.node.construct(&context)?; + let item_type = item_type.node.construct(context)?; Type::ListOf(Box::new(item_type)) } diff --git a/dust-lang/src/abstract_tree/use.rs b/dust-lang/src/abstract_tree/use.rs index 0f70107..877511d 100644 --- a/dust-lang/src/abstract_tree/use.rs +++ b/dust-lang/src/abstract_tree/use.rs @@ -4,7 +4,6 @@ use std::{ sync::{Arc, RwLock}, }; -use chumsky::prelude::*; use serde::{Deserialize, Serialize}; use crate::{ @@ -99,18 +98,18 @@ impl Eq for Use {} impl PartialEq for Use { fn eq(&self, other: &Self) -> bool { - todo!() + self.path == other.path } } impl PartialOrd for Use { fn partial_cmp(&self, other: &Self) -> Option { - todo!() + Some(self.cmp(other)) } } impl Ord for Use { fn cmp(&self, other: &Self) -> std::cmp::Ordering { - todo!() + self.path.cmp(&other.path) } } diff --git a/dust-lang/src/abstract_tree/value_node.rs b/dust-lang/src/abstract_tree/value_node.rs index 8ce19e5..e573323 100644 --- a/dust-lang/src/abstract_tree/value_node.rs +++ b/dust-lang/src/abstract_tree/value_node.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use crate::{ context::Context, error::{RuntimeError, ValidationError}, - identifier::{self, Identifier}, + identifier::Identifier, Value, }; @@ -66,7 +66,7 @@ impl AbstractNode for ValueNode { scope: SourcePosition, ) -> Result<(), ValidationError> { if let ValueNode::List(list) = self { - let mut items = list.into_iter(); + let mut items = list.iter(); let first_item = if let Some(item) = items.next() { item } else { @@ -161,7 +161,7 @@ impl AbstractNode for ValueNode { expression.position(), )); }; - let expected_type = constructor.clone().construct(&context)?; + let expected_type = constructor.clone().construct(context)?; expected_type.check(&actual_type).map_err(|conflict| { ValidationError::TypeCheck { @@ -375,7 +375,7 @@ impl AbstractNode for ValueNode { let mut parameters = Vec::with_capacity(value_parameters.len()); for (identifier, constructor) in value_parameters { - let r#type = constructor.construct(&outer_context)?; + let r#type = constructor.construct(outer_context)?; parameters.push((identifier, r#type)); } @@ -385,7 +385,7 @@ impl AbstractNode for ValueNode { None }; let return_type = if let Some(constructor) = return_type { - Some(constructor.construct(&outer_context)?) + Some(constructor.construct(outer_context)?) } else { None }; @@ -456,12 +456,10 @@ impl AbstractNode for ValueNode { for (identifier, constructor_option, expression) in fields { let r#type = if let Some(constructor) = constructor_option { constructor.construct(context)? + } else if let Some(r#type) = expression.expected_type(context)? { + r#type } else { - if let Some(r#type) = expression.expected_type(context)? { - r#type - } else { - return Err(ValidationError::CannotAssignToNone(expression.position())); - } + return Err(ValidationError::CannotAssignToNone(expression.position())); }; field_types.insert(identifier.clone(), r#type); @@ -481,7 +479,7 @@ impl AbstractNode for ValueNode { let mut parameters = Vec::with_capacity(value_parameters.len()); for (identifier, type_constructor) in value_parameters { - let r#type = type_constructor.clone().construct(&context)?; + let r#type = type_constructor.clone().construct(context)?; parameters.push((identifier.clone(), r#type)); } @@ -490,12 +488,9 @@ impl AbstractNode for ValueNode { } else { None }; - let type_parameters = type_parameters.clone().map(|parameters| { - parameters - .iter() - .map(|identifier| identifier.clone()) - .collect() - }); + let type_parameters = type_parameters + .clone() + .map(|parameters| parameters.into_iter().collect()); let return_type = if let Some(constructor) = return_type { Some(Box::new(constructor.construct(context)?)) } else { diff --git a/dust-lang/src/abstract_tree/while.rs b/dust-lang/src/abstract_tree/while.rs index bacca7f..7669eef 100644 --- a/dust-lang/src/abstract_tree/while.rs +++ b/dust-lang/src/abstract_tree/while.rs @@ -51,10 +51,7 @@ impl AbstractNode for While { ) -> Result, RuntimeError> { let get_boolean = || -> Result { let expression_position = self.expression.position(); - let evaluation = - self.expression - .clone() - .evaluate(&mut _context.clone(), false, scope)?; + let evaluation = self.expression.clone().evaluate(_context, false, scope)?; if let Some(Evaluation::Return(value)) = evaluation { Ok(value) @@ -67,9 +64,7 @@ impl AbstractNode for While { while let ValueInner::Boolean(true) = get_boolean()?.inner().as_ref() { for statement in &self.statements { - let evaluation = statement - .clone() - .evaluate(&mut _context.clone(), false, scope)?; + let evaluation = statement.clone().evaluate(_context, false, scope)?; if let Some(Evaluation::Break) = evaluation { return Ok(evaluation); diff --git a/dust-lang/src/context.rs b/dust-lang/src/context.rs index f7004ee..8e105ba 100644 --- a/dust-lang/src/context.rs +++ b/dust-lang/src/context.rs @@ -17,10 +17,12 @@ use crate::{ Value, }; +type VariableInfo = (VariableData, UsageData, SourcePosition); + #[derive(Clone, Debug)] pub struct Context { id: u32, - variables: Arc>>, + variables: Arc>>, is_clean: Arc>, } @@ -138,7 +140,7 @@ impl Context { usage_data.inner().write()?.actual += 1; *self.is_clean.write()? = false; - return Ok(Some(value.clone())); + Ok(Some(value.clone())) } else { Ok(None) } @@ -202,7 +204,7 @@ impl Context { if let Some((_, usage_data, _)) = variables.get(identifier) { usage_data.inner().write()?.expected += 1; - return Ok(true); + Ok(true) } else { Ok(false) } @@ -295,3 +297,9 @@ impl UsageData { }))) } } + +impl Default for UsageData { + fn default() -> Self { + UsageData::new() + } +} diff --git a/dust-lang/src/identifier.rs b/dust-lang/src/identifier.rs index 2ad0539..99991da 100644 --- a/dust-lang/src/identifier.rs +++ b/dust-lang/src/identifier.rs @@ -32,7 +32,7 @@ impl Identifier { } pub fn as_str(&self) -> &str { - &self.0.as_str() + self.0.as_str() } } diff --git a/dust-lang/src/lexer.rs b/dust-lang/src/lexer.rs index 0f81248..f298aa1 100644 --- a/dust-lang/src/lexer.rs +++ b/dust-lang/src/lexer.rs @@ -1,7 +1,4 @@ -use std::{ - f64::{INFINITY, NAN, NEG_INFINITY}, - fmt::{self, Display, Formatter}, -}; +use std::fmt::{self, Display, Formatter}; use chumsky::prelude::*; @@ -176,7 +173,7 @@ impl Display for Symbol { } } -pub fn lex<'src>(source: &'src str) -> Result, SimpleSpan)>, Vec> { +pub fn lex(source: &str) -> Result, Vec> { lexer() .parse(source) .into_result() @@ -222,9 +219,9 @@ pub fn lexer<'src>() -> impl Parser< let float = choice(( float_numeric, - just("Infinity").to(Token::Float(INFINITY)), - just("-Infinity").to(Token::Float(NEG_INFINITY)), - just("NaN").to(Token::Float(NAN)), + just("Infinity").to(Token::Float(f64::INFINITY)), + just("-Infinity").to(Token::Float(f64::NEG_INFINITY)), + just("NaN").to(Token::Float(f64::NAN)), )); let integer = just('-') diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 149d16f..c6b1318 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -29,9 +29,11 @@ pub fn interpret(source_id: &str, source: &str) -> Result, Interpr interpreter.run(Arc::from(source_id), Arc::from(source)) } +type Source = (Arc, Arc); + pub struct Interpreter { context: Context, - sources: Arc, Arc)>>>, + sources: Arc>>, } impl Interpreter { @@ -52,15 +54,15 @@ impl Interpreter { sources.clear(); sources.push((source_id.clone(), Arc::from(source))); - lex(source.as_ref()) + lex(source) .map(|tokens| tokens.into_iter().map(|(token, _)| token).collect()) .map_err(|errors| InterpreterError { source_id, errors }) } - pub fn parse<'src>( + pub fn parse( &self, source_id: Arc, - source: &'src str, + source: &str, ) -> Result { let mut sources = self.sources.write().unwrap(); @@ -229,9 +231,9 @@ impl InterpreterError { ValidationError::CannotAssignToNone(postion) => { builder.add_label( Label::new((self.source_id.clone(), postion.0..postion.1)) - .with_message(format!( + .with_message( "This statement does not yield a value, you cannot assign a variable to it." - )), + ), ); } ValidationError::ExpectedBoolean { actual, position } => { diff --git a/dust-lang/src/parser/mod.rs b/dust-lang/src/parser/mod.rs index 5351879..6f9b7bc 100644 --- a/dust-lang/src/parser/mod.rs +++ b/dust-lang/src/parser/mod.rs @@ -30,7 +30,7 @@ pub fn parse<'src>( .map_err(|errors| { errors .into_iter() - .map(|error| DustError::from(error)) + .map(DustError::from) .collect::>() }) } @@ -41,13 +41,13 @@ pub fn parser<'src>( let comment = select_ref! { Token::Comment(_) => {} }; + let identifier = select! { Token::Identifier(text) => Identifier::from(text), }; - let positioned_identifier = identifier - .clone() - .map_with(|identifier, state| identifier.with_position(state.span())); + let positioned_identifier = + identifier.map_with(|identifier, state| identifier.with_position(state.span())); let basic_value = select! { Token::Boolean(boolean) => ValueNode::Boolean(boolean), @@ -74,10 +74,13 @@ pub fn parser<'src>( TypeConstructor::Raw(raw_constructor.with_position(state.span())) }); + type TypeParameters = Vec>; + type ValueParameters = Vec<(WithPosition, TypeConstructor)>; + type FunctionTypeParameters = (Option, ValueParameters); + let function_type = just(Token::Keyword(Keyword::Fn)) .ignore_then( positioned_identifier - .clone() .separated_by(just(Token::Symbol(Symbol::Comma))) .at_least(1) .collect() @@ -89,7 +92,6 @@ pub fn parser<'src>( ) .then( positioned_identifier - .clone() .then_ignore(just(Token::Symbol(Symbol::Colon))) .then(type_constructor.clone()) .separated_by(just(Token::Symbol(Symbol::Comma))) @@ -106,10 +108,7 @@ pub fn parser<'src>( ) .map_with( |((type_parameters, value_parameters), return_type): ( - ( - Option>>, - Vec<(WithPosition, TypeConstructor)>, - ), + FunctionTypeParameters, Option, ), state| { @@ -123,7 +122,7 @@ pub fn parser<'src>( FunctionTypeConstructor { type_parameters, value_parameters, - return_type: return_type.map(|r#type| Box::new(r#type)), + return_type: return_type.map(Box::new), } .with_position(state.span()), ) @@ -133,7 +132,7 @@ pub fn parser<'src>( let list_type = type_constructor .clone() .then_ignore(just(Token::Symbol(Symbol::Semicolon))) - .then(raw_integer.clone()) + .then(raw_integer) .delimited_by( just(Token::Symbol(Symbol::SquareOpen)), just(Token::Symbol(Symbol::SquareClose)), @@ -159,7 +158,6 @@ pub fn parser<'src>( }); let type_invokation = positioned_identifier - .clone() .then( type_constructor .clone() @@ -181,7 +179,6 @@ pub fn parser<'src>( }); let map_type = positioned_identifier - .clone() .then_ignore(just(Token::Symbol(Symbol::Colon))) .then(type_constructor.clone()) .separated_by(just(Token::Symbol(Symbol::Comma))) @@ -210,8 +207,6 @@ pub fn parser<'src>( just(Token::Symbol(Symbol::Colon)).ignore_then(type_constructor.clone()); let statement = recursive(|statement| { - let allow_built_ins = allow_built_ins.clone(); - let block = statement .clone() .repeated() @@ -224,14 +219,11 @@ pub fn parser<'src>( .map_with(|statements, state| Block::new(statements).with_position(state.span())); let expression = recursive(|expression| { - let allow_built_ins = allow_built_ins.clone(); - - let identifier_expression = identifier.clone().map_with(|identifier, state| { + let identifier_expression = identifier.map_with(|identifier, state| { Expression::Identifier(identifier.with_position(state.span())) }); let range = raw_integer - .clone() .then_ignore(just(Token::Symbol(Symbol::DoubleDot))) .then(raw_integer) .map_with(|(start, end), state| { @@ -252,7 +244,6 @@ pub fn parser<'src>( }); let map_fields = identifier - .clone() .then(type_specification.clone().or_not()) .then_ignore(just(Token::Symbol(Symbol::Equal))) .then(expression.clone()) @@ -272,10 +263,13 @@ pub fn parser<'src>( ) }); + type TypeParameters = Vec; + type ValueParameters = Vec<(Identifier, TypeConstructor)>; + type FunctionParameters = (Option, ValueParameters); + let function = just(Token::Keyword(Keyword::Fn)) .ignore_then( identifier - .clone() .separated_by(just(Token::Symbol(Symbol::Comma))) .at_least(1) .allow_trailing() @@ -288,7 +282,6 @@ pub fn parser<'src>( ) .then( identifier - .clone() .then_ignore(just(Token::Symbol(Symbol::Colon))) .then(type_constructor.clone()) .separated_by(just(Token::Symbol(Symbol::Comma))) @@ -307,10 +300,7 @@ pub fn parser<'src>( .then(block.clone()) .map_with( |(((type_parameters, value_parameters), return_type), body): ( - ( - (Option>, Vec<(Identifier, TypeConstructor)>), - Option, - ), + (FunctionParameters, Option), WithPosition, ), state| { @@ -333,9 +323,8 @@ pub fn parser<'src>( ); let enum_instance = positioned_identifier - .clone() .then_ignore(just(Token::Symbol(Symbol::DoubleColon))) - .then(positioned_identifier.clone()) + .then(positioned_identifier) .then( expression .clone() @@ -427,14 +416,14 @@ pub fn parser<'src>( ); let atom = choice(( - built_in_function.clone(), + built_in_function, enum_instance.clone(), - range.clone(), + range, function.clone(), list.clone(), map.clone(), - basic_value.clone(), - identifier_expression.clone(), + basic_value, + identifier_expression, expression.clone().delimited_by( just(Token::Symbol(Symbol::ParenOpen)), just(Token::Symbol(Symbol::ParenClose)), @@ -630,9 +619,7 @@ pub fn parser<'src>( )) }); - let expression_statement = expression - .clone() - .map(|expression| Statement::Expression(expression)); + let expression_statement = expression.clone().map(Statement::Expression); let async_block = just(Token::Keyword(Keyword::Async)) .ignore_then(statement.clone().repeated().collect().delimited_by( @@ -647,7 +634,6 @@ pub fn parser<'src>( .map_with(|_, state| Statement::Break(().with_position(state.span()))); let assignment = positioned_identifier - .clone() .then(type_specification.clone().or_not()) .then(choice(( just(Token::Symbol(Symbol::Equal)).to(AssignmentOperator::Assign), @@ -662,7 +648,7 @@ pub fn parser<'src>( ) }); - let block_statement = block.clone().map(|block| Statement::Block(block)); + let block_statement = block.clone().map(Statement::Block); let r#loop = statement .clone() @@ -715,7 +701,7 @@ pub fn parser<'src>( ); let type_alias = just(Token::Keyword(Keyword::Type)) - .ignore_then(positioned_identifier.clone()) + .ignore_then(positioned_identifier) .then_ignore(just(Token::Symbol(Symbol::Equal))) .then(type_constructor.clone()) .map_with(|(identifier, constructor), state| { @@ -725,7 +711,6 @@ pub fn parser<'src>( }); let enum_variant = positioned_identifier - .clone() .then( type_constructor .clone() @@ -743,10 +728,9 @@ pub fn parser<'src>( }); let enum_declaration = just(Token::Keyword(Keyword::Enum)) - .ignore_then(positioned_identifier.clone()) + .ignore_then(positioned_identifier) .then( positioned_identifier - .clone() .separated_by(just(Token::Symbol(Symbol::Comma))) .allow_trailing() .collect() @@ -801,8 +785,5 @@ pub fn parser<'src>( ))) }); - statement - .repeated() - .collect() - .map(|statements| AbstractTree::new(statements)) + statement.repeated().collect().map(AbstractTree::new) } diff --git a/dust-lang/src/value.rs b/dust-lang/src/value.rs index ac3785b..c90d28a 100644 --- a/dust-lang/src/value.rs +++ b/dust-lang/src/value.rs @@ -16,8 +16,7 @@ use serde::{ use crate::{ abstract_tree::{ - AbstractNode, Block, BuiltInFunction, Evaluation, SourcePosition, Type, - WithPosition, + AbstractNode, Block, BuiltInFunction, Evaluation, SourcePosition, Type, WithPosition, }, context::Context, error::{RuntimeError, ValidationError}, @@ -157,7 +156,7 @@ impl Display for Value { ValueInner::List(list) => { write!(f, "[")?; - for (index, value) in list.into_iter().enumerate() { + for (index, value) in list.iter().enumerate() { if index == list.len() - 1 { write!(f, "{}", value)?; } else { @@ -170,7 +169,7 @@ impl Display for Value { ValueInner::Map(map) => { write!(f, "{{ ")?; - for (index, (key, value)) in map.into_iter().enumerate() { + for (index, (key, value)) in map.iter().enumerate() { write!(f, "{key} = {value}")?; if index != map.len() - 1 { @@ -194,7 +193,7 @@ impl Display for Value { if let Some(type_parameters) = type_parameters { write!(f, "<")?; - for (index, identifier) in type_parameters.into_iter().enumerate() { + for (index, identifier) in type_parameters.iter().enumerate() { if index == type_parameters.len() - 1 { write!(f, "{}", identifier)?; } else { @@ -629,7 +628,7 @@ impl ValueInner { ValueInner::Range(_) => Type::Range, ValueInner::String(_) => Type::String, ValueInner::Function(function) => { - let return_type = function.return_type.clone().map(|r#type| Box::new(r#type)); + let return_type = function.return_type.clone().map(Box::new); Type::Function { type_parameters: function.type_parameters().clone(), @@ -856,6 +855,24 @@ impl PartialOrd for Function { impl Ord for Function { fn cmp(&self, other: &Self) -> Ordering { - todo!() + let type_param_cmp = self.type_parameters().cmp(&other.type_parameters); + + if type_param_cmp.is_eq() { + let value_param_cmp = self.value_parameters.cmp(&other.value_parameters); + + if value_param_cmp.is_eq() { + let return_type_cmp = self.return_type.cmp(&other.return_type); + + if return_type_cmp.is_eq() { + self.body.cmp(&other.body) + } else { + return_type_cmp + } + } else { + value_param_cmp + } + } else { + type_param_cmp + } } }