Skip to content

Conversation

@mizchi
Copy link

@mizchi mizchi commented Jan 15, 2026

Summary

Add a new printer package that converts MoonBit AST back to source code.

Motivation

Currently, there's no public API to pretty-print AST back to source code. I'm working on an AST transformer and needed this functionality.

I believe parser and printer should be maintained together as a pair - when the AST structure changes, both need to be updated accordingly. Having the printer in this repository ensures they stay in sync.

If this PR is not suitable for this repository, I will maintain it as a separate package.

Features

  • Complete coverage of top-level declarations (functions, structs, enums, traits, impl blocks, type aliases, etc.)
  • Expression printing with proper operator precedence
  • Pattern and type printing
  • Tab-based indentation for nested structures
  • Round-trip tests verifying parse → print → parse → print consistency

Test Plan

  • All 23 printer tests pass
  • Round-trip tests ensure printed output can be re-parsed correctly

🤖 Generated with Claude Code

mizchi and others added 3 commits January 16, 2026 00:15
Add a new printer package that converts MoonBit AST back to source code.

Features:
- Support all top-level declarations (functions, structs, enums, traits, impl, type aliases, etc.)
- Support all expression types with proper operator precedence
- Support patterns, types, and constants
- Tab-based indentation for nested structures
- Round-trip test suite to verify parse → print → parse consistency

Files:
- printer/printer.mbt: Top-level declarations and main entry points
- printer/expr.mbt: Expression printing with indentation support
- printer/pattern.mbt: Pattern printing
- printer/type.mbt: Type printing
- printer/constant.mbt: Constant literal printing
- printer/printer_test.mbt: Unit tests and round-trip tests

Co-Authored-By: Claude Opus 4.5 <[email protected]>
Add print_code(parsed) that takes parse_string result directly
and returns None if there are parse errors.

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@Yoorkin
Copy link
Contributor

Yoorkin commented Jan 16, 2026

Thank you for your interest! There are some source printers in the community, such as illusory0x0/printer. There is also a pretty-printer, moonbitlang/formatter (though it is unfinished). You can consider contributing to illusory0x0/printer or maintaining it as a separate package.

@Yoorkin Yoorkin closed this Jan 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants