1
0
dust/src/abstract_tree/mod.rs

73 lines
1.7 KiB
Rust
Raw Normal View History

2024-02-25 18:49:26 +00:00
pub mod assignment;
pub mod block;
2024-02-26 21:27:01 +00:00
pub mod expression;
2024-03-09 12:34:34 +00:00
pub mod function_call;
2024-02-25 18:49:26 +00:00
pub mod identifier;
2024-03-08 19:01:05 +00:00
pub mod if_else;
2024-03-07 17:29:07 +00:00
pub mod index;
2024-02-25 18:49:26 +00:00
pub mod logic;
pub mod r#loop;
2024-03-07 11:33:54 +00:00
pub mod math;
2024-02-25 18:49:26 +00:00
pub mod statement;
pub mod r#type;
2024-02-26 21:27:01 +00:00
pub mod value_node;
2024-02-25 18:49:26 +00:00
pub use self::{
assignment::{Assignment, AssignmentOperator},
block::Block,
expression::Expression,
2024-03-09 12:34:34 +00:00
function_call::FunctionCall,
identifier::Identifier,
2024-03-08 19:01:05 +00:00
if_else::IfElse,
index::Index,
logic::Logic,
math::Math,
r#loop::Loop,
r#type::Type,
statement::Statement,
2024-03-07 11:33:54 +00:00
value_node::ValueNode,
2024-02-25 18:49:26 +00:00
};
use crate::{
context::Context,
error::{RuntimeError, ValidationError},
Value,
};
2024-02-25 18:49:26 +00:00
pub trait AbstractTree {
fn expected_type(&self, context: &Context) -> Result<Type, ValidationError>;
fn validate(&self, context: &Context) -> Result<(), ValidationError>;
2024-03-08 17:24:11 +00:00
fn run(self, context: &Context) -> Result<Action, RuntimeError>;
}
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord)]
pub enum Action {
2024-03-11 19:17:01 +00:00
Break(Option<Value>),
2024-03-08 17:24:11 +00:00
Return(Value),
None,
}
impl Action {
2024-03-09 13:10:54 +00:00
pub fn as_value(self) -> Result<Value, ValidationError> {
match self {
2024-03-11 19:17:01 +00:00
Action::Break(value) => {
if let Some(value) = value {
Ok(value)
} else {
Err(ValidationError::ExpectedValue)
}
}
2024-03-09 13:10:54 +00:00
Action::Return(value) => Ok(value),
Action::None => Err(ValidationError::ExpectedValue),
}
}
2024-03-08 17:24:11 +00:00
pub fn as_return_value(self) -> Result<Value, ValidationError> {
if let Action::Return(value) = self {
Ok(value)
} else {
Err(ValidationError::InterpreterExpectedReturn)
}
}
2024-02-25 18:49:26 +00:00
}