From ed4dd6a819869b43a69c85d5f0894cc633df528c Mon Sep 17 00:00:00 2001 From: Jeff Date: Tue, 5 Dec 2023 17:08:22 -0500 Subject: [PATCH] Improve internal API --- src/abstract_tree/assignment.rs | 24 +++++++++++++++++------- src/abstract_tree/block.rs | 6 +++--- src/abstract_tree/expression.rs | 5 ++--- src/abstract_tree/for.rs | 8 +++----- src/abstract_tree/function_call.rs | 24 ++++++------------------ src/abstract_tree/identifier.rs | 8 ++++---- src/abstract_tree/if_else.rs | 4 ++-- src/abstract_tree/index.rs | 4 ++-- src/abstract_tree/index_assignment.rs | 6 +++--- src/abstract_tree/logic.rs | 6 +++--- src/abstract_tree/match.rs | 4 ++-- src/abstract_tree/math.rs | 4 ++-- src/abstract_tree/mod.rs | 4 ++-- src/abstract_tree/statement.rs | 4 ++-- src/abstract_tree/type_defintion.rs | 18 ++++-------------- src/abstract_tree/use.rs | 6 +++--- src/abstract_tree/value_node.rs | 24 ++++++++++++------------ src/abstract_tree/while.rs | 4 ++-- src/abstract_tree/yield.rs | 4 ++-- src/built_in_functions/assert.rs | 14 +++++++------- src/built_in_functions/collections.rs | 8 ++++---- src/built_in_functions/data_formats.rs | 14 +++++++------- src/built_in_functions/fs.rs | 20 ++++++++++---------- src/built_in_functions/mod.rs | 4 ++-- src/built_in_functions/network.rs | 8 ++++---- src/built_in_functions/output.rs | 8 ++++---- src/built_in_functions/random.rs | 26 +++++++++++++------------- src/built_in_functions/type.rs | 8 ++++---- src/value/mod.rs | 12 ++++++------ 29 files changed, 137 insertions(+), 152 deletions(-) diff --git a/src/abstract_tree/assignment.rs b/src/abstract_tree/assignment.rs index 28c1ed0..5783122 100644 --- a/src/abstract_tree/assignment.rs +++ b/src/abstract_tree/assignment.rs @@ -63,7 +63,12 @@ impl AbstractTree for Assignment { match operator { AssignmentOperator::Equal => { - type_definition.check(&statement_type, context, statement_node, source)?; + type_definition.inner().check( + &statement_type, + context, + statement_node, + source, + )?; } AssignmentOperator::PlusEqual => { let identifier_type = identifier.expected_type(context)?; @@ -71,26 +76,31 @@ impl AbstractTree for Assignment { if let Type::List(item_type) = type_definition.inner() { let item_type_definition = TypeDefinition::new(*item_type.clone()); - item_type_definition.check( + item_type_definition.inner().check( &identifier_type, context, identifier_node, source, )?; - item_type_definition.check( + item_type_definition.inner().check( &statement_type, context, statement_node, source, )?; } else { - type_definition.check( + type_definition.inner().check( &identifier_type, context, identifier_node, source, )?; - type_definition.check(&statement_type, context, statement_node, source)?; + type_definition.inner().check( + &statement_type, + context, + statement_node, + source, + )?; } } AssignmentOperator::MinusEqual => todo!(), @@ -134,8 +144,8 @@ impl AbstractTree for Assignment { Ok(Value::Empty) } - fn expected_type(&self, _context: &Map) -> Result { - Ok(TypeDefinition::new(Type::Empty)) + fn expected_type(&self, _context: &Map) -> Result { + Ok(Type::Empty) } } diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index 8d017d5..42dc1ae 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -4,7 +4,7 @@ use rayon::prelude::*; use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Map, Result, Statement, Type, TypeDefinition, Value}; +use crate::{AbstractTree, Error, Map, Result, Statement, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Block { @@ -82,9 +82,9 @@ impl AbstractTree for Block { } } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { if self.is_async { - Ok(TypeDefinition::new(Type::Any)) + Ok(Type::Any) } else { self.statements.last().unwrap().expected_type(context) } diff --git a/src/abstract_tree/expression.rs b/src/abstract_tree/expression.rs index 6954a91..e62d9d3 100644 --- a/src/abstract_tree/expression.rs +++ b/src/abstract_tree/expression.rs @@ -2,8 +2,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; use crate::{ - value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, TypeDefinition, - Value, Yield, + value_node::ValueNode, AbstractTree, Error, Identifier, Index, Map, Result, Type, Value, Yield, }; use super::{function_call::FunctionCall, logic::Logic, math::Math}; @@ -72,7 +71,7 @@ impl AbstractTree for Expression { } } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { match self { Expression::Value(value_node) => value_node.expected_type(context), Expression::Identifier(identifier) => identifier.expected_type(context), diff --git a/src/abstract_tree/for.rs b/src/abstract_tree/for.rs index 92c0935..7271125 100644 --- a/src/abstract_tree/for.rs +++ b/src/abstract_tree/for.rs @@ -2,9 +2,7 @@ use rayon::prelude::*; use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{ - AbstractTree, Block, Error, Expression, Identifier, Map, Result, Type, TypeDefinition, Value, -}; +use crate::{AbstractTree, Block, Error, Expression, Identifier, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct For { @@ -79,7 +77,7 @@ impl AbstractTree for For { Ok(Value::Empty) } - fn expected_type(&self, _context: &Map) -> Result { - Ok(TypeDefinition::new(Type::Empty)) + fn expected_type(&self, _context: &Map) -> Result { + Ok(Type::Empty) } } diff --git a/src/abstract_tree/function_call.rs b/src/abstract_tree/function_call.rs index 38539b7..c01682c 100644 --- a/src/abstract_tree/function_call.rs +++ b/src/abstract_tree/function_call.rs @@ -1,9 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{ - AbstractTree, Error, Map, Result, Type, TypeDefinition, Value, ValueNode, BUILT_IN_FUNCTIONS, -}; +use crate::{AbstractTree, Error, Map, Result, Type, Value, ValueNode, BUILT_IN_FUNCTIONS}; use super::expression::Expression; @@ -41,7 +39,7 @@ impl AbstractTree for FunctionCall { } } - let function_type = function_expression.expected_type(context)?.take_inner(); + let function_type = function_expression.expected_type(context)?; if let Type::Function { parameter_types, @@ -53,7 +51,7 @@ impl AbstractTree for FunctionCall { for (argument, r#type) in argument_type_pairs { let argument_type = argument.expected_type(context)?; - r#type.check(argument_type.inner(), context, node, source)?; + r#type.check(&argument_type, context, node, source)?; } } @@ -102,28 +100,18 @@ impl AbstractTree for FunctionCall { value.as_function()?.call(&self.arguments, source, context) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { match &self.function_expression { Expression::Value(value_node) => { if let ValueNode::Function(function) = value_node { let return_type = function.return_type()?.clone(); - Ok(TypeDefinition::new(return_type)) + Ok(return_type) } else { value_node.expected_type(context) } } - Expression::Identifier(identifier) => { - let function_name = identifier.inner(); - - if let Some(value) = context.variables()?.get(function_name) { - let return_type = value.as_function()?.return_type()?.clone(); - - Ok(TypeDefinition::new(return_type)) - } else { - self.function_expression.expected_type(context) - } - } + Expression::Identifier(identifier) => identifier.expected_type(context), Expression::Index(index) => index.expected_type(context), Expression::Math(math) => math.expected_type(context), Expression::Logic(logic) => logic.expected_type(context), diff --git a/src/abstract_tree/identifier.rs b/src/abstract_tree/identifier.rs index 2bb30e4..b377629 100644 --- a/src/abstract_tree/identifier.rs +++ b/src/abstract_tree/identifier.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Map, Result, Type, TypeDefinition, Value, BUILT_IN_FUNCTIONS}; +use crate::{AbstractTree, Error, Map, Result, Type, Value, BUILT_IN_FUNCTIONS}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Identifier(String); @@ -37,17 +37,17 @@ impl AbstractTree for Identifier { } } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { if let Some(value) = context.variables()?.get(&self.0) { value.r#type(context) } else { for built_in_function in BUILT_IN_FUNCTIONS { if self.0 == built_in_function.name() { - return Ok(built_in_function.type_definition()); + return Ok(built_in_function.r#type()); } } - Ok(TypeDefinition::new(Type::Any)) + Ok(Type::Any) } } } diff --git a/src/abstract_tree/if_else.rs b/src/abstract_tree/if_else.rs index 09f7b7d..a244d63 100644 --- a/src/abstract_tree/if_else.rs +++ b/src/abstract_tree/if_else.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Block, Expression, Map, Result, TypeDefinition, Value}; +use crate::{AbstractTree, Block, Expression, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct IfElse { @@ -81,7 +81,7 @@ impl AbstractTree for IfElse { } } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { self.if_block.expected_type(context) } } diff --git a/src/abstract_tree/index.rs b/src/abstract_tree/index.rs index bfb8bfc..ef63e90 100644 --- a/src/abstract_tree/index.rs +++ b/src/abstract_tree/index.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, List, Map, Result, TypeDefinition, Value}; +use crate::{AbstractTree, Error, Expression, List, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Index { @@ -78,7 +78,7 @@ impl AbstractTree for Index { } } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { self.collection.expected_type(context) } } diff --git a/src/abstract_tree/index_assignment.rs b/src/abstract_tree/index_assignment.rs index d5500ee..a5588ff 100644 --- a/src/abstract_tree/index_assignment.rs +++ b/src/abstract_tree/index_assignment.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Index, Map, Result, Statement, Type, TypeDefinition, Value}; +use crate::{AbstractTree, Error, Index, Map, Result, Statement, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct IndexAssignment { @@ -91,7 +91,7 @@ impl AbstractTree for IndexAssignment { Ok(Value::Empty) } - fn expected_type(&self, _context: &Map) -> Result { - Ok(TypeDefinition::new(Type::Empty)) + fn expected_type(&self, _context: &Map) -> Result { + Ok(Type::Empty) } } diff --git a/src/abstract_tree/logic.rs b/src/abstract_tree/logic.rs index a26340c..53be0c2 100644 --- a/src/abstract_tree/logic.rs +++ b/src/abstract_tree/logic.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, Map, Result, Type, TypeDefinition, Value}; +use crate::{AbstractTree, Error, Expression, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Logic { @@ -85,8 +85,8 @@ impl AbstractTree for Logic { Ok(Value::Boolean(result)) } - fn expected_type(&self, _context: &Map) -> Result { - Ok(TypeDefinition::new(Type::Boolean)) + fn expected_type(&self, _context: &Map) -> Result { + Ok(Type::Boolean) } } diff --git a/src/abstract_tree/match.rs b/src/abstract_tree/match.rs index 71dd95f..a361e14 100644 --- a/src/abstract_tree/match.rs +++ b/src/abstract_tree/match.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Map, Result, TypeDefinition, Value}; +use crate::{AbstractTree, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Match {} @@ -20,7 +20,7 @@ impl AbstractTree for Match { todo!() } - fn expected_type(&self, _context: &Map) -> Result { + fn expected_type(&self, _context: &Map) -> Result { todo!() } } diff --git a/src/abstract_tree/math.rs b/src/abstract_tree/math.rs index b25b043..538441f 100644 --- a/src/abstract_tree/math.rs +++ b/src/abstract_tree/math.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Error, Expression, Map, Result, TypeDefinition, Value}; +use crate::{AbstractTree, Error, Expression, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Math { @@ -56,7 +56,7 @@ impl AbstractTree for Math { Ok(value) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { self.left.expected_type(context) } } diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index cf7439f..c2154f2 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -66,7 +66,7 @@ impl AbstractTree for Root { Ok(value) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { self.statements.last().unwrap().expected_type(context) } } @@ -88,5 +88,5 @@ pub trait AbstractTree: Sized { /// Execute dust code by traversing the tree. fn run(&self, source: &str, context: &Map) -> Result; - fn expected_type(&self, context: &Map) -> Result; + fn expected_type(&self, context: &Map) -> Result; } diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index 2dd6334..eb5e346 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -3,7 +3,7 @@ use tree_sitter::Node; use crate::{ AbstractTree, Assignment, Block, Error, Expression, For, IfElse, IndexAssignment, Map, Match, - Result, TypeDefinition, Use, Value, While, + Result, Type, Use, Value, While, }; /// Abstract representation of a statement. @@ -82,7 +82,7 @@ impl AbstractTree for Statement { } } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { match self { Statement::Assignment(assignment) => assignment.expected_type(context), Statement::Return(expression) => expression.expected_type(context), diff --git a/src/abstract_tree/type_defintion.rs b/src/abstract_tree/type_defintion.rs index 233e28b..75f2ac6 100644 --- a/src/abstract_tree/type_defintion.rs +++ b/src/abstract_tree/type_defintion.rs @@ -22,16 +22,6 @@ impl TypeDefinition { pub fn take_inner(self) -> Type { self.r#type } - - pub fn check( - &self, - other: &TypeDefinition, - context: &Map, - node: Node, - source: &str, - ) -> Result<()> { - self.r#type.check(&other.r#type, context, node, source) - } } impl AbstractTree for TypeDefinition { @@ -48,7 +38,7 @@ impl AbstractTree for TypeDefinition { self.r#type.run(source, context) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { self.r#type.expected_type(context) } } @@ -175,7 +165,7 @@ impl AbstractTree for Type { "str" => Type::String, _ => { return Err(Error::UnexpectedSyntaxNode { - expected: "any, bool, float, fn, int, list, map, num, str or table", + expected: "any, bool, float, fn, int, list, map, num or str", actual: type_node.kind(), location: type_node.start_position(), relevant_source: source[type_node.byte_range()].to_string(), @@ -190,8 +180,8 @@ impl AbstractTree for Type { Ok(Value::Empty) } - fn expected_type(&self, _context: &Map) -> Result { - Ok(TypeDefinition::new(Type::Empty)) + fn expected_type(&self, _context: &Map) -> Result { + Ok(Type::Empty) } } diff --git a/src/abstract_tree/use.rs b/src/abstract_tree/use.rs index d5ef0a6..28adfd7 100644 --- a/src/abstract_tree/use.rs +++ b/src/abstract_tree/use.rs @@ -3,7 +3,7 @@ use std::fs::read_to_string; use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{evaluate_with_context, AbstractTree, Error, Map, Result, Type, TypeDefinition, Value}; +use crate::{evaluate_with_context, AbstractTree, Error, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Use { @@ -29,7 +29,7 @@ impl AbstractTree for Use { Ok(Value::Map(file_context)) } - fn expected_type(&self, _context: &Map) -> Result { - Ok(TypeDefinition::new(Type::Map)) + fn expected_type(&self, _context: &Map) -> Result { + Ok(Type::Map) } } diff --git a/src/abstract_tree/value_node.rs b/src/abstract_tree/value_node.rs index 673a1f6..25bce08 100644 --- a/src/abstract_tree/value_node.rs +++ b/src/abstract_tree/value_node.rs @@ -5,7 +5,7 @@ use tree_sitter::Node; use crate::{ AbstractTree, Block, Error, Expression, Function, Identifier, List, Map, Result, Statement, - Type, TypeDefinition, Value, + Type, Value, }; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] @@ -144,13 +144,13 @@ impl AbstractTree for ValueNode { Ok(value) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { let type_definition = match self { - ValueNode::Boolean(_) => TypeDefinition::new(Type::Boolean), - ValueNode::Float(_) => TypeDefinition::new(Type::Float), - ValueNode::Function(function) => TypeDefinition::new(function.r#type().clone()), - ValueNode::Integer(_) => TypeDefinition::new(Type::Integer), - ValueNode::String(_) => TypeDefinition::new(Type::String), + ValueNode::Boolean(_) => Type::Boolean, + ValueNode::Float(_) => Type::Float, + ValueNode::Function(function) => function.r#type().clone(), + ValueNode::Integer(_) => Type::Integer, + ValueNode::String(_) => Type::String, ValueNode::List(expressions) => { let mut previous_type = None; @@ -159,7 +159,7 @@ impl AbstractTree for ValueNode { if let Some(previous) = previous_type { if expression_type != previous { - return Ok(TypeDefinition::new(Type::List(Box::new(Type::Any)))); + return Ok(Type::List(Box::new(Type::Any))); } } @@ -167,13 +167,13 @@ impl AbstractTree for ValueNode { } if let Some(previous) = previous_type { - TypeDefinition::new(Type::List(Box::new(previous.take_inner()))) + Type::List(Box::new(previous)) } else { - TypeDefinition::new(Type::List(Box::new(Type::Any))) + Type::List(Box::new(Type::Any)) } } - ValueNode::Empty => TypeDefinition::new(Type::Any), - ValueNode::Map(_) => TypeDefinition::new(Type::Map), + ValueNode::Empty => Type::Any, + ValueNode::Map(_) => Type::Map, }; Ok(type_definition) diff --git a/src/abstract_tree/while.rs b/src/abstract_tree/while.rs index 6ea74c5..d9cbf21 100644 --- a/src/abstract_tree/while.rs +++ b/src/abstract_tree/while.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Block, Expression, Map, Result, TypeDefinition, Value}; +use crate::{AbstractTree, Block, Expression, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct While { @@ -30,7 +30,7 @@ impl AbstractTree for While { Ok(Value::Empty) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { self.block.expected_type(context) } } diff --git a/src/abstract_tree/yield.rs b/src/abstract_tree/yield.rs index ed31c08..99dcdc0 100644 --- a/src/abstract_tree/yield.rs +++ b/src/abstract_tree/yield.rs @@ -1,7 +1,7 @@ use serde::{Deserialize, Serialize}; use tree_sitter::Node; -use crate::{AbstractTree, Expression, FunctionCall, Map, Result, TypeDefinition, Value}; +use crate::{AbstractTree, Expression, FunctionCall, Map, Result, Type, Value}; #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub struct Yield { @@ -39,7 +39,7 @@ impl AbstractTree for Yield { self.call.run(source, context) } - fn expected_type(&self, context: &Map) -> Result { + fn expected_type(&self, context: &Map) -> Result { self.call.expected_type(context) } } diff --git a/src/built_in_functions/assert.rs b/src/built_in_functions/assert.rs index 1b9516d..3df2fb2 100644 --- a/src/built_in_functions/assert.rs +++ b/src/built_in_functions/assert.rs @@ -1,4 +1,4 @@ -use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, Error, Map, Result, Type, Value}; pub struct Assert; @@ -17,11 +17,11 @@ impl BuiltInFunction for Assert { Ok(Value::Empty) } - fn type_definition(&self) -> TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::Any], return_type: Box::new(Type::Empty), - }) + } } } @@ -48,10 +48,10 @@ impl BuiltInFunction for AssertEqual { } } - fn type_definition(&self) -> TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::Any, Type::Any], return_type: Box::new(Type::Boolean), - }) + } } } diff --git a/src/built_in_functions/collections.rs b/src/built_in_functions/collections.rs index dc75a9c..a70f376 100644 --- a/src/built_in_functions/collections.rs +++ b/src/built_in_functions/collections.rs @@ -1,4 +1,4 @@ -use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, Error, Map, Result, Type, Value}; pub struct Length; @@ -15,10 +15,10 @@ impl BuiltInFunction for Length { Ok(Value::Integer(length as i64)) } - fn type_definition(&self) -> TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::List(Box::new(Type::Any))], return_type: Box::new(Type::Integer), - }) + } } } diff --git a/src/built_in_functions/data_formats.rs b/src/built_in_functions/data_formats.rs index c5d5f37..45e939b 100644 --- a/src/built_in_functions/data_formats.rs +++ b/src/built_in_functions/data_formats.rs @@ -1,4 +1,4 @@ -use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, Error, Map, Result, Type, Value}; pub struct FromJson; @@ -16,11 +16,11 @@ impl BuiltInFunction for FromJson { Ok(value) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::String], return_type: Box::new(Type::Any), - }) + } } } @@ -40,10 +40,10 @@ impl BuiltInFunction for ToJson { Ok(Value::String(json_string)) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::Any], return_type: Box::new(Type::String), - }) + } } } diff --git a/src/built_in_functions/fs.rs b/src/built_in_functions/fs.rs index d56c138..d14554e 100644 --- a/src/built_in_functions/fs.rs +++ b/src/built_in_functions/fs.rs @@ -4,7 +4,7 @@ use std::{ path::PathBuf, }; -use crate::{BuiltInFunction, List, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, List, Map, Result, Type, Value}; pub struct Read; @@ -47,11 +47,11 @@ impl BuiltInFunction for Read { Ok(Value::String(file_content)) } - fn type_definition(&self) -> TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::String], return_type: Box::new(Type::String), - }) + } } } @@ -71,11 +71,11 @@ impl BuiltInFunction for Write { Ok(Value::Empty) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::String], return_type: Box::new(Type::Empty), - }) + } } } @@ -99,10 +99,10 @@ impl BuiltInFunction for Append { Ok(Value::Empty) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::String, Type::String], return_type: Box::new(Type::Empty), - }) + } } } diff --git a/src/built_in_functions/mod.rs b/src/built_in_functions/mod.rs index 6fec07f..02f8871 100644 --- a/src/built_in_functions/mod.rs +++ b/src/built_in_functions/mod.rs @@ -1,4 +1,4 @@ -use crate::{Map, Result, TypeDefinition, Value}; +use crate::{Map, Result, Type, Value}; mod assert; mod collections; @@ -29,5 +29,5 @@ pub const BUILT_IN_FUNCTIONS: [&dyn BuiltInFunction; 14] = [ pub trait BuiltInFunction { fn name(&self) -> &'static str; fn run(&self, arguments: &[Value], context: &Map) -> Result; - fn type_definition(&self) -> TypeDefinition; + fn r#type(&self) -> Type; } diff --git a/src/built_in_functions/network.rs b/src/built_in_functions/network.rs index 3fca3ac..6b50190 100644 --- a/src/built_in_functions/network.rs +++ b/src/built_in_functions/network.rs @@ -1,6 +1,6 @@ use reqwest::blocking::get; -use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, Error, Map, Result, Type, Value}; pub struct Download; @@ -18,10 +18,10 @@ impl BuiltInFunction for Download { Ok(Value::String(response.text()?)) } - fn type_definition(&self) -> TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::String], return_type: Box::new(Type::String), - }) + } } } diff --git a/src/built_in_functions/output.rs b/src/built_in_functions/output.rs index 44105cb..eab4c0e 100644 --- a/src/built_in_functions/output.rs +++ b/src/built_in_functions/output.rs @@ -1,4 +1,4 @@ -use crate::{BuiltInFunction, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, Map, Result, Type, Value}; pub struct Output; @@ -15,10 +15,10 @@ impl BuiltInFunction for Output { Ok(Value::Empty) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::Any], return_type: Box::new(Type::Empty), - }) + } } } diff --git a/src/built_in_functions/random.rs b/src/built_in_functions/random.rs index 05a4c83..f66400b 100644 --- a/src/built_in_functions/random.rs +++ b/src/built_in_functions/random.rs @@ -1,6 +1,6 @@ use rand::{random, thread_rng, Rng}; -use crate::{BuiltInFunction, Error, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, Error, Map, Result, Type, Value}; pub struct Random; @@ -20,11 +20,11 @@ impl BuiltInFunction for Random { Ok(random_argument.clone()) } - fn type_definition(&self) -> TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::List(Box::new(Type::Any))], return_type: Box::new(Type::Any), - }) + } } } @@ -41,11 +41,11 @@ impl BuiltInFunction for RandomInteger { Ok(Value::Integer(random())) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: Vec::with_capacity(0), return_type: Box::new(Type::Integer), - }) + } } } @@ -62,11 +62,11 @@ impl BuiltInFunction for RandomFloat { Ok(Value::Float(random())) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: Vec::with_capacity(0), return_type: Box::new(Type::Float), - }) + } } } @@ -83,10 +83,10 @@ impl BuiltInFunction for RandomBoolean { Ok(Value::Boolean(random())) } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: Vec::with_capacity(0), return_type: Box::new(Type::Boolean), - }) + } } } diff --git a/src/built_in_functions/type.rs b/src/built_in_functions/type.rs index b9ed893..4ac9582 100644 --- a/src/built_in_functions/type.rs +++ b/src/built_in_functions/type.rs @@ -1,4 +1,4 @@ -use crate::{BuiltInFunction, Error, List, Map, Result, Type, TypeDefinition, Value}; +use crate::{BuiltInFunction, Error, List, Map, Result, Type, Value}; pub struct TypeFunction; @@ -32,10 +32,10 @@ impl BuiltInFunction for TypeFunction { } } - fn type_definition(&self) -> crate::TypeDefinition { - TypeDefinition::new(Type::Function { + fn r#type(&self) -> Type { + Type::Function { parameter_types: vec![Type::String], return_type: Box::new(Type::Any), - }) + } } } diff --git a/src/value/mod.rs b/src/value/mod.rs index 88eceda..dc6cc98 100644 --- a/src/value/mod.rs +++ b/src/value/mod.rs @@ -1,7 +1,7 @@ //! Types that represent runtime values. use crate::{ error::{Error, Result}, - Function, List, Map, Type, TypeDefinition, + Function, List, Map, Type, }; use serde::{ @@ -42,7 +42,7 @@ pub enum Value { } impl Value { - pub fn r#type(&self, context: &Map) -> Result { + pub fn r#type(&self, context: &Map) -> Result { let r#type = match self { Value::List(list) => { let mut previous_type = None; @@ -52,7 +52,7 @@ impl Value { if let Some(previous) = &previous_type { if &value_type != previous { - return Ok(TypeDefinition::new(Type::List(Box::new(Type::Any)))); + return Ok(Type::List(Box::new(Type::Any))); } } @@ -60,13 +60,13 @@ impl Value { } if let Some(previous) = previous_type { - Type::List(Box::new(previous.take_inner())) + Type::List(Box::new(previous)) } else { Type::List(Box::new(Type::Any)) } } Value::Map(_) => Type::Map, - Value::Function(function) => return Ok(TypeDefinition::new(function.r#type().clone())), + Value::Function(function) => function.r#type().clone(), Value::String(_) => Type::String, Value::Float(_) => Type::Float, Value::Integer(_) => Type::Integer, @@ -74,7 +74,7 @@ impl Value { Value::Empty => Type::Empty, }; - Ok(TypeDefinition::new(r#type)) + Ok(r#type) } pub fn is_string(&self) -> bool {