Begin adding use statement

This commit is contained in:
Jeff 2024-07-01 10:40:36 -04:00
parent 1e7636903e
commit a79cb0b3e1
4 changed files with 53 additions and 2 deletions

View File

@ -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,

View File

@ -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<Option<Evaluation>, 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<Option<Type>, ValidationError> {
todo!()
}
}

View File

@ -56,6 +56,7 @@ pub enum RuntimeError {
RwLockPoison(PoisonError),
ValidationFailure(ValidationError),
SerdeJson(serde_json::Error),
Use(Vec<DustError>),
}
impl From<PoisonError> for RuntimeError {

View File

@ -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((