Interpreters have been around for many years ... in fact almost as many years as I have been writing software. Interpreters are fed input from the user line by line and give editing facilities should you enter incorrect code. They report syntax errors immediately upon input so it is not possible to enter syntactically incorrect code (although some of the semantic errors are reported at run-time). One of the most common interpreters on early PC's was Basic which stood for Beginners All-purpose Symbolic Instruction Code.

The sophisticated Microsoft Visual Basic has it's roots in such early versions of Basic implemented on the IBM PC, and interpreters are still very much alive and well.

Prerequisites

Anybody that intends to write an interpreter has to understand the principles of parsing, expression evaluation and dynamic memory allocation. Without these prinicples, you will not be able to understand what the solutions to many of the problems that will be encountered in writing even the simplest intepreter.

I have chosen to elect a simple BASIC interpreter as our goal for the purpose of these tutorials, and we will hopefully end up with a working interpreter at the end of them.

The Interpreter Environment

Interpreters are not, generally speaking, stand-alone programs that the developer invokes to compile a piece of code. They also offer an Integrated Development Environment in much the same way as applications such as Visual Studio, Delphi and Eclipse. We will therefore need to write such an environment for our basic interpreted.

The aim of this tutorial is to write a simple line editor that gives us the ability to edit code and form a platform for our interpreter.

All of the code has to be portable (at least as portable as is possible) so the interpreter can be made to run on any OS with minimal change.

The Basic Line Editor

This will be a VEry Tiny LINe Editor - VETLINE (I love naming software) which gives you the ability to move up and down lines, list, insert and delete lines and modify lines. The commands will be simple:

U will move up a line
Un will move up n lines
D will move down a line
Dn will move down n lines
I will place the editor in insert mode where lines may be entered
R will remove the current line
Rn will remove the n lines
C will display the current line
O will place the current line in 'overwrite mode' (not implemented in this version)
S will show all lines
Sn will show the next n lines (not implemented in this version)
Lfilename will load a file from disc (not implemented in this version)
Sfilename will save a file to disc (not implemented in this version)
X will execute the source (not implemented in this version)

I have included the code for the line editor below. It has been tested, but may still contain errors ... which I am sure we will find as we develop the interpreter. The editor may also develop as we progress through these tutorials.

This concludes Part I of this tutorial. In part II, we will look at the basic language that we will interpret, and the parser that will be used to parse the user input in readiness for interpretation. Hold on to your seats ... this is going to be good!!!

Replies To: Writing An Interpreter

I noticed a couple of errors in this line editor that you may want to change. In the switch statement, in case 'I'(insertion), while the editor is reading in strings, it checks for if(cin.eof()) break. When I was testing this out it would look something like this:

VEtLINe line editor - version 1.0
[Basic]: i
1 hello
2 [Basic]:

if you hit Ctrl-D to to stop inputting on the first line, it prints the [Basic]: prompt on the next insertion line. So I just added a "cout << endl;" in the if statement that checks for cin.eof(). Also, when I first ran the editor and it's looking for a command character, if I press Ctrl-D, it makes the loop in main go infinite and it keeps printing '[Basic]:' until I hit Ctrl-Z. How and where would you check for input like that to stop this from occurring?