History
In the early days of computing, compilers were more commonly found and used than interpreters because hardware at that time could not support both the interpreter and interpreted code and the typical batch environment of the time limited the advantages of interpretation.1
Interpreters were used as early as 1952 to ease programming within the limitations of computers at the time (e.g. a shortage of program storage space, or no native support for floating point numbers). Interpreters were also used to translate between low-level machine languages, allowing code to be written for machines that were still under construction and tested on computers that already existed.2 The first interpreted high-level language was Lisp. Lisp was first implemented by Steve Russell on an IBM 704 computer. Russell had read John McCarthy's paper, "Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I", and realized (to McCarthy's surprise) that the Lisp eval function could be implemented in machine code.3 The result was a working Lisp interpreter which could be used to run Lisp programs, or more properly, "evaluate Lisp expressions".
The development of editing interpreters was influenced by the need for interactive computing. In the 1960s, the introduction of time-sharing systems allowed multiple users to access a computer simultaneously, and editing interpreters became essential for managing and modifying code in real-time. The first editing interpreters were likely developed for mainframe computers, where they were used to create and modify programs on the fly. One of the earliest examples of an editing interpreter is the EDT (Editor and Debugger for the TECO) system, which was developed in the late 1960s for the PDP-1 computer. EDT allowed users to edit and debug programs using a combination of commands and macros, paving the way for modern text editors and interactive development environments.
Efficiency
Interpretive overhead is the runtime cost of executing code via an interpreter instead of as native (compiled) code. Interpreting is slower because the interpreter executes multiple machine-code instructions for the equivalent functionality in the native code. In particular, access to variables is slower in an interpreter because the mapping of identifiers to storage locations must be done repeatedly at run-time rather than at compile time.4 But faster development (due to factors such as shorter edit-build-run cycle) can outweigh the value of faster execution speed—especially when prototyping and testing when the edit-build-run cycle is frequent.45
An interpreter may generate an intermediate representation (IR) of the program from source code in order to achieve goals such as fast runtime performance. A compiler may also generate an IR, but the compiler generates machine code for later execution whereas the interpreter prepares to execute the program. These differing goals lead to differing IR design. Many BASIC interpreters replace keywords with single byte tokens which can be used to find the instruction in a jump table.4 A few interpreters, such as the PBASIC interpreter, achieve even higher levels of program compaction by using a bit-oriented rather than a byte-oriented program memory structure, where commands tokens occupy perhaps 5 bits, nominally "16-bit" constants are stored in a variable-length code requiring 3, 6, 10, or 18 bits, and address operands include a "bit offset". Many BASIC interpreters can store and read back their own tokenized internal representation.
Example
An expression interpreter written in C++.
import std;
using std::runtime_error;
using std::unique_ptr;
using std::variant;
// data types for abstract syntax tree
enum class Kind: char {
VAR,
CONST,
SUM,
DIFF,
MULT,
DIV,
PLUS,
MINUS,
NOT
};
// forward declaration
class Node;
class Variable {
public:
int* memory;
};
class Constant {
public:
int value;
};
class UnaryOperation {
public:
unique_ptr<Node> right;
};
class BinaryOperation {
public:
unique_ptr<Node> left;
unique_ptr<Node> right;
};
using Expression = variant<Variable, Constant, BinaryOperation, UnaryOperation>;
class Node {
public:
Kind kind;
Expression e;
};
// interpreter procedure
[[nodiscard]]
int executeIntExpression(const Node& n) {
int leftValue;
int rightValue;
switch (n->kind) {
case Kind::VAR:
return std::get<Variable>(n.e).memory;
case Kind::CONST:
return std::get<Constant>(n.e).value;
case Kind::SUM:
case Kind::DIFF:
case Kind::MULT:
case Kind::DIV:
const BinaryOperation& bin = std::get<BinaryOperation>(n.e);
leftValue = executeIntExpression(bin.left.get());
rightValue = executeIntExpression(bin.right.get());
switch (n.kind) {
case Kind::SUM:
return leftValue + rightValue;
case Kind::DIFF:
return leftValue - rightValue;
case Kind::MULT:
return leftValue * rightValue;
case Kind::DIV:
if (rightValue == 0) {
throw runtime_error("Division by zero");
}
return leftValue / rightValue;
}
case Kind::PLUS:
case Kind::MINUS:
case Kind::NOT:
const UnaryOperation& un = std::get<UnaryOperation>(n.e);
rightValue = executeIntExpression(un.right.get());
switch (n.kind) {
case Kind::PLUS:
return +rightValue;
case Kind::MINUS:
return -rightValue;
case Kind::NOT:
return !rightValue;
}
default:
std::unreachable();
}
}