aboutsummaryrefslogtreecommitdiff
path: root/compiler/src/ast
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/src/ast')
-rw-r--r--compiler/src/ast/mod.rs34
-rw-r--r--compiler/src/ast/models.rs36
-rw-r--r--compiler/src/ast/parser.rs12
-rw-r--r--compiler/src/ast/tests.rs16
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 @@
1mod error; 1mod error;
2mod models;
2mod parser; 3mod parser;
3 4
4use std::rc::Rc;
5
6use crate::span::Spanned;
7pub use error::Error; 5pub use error::Error;
6pub use models::{Ast, Atom, Expr};
8pub use parser::Parser; 7pub use parser::Parser;
9 8
10#[cfg(test)] 9#[cfg(test)]
11mod tests; 10mod tests;
12
13#[derive(Clone, Debug, PartialEq)]
14pub 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)]
24pub enum Expr {
25 Atom(Atom),
26 List(Vec<Spanned<Expr>>),
27}
28
29#[derive(Clone, Debug, PartialEq)]
30pub struct Program(Vec<Spanned<Expr>>);
31
32impl 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 @@
1use std::rc::Rc;
2
3use crate::span::Spanned;
4
5#[derive(Clone, Debug, PartialEq)]
6pub 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)]
16pub enum Expr {
17 Atom(Atom),
18 List(Vec<Spanned<Expr>>),
19}
20
21#[derive(Clone, Debug, PartialEq)]
22pub struct Ast(Vec<Spanned<Expr>>);
23
24impl 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 @@
1use std::iter::Peekable; 1use std::iter::Peekable;
2 2
3use crate::{ 3use 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 @@
1use std::f64; 1use std::{fmt::Debug, iter::repeat_n, rc::Rc};
2use std::fmt::Debug;
3use std::iter::repeat_n;
4use std::rc::Rc;
5 2
6use self::E::*; 3use crate::{
7use super::{Error, Parser, parser::MAX_DEPTH}; 4 ast::{Atom, Error, Expr, Parser, parser::MAX_DEPTH, tests::E::*},
8use crate::ast::{Atom, Expr}; 5 lex::Token::{self, *},
9use crate::lexer::Token; 6 span::{Pos, Span, Spanned},
10use crate::lexer::Token::*; 7};
11use crate::span::{Pos, Span, Spanned};
12 8
13#[derive(Debug, PartialEq)] 9#[derive(Debug, PartialEq)]
14enum E { 10enum E {