Skip to content

Commit

Permalink
🚚 (parser): Move AST structs to own file
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Somfic committed Jun 12, 2024
1 parent 6a7943f commit d35d26a
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 35 deletions.
32 changes: 32 additions & 0 deletions src/parser/ast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::scanner::lexeme::Lexeme;

#[derive(Debug, Clone, PartialEq)]
pub enum Symbol {
Expression(Expression),
Statement(Statement),
Unknown(Lexeme),
}

#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
Number(f64),
String(String),
Identifier(String),
Binary(Box<Expression>, BinaryOperation, Box<Expression>),
Grouping(Box<Expression>),
}

#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Block(Vec<Statement>),
Declaration(String, Expression),
Expression(Expression),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BinaryOperation {
Plus,
Minus,
Times,
Divide,
}
2 changes: 1 addition & 1 deletion src/parser/expression.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{lookup::BindingPower, Diagnostic, Expression, Parser};
use super::{ast::Expression, lookup::BindingPower, Diagnostic, Parser};
use crate::scanner::lexeme::{Lexeme, Range};

pub fn parse(
Expand Down
7 changes: 5 additions & 2 deletions src/parser/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use crate::scanner::lexeme::{Lexeme, TokenType, TokenValue};
use core::panic;
use std::collections::HashMap;

use super::{
expression, macros::expect_token, BinaryOperation, Diagnostic, Expression, Parser, Statement,
ast::{BinaryOperation, Expression, Statement},
macros::{expect_expression, expect_token},
statement::parse_variable_declaration,
Diagnostic, Parser,
};
use crate::scanner::lexeme::{Lexeme, TokenType, TokenValue};

#[derive(Debug, PartialEq, PartialOrd)]
pub enum BindingPower {
Expand Down
34 changes: 4 additions & 30 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
use std::collections::HashSet;

use ast::{Statement, Symbol};
use lookup::Lookup;

use crate::scanner::lexeme::{Lexeme, Range};

pub mod ast;
pub mod expression;
pub mod lookup;
pub mod macros;
pub mod statement;

#[derive(Debug, Clone, PartialEq)]
pub enum Symbol {
Expression(Expression),
Statement(Statement),
Unknown(Lexeme),
}

#[derive(Debug, Clone, PartialEq)]
pub enum Expression {
Number(f64),
String(String),
Identifier(String),
Binary(Box<Expression>, BinaryOperation, Box<Expression>),
Grouping(Box<Expression>),
}

#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
Block(Vec<Statement>),
Expression(Expression),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BinaryOperation {
Plus,
Minus,
Times,
Divide,
}

pub struct Parser {
lookup: Lookup,
lexemes: Vec<Lexeme>,
Expand Down Expand Up @@ -142,6 +114,8 @@ impl Diagnostic {

#[cfg(test)]
mod tests {
use ast::{BinaryOperation, Expression};

use crate::scanner::Scanner;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion src/transpiler/bend.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::parser::{BinaryOperation, Expression, Statement, Symbol};
use crate::parser::ast::{BinaryOperation, Expression, Statement, Symbol};

use super::Transpiler;

Expand Down
2 changes: 1 addition & 1 deletion src/transpiler/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::parser::Symbol;
use crate::parser::ast::Symbol;

pub mod bend;

Expand Down

0 comments on commit d35d26a

Please sign in to comment.