Skip to content

Commit

Permalink
♻️ Refactor typed and untyped ast
Browse files Browse the repository at this point in the history
semver: chore
  • Loading branch information
Somfic committed Dec 29, 2024
1 parent 000d039 commit 3772ae9
Show file tree
Hide file tree
Showing 16 changed files with 157 additions and 471 deletions.
50 changes: 29 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
use crate::parser::typechecker::TypeChecker;
use lexer::{Lexer, TokenKind};
use owo_colors::{Style, Styled};
use parser::{
ast::{Expression, ExpressionValue, Statement},
ast::untyped::{Expression, ExpressionValue},
Parser,
};
use passer::{typing::Typing, Passer};
use std::vec;

pub mod lexer;
pub mod parser;
pub mod passer;

const INPUT: &str = "
fn main() {
let string = \"Hello, world!\";
return 12;
{
let string = \"Hello, world!\";
return 12;
};
enum Test: a, b, c;
let abc = 12;
fn main() {
let a = 12;
let b = 'a';
let c = a + b;
}
";

Expand All @@ -38,7 +33,9 @@ fn main() {
}))
.unwrap();

let mut parser = Parser::new(INPUT);
let lexer = Lexer::new(INPUT);

let mut parser = Parser::new(lexer);
let symbol = match parser.parse() {
Ok(symbol) => symbol,
Err(err) => {
Expand All @@ -47,16 +44,27 @@ fn main() {
}
};

let typing_pass = passer::typing::TypingPasser::pass(&symbol).unwrap();
let typing_pass = typing_pass.combine(passer::unused::UnusedPass::pass(&symbol).unwrap());
let mut typechecker = TypeChecker::new(symbol);
let symbol = match typechecker.check() {
Ok(symbol) => symbol,
Err(err) => {
println!("{:?}", err.with_source_code(INPUT));
return;
}
};

for note in typing_pass.non_critical {
println!("{:?}", note.with_source_code(INPUT));
}
println!("{:?}", symbol);

for note in typing_pass.critical {
println!("{:?}", note.with_source_code(INPUT));
}
// let typing_pass = passer::typing::TypingPasser::pass(&symbol).unwrap();
//let typing_pass = typing_pass.combine(passer::unused::UnusedPass::pass(&symbol).unwrap());

// for note in typing_pass.non_critical {
// println!("{:?}", note.with_source_code(INPUT));
// }

// for note in typing_pass.critical {
// println!("{:?}", note.with_source_code(INPUT));
// }
}

struct SomHighlighter {}
Expand Down
74 changes: 74 additions & 0 deletions src/parser/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use miette::SourceSpan;

pub mod typed;
pub mod untyped;

pub trait Spannable<'de>: Sized {
type Value;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self;

fn at_multiple(spans: Vec<impl Into<miette::SourceSpan>>, value: Self::Value) -> Self {
let spans = spans.into_iter().map(|s| s.into()).collect::<Vec<_>>();

let start = spans
.iter()
.min_by_key(|s| s.offset())
.map(|s| s.offset())
.unwrap_or(0);

let end = spans
.iter()
.max_by_key(|s| s.offset() + s.len())
.map(|s| s.offset() + s.len())
.unwrap_or(0);

let span = miette::SourceSpan::new(start.into(), end - start);

Self::at(span, value)
}
}

pub trait CombineSpan {
fn combine(spans: Vec<SourceSpan>) -> SourceSpan {
let start = spans
.iter()
.min_by_key(|s| s.offset())
.map(|s| s.offset())
.unwrap_or(0);

let end = spans
.iter()
.max_by_key(|s| s.offset() + s.len())
.map(|s| s.offset() + s.len())
.unwrap_or(0);

SourceSpan::new(start.into(), end - start)
}
}

impl CombineSpan for SourceSpan {}

impl<'de> Spannable<'de> for untyped::Expression<'de> {
type Value = untyped::ExpressionValue<'de>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}

impl<'de> Spannable<'de> for untyped::Statement<'de> {
type Value = untyped::StatementValue<'de>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}

impl<'de> Spannable<'de> for untyped::Type<'de> {
type Value = untyped::TypeValue<'de>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}
Empty file added src/parser/ast/typed.rs
Empty file.
73 changes: 1 addition & 72 deletions src/parser/ast.rs → src/parser/ast/untyped.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{borrow::Cow, fmt::Display};

use miette::SourceSpan;
use std::{borrow::Cow, fmt::Display};

#[derive(Debug, Clone)]
pub enum Symbol<'de> {
Expand Down Expand Up @@ -287,73 +286,3 @@ impl Display for Type<'_> {
}
}
}

pub trait Spannable<'de>: Sized {
type Value;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self;

fn at_multiple(spans: Vec<impl Into<miette::SourceSpan>>, value: Self::Value) -> Self {
let spans = spans.into_iter().map(|s| s.into()).collect::<Vec<_>>();

let start = spans
.iter()
.min_by_key(|s| s.offset())
.map(|s| s.offset())
.unwrap_or(0);

let end = spans
.iter()
.max_by_key(|s| s.offset() + s.len())
.map(|s| s.offset() + s.len())
.unwrap_or(0);

let span = miette::SourceSpan::new(start.into(), end - start);

Self::at(span, value)
}
}

pub trait CombineSpan {
fn combine(spans: Vec<SourceSpan>) -> SourceSpan {
let start = spans
.iter()
.min_by_key(|s| s.offset())
.map(|s| s.offset())
.unwrap_or(0);

let end = spans
.iter()
.max_by_key(|s| s.offset() + s.len())
.map(|s| s.offset() + s.len())
.unwrap_or(0);

SourceSpan::new(start.into(), end - start)
}
}

impl CombineSpan for SourceSpan {}

impl<'de> Spannable<'de> for Expression<'de> {
type Value = ExpressionValue<'de>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}

impl<'de> Spannable<'de> for Statement<'de> {
type Value = StatementValue<'de>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}

impl<'de> Spannable<'de> for Type<'de> {
type Value = TypeValue<'de>;

fn at(span: miette::SourceSpan, value: Self::Value) -> Self {
Self { value, span }
}
}
5 changes: 4 additions & 1 deletion src/parser/expression/binary.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::parser::{
ast::{BinaryOperator, Expression, ExpressionValue, Spannable},
ast::{
untyped::{BinaryOperator, Expression, ExpressionValue},
Spannable,
},
lookup::BindingPower,
Parser,
};
Expand Down
5 changes: 4 additions & 1 deletion src/parser/expression/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use miette::Result;

use super::{
ast::{Expression, ExpressionValue, Spannable},
ast::{
untyped::{Expression, ExpressionValue},
Spannable,
},
Parser,
};
use crate::{lexer::TokenKind, parser::lookup::BindingPower};
Expand Down
5 changes: 4 additions & 1 deletion src/parser/expression/primitive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
lexer::{TokenKind, TokenValue},
parser::{
ast::{Expression, ExpressionValue, Primitive, Spannable},
ast::{
untyped::{Expression, ExpressionValue, Primitive},
Spannable,
},
Parser,
},
};
Expand Down
5 changes: 4 additions & 1 deletion src/parser/expression/unary.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::{
lexer::TokenKind,
parser::{
ast::{Expression, ExpressionValue, Spannable, UnaryOperator},
ast::{
untyped::{Expression, ExpressionValue, UnaryOperator},
Spannable,
},
lookup::BindingPower,
Parser,
},
Expand Down
5 changes: 4 additions & 1 deletion src/parser/lookup.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{
ast::{Expression, ExpressionValue, Primitive, Spannable, Statement, StatementValue, Type},
ast::{
untyped::{Expression, ExpressionValue, Primitive, Statement, StatementValue, Type},
Spannable,
},
expression, statement, typing, Parser,
};
use crate::lexer::{TokenKind, TokenValue};
Expand Down
12 changes: 8 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
use crate::lexer::Lexer;
use ast::{Spannable, Statement, StatementValue, Symbol};
use ast::{
untyped::{Statement, StatementValue, Symbol},
Spannable,
};
use lookup::Lookup;
use miette::Result;

pub mod ast;
pub mod expression;
pub mod lookup;
pub mod statement;
pub mod typechecker;
pub mod typing;

pub struct Parser<'de> {
Expand All @@ -15,9 +19,9 @@ pub struct Parser<'de> {
}

impl<'de> Parser<'de> {
pub fn new(input: &'de str) -> Self {
Parser {
lexer: Lexer::new(input),
pub fn new(lexer: Lexer<'de>) -> Self {
Self {
lexer,
lookup: Lookup::default(),
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/parser/statement.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use super::{
ast::{
EnumMemberDeclaration, FunctionHeader, ParameterDeclaration, Spannable, Statement,
StatementValue, StructMemberDeclaration,
untyped::{
EnumMemberDeclaration, FunctionHeader, ParameterDeclaration, Statement, StatementValue,
StructMemberDeclaration,
},
Spannable,
},
expression,
lookup::BindingPower,
Expand Down
16 changes: 16 additions & 0 deletions src/parser/typechecker/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use super::ast::{typed, untyped};
use miette::Result;

pub struct TypeChecker<'de> {
symbol: untyped::Symbol<'de>,
}

impl<'de> TypeChecker<'de> {
pub fn new(symbol: untyped::Symbol<'de>) -> Self {
Self { symbol }
}

pub fn check(&mut self) -> Result<typed::Symbol<'de>> {
Ok(self.symbol.clone())
}
}
5 changes: 4 additions & 1 deletion src/parser/typing.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use super::{
ast::{Spannable, Type, TypeValue},
ast::{
untyped::{Type, TypeValue},
Spannable,
},
lookup::BindingPower,
Parser,
};
Expand Down
Loading

0 comments on commit 3772ae9

Please sign in to comment.