Skip to content
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

[DNM] Support box-drawing-based blocks #2928

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

beccadax
Copy link
Contributor

@beccadax beccadax commented Dec 27, 2024

There’s a social media post circulating where #define is used to write blocks using box-drawing characters in C code. Swift macros aren’t suitable for that kind of thing, but I wanted to see how difficult it would be to implement, just for funsies.

This is—and I cannot believe I am about to write this sentence—perhaps not the way we would actually want to implement box-drawing-based blocks, as it works by treating most of the characters as trivia and only assigning a semantic meaning to a few corner characters that happen to fall into the same positions as braces. That means it doesn’t actually enforce the validity of the box shapes.

Nor is this—and I cannot believe I am about to write this sentence, either—a production-quality implementation of box-drawing-based blocks. There’s a lot I haven’t tested, the diagnostics are probably a mess, there’s no formatter support (which would actually be a super cool addition), and there are a number of places where I suspect I made changes that would cost us some speed.

However, it’s certainly a fun little hack:

    assertParse(
      #"""
      ╔═════════════ func fizzBuzz() ═════════════╗
      ║ ╔═══════════ for i in 0..<100 ══════════╗ ║
      ║ ║ ╔═════ if i.isMultiple(of: 15) ═════╗ ║ ║
      ║ ║ ║        print("fizzbuzz ")         ║ ║ ║
      ║ ║ ╠═══ else if i.isMultiple(of: 3) ═══╣ ║ ║
      ║ ║ ║          print("fizz ")           ║ ║ ║
      ║ ║ ╠═══ else if i.isMultiple(of: 5) ═══╣ ║ ║
      ║ ║ ║          print("buzz ")           ║ ║ ║
      ║ ║ ╠══════════════ else ═══════════════╣ ║ ║
      ║ ║ ║          print("\(i) ")           ║ ║ ║
      ║ ║ ╚═══════════════════════════════════╝ ║ ║
      ║ ╚═══════════════════════════════════════╝ ║
      ╚═══════════════════════════════════════════╝
      """#
    )

There’s a social media post circulating where `#define` is used to write blocks using box-drawing characters in C code. Swift macros aren’t suitable for that kind of thing, but I wanted to see how difficult it would be to implement, just for funsies.

This is—and I cannot believe I am about to write this sentence—perhaps not the way we would actually want to implement box-drawing-based blocks, as it works by treating most of the characters as trivia and only assigning a semantic meaning to a few corners. That means it doesn’t actually enforce the validity of the box shapes.

Nor is this—and I cannot believe I am about to write *this* sentence, either—a production-quality implementation of box-drawing-based blocks. There’s a lot I haven’t tested, the diagnostics are probably a mess, and there are a number of places where I suspect I made changes that would cost us some speed.

However, it’s certainly a fun little hack:

```
    assertParse(
      #"""
      ╔═════════════ func fizzBuzz() ═════════════╗
      ║ ╔═══════════ for i in 0..<100 ══════════╗ ║
      ║ ║ ╔═════ if i.isMultiple(of: 15) ═════╗ ║ ║
      ║ ║ ║        print("fizzbuzz ")         ║ ║ ║
      ║ ║ ╠═══ else if i.isMultiple(of: 3) ═══╣ ║ ║
      ║ ║ ║          print("fizz ")           ║ ║ ║
      ║ ║ ╠═══ else if i.isMultiple(of: 5) ═══╣ ║ ║
      ║ ║ ║          print("buzz ")           ║ ║ ║
      ║ ║ ╠══════════════ else ═══════════════╣ ║ ║
      ║ ║ ║          print("\(i) ")           ║ ║ ║
      ║ ║ ╚═══════════════════════════════════╝ ║ ║
      ║ ╚═══════════════════════════════════════╝ ║
      ╚═══════════════════════════════════════════╝
      """#
    )
```
@hamishknight
Copy link
Contributor

SE-NNNN: Allow control flow using arrows 😄

@vanvoorden
Copy link

This involves, among other things, introducing new SyntaxNodeWithBody parameters and methods.
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.

3 participants