1
0
dust/dust-lang/src/abstract_tree/mod.rs

189 lines
4.9 KiB
Rust
Raw Normal View History

2024-02-25 18:49:26 +00:00
pub mod assignment;
2024-03-20 21:05:37 +00:00
pub mod async_block;
2024-02-25 18:49:26 +00:00
pub mod block;
2024-04-21 21:00:08 +00:00
pub mod built_in_function_call;
2024-02-26 21:27:01 +00:00
pub mod expression;
2024-03-09 12:34:34 +00:00
pub mod function_call;
2024-03-08 19:01:05 +00:00
pub mod if_else;
2024-03-18 01:07:03 +00:00
pub mod list_index;
2024-02-25 18:49:26 +00:00
pub mod logic;
pub mod r#loop;
2024-03-18 01:07:03 +00:00
pub mod map_index;
2024-03-07 11:33:54 +00:00
pub mod math;
2024-02-25 18:49:26 +00:00
pub mod statement;
2024-03-19 21:49:24 +00:00
pub mod structure_definition;
pub mod r#type;
2024-02-26 21:27:01 +00:00
pub mod value_node;
2024-03-11 21:58:26 +00:00
pub mod r#while;
2024-02-25 18:49:26 +00:00
use std::{cmp::Ordering, ops::Index};
2024-03-17 06:51:33 +00:00
use chumsky::span::{SimpleSpan, Span};
2024-02-25 18:49:26 +00:00
pub use self::{
assignment::{Assignment, AssignmentOperator},
2024-03-20 21:05:37 +00:00
async_block::AsyncBlock,
block::Block,
2024-04-21 21:00:08 +00:00
built_in_function_call::BuiltInFunctionCall,
expression::Expression,
2024-03-09 12:34:34 +00:00
function_call::FunctionCall,
2024-03-08 19:01:05 +00:00
if_else::IfElse,
2024-03-18 01:07:03 +00:00
list_index::ListIndex,
logic::Logic,
2024-03-18 01:07:03 +00:00
map_index::MapIndex,
math::Math,
r#loop::Loop,
r#type::Type,
2024-03-11 21:58:26 +00:00
r#while::While,
statement::Statement,
2024-03-19 21:49:24 +00:00
structure_definition::StructureDefinition,
2024-03-07 11:33:54 +00:00
value_node::ValueNode,
2024-02-25 18:49:26 +00:00
};
use crate::{
context::Context,
error::{Error, RuntimeError, ValidationError},
Value,
};
2024-02-25 18:49:26 +00:00
2024-03-17 04:49:01 +00:00
#[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
2024-03-17 11:31:45 +00:00
pub struct WithPosition<T> {
2024-04-22 07:41:21 +00:00
pub item: T,
2024-03-17 11:48:06 +00:00
pub position: SourcePosition,
}
2024-03-24 16:21:08 +00:00
pub trait WithPos: Sized {
fn with_position<T: Into<SourcePosition>>(self, span: T) -> WithPosition<Self> {
WithPosition {
2024-04-22 07:41:21 +00:00
item: self,
2024-03-24 16:21:08 +00:00
position: span.into(),
}
}
}
impl<T> WithPos for T {}
2024-03-17 11:48:06 +00:00
#[derive(Clone, Copy, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct SourcePosition(pub usize, pub usize);
impl From<SimpleSpan> for SourcePosition {
fn from(span: SimpleSpan) -> Self {
SourcePosition(span.start(), span.end())
}
}
impl From<(usize, usize)> for SourcePosition {
fn from((start, end): (usize, usize)) -> Self {
SourcePosition(start, end)
}
2024-03-17 04:49:01 +00:00
}
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum Action {
Return(Value),
Break,
None,
}
2024-03-25 04:16:55 +00:00
pub struct AbstractTree(Vec<Statement>);
impl AbstractTree {
2024-03-25 04:16:55 +00:00
pub fn new(mut statements: Vec<Statement>) -> Self {
statements.sort_by(|left, right| match (&left, &right) {
(Statement::StructureDefinition(_), _) => Ordering::Less,
(_, Statement::StructureDefinition(_)) => Ordering::Greater,
(_, _) => Ordering::Equal,
});
AbstractTree(statements)
}
pub fn run(
self,
context: &mut Context,
2024-04-22 11:56:03 +00:00
manage_memory: bool,
) -> Result<Option<Value>, Vec<Error>> {
2024-04-22 11:56:03 +00:00
let valid_statements = self.validate(context, manage_memory)?;
2024-03-21 03:13:21 +00:00
let mut previous_value = None;
for statement in valid_statements {
2024-03-25 04:16:55 +00:00
let position = statement.position();
2024-04-22 11:56:03 +00:00
let run = statement.run(context, manage_memory);
2024-03-21 03:13:21 +00:00
match run {
Ok(action) => match action {
Action::Return(value) => previous_value = Some(value),
2024-04-21 22:22:59 +00:00
Action::None => previous_value = None,
2024-03-21 03:13:21 +00:00
_ => {}
},
Err(runtime_error) => {
return Err(vec![Error::Runtime {
error: runtime_error,
2024-03-25 04:16:55 +00:00
position,
2024-03-21 03:13:21 +00:00
}]);
}
}
}
Ok(previous_value)
}
2024-04-22 11:56:03 +00:00
fn validate(
self,
context: &mut Context,
add_variable_uses: bool,
) -> Result<Vec<Statement>, Vec<Error>> {
let mut errors = Vec::new();
let mut valid_statements = Vec::new();
for statement in self.0 {
2024-04-22 11:56:03 +00:00
let validation = statement.validate(context, add_variable_uses);
if let Err(validation_error) = validation {
errors.push(Error::Validation {
error: validation_error,
2024-03-25 04:16:55 +00:00
position: statement.position(),
})
} else if errors.is_empty() {
2024-03-25 04:16:55 +00:00
if let Statement::StructureDefinition(_) = statement {
let position = statement.position();
let run = statement.run(context, true);
if let Err(runtime_error) = run {
errors.push(Error::Runtime {
error: runtime_error,
2024-03-25 04:16:55 +00:00
position,
});
return Err(errors);
}
} else {
valid_statements.push(statement)
}
2024-04-22 01:33:21 +00:00
} else {
continue;
}
}
2024-03-21 03:13:21 +00:00
if errors.is_empty() {
Ok(valid_statements)
} else {
Err(errors)
}
}
}
impl Index<usize> for AbstractTree {
2024-03-25 04:16:55 +00:00
type Output = Statement;
fn index(&self, index: usize) -> &Self::Output {
&self.0[index]
}
}
pub trait AbstractNode: Sized {
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError>;
2024-04-22 11:56:03 +00:00
fn validate(&self, context: &Context, manage_memory: bool) -> Result<(), ValidationError>;
fn run(self, context: &mut Context, manage_memory: bool) -> Result<Action, RuntimeError>;
2024-03-08 17:24:11 +00:00
}