1
0

Remove return and use statements; Clean up

This commit is contained in:
Jeff 2023-12-31 09:47:20 -05:00
parent bf79526764
commit 12e00bfc42
7 changed files with 26 additions and 100 deletions

View File

@ -58,10 +58,6 @@ impl AbstractTree for Block {
.into_par_iter() .into_par_iter()
.enumerate() .enumerate()
.find_map_first(|(index, statement)| { .find_map_first(|(index, statement)| {
if let Statement::Return(expression) = statement {
return Some(expression.run(source, context));
}
let result = statement.run(source, context); let result = statement.run(source, context);
if result.is_err() { if result.is_err() {
@ -79,10 +75,6 @@ impl AbstractTree for Block {
let mut prev_result = None; let mut prev_result = None;
for statement in &self.statements { for statement in &self.statements {
if let Statement::Return(expression) = statement {
return expression.run(source, context);
}
prev_result = Some(statement.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<Type> { fn expected_type(&self, context: &Map) -> Result<Type> {
if self.is_async {
Ok(Type::Any)
} else {
self.statements.last().unwrap().expected_type(context) self.statements.last().unwrap().expected_type(context)
} }
}
} }

View File

@ -30,6 +30,12 @@ impl AbstractTree for Identifier {
let text = &source[node.byte_range()]; let text = &source[node.byte_range()];
if text.is_empty() {
println!("{node:?}");
}
debug_assert!(!text.is_empty());
Ok(Identifier(text.to_string())) Ok(Identifier(text.to_string()))
} }

View File

@ -21,7 +21,6 @@ pub mod r#match;
pub mod math; pub mod math;
pub mod statement; pub mod statement;
pub mod type_definition; pub mod type_definition;
pub mod r#use;
pub mod value_node; pub mod value_node;
pub mod r#while; pub mod r#while;
pub mod r#yield; pub mod r#yield;
@ -29,8 +28,7 @@ pub mod r#yield;
pub use { pub use {
assignment::*, block::*, expression::*, function_call::*, function_expression::*, assignment::*, block::*, expression::*, function_call::*, function_expression::*,
identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, logic::*, math::*, identifier::*, if_else::*, index::*, index_assignment::IndexAssignment, logic::*, math::*,
r#for::*, r#match::*, r#use::*, r#while::*, r#yield::*, statement::*, type_definition::*, r#for::*, r#match::*, r#while::*, r#yield::*, statement::*, type_definition::*, value_node::*,
value_node::*,
}; };
use tree_sitter::Node; use tree_sitter::Node;

View File

@ -3,21 +3,19 @@ use tree_sitter::Node;
use crate::{ use crate::{
AbstractTree, Assignment, Block, Error, Expression, For, IfElse, IndexAssignment, Map, Match, AbstractTree, Assignment, Block, Error, Expression, For, IfElse, IndexAssignment, Map, Match,
Result, Type, Use, Value, While, Result, Type, Value, While,
}; };
/// Abstract representation of a statement. /// Abstract representation of a statement.
#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)] #[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum Statement { pub enum Statement {
Assignment(Box<Assignment>), Assignment(Box<Assignment>),
Return(Expression),
Expression(Expression), Expression(Expression),
IfElse(Box<IfElse>), IfElse(Box<IfElse>),
Match(Match), Match(Match),
While(Box<While>), While(Box<While>),
Block(Box<Block>), Block(Box<Block>),
For(Box<For>), For(Box<For>),
Use(Use),
IndexAssignment(Box<IndexAssignment>), IndexAssignment(Box<IndexAssignment>),
} }
@ -31,15 +29,6 @@ impl AbstractTree for Statement {
"assignment" => Ok(Statement::Assignment(Box::new( "assignment" => Ok(Statement::Assignment(Box::new(
Assignment::from_syntax_node(source, child, context)?, 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( "expression" => Ok(Statement::Expression(Expression::from_syntax_node(
source, child, context, source, child, context,
)?)), )?)),
@ -55,9 +44,6 @@ impl AbstractTree for Statement {
"for" => Ok(Statement::For(Box::new(For::from_syntax_node( "for" => Ok(Statement::For(Box::new(For::from_syntax_node(
source, child, context, source, child, context,
)?))), )?))),
"use" => Ok(Statement::Use(Use::from_syntax_node(
source, child, context,
)?)),
"index_assignment" => Ok(Statement::IndexAssignment(Box::new( "index_assignment" => Ok(Statement::IndexAssignment(Box::new(
IndexAssignment::from_syntax_node(source, child, context)?, IndexAssignment::from_syntax_node(source, child, context)?,
))), ))),
@ -77,14 +63,12 @@ impl AbstractTree for Statement {
fn run(&self, source: &str, context: &Map) -> Result<Value> { fn run(&self, source: &str, context: &Map) -> Result<Value> {
match self { match self {
Statement::Assignment(assignment) => assignment.run(source, context), Statement::Assignment(assignment) => assignment.run(source, context),
Statement::Return(expression) => expression.run(source, context),
Statement::Expression(expression) => expression.run(source, context), Statement::Expression(expression) => expression.run(source, context),
Statement::IfElse(if_else) => if_else.run(source, context), Statement::IfElse(if_else) => if_else.run(source, context),
Statement::Match(r#match) => r#match.run(source, context), Statement::Match(r#match) => r#match.run(source, context),
Statement::While(r#while) => r#while.run(source, context), Statement::While(r#while) => r#while.run(source, context),
Statement::Block(block) => block.run(source, context), Statement::Block(block) => block.run(source, context),
Statement::For(r#for) => r#for.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), Statement::IndexAssignment(index_assignment) => index_assignment.run(source, context),
} }
} }
@ -92,14 +76,12 @@ impl AbstractTree for Statement {
fn expected_type(&self, context: &Map) -> Result<Type> { fn expected_type(&self, context: &Map) -> Result<Type> {
match self { match self {
Statement::Assignment(assignment) => assignment.expected_type(context), Statement::Assignment(assignment) => assignment.expected_type(context),
Statement::Return(expression) => expression.expected_type(context),
Statement::Expression(expression) => expression.expected_type(context), Statement::Expression(expression) => expression.expected_type(context),
Statement::IfElse(if_else) => if_else.expected_type(context), Statement::IfElse(if_else) => if_else.expected_type(context),
Statement::Match(r#match) => r#match.expected_type(context), Statement::Match(r#match) => r#match.expected_type(context),
Statement::While(r#while) => r#while.expected_type(context), Statement::While(r#while) => r#while.expected_type(context),
Statement::Block(block) => block.expected_type(context), Statement::Block(block) => block.expected_type(context),
Statement::For(r#for) => r#for.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), Statement::IndexAssignment(index_assignment) => index_assignment.expected_type(context),
} }
} }

View File

@ -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<Self> {
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<Value> {
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<Type> {
Ok(Type::Map)
}
}

View File

@ -84,14 +84,8 @@ impl BuiltInFunction for Append {
} }
fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> { fn run(&self, arguments: &[Value], _context: &Map) -> Result<Value> {
let file_content = arguments let file_content = arguments.get(0).unwrap_or_default().as_string()?;
.first() let path = arguments.get(1).unwrap_or_default().as_string()?;
.unwrap_or(&Value::none())
.as_string()?;
let path = arguments
.get(1)
.unwrap_or(&Value::none())
.as_string()?;
File::options() File::options()
.append(true) .append(true)

View File

@ -76,19 +76,6 @@ mod for_loop {
result result
); );
} }
#[test]
fn modify_value_async() {
let result = interpret(
"
fn = (x <int>) <none> {}
fn(1)
",
);
assert_eq!(Ok(Value::none()), result);
}
} }
mod logic { mod logic {
@ -441,4 +428,19 @@ mod type_definition {
}, },
})); }));
} }
#[test]
fn modify_value_async() {
let result = interpret(
"
fn = (x <int>) <none> {
}
fn(1)
",
);
assert_eq!(Ok(Value::none()), result);
}
} }