diff options
Diffstat (limited to 'compiler/src/ast')
| -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 |
4 files changed, 50 insertions, 48 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 { |
