-
Notifications
You must be signed in to change notification settings - Fork 20
fix(ast): correct tree indentation, render use statements, print source-syntax operators #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Boreas618
wants to merge
2
commits into
main
Choose a base branch
from
fix/ast-tree-printer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| //! Tree pretty-printer for the AST. | ||
| //! | ||
| //! This module defines the [`DisplayAsTree`] trait and provides implementations | ||
| //! for every AST node type. When a node is printed with this trait it | ||
| //! produces an indented, Unicode-box-drawing tree that mirrors the logical | ||
| //! structure of the AST, making it easy to read program structure at a glance. | ||
| //! for every AST node type. A node printed with this trait produces an | ||
| //! indented, Unicode-box-drawing tree that mirrors the logical structure of | ||
| //! the AST. | ||
| //! | ||
| //! The indentation state is passed down through the `indent_levels` slice. | ||
| //! Each element records whether the corresponding ancestor was the *last* | ||
|
|
@@ -50,25 +50,21 @@ fn tree_indent(indent_levels: &[bool], is_last: bool) -> String { | |
| let mut s = String::new(); | ||
| for &last in indent_levels.iter() { | ||
| if last { | ||
| // Ancestor was the last child — no vertical connector needed. | ||
| s.push_str(" "); | ||
| } else { | ||
| // More siblings exist at this ancestor level — draw vertical bar. | ||
| s.push_str("│ "); | ||
| } | ||
| } | ||
| if is_last { | ||
| // This node is the last child — use a corner connector. | ||
| s.push_str("└─"); | ||
| } else { | ||
| // More siblings follow — use a tee connector. | ||
| s.push_str("├─"); | ||
| } | ||
| s | ||
| } | ||
|
|
||
| /// Formats the root `Program` node, listing every top-level element as a | ||
| /// child in the tree. | ||
| /// Formats the root `Program` node, listing every `use` statement followed | ||
| /// by every top-level element as children in the tree. | ||
| impl DisplayAsTree for Program { | ||
| fn fmt_tree( | ||
| &self, | ||
|
|
@@ -77,17 +73,38 @@ impl DisplayAsTree for Program { | |
| is_last: bool, | ||
| ) -> Result<(), Error> { | ||
| writeln!(f, "{}Program", tree_indent(indent_levels, is_last))?; | ||
| // Build the indentation context for children. | ||
| let mut new_indent = indent_levels.to_vec(); | ||
| new_indent.push(!is_last); | ||
| let last_index = self.elements.len().saturating_sub(1); | ||
| new_indent.push(is_last); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why !is_last to is_last? |
||
| // `last_index` indexes into the concatenation of `use_stmts` and | ||
| // `elements`, so only the final child overall is marked `is_last`. | ||
| let last_index = (self.use_stmts.len() + self.elements.len()).saturating_sub(1); | ||
| for (i, use_stmt) in self.use_stmts.iter().enumerate() { | ||
| use_stmt.fmt_tree(f, &new_indent, i == last_index)?; | ||
| } | ||
| for (i, elem) in self.elements.iter().enumerate() { | ||
| elem.fmt_tree(f, &new_indent, i == last_index)?; | ||
| elem.fmt_tree(f, &new_indent, self.use_stmts.len() + i == last_index)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Prints a leaf `UseStmt <module_path>` node. | ||
| impl DisplayAsTree for UseStmt { | ||
| fn fmt_tree( | ||
| &self, | ||
| f: &mut Formatter<'_>, | ||
| indent_levels: &[bool], | ||
| is_last: bool, | ||
| ) -> Result<(), Error> { | ||
| writeln!( | ||
| f, | ||
| "{}UseStmt {}", | ||
| tree_indent(indent_levels, is_last), | ||
| self.module_name | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| /// Delegates formatting to the concrete element variant. | ||
| impl DisplayAsTree for ProgramElement { | ||
| fn fmt_tree( | ||
|
|
@@ -144,7 +161,6 @@ impl DisplayAsTree for VarDecl { | |
| indent_levels: &[bool], | ||
| is_last: bool, | ||
| ) -> Result<(), Error> { | ||
| // Render the type specifier, falling back to "unknown" if absent. | ||
| let type_str = self | ||
| .type_specifier | ||
| .as_ref() | ||
|
|
@@ -205,9 +221,8 @@ impl DisplayAsTree for FnDecl { | |
| self.identifier | ||
| )?; | ||
| if let Some(params) = &self.param_decl { | ||
| // Extend the indentation context for the parameter subtree. | ||
| let mut new_indent = indent_levels.to_vec(); | ||
| new_indent.push(!is_last); | ||
| new_indent.push(is_last); | ||
| writeln!(f, "{}Params:", tree_indent(&new_indent, false))?; | ||
| params.decls.fmt_tree(f, &new_indent, true)?; | ||
| } | ||
|
|
@@ -242,7 +257,7 @@ impl DisplayAsTree for FnDef { | |
| self.fn_decl.identifier | ||
| )?; | ||
| let mut new_indent = indent_levels.to_vec(); | ||
| new_indent.push(!is_last); | ||
| new_indent.push(is_last); | ||
| self.stmts.fmt_tree(f, &new_indent, true) | ||
| } | ||
| } | ||
|
|
@@ -261,11 +276,9 @@ impl DisplayAsTree for VarDef { | |
| VarDefInner::Scalar(s) => writeln!(f, "{}{} = {}", prefix, self.identifier, s.val), | ||
| VarDefInner::Array(a) => match &a.initializer { | ||
| ArrayInitializer::ExplicitList(vals) => { | ||
| // Print the debug representation of all explicit values. | ||
| writeln!(f, "{}{} = {:?}", prefix, self.identifier, vals) | ||
| } | ||
| ArrayInitializer::Fill { val, count } => { | ||
| // Print the fill syntax: `name = [val; count]`. | ||
| writeln!(f, "{}{} = [{}; {}]", prefix, self.identifier, val, count) | ||
| } | ||
| }, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments should be clean-slate. don't need to specially mention "The LLVM IR mnemonics...", applicable also to another change.