Skip to content

Commit

Permalink
refactor(ast_codegen): abstract passes and generators behind Runner
Browse files Browse the repository at this point in the history
… trait.
  • Loading branch information
rzvxa committed Jul 30, 2024
1 parent 9aa6953 commit 425f59c
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 154 deletions.
6 changes: 5 additions & 1 deletion tasks/ast_codegen/src/generators/ast_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ use crate::{
CodegenCtx, Generator, GeneratorOutput, TypeRef,
};

pub struct AstBuilderGenerator;
use super::define_generator;

define_generator! {
pub struct AstBuilderGenerator;
}

impl Generator for AstBuilderGenerator {
fn name(&self) -> &'static str {
Expand Down
6 changes: 4 additions & 2 deletions tasks/ast_codegen/src/generators/ast_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ use crate::{
GeneratorOutput, TypeRef,
};

use super::generated_header;
use super::{define_generator, generated_header};

pub struct AstKindGenerator;
define_generator! {
pub struct AstKindGenerator;
}

pub const BLACK_LIST: [&str; 61] = [
"Expression",
Expand Down
6 changes: 4 additions & 2 deletions tasks/ast_codegen/src/generators/impl_get_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ use crate::{
CodegenCtx, Generator, GeneratorOutput,
};

use super::generated_header;
use super::{define_generator, generated_header};

pub struct ImplGetSpanGenerator;
define_generator! {
pub struct ImplGetSpanGenerator;
}

impl Generator for ImplGetSpanGenerator {
fn name(&self) -> &'static str {
Expand Down
24 changes: 24 additions & 0 deletions tasks/ast_codegen/src/generators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,27 @@ pub use ast_builder::AstBuilderGenerator;
pub use ast_kind::AstKindGenerator;
pub use impl_get_span::ImplGetSpanGenerator;
pub use visit::{VisitGenerator, VisitMutGenerator};

use crate::{CodegenCtx, GeneratorOutput};

pub trait Generator {
fn name(&self) -> &'static str;
fn generate(&mut self, ctx: &CodegenCtx) -> GeneratorOutput;
}

macro_rules! define_generator {
($vis:vis struct $ident:ident $($lifetime:lifetime)? $($rest:tt)*) => {
$vis struct $ident $($lifetime)? $($rest)*
impl $($lifetime)? $crate::Runner for $ident $($lifetime)? {
fn name(&self) -> &'static str {
$crate::Generator::name(self)
}

fn run(&mut self, ctx: &$crate::CodegenCtx) -> $crate::Result<$crate::GeneratorOutput> {
Ok(self.generate(ctx))
}
}
};
}

pub(crate) use define_generator;
11 changes: 8 additions & 3 deletions tasks/ast_codegen/src/generators/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ use crate::{
CodegenCtx, Generator, GeneratorOutput, TypeRef,
};

use super::generated_header;
use super::{define_generator, generated_header};

pub struct VisitGenerator;
pub struct VisitMutGenerator;
define_generator! {
pub struct VisitGenerator;
}

define_generator! {
pub struct VisitMutGenerator;
}

impl Generator for VisitGenerator {
fn name(&self) -> &'static str {
Expand Down
110 changes: 0 additions & 110 deletions tasks/ast_codegen/src/linker.rs

This file was deleted.

Loading

0 comments on commit 425f59c

Please sign in to comment.