dust/src/abstract_tree/mod.rs

60 lines
1.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-16 20:48:02 +00:00
pub mod r#async;
2023-10-06 17:32:58 +00:00
pub mod expression;
2023-10-17 21:52:41 +00:00
pub mod filter;
2023-10-18 22:26:45 +00:00
pub mod find;
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;
pub mod insert;
2023-10-06 17:32:58 +00:00
pub mod item;
pub mod logic;
pub mod r#match;
pub mod math;
2023-10-19 01:50:45 +00:00
pub mod remove;
2023-10-21 17:04:17 +00:00
pub mod select;
2023-10-06 17:32:58 +00:00
pub mod statement;
2023-10-28 14:28:43 +00:00
pub mod sublist;
2023-10-21 17:47:08 +00:00
pub mod tool;
2023-10-17 20:40:07 +00:00
pub mod transform;
2023-10-10 18:12:07 +00:00
pub mod value_node;
2023-10-07 02:45:36 +00:00
pub mod r#while;
2023-10-06 17:32:58 +00:00
pub use {
2023-10-21 17:19:01 +00:00
assignment::*, expression::*, filter::*, find::*, function_call::*, identifier::*, if_else::*,
2023-10-29 23:31:06 +00:00
index::*, insert::*, item::*, logic::*, math::*, r#async::*, r#for::*, r#match::*, r#while::*,
remove::*, select::*, statement::*, sublist::*, tool::*, transform::*, value_node::*,
2023-10-06 17:32:58 +00:00
};
use tree_sitter::Node;
2023-10-25 20:44:50 +00:00
use crate::{Map, Result, Value};
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
/// Execute dust code by traversing the tree
2023-10-25 20:44:50 +00:00
fn run(&self, source: &str, context: &mut Map) -> Result<Value>;
2023-10-06 17:32:58 +00:00
}