Many years ago, a young and curious version of myself took an interest in calculators, and with such an interest, I felt that it was necessary for me to own every different kind of calculator that I could get my hands on. After expressing my interest to my father, he went out and bought me a Casio fx-9750G PLUS programmable calculator. At the time, however, I was not that big into programming, and thus the calculator was left to collect dust in my closet.

Recently, however, I decided to take it out again, simply because it had advanced expression parsing that I believed to be necessary for certain subjects in school. For instance, the calculator allows the entry of expressions such as “(((2.5*9.81)^2 + 2) – 32)/(64^2)”, making something that would otherwise be a hassle to calculate into a piece of cake. At first, my only ambition was to be able to do such calculations with ease, which would lessen my anxiety on science and math exams. Being a programmer, though, I soon discovered the underlying delight of my calculator, the BASIC programming environment. The fx-9750G PLUS includes an environment in which you can enter programs in Casio’s BASIC language, using one of many built-in functions and operations. I quickly began to write programs that helped me with problems on tests, and other programs that really weren’t useful for school at all.

Pretty soon, I realized that entering programs on my calculator before I planned them wasn’t the best practice, since I would then have to modify the program on my calculator in order to fix bugs. I took to the habit of writing programs in my notebook before entering them on my calculator, which did ease some of the pain, but also left something to be desired. The problem was, I could think through programs all I wanted to on paper, but how could I test them? That’s when I made the decision to make a custom BASIC interpreter for my computer that would allow me to enter and test BASIC programs on the fly, with the joy of typing on a qwerty keyboard instead of Casio’s poorly thought out keyboard layout.

Since I did not plan on fully recreating Casio’s language, I decided to call my mutated spin-off ANBasic. Before I even set out on this project, I’d been dreaming for a long while of making some form of proprietary byte-code, just because I liked the idea of creating data that is unreadable to humans but easily understandable to machines. So, it was my decision early on that my ANBasic interpreter would process a script, compile it to ANBasic byte-code, and then would be able to execute this byte-code at a later point in time.

The first step to implementing this project was to design a simple tokenizer for the ANBasic language. This tokenizer would read a raw script file, split it up line by line, and pick out tokens, such as mathematical operators, function and variable names, etc. Once the tokenizer was done, I created a “grouper,” a small set of subroutines that processed control-flow statements, applied the order of operations (PEMDAS), and grouped functions with arguments. The grouped script would then be written to a binary byte-code file in my custom byte-code format. The runtime would then load this grouped script from a file, and execute the grouped script using a series of Objective-C categories on different code objects.

Although the compiler itself generates a grouped script, and could easily execute it without writing it to a file, I already had my mind set on designing a byte-code format, so that is what I did. And, if I do say so myself, the final product is pretty nifty. Although my ANBasic project isn’t quite done as of yet, it can currently compile a variety of control-flow statements, functions, expressions, etc., and can execute them. I have tested it with some of the programs that I wrote for my calculator, and it works like a charm. You can check out the ANBasicCompiler Github repository for yourself and see what I’ve been working on. At this point, I’ve already pretty much lost interest in my Casio, as I’ve recently obtained a new TI-Nspire calculator. Despite this change, I still stuck to finishing my ANBasic project, which ended up teaching me some valuable lessons about tokenization and lexical analyzation, not to mention the fact that I simply had nothing better to do.