Skip to content

Commit

Permalink
rename ast_node2 to ast_node
Browse files Browse the repository at this point in the history
  • Loading branch information
andogq committed Aug 25, 2024
1 parent 28847a8 commit 45388bc
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 106 deletions.
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/assign.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::ast_node2;
use crate::ast_node;

ast_node2! {
ast_node! {
Assign<M> {
binding: M::IdentIdentifier,
value: Box<Expression<M>>,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/block.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::ast_node2;
use crate::ast_node;

ast_node2! {
ast_node! {
Block<M> {
statements: Vec<Statement<M>>,
span,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ast_node2;
use crate::ast_node;

ast_node2! {
ast_node! {
Boolean<M> {
value: bool,
span,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/call.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ast_node2;
use crate::ast_node;

use super::*;

ast_node2! {
ast_node! {
Call<M> {
name: M::FnIdentifier,
args: Vec<Expression<M>>,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/cast.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{ast_node2, repr::ty::Ty};
use crate::{ast_node, repr::ty::Ty};

use super::Expression;

ast_node2! {
ast_node! {
Cast<M> {
value: Box<Expression<M>>,
target_ty: Ty,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/ident.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::hash::Hash;

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

ast_node2! {
ast_node! {
Ident<M> {
binding: M::IdentIdentifier,
span,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/if_else.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ast_node2;
use crate::ast_node;

use super::*;

ast_node2! {
ast_node! {
If<M> {
condition: Box<Expression<M>>,
success: Block<M>,
Expand Down
4 changes: 2 additions & 2 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_node2, repr::token::Token};
use crate::{ast_node, repr::token::Token};

use super::Expression;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl TryFrom<Token> for InfixOperation {
}
}

ast_node2! {
ast_node! {
Infix<M> {
left: Box<Expression<M>>,
operation: InfixOperation,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/integer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ast_node2;
use crate::ast_node;

ast_node2! {
ast_node! {
Integer<M> {
value: i64,
span,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/loop_block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ast_node2;
use crate::ast_node;

use super::*;

ast_node2! {
ast_node! {
Loop<M> {
body: Block<M>,
span,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ pub use infix::*;
pub use integer::*;
pub use loop_block::*;

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

use super::{AstMetadata, Statement};

ast_node2! {
ast_node! {
Expression<M>(
Infix,
Integer,
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/function.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{ast_node2, repr::ty::Ty};
use crate::{ast_node, repr::ty::Ty};

use super::*;

ast_node2! {
ast_node! {
Function<M> {
name: M::FnIdentifier,
parameters: Vec<(M::IdentIdentifier, Ty)>,
Expand Down
78 changes: 5 additions & 73 deletions src/repr/ast/base/macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_export]
macro_rules! ast_node2 {
macro_rules! ast_node {
($name:ident<$metadata:ident> { $($tokens:tt)* }) => {
ast_node2! { @ $name<$metadata> { $($tokens)* } -> () }
ast_node! { @ $name<$metadata> { $($tokens)* } -> () }
};

($name:ident<$metadata:ident>( $($variant:ident,)* )) => {
Expand Down Expand Up @@ -39,7 +39,7 @@ macro_rules! ast_node2 {
};

(@ $name:ident<$metadata:ident> { span, $($tokens:tt)* } -> ( $($result:tt)*) ) => {
ast_node2! {
ast_node! {
@ $name<$metadata> { $($tokens)* } -> (
$($result)*
span: $metadata::Span,
Expand All @@ -48,7 +48,7 @@ macro_rules! ast_node2 {
};

(@ $name:ident<$metadata:ident> { ty_info, $($tokens:tt)* } -> ( $($result:tt)*) ) => {
ast_node2! {
ast_node! {
@ $name<$metadata> { $($tokens)* } -> (
$($result)*
ty_info: $metadata::TyInfo,
Expand All @@ -57,7 +57,7 @@ macro_rules! ast_node2 {
};

(@ $name:ident<$metadata:ident> { $field:ident: $ty:ty, $($tokens:tt)* } -> ( $($result:tt)*) ) => {
ast_node2! {
ast_node! {
@ $name<$metadata> { $($tokens)* } -> (
$($result)*
$field: $ty,
Expand All @@ -66,74 +66,6 @@ macro_rules! ast_node2 {
};
}

#[macro_export]
macro_rules! ast_node {
// Common components for all variants of an AST node
(common struct $struct_name:ident$(<$($generic:ident),*>)? { $($name:ident: $ty:ty,)* }) => {
#[derive(Debug, Clone)]
pub struct $struct_name$(<$($generic),*>)? {
pub span: $crate::util::span::Span,
$(pub $name: $ty,)*
}
};

// AST node that is typed
(typed struct $struct_name:ident<$ty_info:ident $(, $($generic:ident),*)?> { $($name:ident: $ty:ty,)* }) => {
ast_node!(common struct $struct_name<$ty_info $(, $($generic),*)?> {
$($name: $ty,)*
ty_info: $ty_info,
});

impl<$ty_info: Default $(, $($generic),*)?> $struct_name<$ty_info $(, $($generic),*)?> {
pub fn new($($name: $ty,)* span: $crate::util::span::Span) -> Self {
Self {
span,
ty_info: Default::default(),
$($name,)*
}
}
}
};

// AST node that contains no type information
(struct $struct_name:ident$(<$($generic:ident),*>)? { $($name:ident: $ty:ty,)* }) => {
ast_node!(common struct $struct_name$(<$($generic),*>)? {
$($name: $ty,)*
});

impl$(<$($generic),*>)? $struct_name$(<$($generic),*>)? {
pub fn new($($name: $ty,)* span: $crate::util::span::Span) -> Self {
Self {
span,
$($name,)*
}
}
}
};

// AST node that consists of other AST nodes
(enum $enum_name:ident<$ty_info:ident $(, $($generic:ident),*)?> { $($name:ident($ty:ty),)* }) => {
#[derive(Debug, Clone)]
pub enum $enum_name<$ty_info $(, $($generic),*)?> {
$($name($ty),)*
}

impl<$ty_info $(, $($generic),*)?> $enum_name<$ty_info $(, $($generic),*)?> {
pub fn get_ty_info(&self) -> &$ty_info {
match self {
$(Self::$name(value) => &value.ty_info),*
}
}

pub fn span(&self) -> &$crate::util::span::Span {
match self {
$(Self::$name(value) => &value.span),*
}
}
}
};
}

#[macro_export]
macro_rules! generate_ast {
($metadata:ty) => {
Expand Down
4 changes: 2 additions & 2 deletions src/repr/ast/base/program.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ast_node2;
use crate::ast_node;

use super::*;

ast_node2! {
ast_node! {
Program<M> {
functions: Vec<Function<M>>,
main: Function<M>,
Expand Down
14 changes: 7 additions & 7 deletions src/repr/ast/base/statement.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::ast_node2;
use crate::ast_node;

use super::*;

ast_node2! {
ast_node! {
Statement<M>(
Return,
Let,
Expand Down Expand Up @@ -31,15 +31,15 @@ impl<M: AstMetadata<TyInfo: Default>> Statement<M> {
}
}

ast_node2! {
ast_node! {
Return<M> {
value: Expression<M>,
span,
ty_info,
}
}

ast_node2! {
ast_node! {
Let<M> {
binding: M::IdentIdentifier,
value: Expression<M>,
Expand All @@ -48,7 +48,7 @@ ast_node2! {
}
}

ast_node2! {
ast_node! {
ExpressionStatement<M> {
expression: Expression<M>,
implicit_return: bool,
Expand All @@ -57,14 +57,14 @@ ast_node2! {
}
}

ast_node2! {
ast_node! {
Break<M> {
span,
ty_info,
}
}

ast_node2! {
ast_node! {
Continue<TyInfo> {
span,
ty_info,
Expand Down

0 comments on commit 45388bc

Please sign in to comment.