dust/src/abstract_tree/mod.rs

93 lines
2.9 KiB
Rust
Raw Normal View History

2023-10-07 02:45:36 +00:00
//! Abstract, executable representations of corresponding items found in Dust
//! source code. The types that implement [AbstractTree] are inteded to be
//! created by an [Evaluator].
//!
//! When adding new lanugage features, first extend the grammar to recognize new
//! syntax nodes. Then add a new AbstractTree type using the existing types as
//! examples.
2023-10-06 17:32:58 +00:00
pub mod assignment;
2023-10-31 17:04:22 +00:00
pub mod block;
2023-10-06 17:32:58 +00:00
pub mod expression;
2023-10-17 18:06:02 +00:00
pub mod r#for;
2023-10-06 17:32:58 +00:00
pub mod function_call;
pub mod identifier;
pub mod if_else;
2023-10-29 23:31:06 +00:00
pub mod index;
2023-11-15 01:41:57 +00:00
pub mod index_assignment;
2023-10-06 17:32:58 +00:00
pub mod logic;
pub mod r#match;
pub mod math;
pub mod statement;
2023-11-30 00:23:42 +00:00
pub mod type_defintion;
pub mod r#use;
2023-10-10 18:12:07 +00:00
pub mod value_node;
2023-10-07 02:45:36 +00:00
pub mod r#while;
pub mod r#yield;
2023-10-06 17:32:58 +00:00
pub use {
2023-11-30 00:23:42 +00:00
assignment::*, block::*, expression::*, function_call::*, identifier::*, if_else::*, index::*,
index_assignment::IndexAssignment, logic::*, math::*, r#for::*, r#match::*, r#use::*,
r#while::*, r#yield::*, statement::*, type_defintion::*, value_node::*,
2023-10-06 17:32:58 +00:00
};
use tree_sitter::Node;
2023-11-30 00:23:42 +00:00
use crate::{Error, Map, Result, Value};
pub struct Root {
statements: Vec<Statement>,
}
impl AbstractTree for Root {
fn from_syntax_node(source: &str, node: Node) -> Result<Self> {
Error::expect_syntax_node(source, "root", node)?;
let statement_count = node.child_count();
let mut statements = Vec::with_capacity(statement_count);
for index in 0..statement_count {
let statement_node = node.child(index).unwrap();
let statement = Statement::from_syntax_node(source, statement_node)?;
statements.push(statement);
}
Ok(Root { statements })
}
fn run(&self, source: &str, context: &Map) -> Result<Value> {
let mut value = Value::Empty;
for statement in &self.statements {
value = statement.run(source, context)?;
}
Ok(value)
}
fn expected_type(&self, context: &Map) -> Result<TypeDefinition> {
self.statements.last().unwrap().expected_type(context)
}
}
2023-10-06 17:32:58 +00:00
/// This trait is implemented by the Evaluator's internal types to form an
/// executable tree that resolves to a single value.
pub trait AbstractTree: Sized {
/// Interpret the syntax tree at the given node and return the abstraction.
///
/// This function is used to convert nodes in the Tree Sitter concrete
/// syntax tree into executable nodes in an abstract tree. This function is
/// where the tree should be traversed by accessing sibling and child nodes.
/// Each node in the CST should be traversed only once.
///
/// If necessary, the source code can be accessed directly by getting the
/// node's byte range.
2023-10-10 17:29:11 +00:00
fn from_syntax_node(source: &str, node: Node) -> Result<Self>;
2023-10-06 17:32:58 +00:00
2023-11-16 07:57:50 +00:00
/// Execute dust code by traversing the tree.
2023-11-30 00:23:42 +00:00
fn run(&self, source: &str, context: &Map) -> Result<Value>;
fn expected_type(&self, context: &Map) -> Result<TypeDefinition>;
2023-10-06 17:32:58 +00:00
}