Skip to content

Add brainfuck interpreter#750

Merged
vinc merged 15 commits intotrunkfrom
feature/brainfuck
Jun 29, 2025
Merged

Add brainfuck interpreter#750
vinc merged 15 commits intotrunkfrom
feature/brainfuck

Conversation

@vinc
Copy link
Owner

@vinc vinc commented Jun 22, 2025

I'm working on MOROS Shell 2.0 and I'll need a parser to handle the new language features. I don't like how hard it is with the nom crate to display good error messages with context for MOROS Lisp (which complicate a lot writing big programs inside MOROS), so I've been looking into the chumsky crate instead. That's the only issue I have with nom because it's a really good library otherwise.

When I learned how to use nom 4 years ago I added a small calc tool with a simple grammar (#263), so I'm now using the Brainfuck interpreter from the examples of the chumsky crate with the goal to extend it a bit and see if I like the crate.

I'm not sur if I'll merge this because I'm more interested in writing my own Brainfuck interpreter in MOROS Lisp, but I'm very interested in having a Brainfuck interpreter! Maybe I'll have both to compare the performance, like I did with the undocumented pi command (that I just remembered now).

@vinc
Copy link
Owner Author

vinc commented Jun 22, 2025

Hello world example:

~
> read hello.bf
++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.++
+.------.--------.>>+.>++.

~
> brainfuck hello.bf
Hello World!

Default error message:

~
> brainfuck error.bf
Error: found 'X' expected '<', '>', '+', '-', ',', '.', '[', or end of input

@vinc
Copy link
Owner Author

vinc commented Jun 22, 2025

Improved error message:

~
> brainfuck error.bf
Error: Unexpected token at error.bf:1:6

+++++X+++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++>
     ^ found 'X' expected '<', '>', '+', '-', ',', '.', '[', or end of input

@vinc
Copy link
Owner Author

vinc commented Jun 22, 2025

The hello world example has been taken from Wikipedia and the 400 bytes quine from Daniel B Cristofani.

TODO: Add comment support for example attribution

@vinc
Copy link
Owner Author

vinc commented Jun 23, 2025

~
> read hello.bf
+++++
               +++[>++
     ++[>+
+>+
++>+
++>
+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.++
+.---X--.--------.>>+.>++.

~
> brainfuck hello.bf
Error: Unexpected token at hello.bf:8:6

+.---X--.--------.>>+.>++.
     ^ unexpected token

@vinc vinc mentioned this pull request Jun 23, 2025
@vinc
Copy link
Owner Author

vinc commented Jun 23, 2025

Screenshot:

brainfuck

@vinc vinc added the enhancement New feature or request label Jun 27, 2025
@vinc vinc marked this pull request as ready for review June 29, 2025 12:34
@vinc vinc merged commit d8f233b into trunk Jun 29, 2025
1 check passed
@vinc vinc deleted the feature/brainfuck branch June 29, 2025 12:34
@viktorpopp
Copy link

Why is the X an unexpected token? Shouldn't all text just be ignored?

@vinc
Copy link
Owner Author

vinc commented Jun 29, 2025

Why is the X an unexpected token? Shouldn't all text just be ignored?

All tokens that are not instructions are now ignored but that was not the case with the example parser I started with from the crate's repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants