From 160b64427d79290a59ac48c9babca064232d8dfd Mon Sep 17 00:00:00 2001 From: Tolmachev Igor Date: Sat, 9 May 2026 20:47:04 +0300 Subject: Make project structure more consistent --- compiler/src/ast/mod.rs | 34 ++-------------------------------- compiler/src/ast/models.rs | 36 ++++++++++++++++++++++++++++++++++++ compiler/src/ast/parser.rs | 12 ++++++------ compiler/src/ast/tests.rs | 18 +++++++----------- 4 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 compiler/src/ast/models.rs (limited to 'compiler/src/ast') diff --git a/compiler/src/ast/mod.rs b/compiler/src/ast/mod.rs index 8e35baf..2a0be03 100644 --- a/compiler/src/ast/mod.rs +++ b/compiler/src/ast/mod.rs @@ -1,40 +1,10 @@ mod error; +mod models; mod parser; -use std::rc::Rc; - -use crate::span::Spanned; pub use error::Error; +pub use models::{Ast, Atom, Expr}; pub use parser::Parser; #[cfg(test)] mod tests; - -#[derive(Clone, Debug, PartialEq)] -pub enum Atom { - Float(f64), - Integer(i64), - String(Rc), - Symbol(Rc), - Bool(bool), - Nil, -} - -#[derive(Clone, Debug, PartialEq)] -pub enum Expr { - Atom(Atom), - List(Vec>), -} - -#[derive(Clone, Debug, PartialEq)] -pub struct Program(Vec>); - -impl Program { - pub fn inner(&self) -> &[Spanned] { - &self.0 - } - - pub fn into_inner(self) -> Vec> { - self.0 - } -} diff --git a/compiler/src/ast/models.rs b/compiler/src/ast/models.rs new file mode 100644 index 0000000..db9728d --- /dev/null +++ b/compiler/src/ast/models.rs @@ -0,0 +1,36 @@ +use std::rc::Rc; + +use crate::span::Spanned; + +#[derive(Clone, Debug, PartialEq)] +pub enum Atom { + Float(f64), + Integer(i64), + String(Rc), + Symbol(Rc), + Bool(bool), + Nil, +} + +#[derive(Clone, Debug, PartialEq)] +pub enum Expr { + Atom(Atom), + List(Vec>), +} + +#[derive(Clone, Debug, PartialEq)] +pub struct Ast(Vec>); + +impl Ast { + pub fn new(ast: Vec>) -> Self { + Self(ast) + } + + pub fn inner(&self) -> &[Spanned] { + &self.0 + } + + pub fn into_inner(self) -> Vec> { + self.0 + } +} diff --git a/compiler/src/ast/parser.rs b/compiler/src/ast/parser.rs index 263e5b7..33b36be 100644 --- a/compiler/src/ast/parser.rs +++ b/compiler/src/ast/parser.rs @@ -1,8 +1,8 @@ use std::iter::Peekable; use crate::{ - ast::{Atom, Error, Expr, Program}, - lexer::Token, + ast::{Ast, Atom, Error, Expr}, + lex::Token, span::{Pos, Span, Spanned}, }; @@ -174,13 +174,13 @@ where Err(Spanned::new(Error::UnclosedLeftPar, left_par_span)) } - pub fn parse(mut self) -> Result> { - let mut program = Vec::new(); + pub fn parse(mut self) -> Result> { + let mut ast = Vec::new(); while self.peek().is_some() { - program.push(self.parse_expr()?) + ast.push(self.parse_expr()?) } - Ok(Program(program)) + Ok(Ast::new(ast)) } } diff --git a/compiler/src/ast/tests.rs b/compiler/src/ast/tests.rs index c6d8c38..8905427 100644 --- a/compiler/src/ast/tests.rs +++ b/compiler/src/ast/tests.rs @@ -1,14 +1,10 @@ -use std::f64; -use std::fmt::Debug; -use std::iter::repeat_n; -use std::rc::Rc; - -use self::E::*; -use super::{Error, Parser, parser::MAX_DEPTH}; -use crate::ast::{Atom, Expr}; -use crate::lexer::Token; -use crate::lexer::Token::*; -use crate::span::{Pos, Span, Spanned}; +use std::{fmt::Debug, iter::repeat_n, rc::Rc}; + +use crate::{ + ast::{Atom, Error, Expr, Parser, parser::MAX_DEPTH, tests::E::*}, + lex::Token::{self, *}, + span::{Pos, Span, Spanned}, +}; #[derive(Debug, PartialEq)] enum E { -- cgit v1.3