I have a Computer Science degree. I attended a whole course of lectures on compilers (and have a certain fondness for “the red dragon book” as a result). However, I had never actually written a compiler from start to finish until a rainy day last weekend. Yes, this is what I do for fun.
I wanted to make a compiler for a real language, but a simple one so I could complete the project in a few hours. I’ve always had a bit of a soft spot for BASIC - it’s the first progamming language I learned as a kid. Luckily there’s a practical variant called TinyBASIC (thank you, wikipedia, for that tip). My version is even simpler than TinyBASIC (no INPUT statement). Hence I call it toybasic . The code is all available on GitHub.
It’s written in Go and it generates Go code from BASIC.
Demo
Example program example/hello.bas
10 PRINT "Hello, world." 20 LET x = (3 * 2) + 3 30 LET x = x + 1 40 IF x == 10 THEN PRINT "Ten!" 45 PRINT x 50 IF x >= 15 THEN GOTO 70 60 GOTO 30 70 END
Example output
$ ./toybasic