Skip to content

Commit

Permalink
fix: mut_visitor no longer clones
Browse files Browse the repository at this point in the history
  • Loading branch information
mhasel committed Jan 21, 2025
1 parent 1f7ee2e commit e33a9d8
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 76 deletions.
27 changes: 25 additions & 2 deletions compiler/plc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{
fmt::{Debug, Display, Formatter},
hash::Hash,
ops::Range,
};

Expand Down Expand Up @@ -722,8 +721,14 @@ pub struct AstNode {
pub location: SourceLocation,
}

impl Default for AstNode {
fn default() -> Self {
AstFactory::create_empty_statement(SourceLocation::internal(), usize::MAX)
}
}

#[derive(Debug, Clone, PartialEq, TryInto)]
#[try_into(ref)]
#[try_into(ref, ref_mut, owned)]
pub enum AstStatement {
EmptyStatement(EmptyStatement),

Expand Down Expand Up @@ -783,6 +788,20 @@ macro_rules! try_from {
};
}

#[macro_export]
/// A `try_from` convenience wrapper for `AstNode`, passed as the `ex:expr` argument.
/// Will try to return a reference to the variants inner type, specified via the `t:ty` parameter.
/// Converts the `try_from`-`Result` into an `Option`
macro_rules! try_from_mut {
() => { None };
($ex:expr, $t:ty) => {
<&mut $t>::try_from($ex.get_stmt_mut()).ok()
};
($($ex:tt)*, $t:ty) => {
try_from_mut!($($ex)*, $t).ok()
};
}

impl Debug for AstNode {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match &self.stmt {
Expand Down Expand Up @@ -932,6 +951,10 @@ impl AstNode {
&self.stmt
}

pub fn get_stmt_mut(&mut self) -> &mut AstStatement {
&mut self.stmt
}

/// Similar to [`AstNode::get_stmt`] with the exception of peeling parenthesized expressions.
/// For example if called on `((1))` this function would return a [`AstStatement::Literal`] ignoring the
/// parenthesized expressions altogether.
Expand Down
Loading

0 comments on commit e33a9d8

Please sign in to comment.