Skip to content

Conversation

@lushoBarlett
Copy link
Collaborator

Compiles without warnings for now...

I will draft this because I need some suggestions:

  1. Should I do the "missing case" bit? I don't think its necessary.
  2. How can I test this? (where should I wire the pretty_print function?
  3. What behavior is expected of this function regarding "width"?

Closes #136.


#include "./cst.hpp"

std::string pretty_print(CST::CST&);
Copy link
Owner

@SebastianMestre SebastianMestre Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be inside namespace CST to match with the definition in pretty_print.cpp


#include "./cst.hpp"

std::string pretty_print(CST::CST&);
Copy link
Owner

@SebastianMestre SebastianMestre Feb 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you implemented pretty-printing over std::ostream, so I would expose the slightly more efficient pretty_print(Cst&, ostream&) (while also keeping the string-returning pretty_print(Cst&) for convenience)

@SebastianMestre
Copy link
Owner

SebastianMestre commented Feb 10, 2026

  1. Should I do the "missing case" bit? I don't think its necessary.

I think assert() would be fine

  1. How can I test this?

Round-tripping between pretty-printing and parsing (CST -> text -> same CST) is a good way to gain confidence in the printer, but it might be kind of arduous to write.

I'll be pragmatic and say that checking some specific input-output (cst, text) pairs is good enough. We can add regression tests if we find any bugs.

(where should I wire the pretty_print function?

I think we could use it in the playground executable. Also very low priority, so feel free to ignore/do in a separate PR.

    1. What behavior is expected of this function regarding "width"?

You mean limiting the number of columns per line of code? It's famously pretty difficult. There is a famous paper by Philip Wadler outlining a pretty decent and simple approach.

Tbh I am happy with the printer in this PR (no column limit), but if you want to add that, a separate PR would be welcome.

Comment on lines +1 to +4
#include <sstream>

#include "log/log.hpp"
#include "pretty_print.hpp"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include <sstream>
#include "log/log.hpp"
#include "pretty_print.hpp"
#include "pretty_print.hpp"
#include "log/log.hpp"
#include <sstream>

as a general rule I try to always include the .hpp corresponding to the current .cpp as the first line

Also, stdlib includes after project includes

@SebastianMestre SebastianMestre self-requested a review February 11, 2026 00:00
Copy link
Owner

@SebastianMestre SebastianMestre left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some minor style thingies but generally LGTM -- feel free to merge when you're happy with it

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.

Print AST as code

2 participants