1
0
This commit is contained in:
Jeff 2024-08-13 18:27:03 -04:00
parent 5757f52dbd
commit a78d560a0d
3 changed files with 28 additions and 4 deletions

View File

@ -34,6 +34,7 @@ impl<T: Display> Display for Node<T> {
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Statement { pub enum Statement {
// Assignment does not return a value, but has a side effect on the context
Assignment { Assignment {
identifier: Node<Identifier>, identifier: Node<Identifier>,
operator: Node<AssignmentOperator>, operator: Node<AssignmentOperator>,
@ -43,7 +44,7 @@ pub enum Statement {
// A sequence of statements // A sequence of statements
Block(Vec<Node<Statement>>), Block(Vec<Node<Statement>>),
// Assignment, logic, math and comparison expressions with two operands // Logic, math and comparison expressions with two operands
BinaryOperation { BinaryOperation {
left: Box<Node<Statement>>, left: Box<Node<Statement>>,
operator: Node<BinaryOperator>, operator: Node<BinaryOperator>,
@ -100,7 +101,10 @@ pub enum Statement {
else_body: Box<Node<Statement>>, else_body: Box<Node<Statement>>,
}, },
// Identifier expression // Identifier
//
// Identifier statements in the syntax tree (i.e. Node<Statement>) are evaluated as
// expressions or reconstructed into a Node<Identifier> by the parser
Identifier(Identifier), Identifier(Identifier),
// Value collection expressions // Value collection expressions
@ -111,7 +115,7 @@ pub enum Statement {
Constant(Value), Constant(Value),
// A statement that always returns None. Created with a semicolon, it causes the preceding // A statement that always returns None. Created with a semicolon, it causes the preceding
// statement to return None. This is analagous to the semicolon or unit type in Rust. // statement to return None. This is analagous to the semicolon in Rust.
Nil(Box<Node<Statement>>), Nil(Box<Node<Statement>>),
} }

View File

@ -1,6 +1,20 @@
//! The Dust programming language. //! The Dust programming language.
//! //!
//! Dust is a statically typed, interpreted programming language. //! To get started, you can use the `run` function to run a Dust program.
//!
//! ```rust
//! use dust_lang::{run, Value};
//!
//! let program = "
//! foo = 21
//! bar = 2
//! foo * bar
//! ";
//!
//! let the_answer = run(program).unwrap();
//!
//! assert_eq!(the_answer, Some(Value::integer(42)));
//! ```
pub mod abstract_tree; pub mod abstract_tree;
pub mod analyzer; pub mod analyzer;
pub mod built_in_function; pub mod built_in_function;

View File

@ -63,6 +63,12 @@ pub fn run_with_context(source: &str, context: Context) -> Result<Option<Value>,
.map_err(|vm_error| DustError::VmError { vm_error, source }) .map_err(|vm_error| DustError::VmError { vm_error, source })
} }
/// Dust virtual machine.
///
/// **Warning**: Do not run an AbstractSyntaxTree that has not been analyzed. Use the `run` or
/// `run_with_context` functions to make sure the program is analyzed before running it.
///
/// See the `run_with_context` function for an example of how to use the Analyzer and the VM.
pub struct Vm { pub struct Vm {
abstract_tree: AbstractSyntaxTree, abstract_tree: AbstractSyntaxTree,
context: Context, context: Context,