diff --git a/dust-lang/src/analyzer.rs b/dust-lang/src/analyzer.rs index cd3f1af..3fbf62d 100644 --- a/dust-lang/src/analyzer.rs +++ b/dust-lang/src/analyzer.rs @@ -10,7 +10,7 @@ use std::{ }; use crate::{ - abstract_tree::{AbstractSyntaxTree, Node, Statement}, + ast::{AbstractSyntaxTree, Node, Statement}, parse, Context, DustError, Identifier, Span, Type, }; diff --git a/dust-lang/src/abstract_tree/expression.rs b/dust-lang/src/ast/expression.rs similarity index 100% rename from dust-lang/src/abstract_tree/expression.rs rename to dust-lang/src/ast/expression.rs index 5613a81..023d069 100644 --- a/dust-lang/src/abstract_tree/expression.rs +++ b/dust-lang/src/ast/expression.rs @@ -679,21 +679,6 @@ pub enum IfExpression { }, } -#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub enum ElseExpression { - Block(Node), - If(Node>), -} - -impl Display for ElseExpression { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - match self { - ElseExpression::Block(block) => write!(f, "{}", block), - ElseExpression::If(r#if) => write!(f, "{}", r#if), - } - } -} - impl Display for IfExpression { fn fmt(&self, f: &mut Formatter) -> fmt::Result { match self { @@ -714,6 +699,21 @@ impl Display for IfExpression { } } +#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] +pub enum ElseExpression { + Block(Node), + If(Node>), +} + +impl Display for ElseExpression { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + match self { + ElseExpression::Block(block) => write!(f, "{}", block), + ElseExpression::If(r#if) => write!(f, "{}", r#if), + } + } +} + #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub enum BlockExpression { Async(Vec), diff --git a/dust-lang/src/ast/mod.rs b/dust-lang/src/ast/mod.rs new file mode 100644 index 0000000..05e8e28 --- /dev/null +++ b/dust-lang/src/ast/mod.rs @@ -0,0 +1,59 @@ +//! In-memory representation of a Dust program. +mod expression; +mod statement; + +pub use expression::*; +pub use statement::*; + +use std::{ + collections::VecDeque, + fmt::{self, Display, Formatter}, +}; + +use serde::{Deserialize, Serialize}; + +use crate::Span; + +/// In-memory representation of a Dust program. +#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct AbstractSyntaxTree { + pub statements: VecDeque, +} + +impl AbstractSyntaxTree { + pub fn new() -> Self { + Self { + statements: VecDeque::new(), + } + } + + pub fn with_statements(statements: [Statement; LEN]) -> Self { + Self { + statements: statements.into(), + } + } +} + +impl Default for AbstractSyntaxTree { + fn default() -> Self { + Self::new() + } +} + +#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] +pub struct Node { + pub inner: T, + pub position: Span, +} + +impl Node { + pub fn new(inner: T, position: Span) -> Self { + Self { inner, position } + } +} + +impl Display for Node { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + write!(f, "{}", self.inner) + } +} diff --git a/dust-lang/src/abstract_tree/mod.rs b/dust-lang/src/ast/statement.rs similarity index 75% rename from dust-lang/src/abstract_tree/mod.rs rename to dust-lang/src/ast/statement.rs index 111a742..49cf9bf 100644 --- a/dust-lang/src/abstract_tree/mod.rs +++ b/dust-lang/src/ast/statement.rs @@ -1,60 +1,10 @@ -//! In-memory representation of a Dust program. -mod expression; - -pub use expression::*; - -use std::{ - collections::VecDeque, - fmt::{self, Display, Formatter}, -}; +use std::fmt::{self, Display, Formatter}; use serde::{Deserialize, Serialize}; use crate::{Context, Identifier, Span, Type}; -/// In-memory representation of a Dust program. -#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub struct AbstractSyntaxTree { - pub statements: VecDeque, -} - -impl AbstractSyntaxTree { - pub fn new() -> Self { - Self { - statements: VecDeque::new(), - } - } - - pub fn with_statements(statements: [Statement; LEN]) -> Self { - Self { - statements: statements.into(), - } - } -} - -impl Default for AbstractSyntaxTree { - fn default() -> Self { - Self::new() - } -} - -#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] -pub struct Node { - pub inner: T, - pub position: Span, -} - -impl Node { - pub fn new(inner: T, position: Span) -> Self { - Self { inner, position } - } -} - -impl Display for Node { - fn fmt(&self, f: &mut Formatter) -> fmt::Result { - write!(f, "{}", self.inner) - } -} +use super::{Expression, Node}; #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] pub enum Statement { diff --git a/dust-lang/src/lib.rs b/dust-lang/src/lib.rs index 755a8f6..4766ef9 100644 --- a/dust-lang/src/lib.rs +++ b/dust-lang/src/lib.rs @@ -15,8 +15,8 @@ //! //! assert_eq!(the_answer, Some(Value::integer(42))); //! ``` -pub mod abstract_tree; pub mod analyzer; +pub mod ast; pub mod built_in_function; pub mod context; pub mod dust_error; @@ -28,8 +28,8 @@ pub mod r#type; pub mod value; pub mod vm; -pub use abstract_tree::{AbstractSyntaxTree, Expression, Node, Statement}; pub use analyzer::{analyze, Analyzer, AnalyzerError}; +pub use ast::{AbstractSyntaxTree, Expression, Node, Statement}; pub use built_in_function::{BuiltInFunction, BuiltInFunctionError}; pub use context::{Context, VariableData}; pub use dust_error::DustError; diff --git a/dust-lang/src/parser.rs b/dust-lang/src/parser.rs index cc8ef4d..553d240 100644 --- a/dust-lang/src/parser.rs +++ b/dust-lang/src/parser.rs @@ -12,8 +12,7 @@ use std::{ }; use crate::{ - abstract_tree::*, DustError, Identifier, LexError, Lexer, Span, Token, TokenKind, TokenOwned, - Type, + ast::*, DustError, Identifier, LexError, Lexer, Span, Token, TokenKind, TokenOwned, Type, }; /// Parses the input into an abstract syntax tree. diff --git a/dust-lang/src/vm.rs b/dust-lang/src/vm.rs index 77dc301..9807508 100644 --- a/dust-lang/src/vm.rs +++ b/dust-lang/src/vm.rs @@ -12,7 +12,7 @@ use std::{ use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; use crate::{ - abstract_tree::{ + ast::{ AbstractSyntaxTree, BlockExpression, CallExpression, ElseExpression, FieldAccessExpression, IfExpression, ListExpression, ListIndexExpression, LiteralExpression, LoopExpression, Node, OperatorExpression, Statement,