Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Brainfuck compiler in scala

Want to make some Domain Specific Language(DSL) for practice, first idea it is to write interpreter or compiler of Brainfuck. First idea was to override functions such as they will behave as Brainfuck commands: ">", "<", "+", "-", ".", ",", "[", "]". Unfortunately you cannot decalare function as ".".

Is there better solution to write it in Scala?

like image 758
Rinat Tainov Avatar asked Jan 30 '12 13:01

Rinat Tainov


3 Answers

You don't say this specifically in your question, but it seems that when you say DSL, you mean Internal DSL?

Internal DSLs are great, but fundamentally you're always limited by the syntax of the language you're trying to use. Scala is a particularly good language for writing an internal DSL, because it has a simple and flexible syntax. But it's not infinitely flexible.

Other avenues you might want to explore might be:

  1. Choose a different symbol instead of ".". Scala can support Unicode identifiers, so if you fancy going down that road, perhaps you could use "∙"?
  2. Create an External DSL instead?
like image 68
Paul Butcher Avatar answered Oct 20 '22 08:10

Paul Butcher


I suppose that you are aware of this example.

Also this example suggested by Mikaël Mayer in comments.

like image 24
Kiril Kirilov Avatar answered Oct 20 '22 07:10

Kiril Kirilov


I wrote a BrainFuck interpreter that makes use of Scala parser combinators. the source code is here if it may help.

like image 25
Antoine Comte Avatar answered Oct 20 '22 08:10

Antoine Comte