Programming Challenge: Build an Interpreter
Hello everyone! It has been a while since last programming challenge, it's time for another one!
This week's goal would be to build your own interpreter.
Interpreter is program that receives input and executes it. For example Python is interpreted language, meaning you are actually writing instructions for the interpreter, which does the magic.
Probably the easiest interpereter to write is Brainfuck interpreter. If someone here doesn't know, Brainfuck is programming language, which contains following instructions: ,.<>[]-+
. Other characters are ignored. It has memory in form of array of integers. At the start, pointer that points to one specific memory cell points to cell 0. We can use <
to move pointer to left (decrement) and >
to move pointer to right (increment). .
can be used to print value of cell the pointer is currently pointing to (ascii). ,
can be used to read one character from stdin and write it to memory. [
is beggining of loop and ]
is end of loop. Loops can be nested. Loop is terminated when we reach ]
character and current value in memory is equal to 0. -
can be used to decrement value in memory by 1 and +
can be used to increment value in memory by 1. Here's Hello World:
++++++++++[>+++++++>++++++++++>+++>+<<<<
-]>++.>+.+++++++..+++.>++.<<++++++++++++
+++.>.+++.------.--------.>+.>.
People with nothing to do today can attemp to make an interpreter for the Taxi programming language.
You can even make your own language! There are no limits for this challenge.
Okay, this is a not-quite Brainfuck language.
It's unoptimised, because I span it up in ~15mins. To prove you can.
It takes input from the first commandline argument.
I'm pretty sure it's portable C, but probably ISO, not ANSI.
Plenty of style problems, like using int over size_t.
I also expect it has a couple bugs.
Every character is either a command, or ignored.
+
Increments the current value of the stack cell. It won't increase beyond 255.-
Decrements the current value of the stack cell. It won't decrease beyond 0..
Prints the current cell value as an ASCII character.,
Reads a single character from stdin, and replaces the cell with it's value. Characters with values greater than 255 are undefined behaviour.[
Marks the beginning of a loop.]
If the current cell is not 0, jump backwards to the previous[
. Otherwise, continue processing.If you're right about the cause of this issue, wikipedia code probably isn't actually correct. What happens if you move pointer below zero is undefined, so you can do whatever you want in this situation.
(Wikipedia, Brainfuck)
The Wikipedia example is not supposed to decrement the pointer below 0. It looks like you're just going to the first opposite bracket character you match without taking nesting into account, which the example does use.
It might be fun to make a JIT compiler. Should be pretty easy considering the simplicity of the language.
I have this if it counts (see below). I wrote it a couple years ago just for fun.
Edit: remove ^L chars.
Yet another brainfuck interpreter... but in pure
sh
! It actually performs pretty well on fast implementations likedash
, outperforming my Bash implementation (you can find it and more here) in certain areas. Info:[
,printf
andread
are not mandatory POSIX built-ins, they are required external utilities