Compilation

An efficient implementation of any language requires compiling it from a textual form into some kind of easily (and quickly) executable form. MiniD is no different.

Note that this part of the spec is not necessarily a binding contract for all implementations of MiniD. Other compilers may compile the source in a different way, using a single-pass compiler, adding layers of optimization, or even compiling it into a completely different format. There is also nothing precluding MiniD from being a natively compiled language.

Phases of Compilation

MiniD follows in D's steps by implementing the compiler in a multi-phase fashion. This form of compiler requires somewhat more memory than a single-pass compiler, but its modular design makes the code much easier to read, debug, and redesign.

Here are the phases:

  1. Lexical Analysis - The source code is read from an input stream and segmented up into tokens. Illegal characters and badly-formatted tokens (such as a float literal reading "4.5n6") are found and rejected in this phase.
  2. Syntactic Analysis - The token stream is parsed to form syntax trees. This phase determines the overall structure of the program and ensures that consecutive tokens make sense and are not just gibberish (like "local local x, . . . 5").
  3. Semantic Analysis and Code Generation - The syntax tree of the program is checked for consistency and validity. This checks references to local variables and ensures proper use of some constructs. At the same time, a stream of opcodes and data structures is generated which is readable by the MDVM (MiniD Virtual Machine).

Once the bytecode has been generated, it is either run directly from memory or saved to a module file for later use. For information on how the bytecode is executed, see Execution.