From a79cb0b3e1e2617ea3be657d17769b10d525cd8e Mon Sep 17 00:00:00 2001 From: Jeff Date: Mon, 1 Jul 2024 10:40:36 -0400 Subject: [PATCH] Begin adding use statement --- dust-lang/src/abstract_tree/mod.rs | 2 ++ dust-lang/src/abstract_tree/use.rs | 48 ++++++++++++++++++++++++++++++ dust-lang/src/error.rs | 1 + dust-lang/src/lexer.rs | 4 +-- 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 dust-lang/src/abstract_tree/use.rs diff --git a/dust-lang/src/abstract_tree/mod.rs b/dust-lang/src/abstract_tree/mod.rs index aafbdfd..1365f35 100644 --- a/dust-lang/src/abstract_tree/mod.rs +++ b/dust-lang/src/abstract_tree/mod.rs @@ -17,6 +17,7 @@ pub mod structure_definition; pub mod r#type; pub mod type_alias; pub mod type_constructor; +pub mod r#use; pub mod value_node; pub mod r#while; @@ -45,6 +46,7 @@ pub use self::{ r#as::As, r#loop::Loop, r#type::Type, + r#use::Use, r#while::While, statement::Statement, structure_definition::StructureDefinition, diff --git a/dust-lang/src/abstract_tree/use.rs b/dust-lang/src/abstract_tree/use.rs new file mode 100644 index 0000000..583f023 --- /dev/null +++ b/dust-lang/src/abstract_tree/use.rs @@ -0,0 +1,48 @@ +use std::{fs::read_to_string, path::Path}; + +use serde::{Deserialize, Serialize}; + +use crate::{ + context::Context, + error::{RuntimeError, ValidationError}, + lexer::{self, lex}, + Type, +}; + +use super::{AbstractNode, Evaluation}; + +#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct Use { + path: String, +} + +impl AbstractNode for Use { + fn define_types(&self, context: &Context) -> Result<(), ValidationError> { + Ok(()) + } + + fn validate(&self, context: &Context, manage_memory: bool) -> Result<(), ValidationError> { + if Path::new(&self.path).exists() { + Ok(()) + } else { + todo!() + } + } + + fn evaluate( + self, + context: &Context, + manage_memory: bool, + ) -> Result, RuntimeError> { + let file_contents = read_to_string(self.path)?; + + let tokens = lex(&file_contents).map_err(|errors| RuntimeError::Use(errors))?; + let abstract_tree = + + Ok(None) + } + + fn expected_type(&self, context: &Context) -> Result, ValidationError> { + todo!() + } +} diff --git a/dust-lang/src/error.rs b/dust-lang/src/error.rs index 3e614b4..973b65f 100644 --- a/dust-lang/src/error.rs +++ b/dust-lang/src/error.rs @@ -56,6 +56,7 @@ pub enum RuntimeError { RwLockPoison(PoisonError), ValidationFailure(ValidationError), SerdeJson(serde_json::Error), + Use(Vec), } impl From for RuntimeError { diff --git a/dust-lang/src/lexer.rs b/dust-lang/src/lexer.rs index a237f07..6a3e7ce 100644 --- a/dust-lang/src/lexer.rs +++ b/dust-lang/src/lexer.rs @@ -318,11 +318,11 @@ pub fn lexer<'src>() -> impl Parser< let identifier = text::ident().map(|text: &str| Token::Identifier(text)); - let r#use = just("use ").ignore_then( + let r#use = just("use").ignore_then( none_of('\n') .repeated() .to_slice() - .map(|text: &str| Token::Use(text)), + .map(|text: &str| Token::Use(text.trim())), ); choice((