The final chapter in this book is devoted to an ambitious project to write a compiler which will convert programs written in a high-level language, similar to Pascal, into the machine code of the Atom and BBC Computer.
In order to keep the compiler as straightforward as possible, and to enable it to run in the memory of the standard Atom or BBC Computer, several simplifications were made. First of all, the compiler is limited to 8-bit numbers; i.e. numbers in the range 0 to 255. Secondly, it handles a restricted set of statements and operators.
The program was primarily developed to illustrate the problems involved in designing a high-level language compiler. It should also serve as a good introduction to recursively-defined languages such as Pascal, and shows the relationship between a statement in such a language and the corresponding machine-code. Finally, the compiler will compile into efficient machine code, and so could be used to develop machine-code programs for applications such as machine control.
Compilers and Interpreters
The BASIC running on the Atom and BBC computers is an 'interpreter'; that is, it executes each statement in the program as it encounters it. A 'compiler', on the other hand, takes a program in one language and converts it into machine code - the language of the processor. The compiled, machine-code version of the program can then be run without further needing the presence of the compiler program. Also, since they are running in the language of the computer itself, compiled programs are likely to run very much faster than interpreted versions of the same programs.
The compiler to be described is written in BASIC, and makes use of the mnemonic assembler built into the Atom and the BBC Computer. The advantage of using the built-in assembler is that an assembler listing is automatically produced, so the code generated for a particular high-level language program is comprehensible and easily checked. However, the compiler could be altered to run on other computers by generating machine code directly; it could also be used to write programs for microprocessors other than the 6502.