Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Beginning Programming for Dummies 2004.pdf
Скачиваний:
109
Добавлен:
17.08.2013
Размер:
8.05 Mб
Скачать

50

Part I: Programming a Computer

Using a Compiler or an Interpreter

After you type your instructions in an editor by using a programming language such as C++ or Java, guess what? The computer doesn’t have the slightest idea what you just created. Computers understand only machine language, so you need to use another special program to convert your source code (the instructions that you write in C++ or Java) into machine language.

You can use either of the following two types of programs to convert source code into machine language:

A compiler

An interpreter

Compilers

A compiler takes your source code, converts the entire thing into machine language, and then stores these equivalent machine language instructions in a separate file, often known as an executable file. The process is like having a translator study an entire novel written in Spanish and then translate it into Arabic.

Whenever a compiler converts source code into machine language, it’s compiling a program.

After you compile a program, you can just give away copies of the executable (machine-language) version of your program without giving away your source code version. As a result, most commercial programs (such as Microsoft PowerPoint and Quicken) are compiled.

After you use a compiler to convert source code into machine language, you never need to use the compiler again (unless you make changes to your source code).

A compiler creates machine language for a specific microprocessor, such as the PowerPC (which the Macintosh uses) or the Intel Pentium family of microprocessors (including clone microprocessors, such as the AMD Athlon). If you write a program in BASIC and want to run it on a Macintosh and a Windows computer, you need to compile your program twice: once for the Macintosh and once for the Windows environment.

Not all compilers are equal, although two different compilers may convert the same language into machine language. Given identical C++ source code, for example, one C++ compiler may create a program that runs quickly, whereas a second C++ compiler may create a smaller file that runs much slower.

Chapter 4: The Tools of a Computer Programmer

51

Interpreters

A second, but less popular, way to convert source code into machine language is to use an interpreter. An interpreter converts each line of your source code into machine language, one line at a time. The process is like giving a speech in English and having someone translate your sentences, one at a time, into another language (such as French).

Unlike what a compiler does, an interpreter converts source code into machine language but stores the machine-language instructions in the computer’s memory. Every time that you turn off the computer, you lose the machine-language version of your program. To run the program again, you must feed the source code into the interpreter again.

If anyone wants to run your program, that person needs both an interpreter and the source code for your program. Because your source code enables everyone to see how you wrote your program (and gives others a chance to copy or modify your program without your permission), very few commercial programs use an interpreter.

Most Web-page programming languages use interpreters, such as JavaScript and VBScript. Because different computers can view Web pages, you can’t compile programs that you write in JavaScript or VBScript into machine language. Instead, your computer’s browser uses an interpreter to run a JavaScript or VBScript program.

In the old days, when computers were slow and lacking in sufficient memory and storage space, interpreters were popular because they gave you instant feedback. The moment you typed an instruction into the computer, the interpreter told you whether that instruction would work and even showed you the results. With an interpreter, you could write and test your program at the same time. Now, computers are so fast that programmers find using a compiler easier than using an interpreter.

P-code: A combination compiler and interpreter

Getting a program to run on different types of computers is often a big pain in the neck. Both Macintosh and Windows programs, for example, use pull-down menus and dialog boxes. You need to write one set of commands to create pull-down menus on the Macintosh, however, and a second set of commands to create the identical menus in Windows.

52

Part I: Programming a Computer

Because one program almost never runs on multiple computers without extensive modification, programmers combined the features of a compiler with an interpreter to create something called p-code.

Instead of compiling source code directly into machine language, p-code compiles source code into a special intermediate file format. To run a program compiled into p-code, you use an interpreter. This two-step process means that after you compile your program into p-code, you can run your p-code interpreted program on any computer that has the right p-code interpreter.

Java is the most popular programming language that uses p-code. After you compile a Java program into p-code, you can copy that p-code to a Macintosh, a Windows computer, or a Linux computer. As long as that computer uses a Java p-code interpreter, you can run the Java program on that computer without modification.

Best of all, programs that you compile into p-code can run without the original source code, which means that you can protect your source code and still give your program away to others.

Just in case you’re wondering, Liberty BASIC, which comes with this book, takes BASIC instructions and saves them in a separate file that uses p-code. If you distribute any compiled programs that you create using Liberty BASIC, you need to also distribute a special run-time file that can translate your Liberty BASIC p-code on another computer.

Naturally, p-code has its own disadvantages. Programs that you create by using p-code tend to run much slower than programs that you compile directly into machine language. Although p-code programs can run without a copy of the original source code that you use to create them, you can also decompile p-code programs.

Decompiling a p-code program can reveal the original source code that the programmer used to create the program. So if you write a program in Java and compile it into p-code, a rival can decompile your p-code program and see your original Java source code. Your rival then ends up with a nearly identical copy of your source code, essentially stealing your program.

You can actually decompile any program, including programs that you compile into machine language. But unlike with decompiling p-code programs, decompiling a machine-language version of a program never gets you the original high-level language source code that the programmer used to write the program. If you compile a program into machine language, the original source code can be written in C++, COBOL, FORTRAN, BASIC, Ada, LISP, Pascal, or any other programming language in the world. Because the decompiler has no idea what language the original source code was written in, it can only decompile a machine-language version of a program into equivalent assembly