From 12e00bfc4227705171483370f3056e90787e5e7e Mon Sep 17 00:00:00 2001 From: Jeff Date: Sun, 31 Dec 2023 09:47:20 -0500 Subject: [PATCH] Remove return and use statements; Clean up --- src/abstract_tree/block.rs | 14 +---------- src/abstract_tree/identifier.rs | 6 +++++ src/abstract_tree/mod.rs | 4 +-- src/abstract_tree/statement.rs | 20 +-------------- src/abstract_tree/use.rs | 44 --------------------------------- src/built_in_functions/fs.rs | 10 ++------ tests/interpret.rs | 28 +++++++++++---------- 7 files changed, 26 insertions(+), 100 deletions(-) delete mode 100644 src/abstract_tree/use.rs diff --git a/src/abstract_tree/block.rs b/src/abstract_tree/block.rs index a11a7f0..d7711cd 100644 --- a/src/abstract_tree/block.rs +++ b/src/abstract_tree/block.rs @@ -58,10 +58,6 @@ impl AbstractTree for Block { .into_par_iter() .enumerate() .find_map_first(|(index, statement)| { - if let Statement::Return(expression) = statement { - return Some(expression.run(source, context)); - } - let result = statement.run(source, context); if result.is_err() { @@ -79,10 +75,6 @@ impl AbstractTree for Block { let mut prev_result = None; for statement in &self.statements { - if let Statement::Return(expression) = statement { - return expression.run(source, context); - } - prev_result = Some(statement.run(source, context)); } @@ -91,10 +83,6 @@ impl AbstractTree for Block { } fn expected_type(&self, context: &Map) -> Result { - if self.is_async { - Ok(Type::Any) - } else { - self.statements.last().unwrap().expected_type(context) - } + self.statements.last().unwrap().expected_type(context) } } diff --git a/src/abstract_tree/identifier.rs b/src/abstract_tree/identifier.rs index 0717a92..06ed5e1 100644 --- a/src/abstract_tree/identifier.rs +++ b/src/abstract_tree/identifier.rs @@ -30,6 +30,12 @@ impl AbstractTree for Identifier { let text = &source[node.byte_range()]; + if text.is_empty() { + println!("{node:?}"); + } + + debug_assert!(!text.is_empty()); + Ok(Identifier(text.to_string())) } diff --git a/src/abstract_tree/mod.rs b/src/abstract_tree/mod.rs index 1e2cdbf..455319a 100644 --- a/src/abstract_tree/mod.rs +++ b/src/abstract_tree/mod.rs @@ -21,7 +21,6 @@ pub mod r#match; pub mod math; pub mod statement; pub mod type_definition; -pub mod r#use; pub mod value_node; pub mod r#while; pub mod r#yield; @@ -29,8 +28,7 @@ pub mod r#yield; pub use { assignment::*, block::*, expression::*, function_call::*, function_expression::*, identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, logic::*, math::*, - r#for::*, r#match::*, r#use::*, r#while::*, r#yield::*, statement::*, type_definition::*, - value_node::*, + r#for::*, r#match::*, r#while::*, r#yield::*, statement::*, type_definition::*, value_node::*, }; use tree_sitter::Node; diff --git a/src/abstract_tree/statement.rs b/src/abstract_tree/statement.rs index 006e191..2c5cfc9 100644 --- a/src/abstract_tree/statement.rs +++ b/src/abstract_tree/statement.rs @@ -3,21 +3,19 @@ use tree_sitter::Node; use crate::{ AbstractTree, Assignment, Block, Error, Expression, For, IfElse, IndexAssignment, Map, Match, - Result, Type, Use, Value, While, + Result, Type, Value, While, }; /// Abstract representation of a statement. #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] pub enum Statement { Assignment(Box), - Return(Expression), Expression(Expression), IfElse(Box), Match(Match), While(Box), Block(Box), For(Box), - Use(Use), IndexAssignment(Box), } @@ -31,15 +29,6 @@ impl AbstractTree for Statement { "assignment" => Ok(Statement::Assignment(Box::new( Assignment::from_syntax_node(source, child, context)?, ))), - "return" => { - let expression_node = child.child(1).unwrap(); - - Ok(Statement::Return(Expression::from_syntax_node( - source, - expression_node, - context, - )?)) - } "expression" => Ok(Statement::Expression(Expression::from_syntax_node( source, child, context, )?)), @@ -55,9 +44,6 @@ impl AbstractTree for Statement { "for" => Ok(Statement::For(Box::new(For::from_syntax_node( source, child, context, )?))), - "use" => Ok(Statement::Use(Use::from_syntax_node( - source, child, context, - )?)), "index_assignment" => Ok(Statement::IndexAssignment(Box::new( IndexAssignment::from_syntax_node(source, child, context)?, ))), @@ -77,14 +63,12 @@ impl AbstractTree for Statement { fn run(&self, source: &str, context: &Map) -> Result { match self { Statement::Assignment(assignment) => assignment.run(source, context), - Statement::Return(expression) => expression.run(source, context), Statement::Expression(expression) => expression.run(source, context), Statement::IfElse(if_else) => if_else.run(source, context), Statement::Match(r#match) => r#match.run(source, context), Statement::While(r#while) => r#while.run(source, context), Statement::Block(block) => block.run(source, context), Statement::For(r#for) => r#for.run(source, context), - Statement::Use(run) => run.run(source, context), Statement::IndexAssignment(index_assignment) => index_assignment.run(source, context), } } @@ -92,14 +76,12 @@ impl AbstractTree for Statement { fn expected_type(&self, context: &Map) -> Result { match self { Statement::Assignment(assignment) => assignment.expected_type(context), - Statement::Return(expression) => expression.expected_type(context), Statement::Expression(expression) => expression.expected_type(context), Statement::IfElse(if_else) => if_else.expected_type(context), Statement::Match(r#match) => r#match.expected_type(context), Statement::While(r#while) => r#while.expected_type(context), Statement::Block(block) => block.expected_type(context), Statement::For(r#for) => r#for.expected_type(context), - Statement::Use(r#use) => r#use.expected_type(context), Statement::IndexAssignment(index_assignment) => index_assignment.expected_type(context), } } diff --git a/src/abstract_tree/use.rs b/src/abstract_tree/use.rs deleted file mode 100644 index 877b6db..0000000 --- a/src/abstract_tree/use.rs +++ /dev/null @@ -1,44 +0,0 @@ -use std::fs::read_to_string; - -use serde::{Deserialize, Serialize}; -use tree_sitter::Node; - -use crate::{interpret_with_context, AbstractTree, Error, Map, Result, Type, Value}; - -/// Abstract representation of a use statement. -/// -/// Use will evaluate the Dust file at the given path. It will create an empty -/// context to do so, then apply every value from that context to the current -/// context. -#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] -pub struct Use { - path: String, -} - -impl AbstractTree for Use { - fn from_syntax_node(source: &str, node: Node, _context: &Map) -> Result { - Error::expect_syntax_node(source, "use", node)?; - - let string_node = node.child(1).unwrap(); - let path = source[string_node.start_byte() + 1..string_node.end_byte() - 1].to_string(); - - Ok(Use { path }) - } - - fn run(&self, _source: &str, context: &Map) -> Result { - let file_contents = read_to_string(&self.path)?; - let file_context = Map::new(); - - interpret_with_context(&file_contents, file_context.clone())?; - - for (key, (value, r#type)) in file_context.variables()?.iter() { - context.set(key.clone(), value.clone(), Some(r#type.clone()))?; - } - - Ok(Value::Map(file_context)) - } - - fn expected_type(&self, _context: &Map) -> Result { - Ok(Type::Map) - } -} diff --git a/src/built_in_functions/fs.rs b/src/built_in_functions/fs.rs index b0c6771..28e8fe9 100644 --- a/src/built_in_functions/fs.rs +++ b/src/built_in_functions/fs.rs @@ -84,14 +84,8 @@ impl BuiltInFunction for Append { } fn run(&self, arguments: &[Value], _context: &Map) -> Result { - let file_content = arguments - .first() - .unwrap_or(&Value::none()) - .as_string()?; - let path = arguments - .get(1) - .unwrap_or(&Value::none()) - .as_string()?; + let file_content = arguments.get(0).unwrap_or_default().as_string()?; + let path = arguments.get(1).unwrap_or_default().as_string()?; File::options() .append(true) diff --git a/tests/interpret.rs b/tests/interpret.rs index 4ac9c5d..78dc4a2 100644 --- a/tests/interpret.rs +++ b/tests/interpret.rs @@ -76,19 +76,6 @@ mod for_loop { result ); } - - #[test] - fn modify_value_async() { - let result = interpret( - " - fn = (x ) {} - - fn(1) - ", - ); - - assert_eq!(Ok(Value::none()), result); - } } mod logic { @@ -441,4 +428,19 @@ mod type_definition { }, })); } + + #[test] + fn modify_value_async() { + let result = interpret( + " + fn = (x ) { + + } + + fn(1) + ", + ); + + assert_eq!(Ok(Value::none()), result); + } }