So I thought to myself, what does the world need? Obviously another brainfuck (BF) compiler. In this series I will use F# and FParsec to compile BF source code into MSIL to run in Microsoft’s CLR. Honestly, this isn’t a particularly ground-breaking task, but it serves as a fun opportunity to showcase a popular parsing library. Beyond that, it shows how easy it is for F# to leverage various parts of the .NET ecosystem.
Since there are a couple subtopics, this process is best addressed as a series. This first post will address FParsec and parsing the BF source code. The goal is intentionally restricted, so I will have placeholders with no functionality for the Ops as they are parsed. In later posts these will be expanded upon. The second post addresses IL generation and program generation. The third post will put the pieces together into something interesting. In the end, I’ll have a BF compiler.
Before I get into the code, here is a quick synopsis of the BF functionality that needs to be supported.
Memory (data) - Array of ints
Data Pointer (data_pointer) - Current pointer position of data array
Instruction Pointer (instruction_pointer) - Position of current program operation
> Increment data_pointer
< Decrement data_pointer
+ Increment value at data[data_pointer]
- Decrement value at data[data_pointer]
[ Loop Start - Start of loop where predicate is: data[data_pointer] == 0
] Loop End - End of loop
, Read char from stdin into value at data[data_pointer]
. Print value at data[data_pointer] to stdout (as char)
For those familar with BF, there are variants that auto-grow memory or wrap overflows/underflows of the pointers. To keep things simple, I will not implement those for this series.
Example program that prints: 42
With that brief introduction, I can begin to address how I will parse a BF program. The language syntax is so simple, I could easily iterate and use F#’s
match. Instead, I’ll use FParsec, a powerful parsing library modeled after the Haskell Parsec library. What I have to parse here won’t do its power justice, but it works as an easily digestible introduction. Now to the code.
Using Paket, here is a sample paket.dependencies file.
Time to load modules. Note the need for FParsecCS.dll and FParsec.dll, order of inclusion matters.
I leverage F#’s Discriminated Unions to represent the operators. In BF there are basically two types of operators. Those that do an immediate action (like increment, decrement, read/print char), and the while loop (a list of instructions to loop over). F# allows this description to be modeled easily.
type actionOps = Gt | Lt | Plus | Minus | Read | Write
Next are the my parser specific types. UserState can be used to keep state as the parser progresses, and
Parser<'t> is a returned value as well as current state.
type UserState = unit
Before I go too much deeper, how does a parser work? Below is a quick example. Parsers can get pretty advanced, and my introduction here won’t cover nearly all of the details. I highly recommend checking out the FParsec Tutorial to learn more (although it won’t be necessary for this post). pGreater looks for the char ‘>’, when found it executes the attached closure. For illustrative purposes I added a print statement, but it ultimately returns the appropriate
let pGreater:Parser<_> =
Here is a sample execution using
run. It finds the ‘>’, prints it, and returns a successful parsing object.
> run pGreater ">>--"
Taking this a bit further, it only matched one of the ‘>’. To match multiple instances, I apply the
many combinator. Note how this time it matches 2 ‘>’, and returns a list of its matches.
> run (many pGreater) ">>--"
What happens if there isn’t a match? Notice how this isn’t currently a wildcard search, it starts parsing at the beginning of the string and fails if it’s a non-match.
> run pGreater "<>--"
Now I introduce a Parser for the ‘<’. I also want to to match multiple instances using
many of either ‘>’ or ‘<’; this is done with the
<|> operator. You can see the results of running this slightly more advanced search.
let pLesser:Parser<_> =
Going a bit further down the rabbithole. Temporarily I make a Letter op, just so the sample parse looks more logical. I make a letter parser that accepts an ‘a’ or ‘b’, this part isn’t new. Then I do a search for multiple a’s and b’s surrounded by angle brackets. The combinators
.>> link a sequence of parsers and indicate which side of the parse to capture (hint, capture on the . side of the operator). In the below output you can see how each character is matched, but only the letters are returned in the parsing result, the brackets are discarded.
// Letter is temporary for demonstration
I could go on for ages, but hopefully this is a basic enough start to how parsers work to get us through the post. Now to the BF parsers. Some of these will look familar. There is a character parser for each valid BF character/operator. For my purposes, its enough to convert the char to a specific type. This type information will be used later for generating the desired IL.
let pGreater:Parser<_> =
The action operators are pretty straight forward. Parsing loops is a bit more complicated. I need to not only parse blocks of operators, but allow embedded loops as well. This reference to something prior to being created causes a complication. What I effectively need is the ability to call loop recursively. Normal F# style would use
let rec, but that isn’t an option here. So I leverage FParsec’s
createParserForwardedToRef(). This allows me to use a parser pLoop as I normally would. I then have to inject the pLoop logic into the pLoopImpl reference. Implementing the parsing of a loop is basically find matching ‘[‘ and ‘]’, and capture any commands or embedded loops within them, recursively parsing the internal loops as necessary. Then return the result as a Loop.
let (pLoop:Parser<allOps>), (pLoopImpl:Parser<allOps> ref) = createParserForwardedToRef()
There is one more parser to implement, the overarching program parser. As you can see, a program is just many instances of operators or loops.
let pProgram = (many (pCommands <|> pLoop))
Now that the parsing component is complete, I need a consumer of the resulting AST generated. Since BF is a list of operators, I iterate through the AST and handle each op as I approach it. Note, this is one of the placeholder functions I mentioned earlier. Here my “handle op” is just an sprintf the op (prefixing operators with ‘O:’ and wrapping loops in ‘[[…]]’). When the real compiler is built, this function will be replaced with something that emits IL.
let rec ProcessAst (ast:allOps) =
The parseCode function pulls it all together. Given program source code, the function parses it and then processes the resulting AST. I also use a success/failure match in case the parsing fails. Again, this function will be expanded once I start emiting IL.
let parseCode code =
All of the pieces are in place, so it is time to look at processing program code. First, a look into the intermediate parsing step.
run pProgram code takes code and returns the AST. Below it shows the operator parsing and the loops embedded in the list of resulting operators. Second, the psuedo-compile is done with parseCode. Again the operator parsing is shown as well, and the result is a a string of ops.
let code = ">[++>+[-]+<]>."
I can now parse BF source code using Fparsec and have it stored in convenient structure for further processing. With phase one of this series complete, this is a good place to pause. Next will be looking at what it takes to generate IL in F#.