Compiler
Earlier, we saw that the most commonly used tools in D are a
text editor and a
compiler . D programs are written in text editors (for example, your CO).
When using
compiled languages such as D, you need to understand the concept of compilation and the function of the compiler.
Machine codes
The brain of a computer is a microprocessor (or CPU, short for
central processing unit ). Encoding is called specifying what exactly the CPU should do, and the instructions that are used in this are called
machine codes .
Most CPU architectures use machine-specific codes. These machine instructions are determined by the constraints of the hardware during the design of the architecture. At the lowest level, these instructions are implemented as electrical signals. Since the simplicity of programming at this level is not the main goal, writing programs directly in machine CPU codes is a very difficult task.
These machine instructions are special numbers that represent the various operations supported by a particular CPU. For example, for an imaginary 8-bit CPU, the number 4 may represent a load operation, the number 5 is a save operation, and the number 6 is an operation of incrementing the value by one. Assuming that the first 3 bits on the left are the operation number and the 5 subsequent bits are the value used in this operation, the example program in the machine codes of this CPU may look like this:
Operation Value Meaning 100 11110 LOAD 11110 101 10100 STORE 10100 110 10100 INCREMENT 10100 000 00000 PAUSE
Being so close to the hardware, machine codes are not suitable for representing high-level concepts such as
playing cards or
student records .
Programming languages
Programming languages are designed to be an efficient way to program CPUs with the ability to represent high-level concepts. Programming languages do not have to deal with hardware limitations; their main task is ease of use and expressiveness. Programming languages are easier understood by people, they are closer to natural languages:
if (a_card_has_been_played()) { display_the_card(); }
However, programming languages adhere to much stricter and more formal rules than any language spoken.
Compiled languages
In some programming languages, instructions must be compiled before becoming an executable program. Such languages create fast-running programs, but the development process involves two basic steps: writing a program and compiling it.
In general, compiled languages help in detecting errors even before a program starts to run.
D - compiled language.
Interpreted languages
Some programming languages do not require compilation. Such languages are called
interpretable . The program can run directly from freshly printed source code. Some examples of interpreted languages are: Python, Ruby and Perl. Since there is no compilation stage, program development for such languages may be easier. On the other hand, since program instructions must be parsed for interpretation each time a program is launched, a program in such languages is slower than their equivalents written in compiled languages.
In the general case for interpreted languages, many types of errors in the program cannot be detected until the moment they start execution.
Compiler
The purpose of the compiler is translation: it translates programs written in a programming language into machine code. This is a translation from the programmer's language to the CPU language. This translation is called
compilation . Each compiler understands a particular programming language and is described as a compiler for that language, for example, “compiler for D”.
Compilation errors
Since the compiler compiles the program according to the rules of the language, it stops the compilation as soon as it reaches
incorrect instructions. Invalid instructions - those that are out of the specifications of the language. Problems such as: mismatch brackets, missing semicolon, misspelled keyword, etc. - all cause compilation errors.
The compiler also generates
compilation warnings when it sees a suspicious piece of code that may be disturbing, but not necessarily an error. However, warnings almost always indicate a real mistake or bad style, so the most common practice is to treat most or all warnings as errors.