On Wed, Dec 28, 2022 at 02:31:45AM +0000, thebluepandabear via Digitalmars-d-learn wrote: > I am reading through the free book on learning D by Ali Çehreli and I > am having difficulties understanding the difference between compile > time execution and run time execution in D language.
It's very simple: in a compiled language like D, your program goes through 3 stages: 1) Source code: the human-readable text that you write. 2) Compilation: this is when the compiler is compiling your program. IOW, "compile time". Compile time happens once when you compile your program. 3) Binary executable: the result of compilation. When you run this executable, that's "runtime". Runtime happens every time you run the program. Stage (2) is transient, and only happens inside the compiler. It can be broken down into several steps: a) Lexing & parsing: the compiler reads the source code, breaks it into tokens (keywords, identifiers, operators, etc.), and constructs from them an abstract syntax tree (AST) that represents your program. Here is where typos and syntax errors are detected. b) Semantic analysis: the compiler processes the AST and assigns meaning to each corresponding programming construct. Here is where (some) logic errors are detected (reference to an undefined identifier, invalid operations on a data type, calling a function with the wrong number of arguments, etc.). c) Code generation: based on the semantics the compiler assigned to your program, it emits a series of CPU instructions that implement these semantics. These instructions are generally saved into either intermediate object files that must later be linked together, or directly into the final executable. D's compile-time capabilities are mainly of two categories: i) AST manipulation: templates, static if, static foreach, and pragma(msg) belong to this category. This generally happens between steps (a) and (b). ii) CTFE (compile-time function evaluation): this happens somewhere around step (c), and mainly consists of the compiler interpreting part of the program using an internal interpreter in order to compute the value of a function at compile-time. This is triggered when this value is required in order to resolve something that's needed during compilation. For more details, see: https://wiki.dlang.org/Compile-time_vs._compile-time T -- If you think you are too small to make a difference, try sleeping in a closed room with a mosquito. -- Jan van Steenbergen