From e4ea402dfa2c28f81a2616e9be0085db0ec31126 Mon Sep 17 00:00:00 2001 From: Jeff Date: Wed, 7 Aug 2024 19:44:01 -0400 Subject: [PATCH] Move built-in functions to a separate module --- dust-lang/src/abstract_tree.rs | 93 +---------------------------- dust-lang/src/built_in_function.rs | 96 ++++++++++++++++++++++++++++++ dust-lang/src/lib.rs | 4 +- dust-lang/src/parse.rs | 2 +- dust-lang/src/vm.rs | 4 +- 5 files changed, 103 insertions(+), 96 deletions(-) create mode 100644 dust-lang/src/built_in_function.rs diff --git a/dust-lang/src/abstract_tree.rs b/dust-lang/src/abstract_tree.rs index 2718320..8888cbe 100644 --- a/dust-lang/src/abstract_tree.rs +++ b/dust-lang/src/abstract_tree.rs @@ -5,7 +5,7 @@ use std::{ use serde::{Deserialize, Serialize}; -use crate::{Identifier, Span, Type, Value}; +use crate::{BuiltInFunction, Identifier, Span, Type, Value}; #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub struct AbstractSyntaxTree { @@ -169,94 +169,3 @@ impl Display for Statement { } } } - -#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub enum BuiltInFunction { - IsEven, - IsOdd, - Length, -} - -impl BuiltInFunction { - pub fn name(&self) -> &'static str { - match self { - BuiltInFunction::IsEven => "is_even", - BuiltInFunction::IsOdd => "is_odd", - BuiltInFunction::Length => "length", - } - } - - pub fn call( - &self, - _type_arguments: Option>, - value_arguments: Option>, - ) -> Result { - match self { - BuiltInFunction::IsEven => { - if let Some(value_arguments) = value_arguments { - if value_arguments.len() == 1 { - if let Some(integer) = value_arguments[0].as_integer() { - Ok(Value::boolean(integer % 2 == 0)) - } else { - Err(BuiltInFunctionError::ExpectedInteger) - } - } else { - Err(BuiltInFunctionError::WrongNumberOfValueArguments) - } - } else { - Err(BuiltInFunctionError::WrongNumberOfValueArguments) - } - } - BuiltInFunction::IsOdd => { - if let Some(value_arguments) = value_arguments { - if value_arguments.len() == 1 { - if let Some(integer) = value_arguments[0].as_integer() { - Ok(Value::boolean(integer % 2 != 0)) - } else { - Err(BuiltInFunctionError::ExpectedInteger) - } - } else { - Err(BuiltInFunctionError::WrongNumberOfValueArguments) - } - } else { - Err(BuiltInFunctionError::WrongNumberOfValueArguments) - } - } - BuiltInFunction::Length => { - if let Some(value_arguments) = value_arguments { - if value_arguments.len() == 1 { - if let Some(list) = value_arguments[0].as_list() { - Ok(Value::integer(list.len() as i64)) - } else { - Err(BuiltInFunctionError::ExpectedInteger) - } - } else { - Err(BuiltInFunctionError::WrongNumberOfValueArguments) - } - } else { - Err(BuiltInFunctionError::WrongNumberOfValueArguments) - } - } - } - } - - pub fn expected_type(&self) -> Option { - match self { - BuiltInFunction::IsEven => Some(Type::Boolean), - BuiltInFunction::IsOdd => Some(Type::Boolean), - BuiltInFunction::Length => Some(Type::Integer), - } - } -} - -impl Display for BuiltInFunction { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}", self.name()) - } -} - -#[derive(Debug, Clone, PartialEq)] -pub enum BuiltInFunctionError { - ExpectedInteger, - WrongNumberOfValueArguments, -} diff --git a/dust-lang/src/built_in_function.rs b/dust-lang/src/built_in_function.rs new file mode 100644 index 0000000..8365e46 --- /dev/null +++ b/dust-lang/src/built_in_function.rs @@ -0,0 +1,96 @@ +use std::fmt::{self, Display, Formatter}; + +use serde::{Deserialize, Serialize}; + +use crate::{Type, Value}; + +#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] +pub enum BuiltInFunction { + IsEven, + IsOdd, + Length, +} + +impl BuiltInFunction { + pub fn name(&self) -> &'static str { + match self { + BuiltInFunction::IsEven => "is_even", + BuiltInFunction::IsOdd => "is_odd", + BuiltInFunction::Length => "length", + } + } + + pub fn call( + &self, + _type_arguments: Option>, + value_arguments: Option>, + ) -> Result { + match self { + BuiltInFunction::IsEven => { + if let Some(value_arguments) = value_arguments { + if value_arguments.len() == 1 { + if let Some(integer) = value_arguments[0].as_integer() { + Ok(Value::boolean(integer % 2 == 0)) + } else { + Err(BuiltInFunctionError::ExpectedInteger) + } + } else { + Err(BuiltInFunctionError::WrongNumberOfValueArguments) + } + } else { + Err(BuiltInFunctionError::WrongNumberOfValueArguments) + } + } + BuiltInFunction::IsOdd => { + if let Some(value_arguments) = value_arguments { + if value_arguments.len() == 1 { + if let Some(integer) = value_arguments[0].as_integer() { + Ok(Value::boolean(integer % 2 != 0)) + } else { + Err(BuiltInFunctionError::ExpectedInteger) + } + } else { + Err(BuiltInFunctionError::WrongNumberOfValueArguments) + } + } else { + Err(BuiltInFunctionError::WrongNumberOfValueArguments) + } + } + BuiltInFunction::Length => { + if let Some(value_arguments) = value_arguments { + if value_arguments.len() == 1 { + if let Some(list) = value_arguments[0].as_list() { + Ok(Value::integer(list.len() as i64)) + } else { + Err(BuiltInFunctionError::ExpectedInteger) + } + } else { + Err(BuiltInFunctionError::WrongNumberOfValueArguments) + } + } else { + Err(BuiltInFunctionError::WrongNumberOfValueArguments) + } + } + } + } + + pub fn expected_type(&self) -> Option { + match self { + BuiltInFunction::IsEven => Some(Type::Boolean), + BuiltInFunction::IsOdd => Some(Type::Boolean), + BuiltInFunction::Length => Some(Type::Integer), + } + } +} + +impl Display for BuiltInFunction { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}", self.name()) + } +} + +#[derive(Debug, Clone, PartialEq)] +pub enum BuiltInFunctionError { + ExpectedInteger, + WrongNumberOfValueArguments, +} diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 5007c1f..71ad4b8 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -7,6 +7,7 @@ //! `Interpreter` and runs the given source code. pub mod abstract_tree; pub mod analyzer; +pub mod built_in_function; pub mod identifier; pub mod lex; pub mod parse; @@ -15,8 +16,9 @@ pub mod r#type; pub mod value; pub mod vm; -pub use abstract_tree::{AbstractSyntaxTree, BuiltInFunction, Node, Statement}; +pub use abstract_tree::{AbstractSyntaxTree, Node, Statement}; pub use analyzer::{analyze, Analyzer, AnalyzerError}; +pub use built_in_function::{BuiltInFunction, BuiltInFunctionError}; pub use identifier::Identifier; pub use lex::{lex, LexError, Lexer}; pub use parse::{parse, ParseError, Parser}; diff --git a/dust-lang/src/parse.rs b/dust-lang/src/parse.rs index d3f36af..1d373d8 100644 --- a/dust-lang/src/parse.rs +++ b/dust-lang/src/parse.rs @@ -6,7 +6,7 @@ use std::collections::VecDeque; use crate::{ - abstract_tree::BuiltInFunction, AbstractSyntaxTree, LexError, Lexer, Node, Span, Statement, + built_in_function::BuiltInFunction, AbstractSyntaxTree, LexError, Lexer, Node, Span, Statement, Token, Value, }; diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index 3a09233..8c695e5 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -1,8 +1,8 @@ use std::collections::HashMap; use crate::{ - abstract_tree::BuiltInFunctionError, parse, AbstractSyntaxTree, Analyzer, AnalyzerError, - Identifier, Node, ParseError, Span, Statement, Value, ValueError, + parse, AbstractSyntaxTree, Analyzer, AnalyzerError, BuiltInFunctionError, Identifier, Node, + ParseError, Span, Statement, Value, ValueError, }; pub fn run(