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;
|
2024-02-29 02:04:38 +00:00
|
|
|
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
|
|
|
|
|
|
|
pub use self::{
|
2024-03-08 17:39:35 +00:00
|
|
|
assignment::{Assignment, AssignmentOperator},
|
|
|
|
block::Block,
|
|
|
|
expression::Expression,
|
2024-03-09 12:34:34 +00:00
|
|
|
function_call::FunctionCall,
|
2024-03-08 17:39:35 +00:00
|
|
|
identifier::Identifier,
|
2024-03-08 19:01:05 +00:00
|
|
|
if_else::IfElse,
|
2024-03-08 17:39:35 +00:00
|
|
|
index::Index,
|
|
|
|
logic::Logic,
|
|
|
|
math::Math,
|
|
|
|
r#loop::Loop,
|
|
|
|
r#type::Type,
|
2024-03-11 21:58:26 +00:00
|
|
|
r#while::While,
|
2024-03-08 17:39:35 +00:00
|
|
|
statement::Statement,
|
2024-03-07 11:33:54 +00:00
|
|
|
value_node::ValueNode,
|
2024-02-25 18:49:26 +00:00
|
|
|
};
|
|
|
|
|
2024-02-29 02:04:38 +00:00
|
|
|
use crate::{
|
|
|
|
context::Context,
|
|
|
|
error::{RuntimeError, ValidationError},
|
|
|
|
Value,
|
|
|
|
};
|
2024-02-25 18:49:26 +00:00
|
|
|
|
|
|
|
pub trait AbstractTree {
|
2024-02-29 02:04:38 +00:00
|
|
|
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 21:44:52 +00:00
|
|
|
Break,
|
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 {
|
|
|
|
Action::Return(value) => Ok(value),
|
2024-03-11 21:44:52 +00:00
|
|
|
_ => Err(ValidationError::ExpectedValue),
|
2024-03-09 13:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|