# 4. Bytecodes and Compilation

## 4.1 Overview

After a Chaprola program is written and entered as a .CS file, it is not in a form that is directly usable by the computer. This file must be translated ("compiled") into a language that the computer itself understands.

## 4.2 Historical Context

At the time the Chaprola Language was defined and implemented (before 1975), the compilation of common computer languages produced sets of native instructions for a specific computer. Compiled programs could not be used on another computer with a different internal architecture. Also, computers did not possess enough internal memory to hold very long programs.

## 4.3 The Bytecode Technique

A technique was developed that was quite simple: the Chaprola language was broken down into the smallest functional building blocks of instructions that were possible. Each building block was a set of computer instructions, but instead of expressing these in terms of machine instructions, the building block was given a number.

When Chaprola is compiled, the Procedure file (.PR) is comprised of a sequence of these building block numbers.

A processor is then written once (a different one for each different computer architecture). This processor reads the procedure file and interprets each building block number, then executes the sequence of instructions that the number represents.

## 4.4 Significance

Today, procedure files are called bytecode files. The Java language and the .NET runtime library use the same methodology (but with different bytecode meanings). When Chaprola was first implemented in 1974, these other languages did not yet exist.

Bytecode interpretation was a wonderful idea then, and remains so today.

## 4.5 Compilation Pipeline

The compilation of a Chaprola source module involves two programs:

### 4.5.1 CHAPTRAN

The translator program. It reads the Chaprola source file (.CS) and produces an intermediate representation.

### 4.5.2 CHAPCOMP

The compiler program. It takes the output of CHAPTRAN and produces the final procedure file (.PR) containing bytecode.

**Debug option:** When running CHAPCOMP, if an `x` is placed in the third word position, the compiler will number all lines. This enables the DEBUG command to report accurate line numbers in the debug file. Without this option, all line numbers appear as zero. The line-numbering option slows execution and should be removed for production use.

### 4.5.3 CHAPROLA Batch File

The compilation can be performed using a single batch file command:

```
>>? AC CHAPROLA [fnprefix]
```

This executes CHAPTRAN and CHAPCOMP in sequence.

## 4.6 File Types

| Extension | Description |
|-----------|-------------|
| `.CS` | Chaprola Source file (user-written program) |
| `.PR` | Procedure file (compiled bytecode) |

---

*Previous: [Commands](03-commands.md) | Next: [Error Codes](05-error-codes.md)*
