Beginning Programming All-in-One For Dummies. Wallace Wang
Чтение книги онлайн.
Читать онлайн книгу Beginning Programming All-in-One For Dummies - Wallace Wang страница 22

Some popular editors include the following:
GNU Emacs (www.gnu.org/software/emacs
): Editor only for Linux, macOS, and Windows
Playgrounds (www.apple.com/swift/playgrounds
): iPadOS and macOS
Visual Studio (https://visualstudio.microsoft.com
): macOS and Windows, with a limited version available for Linux
Xcode (https://developer.apple.com/xcode
): macOS only
Unlike a word processor, which offers commands for formatting text to make it look pretty or appear in different colors, text editors are just designed for typing commands in a particular programming language, such as C++, Java, or Swift:
The simplest editor just lets you type commands in a file.
More sophisticated editors can help you write a program byColor-coding program commands (to help you identify them easily)Indenting your code automatically (to make it easier to read)Typing in commonly used commands for you
Figure 1-2 shows a simple editor used to write a Swift program that creates a hypotrochoid art figure.
FIGURE 1-2: An editor lets you write and edit the source code of a program.
Without an editor, you can’t write a program. With an editor, you can write a program. And with a really good editor, you can write a program quickly and easily.
Converting source code with an assembler or compiler
An editor lets you type and save program commands (or source code) in a file. Unless you’ve written a program completely in machine language, your source code may as well have been written in Swahili because processors don’t understand any language other than machine language.
So, to convert your source code into machine language commands, you have to use an assembler (if you wrote your program commands in assembly language) or a compiler (if you wrote your program commands in the C language or a high-level language like Java).
After converting your source code into equivalent machine language commands, an assembler or compiler saves these machine language commands in a separate file, often called an executable file (or just an EXE file). When you buy a program, such as a video game or an antivirus program, you’re really buying an executable file. Without an assembler or a compiler, you can’t create your program.
Compilers translate source code into machine language, which is the native language of a specific processor. But what if you want your program to run on different processors? To do this, you have to compile your program into machine language for each different processor. You wind up with one executable file for each processor, such as an executable file for an Intel processor and a separate executable file for an ARM processor.
One executable file contains machine language code for the M-series processor (used in newer Mac computers)
The second executable file contains machine language code for the Intel processor (used in old Mac computers)
Most compilers work only on one specific operating system and processor. So, a Windows compiler may only create programs that run under the Windows operating system. Likewise, a Linux compiler may only create programs that run under the Linux operating system.
If you write a program that runs under Windows, you can recompile it to run under Linux. Unfortunately, you may have to modify your program slightly (or a lot) to make it run under Linux.
To make it easy to create programs for multiple operating systems, you can use a cross-platform compiler. This means you can write a program once and then choose to compile it for two or more operating systems such as macOS and Windows or Android and iOS. Cross-platform tools make it easy to write the same program for multiple operating systems, but you may need to write additional code to take advantage of the unique features of each operating system.
Translating source code with an interpreter
In the old days, compilers were notoriously slow. You could feed source code to a compiler and literally come back the next morning to see if the compiler was done. If you made a single mistake in your program, you had to correct it and recompile your program all over again — with another overnight wait to see if it even worked.
Trying to write a program with such slow compilers proved maddening, so computer scientists created something faster called an interpreter. A computer interpreter is just