1
0
This commit is contained in:
Jeff 2024-08-16 05:24:55 -04:00
parent bfb07047a5
commit e911853cb5
7 changed files with 81 additions and 73 deletions

View File

@ -10,7 +10,7 @@ use std::{
}; };
use crate::{ use crate::{
abstract_tree::{AbstractSyntaxTree, Node, Statement}, ast::{AbstractSyntaxTree, Node, Statement},
parse, Context, DustError, Identifier, Span, Type, parse, Context, DustError, Identifier, Span, Type,
}; };

View File

@ -679,21 +679,6 @@ pub enum IfExpression {
}, },
} }
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ElseExpression {
Block(Node<BlockExpression>),
If(Node<Box<IfExpression>>),
}
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 { impl Display for IfExpression {
fn fmt(&self, f: &mut Formatter) -> fmt::Result { fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self { match self {
@ -714,6 +699,21 @@ impl Display for IfExpression {
} }
} }
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum ElseExpression {
Block(Node<BlockExpression>),
If(Node<Box<IfExpression>>),
}
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)] #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum BlockExpression { pub enum BlockExpression {
Async(Vec<Statement>), Async(Vec<Statement>),

59
dust-lang/src/ast/mod.rs Normal file
View File

@ -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<Statement>,
}
impl AbstractSyntaxTree {
pub fn new() -> Self {
Self {
statements: VecDeque::new(),
}
}
pub fn with_statements<const LEN: usize>(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<T> {
pub inner: T,
pub position: Span,
}
impl<T> Node<T> {
pub fn new(inner: T, position: Span) -> Self {
Self { inner, position }
}
}
impl<T: Display> Display for Node<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}

View File

@ -1,60 +1,10 @@
//! In-memory representation of a Dust program. use std::fmt::{self, Display, Formatter};
mod expression;
pub use expression::*;
use std::{
collections::VecDeque,
fmt::{self, Display, Formatter},
};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::{Context, Identifier, Span, Type}; use crate::{Context, Identifier, Span, Type};
/// In-memory representation of a Dust program. use super::{Expression, Node};
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct AbstractSyntaxTree {
pub statements: VecDeque<Statement>,
}
impl AbstractSyntaxTree {
pub fn new() -> Self {
Self {
statements: VecDeque::new(),
}
}
pub fn with_statements<const LEN: usize>(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<T> {
pub inner: T,
pub position: Span,
}
impl<T> Node<T> {
pub fn new(inner: T, position: Span) -> Self {
Self { inner, position }
}
}
impl<T: Display> Display for Node<T> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)] #[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Statement { pub enum Statement {

View File

@ -15,8 +15,8 @@
//! //!
//! assert_eq!(the_answer, Some(Value::integer(42))); //! assert_eq!(the_answer, Some(Value::integer(42)));
//! ``` //! ```
pub mod abstract_tree;
pub mod analyzer; pub mod analyzer;
pub mod ast;
pub mod built_in_function; pub mod built_in_function;
pub mod context; pub mod context;
pub mod dust_error; pub mod dust_error;
@ -28,8 +28,8 @@ pub mod r#type;
pub mod value; pub mod value;
pub mod vm; pub mod vm;
pub use abstract_tree::{AbstractSyntaxTree, Expression, Node, Statement};
pub use analyzer::{analyze, Analyzer, AnalyzerError}; pub use analyzer::{analyze, Analyzer, AnalyzerError};
pub use ast::{AbstractSyntaxTree, Expression, Node, Statement};
pub use built_in_function::{BuiltInFunction, BuiltInFunctionError}; pub use built_in_function::{BuiltInFunction, BuiltInFunctionError};
pub use context::{Context, VariableData}; pub use context::{Context, VariableData};
pub use dust_error::DustError; pub use dust_error::DustError;

View File

@ -12,8 +12,7 @@ use std::{
}; };
use crate::{ use crate::{
abstract_tree::*, DustError, Identifier, LexError, Lexer, Span, Token, TokenKind, TokenOwned, ast::*, DustError, Identifier, LexError, Lexer, Span, Token, TokenKind, TokenOwned, Type,
Type,
}; };
/// Parses the input into an abstract syntax tree. /// Parses the input into an abstract syntax tree.

View File

@ -12,7 +12,7 @@ use std::{
use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator}; use rayon::iter::{IndexedParallelIterator, IntoParallelIterator, ParallelIterator};
use crate::{ use crate::{
abstract_tree::{ ast::{
AbstractSyntaxTree, BlockExpression, CallExpression, ElseExpression, FieldAccessExpression, AbstractSyntaxTree, BlockExpression, CallExpression, ElseExpression, FieldAccessExpression,
IfExpression, ListExpression, ListIndexExpression, LiteralExpression, LoopExpression, Node, IfExpression, ListExpression, ListIndexExpression, LiteralExpression, LoopExpression, Node,
OperatorExpression, Statement, OperatorExpression, Statement,