This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email email@example.com to talk privately.
Exercise 37: Little BASIC
You are now going to take a trip back in time to my childhood and implement a BASIC interpreter. No, I don't mean BASIC as in "a really simple basic bland interpreter." I mean the programming language BASIC. It was one of the very first programming languages, originally created at Dartmouth by John Kemeny, and Thomas Kurtz. This version of basic is called Dartmouth BASIC, and the code looked something like this from the Dartmouth BASIC Wikipedia page:
5 LET S = 0 10 MAT INPUT V 20 LET N = NUM 30 IF N = 0 THEN 99 40 FOR I = 1 TO N 45 LET S = S + V(I) 50 NEXT I 60 PRINT S/N 70 GO TO 5 99 END
Those numbers to the left are actually manually entered line numbers. You told BASIC a number for each line, and then you could loop by simply telling it to "GO TO" that line. This later became GOTO in other versions of BASIC and became a kind of symbol of that era of computing.
Later versions of BASIC are documented on the main BASIC Wikipedia page, which shows a long evolution of the language into more and more--ehem--modern forms. After a while it picked up structure like C and Algol, then it got object-oriented, and today you can find fairly advanced versions of BASIC. Check out Gambas BASIC at http://gambas.sourceforge.net/en/main.html if you want to see a modern free BASIC.
Your challenge is to implement an original BASIC interpreter--the one with the manual line numbers and all CAPS mainframe text style. You'll want to look at the main BASIC wikipedia page for possible tokens and sample code and also read the Dartmouth BASIC Wikipedia page for more clues. Your interpreter should be able to handle as much of the original BASIC as possible and produce valid output.
When you attempt this I suggest trying to do simple math, printing, and keeping track of line numbers. After that I'd work on getting GOTO to function correctly. If you have that, then you can finish the rest and slowly develop a suite of test programs to make sure your interpreter works well.
Good luck! This could take you a while, but it should be so much fun. I could see myself spending months on something like this just adding silly features like graphics so I can create all those stupid little programs I made when I was a kid. I wrote so much BASIC code it definitely warped my brain to count line numbers. That's probably why I like Vim so much.
This exercise is difficult enough, but if you want some additional challenges, do the following:
- Create an alternative interpreter that uses a parser generator like SLY. Once you have the ABNF this may end up being much easier, but it could be harder with a language like BASIC. You'll have to do it and find out.
- Try to make a version of "structured BASIC" that has functions, loops, if-statements, and everything you'd find in an older non-OOP language like C or Pascal. This is a huge task, so I suggest if you attempt it to not write an RDP parser by hand. Use a tool like SLY to generate your parser and save your brain for more important things.