Game Development Reference
In-Depth Information
Fig. 2.3 Translating a
program using an interpreter
2.4.3 Interpreter
A more direct way to translate programs is to use an interpreter . This is a pro-
gram that reads the source code and immediately executes the instructions contained
within it, without translating it to machine code first. The interpreter is specific for
a machine, but the source code is machine-independent, just like the compiler. In
a sense, the interpreter program does exactly the same as a human interpreter (or
translator). You could say that a compiler is doing something similar to translating
a written text into another written text, while an interpreter translates and speaks at
the same time.
The advantage of an interpreter over a compiler is that a translation into machine
code no longer is required. The disadvantage is that the execution of the program
is slower, because translating a program also takes time. Furthermore, any errors in
the program cannot be detected beforehand, whereas the compiler can detect such
errors. Translating using an interpreter is quite common for simpler languages, such
as script languages (see Fig. 2.3 ).
2.4.4 Compiler + Interpreter
The Java language chose a mixed approach. The goal of Java programs is to dis-
tribute them over the Internet. Spreading around already compiled programs is not a
very good idea: recall that compiled programs are machine-dependent, so you would
need to distribute separate versions for every possible computer. On the other hand,
spreading the source code is also not always a suitable option. The complete text
of the program is then available for everyone to modify, copy and use in their own
programs, which makes it difficult to earn money writing software. In many cases,
users are allowed to use a program, but they may not read it or modify it. Machine
code is very useful for that goal.
The approach that Java uses is a compiler that translates the source code; not into
machine code, but into another machine-independent intermediate language, called
the bytecode (see Fig. 2.4 ). The bytecode can be spread on the Internet and it is
executed on the user machine by an interpreter. The bytecode uses a very simple
language that is easy to translate into machine code, which makes it easier to in-
corporate it in Internet browsers. Because the main translation is already done by a
Search WWH ::




Custom Search