Skip to content

Commit

Permalink
create AstMetadata trait to reduce generics
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 22, 2024
1 parent ec65544 commit e3d53b0
Show file tree
Hide file tree
Showing 32 changed files with 369 additions and 244 deletions.
12 changes: 7 additions & 5 deletions src/repr/ast/base/expression/assign.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use super::*;
use crate::ast_node;
use crate::ast_node2;

ast_node! {
typed struct Assign<TyInfo, FnIdentifier, IdentIdentifier> {
binding: IdentIdentifier,
value: Box<Expression<TyInfo, FnIdentifier, IdentIdentifier>>,
ast_node2! {
Assign<M> {
binding: M::IdentIdentifier,
value: Box<Expression<M>>,
span,
ty_info,
}
}
10 changes: 6 additions & 4 deletions src/repr/ast/base/expression/block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::*;
use crate::ast_node;
use crate::ast_node2;

ast_node! {
typed struct Block<TyInfo, FnIdentifier, IdentIdentifier> {
statements: Vec<Statement<TyInfo, FnIdentifier, IdentIdentifier>>,
ast_node2! {
Block<M> {
statements: Vec<Statement<M>>,
span,
ty_info,
}
}
8 changes: 5 additions & 3 deletions src/repr/ast/base/expression/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::ast_node;
use crate::ast_node2;

ast_node! {
typed struct Boolean<TyInfo> {
ast_node2! {
Boolean<M> {
value: bool,
span,
ty_info,
}
}
12 changes: 7 additions & 5 deletions src/repr/ast/base/expression/call.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::ast_node;
use crate::ast_node2;

use super::*;

ast_node! {
typed struct Call<TyInfo, FnIdentifier, IdentIdentifier> {
name: FnIdentifier,
args: Vec<Expression<TyInfo, FnIdentifier, IdentIdentifier>>,
ast_node2! {
Call<M> {
name: M::FnIdentifier,
args: Vec<Expression<M>>,
span,
ty_info,
}
}
19 changes: 12 additions & 7 deletions src/repr/ast/base/expression/ident.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use std::hash::Hash;

use crate::ast_node;
use crate::{ast_node2, repr::ast::base::AstMetadata};

ast_node! {
typed struct Ident<TyInfo, IdentIdentifier> {
binding: IdentIdentifier,
ast_node2! {
Ident<M> {
binding: M::IdentIdentifier,
span,
ty_info,
}
}

impl<TyInfo, IdentIdentifier: Hash> Hash for Ident<TyInfo, IdentIdentifier> {
impl<M: AstMetadata<IdentIdentifier: Hash>> Hash for Ident<M> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.binding.hash(state);
}
}

impl<TyInfo, IdentIdentifier: PartialEq> PartialEq for Ident<TyInfo, IdentIdentifier> {
impl<M: AstMetadata<IdentIdentifier: PartialEq>> PartialEq for Ident<M>
where
M::IdentIdentifier: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.binding == other.binding
}
}

impl<TyInfo, IdentIdentifier: Eq> Eq for Ident<TyInfo, IdentIdentifier> {}
impl<M: AstMetadata> Eq for Ident<M> where M::IdentIdentifier: Eq {}
14 changes: 8 additions & 6 deletions src/repr/ast/base/expression/if_else.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::ast_node;
use crate::ast_node2;

use super::*;

ast_node! {
typed struct If<TyInfo, FnIdentifier, IdentIdentifier> {
condition: Box<Expression<TyInfo, FnIdentifier, IdentIdentifier>>,
success: Block<TyInfo, FnIdentifier, IdentIdentifier>,
otherwise: Option<Block<TyInfo, FnIdentifier, IdentIdentifier>>,
ast_node2! {
If<M> {
condition: Box<Expression<M>>,
success: Block<M>,
otherwise: Option<Block<M>>,
span,
ty_info,
}
}
12 changes: 7 additions & 5 deletions src/repr/ast/base/expression/infix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ast_node, repr::token::Token};
use crate::{ast_node2, repr::token::Token};

use super::Expression;

Expand Down Expand Up @@ -38,10 +38,12 @@ impl TryFrom<Token> for InfixOperation {
}
}

ast_node! {
typed struct Infix<TyInfo, FnIdentifier, IdentIdentifier> {
left: Box<Expression<TyInfo, FnIdentifier, IdentIdentifier>>,
ast_node2! {
Infix<M> {
left: Box<Expression<M>>,
operation: InfixOperation,
right: Box<Expression<TyInfo, FnIdentifier, IdentIdentifier>>,
right: Box<Expression<M>>,
span,
ty_info,
}
}
8 changes: 5 additions & 3 deletions src/repr/ast/base/expression/integer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::ast_node;
use crate::ast_node2;

ast_node! {
typed struct Integer<TyInfo> {
ast_node2! {
Integer<M> {
value: i64,
span,
ty_info,
}
}
10 changes: 6 additions & 4 deletions src/repr/ast/base/expression/loop_block.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::ast_node;
use crate::ast_node2;

use super::*;

ast_node! {
typed struct Loop<TyInfo, FnIdentifier, IdentIdentifier> {
body: Block<TyInfo, FnIdentifier, IdentIdentifier>,
ast_node2! {
Loop<M> {
body: Block<M>,
span,
ty_info,
}
}
77 changes: 32 additions & 45 deletions src/repr/ast/base/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,68 @@ pub use infix::*;
pub use integer::*;
pub use loop_block::*;

use crate::{ast_node, util::span::Span};
use crate::{ast_node2, util::span::Span};

use super::Statement;
use super::{AstMetadata, Statement};

ast_node!(
enum Expression<TyInfo, FnIdentifier, IdentIdentifier> {
Infix(Infix<TyInfo, FnIdentifier, IdentIdentifier>),
Integer(Integer<TyInfo>),
Boolean(Boolean<TyInfo>),
Ident(Ident<TyInfo, IdentIdentifier>),
Block(Block<TyInfo, FnIdentifier, IdentIdentifier>),
If(If<TyInfo, FnIdentifier, IdentIdentifier>),
Call(Call<TyInfo, FnIdentifier, IdentIdentifier>),
Loop(Loop<TyInfo, FnIdentifier, IdentIdentifier>),
Assign(Assign<TyInfo, FnIdentifier, IdentIdentifier>),
}
);
ast_node2! {
Expression<M>(
Infix,
Integer,
Boolean,
Ident,
Block,
If,
Call,
Loop,
Assign,
)
}

impl<TyInfo: Default, FnIdentifier, IdentIdentifier>
Expression<TyInfo, FnIdentifier, IdentIdentifier>
{
pub fn infix(
left: Expression<TyInfo, FnIdentifier, IdentIdentifier>,
operation: InfixOperation,
right: Expression<TyInfo, FnIdentifier, IdentIdentifier>,
) -> Self {
impl<M: AstMetadata<Span = Span, TyInfo: Default>> Expression<M> {
pub fn infix(left: Expression<M>, operation: InfixOperation, right: Expression<M>) -> Self {
let span = left.span().start..right.span().end;
Self::Infix(Infix::<TyInfo, FnIdentifier, IdentIdentifier>::new(
Self::Infix(Infix::<M>::new(
Box::new(left),
operation,
Box::new(right),
span,
M::TyInfo::default(),
))
}

pub fn integer(value: i64, span: Span) -> Self {
Self::Integer(Integer::<TyInfo>::new(value, span))
Self::Integer(Integer::new(value, span, M::TyInfo::default()))
}

pub fn boolean(value: bool, span: Span) -> Self {
Self::Boolean(Boolean::<TyInfo>::new(value, span))
Self::Boolean(Boolean::new(value, span, M::TyInfo::default()))
}

pub fn ident(name: IdentIdentifier, span: Span) -> Self {
Self::Ident(Ident::<TyInfo, IdentIdentifier>::new(name, span))
pub fn ident(name: M::IdentIdentifier, span: Span) -> Self {
Self::Ident(Ident::new(name, span, M::TyInfo::default()))
}

pub fn block(
statements: Vec<Statement<TyInfo, FnIdentifier, IdentIdentifier>>,
span: Span,
) -> Self {
Self::Block(Block::<TyInfo, FnIdentifier, IdentIdentifier>::new(
statements, span,
))
pub fn block(statements: Vec<Statement<M>>, span: Span) -> Self {
Self::Block(Block::new(statements, span, M::TyInfo::default()))
}

pub fn _if(
condition: Expression<TyInfo, FnIdentifier, IdentIdentifier>,
success: Block<TyInfo, FnIdentifier, IdentIdentifier>,
otherwise: Option<Block<TyInfo, FnIdentifier, IdentIdentifier>>,
condition: Expression<M>,
success: Block<M>,
otherwise: Option<Block<M>>,
span: Span,
) -> Self {
Self::If(If::<TyInfo, FnIdentifier, IdentIdentifier>::new(
Self::If(If::new(
Box::new(condition),
success,
otherwise,
span,
M::TyInfo::default(),
))
}

pub fn call(
identifier: FnIdentifier,
args: Vec<Expression<TyInfo, FnIdentifier, IdentIdentifier>>,
span: Span,
) -> Self {
Self::Call(Call::new(identifier, args, span))
pub fn call(identifier: M::FnIdentifier, args: Vec<Expression<M>>, span: Span) -> Self {
Self::Call(Call::new(identifier, args, span, M::TyInfo::default()))
}
}
13 changes: 7 additions & 6 deletions src/repr/ast/base/function.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{ast_node, repr::ty::Ty};
use crate::{ast_node2, repr::ty::Ty};

use super::*;

ast_node! {
struct Function<TyInfo, FnIdentifier, IdentIdentifier> {
name: FnIdentifier,
parameters: Vec<(IdentIdentifier, Ty)>,
ast_node2! {
Function<M> {
name: M::FnIdentifier,
parameters: Vec<(M::IdentIdentifier, Ty)>,
return_ty: Ty,
body: Block<TyInfo, FnIdentifier, IdentIdentifier>,
body: Block<M>,
span,
}
}
Loading

0 comments on commit e3d53b0

Please sign in to comment.