diff options
| author | Tolmachev Igor <me@igorek.dev> | 2026-05-09 20:47:04 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2026-05-09 20:47:04 +0300 |
| commit | 160b64427d79290a59ac48c9babca064232d8dfd (patch) | |
| tree | 0c2cc79f0a266761866ff325abdd4f2f0c7e7301 | |
| parent | 6be28381d6081dfb3a1dc9d1ec15062b67ba1ef9 (diff) | |
| download | crisp-dev.tar.gz crisp-dev.zip | |
Make project structure more consistentdev
| -rw-r--r-- | compiler/src/ast/mod.rs | 34 | ||||
| -rw-r--r-- | compiler/src/ast/models.rs | 36 | ||||
| -rw-r--r-- | compiler/src/ast/parser.rs | 12 | ||||
| -rw-r--r-- | compiler/src/ast/tests.rs | 16 | ||||
| -rw-r--r-- | compiler/src/lex/lexer.rs (renamed from compiler/src/lexer/mod.rs) | 19 | ||||
| -rw-r--r-- | compiler/src/lex/mod.rs | 8 | ||||
| -rw-r--r-- | compiler/src/lex/tests.rs (renamed from compiler/src/lexer/tests.rs) | 8 | ||||
| -rw-r--r-- | compiler/src/lex/token.rs | 10 | ||||
| -rw-r--r-- | compiler/src/lib.rs | 2 |
9 files changed, 77 insertions, 68 deletions
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 @@ | |||
| 1 | mod error; | 1 | mod error; |
| 2 | mod models; | ||
| 2 | mod parser; | 3 | mod parser; |
| 3 | 4 | ||
| 4 | use std::rc::Rc; | ||
| 5 | |||
| 6 | use crate::span::Spanned; | ||
| 7 | pub use error::Error; | 5 | pub use error::Error; |
| 6 | pub use models::{Ast, Atom, Expr}; | ||
| 8 | pub use parser::Parser; | 7 | pub use parser::Parser; |
| 9 | 8 | ||
| 10 | #[cfg(test)] | 9 | #[cfg(test)] |
| 11 | mod tests; | 10 | mod tests; |
| 12 | |||
| 13 | #[derive(Clone, Debug, PartialEq)] | ||
| 14 | pub enum Atom { | ||
| 15 | Float(f64), | ||
| 16 | Integer(i64), | ||
| 17 | String(Rc<str>), | ||
| 18 | Symbol(Rc<str>), | ||
| 19 | Bool(bool), | ||
| 20 | Nil, | ||
| 21 | } | ||
| 22 | |||
| 23 | #[derive(Clone, Debug, PartialEq)] | ||
| 24 | pub enum Expr { | ||
| 25 | Atom(Atom), | ||
| 26 | List(Vec<Spanned<Expr>>), | ||
| 27 | } | ||
| 28 | |||
| 29 | #[derive(Clone, Debug, PartialEq)] | ||
| 30 | pub struct Program(Vec<Spanned<Expr>>); | ||
| 31 | |||
| 32 | impl Program { | ||
| 33 | pub fn inner(&self) -> &[Spanned<Expr>] { | ||
| 34 | &self.0 | ||
| 35 | } | ||
| 36 | |||
| 37 | pub fn into_inner(self) -> Vec<Spanned<Expr>> { | ||
| 38 | self.0 | ||
| 39 | } | ||
| 40 | } | ||
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 @@ | |||
| 1 | use std::rc::Rc; | ||
| 2 | |||
| 3 | use crate::span::Spanned; | ||
| 4 | |||
| 5 | #[derive(Clone, Debug, PartialEq)] | ||
| 6 | pub enum Atom { | ||
| 7 | Float(f64), | ||
| 8 | Integer(i64), | ||
| 9 | String(Rc<str>), | ||
| 10 | Symbol(Rc<str>), | ||
| 11 | Bool(bool), | ||
| 12 | Nil, | ||
| 13 | } | ||
| 14 | |||
| 15 | #[derive(Clone, Debug, PartialEq)] | ||
| 16 | pub enum Expr { | ||
| 17 | Atom(Atom), | ||
| 18 | List(Vec<Spanned<Expr>>), | ||
| 19 | } | ||
| 20 | |||
| 21 | #[derive(Clone, Debug, PartialEq)] | ||
| 22 | pub struct Ast(Vec<Spanned<Expr>>); | ||
| 23 | |||
| 24 | impl Ast { | ||
| 25 | pub fn new(ast: Vec<Spanned<Expr>>) -> Self { | ||
| 26 | Self(ast) | ||
| 27 | } | ||
| 28 | |||
| 29 | pub fn inner(&self) -> &[Spanned<Expr>] { | ||
| 30 | &self.0 | ||
| 31 | } | ||
| 32 | |||
| 33 | pub fn into_inner(self) -> Vec<Spanned<Expr>> { | ||
| 34 | self.0 | ||
| 35 | } | ||
| 36 | } | ||
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 @@ | |||
| 1 | use std::iter::Peekable; | 1 | use std::iter::Peekable; |
| 2 | 2 | ||
| 3 | use crate::{ | 3 | use crate::{ |
| 4 | ast::{Atom, Error, Expr, Program}, | 4 | ast::{Ast, Atom, Error, Expr}, |
| 5 | lexer::Token, | 5 | lex::Token, |
| 6 | span::{Pos, Span, Spanned}, | 6 | span::{Pos, Span, Spanned}, |
| 7 | }; | 7 | }; |
| 8 | 8 | ||
| @@ -174,13 +174,13 @@ where | |||
| 174 | Err(Spanned::new(Error::UnclosedLeftPar, left_par_span)) | 174 | Err(Spanned::new(Error::UnclosedLeftPar, left_par_span)) |
| 175 | } | 175 | } |
| 176 | 176 | ||
| 177 | pub fn parse(mut self) -> Result<Program, Spanned<Error>> { | 177 | pub fn parse(mut self) -> Result<Ast, Spanned<Error>> { |
| 178 | let mut program = Vec::new(); | 178 | let mut ast = Vec::new(); |
| 179 | 179 | ||
| 180 | while self.peek().is_some() { | 180 | while self.peek().is_some() { |
| 181 | program.push(self.parse_expr()?) | 181 | ast.push(self.parse_expr()?) |
| 182 | } | 182 | } |
| 183 | 183 | ||
| 184 | Ok(Program(program)) | 184 | Ok(Ast::new(ast)) |
| 185 | } | 185 | } |
| 186 | } | 186 | } |
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 @@ | |||
| 1 | use std::f64; | 1 | use std::{fmt::Debug, iter::repeat_n, rc::Rc}; |
| 2 | use std::fmt::Debug; | ||
| 3 | use std::iter::repeat_n; | ||
| 4 | use std::rc::Rc; | ||
| 5 | 2 | ||
| 6 | use self::E::*; | 3 | use crate::{ |
| 7 | use super::{Error, Parser, parser::MAX_DEPTH}; | 4 | ast::{Atom, Error, Expr, Parser, parser::MAX_DEPTH, tests::E::*}, |
| 8 | use crate::ast::{Atom, Expr}; | 5 | lex::Token::{self, *}, |
| 9 | use crate::lexer::Token; | 6 | span::{Pos, Span, Spanned}, |
| 10 | use crate::lexer::Token::*; | 7 | }; |
| 11 | use crate::span::{Pos, Span, Spanned}; | ||
| 12 | 8 | ||
| 13 | #[derive(Debug, PartialEq)] | 9 | #[derive(Debug, PartialEq)] |
| 14 | enum E { | 10 | enum E { |
diff --git a/compiler/src/lexer/mod.rs b/compiler/src/lex/lexer.rs index f3c8b76..801d382 100644 --- a/compiler/src/lexer/mod.rs +++ b/compiler/src/lex/lexer.rs | |||
| @@ -1,23 +1,12 @@ | |||
| 1 | use crate::span::{Pos, Span, Spanned}; | 1 | use crate::{ |
| 2 | 2 | lex::Token, | |
| 3 | #[cfg(test)] | 3 | span::{Pos, Span, Spanned}, |
| 4 | mod tests; | 4 | }; |
| 5 | 5 | ||
| 6 | fn is_terminator(ch: char) -> bool { | 6 | fn is_terminator(ch: char) -> bool { |
| 7 | ch.is_whitespace() || matches!(ch, '(' | ')' | '\'' | '"' | ';') | 7 | ch.is_whitespace() || matches!(ch, '(' | ')' | '\'' | '"' | ';') |
| 8 | } | 8 | } |
| 9 | 9 | ||
| 10 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| 11 | pub enum Token<'a> { | ||
| 12 | LeftPar, | ||
| 13 | RightPar, | ||
| 14 | Quote, | ||
| 15 | Number(&'a str), | ||
| 16 | String(&'a str), | ||
| 17 | UnclosedString(&'a str), | ||
| 18 | Symbol(&'a str), | ||
| 19 | } | ||
| 20 | |||
| 21 | pub struct Lexer<'a> { | 10 | pub struct Lexer<'a> { |
| 22 | input: &'a str, | 11 | input: &'a str, |
| 23 | cursor: usize, | 12 | cursor: usize, |
diff --git a/compiler/src/lex/mod.rs b/compiler/src/lex/mod.rs new file mode 100644 index 0000000..7bc4440 --- /dev/null +++ b/compiler/src/lex/mod.rs | |||
| @@ -0,0 +1,8 @@ | |||
| 1 | mod lexer; | ||
| 2 | mod token; | ||
| 3 | |||
| 4 | pub use lexer::Lexer; | ||
| 5 | pub use token::Token; | ||
| 6 | |||
| 7 | #[cfg(test)] | ||
| 8 | mod tests; | ||
diff --git a/compiler/src/lexer/tests.rs b/compiler/src/lex/tests.rs index 6f96c65..2d872a2 100644 --- a/compiler/src/lexer/tests.rs +++ b/compiler/src/lex/tests.rs | |||
| @@ -1,7 +1,7 @@ | |||
| 1 | use crate::span::Pos; | 1 | use crate::{ |
| 2 | 2 | lex::{Lexer, Token, Token::*}, | |
| 3 | use super::Token::*; | 3 | span::Pos, |
| 4 | use super::{Lexer, Token}; | 4 | }; |
| 5 | 5 | ||
| 6 | fn tokenize<'a>(input: &'a str) -> Vec<Token<'a>> { | 6 | fn tokenize<'a>(input: &'a str) -> Vec<Token<'a>> { |
| 7 | Lexer::new(input).map(|s| s.inner).collect() | 7 | Lexer::new(input).map(|s| s.inner).collect() |
diff --git a/compiler/src/lex/token.rs b/compiler/src/lex/token.rs new file mode 100644 index 0000000..2d07885 --- /dev/null +++ b/compiler/src/lex/token.rs | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | ||
| 2 | pub enum Token<'a> { | ||
| 3 | LeftPar, | ||
| 4 | RightPar, | ||
| 5 | Quote, | ||
| 6 | Number(&'a str), | ||
| 7 | String(&'a str), | ||
| 8 | UnclosedString(&'a str), | ||
| 9 | Symbol(&'a str), | ||
| 10 | } | ||
diff --git a/compiler/src/lib.rs b/compiler/src/lib.rs index b9b7a46..80311b4 100644 --- a/compiler/src/lib.rs +++ b/compiler/src/lib.rs | |||
| @@ -1,3 +1,3 @@ | |||
| 1 | pub mod ast; | 1 | pub mod ast; |
| 2 | pub mod lexer; | 2 | pub mod lex; |
| 3 | pub mod span; | 3 | pub mod span; |
