Turbo Pascal

Turbo Pascal Compiler Source Code

Turbo Pascal combines the simplicity of Object Pascal language with the power and efficiency of C++. Simple intuitive syntax, efficient generated code and easy development contributed to the popularity of Borland Turbo Pascal and Pascal programming language in general. While Pascal programming laguage was designed and used to teach structured programmning, Turbo Pascal went far beyond teaching. Borland advertised it as the fastest way to learn object-oriented programming. And they were right. Turbo Pascal became the most popular Pascal compiler ever.

If you are interested in the secrets of this legendary compiler or you would like to make your own compiler then you came to the right place. Get the complete source code of the Turbo Pascal compiler written in Turbo Pascal:


Compiler Units

turbo pascal 7 box
  • Symbol Tables
    Functions to create, manage and search symbol tables using hash functions
  • Type Definitions
    Functions to create and check typr definitions
  • Scanner
    Functions to process source code, skip comments, process compiler directives and extract tokens
  • Parser
    Functions to process stream of tokens, check syntax and generate intermediate code
  • Expressions
    Functions to process Turbo Pascal expressions and mathematical operations
  • Assembler
    Functions to process assembly instructions
  • Object Files
    Functions to import and process OMF object files
  • Code Generator
    Functions to process intermediate code
  • System Functions
    Processing of built-in system functions
  • System Procedures
    Processing of built-in system procedures
  • Linker
    Functions to link modules and to create the executable file

Every compiler is a combination of many sophisticated algorithms and complex data structures. With right approach you can achieve fast compilation and optimized code. If you have ever tried to write a compiler then you know what right algorithms and data structures mean. There are many excellent books on compiler construction. However, Turbo Pascal compiler source code is the best book on compiler design. Why starting from scratch when you can have the source code of a working compiler? And not just any compiler - Turbo Pascal is famous for compilation speed. Interested in the secrets of the legendary compiler?

Below you can find few code snippets from the Turbo Pascal compiler.
Procedure TExpression.ProcessExpression;
Var RightSimpleExpression: TExpression;
    Operation: TCalcOperation;
  Case Token of
    Token_Equal:          Operation := Calc_IsEqual;
    Token_NotEqual:       Operation := Calc_IsNotEqual;
    Token_Greater:        Operation := Calc_IsGreater;
    Token_Lower:          Operation := Calc_IsLower;
    Token_GreaterOrEqual: Operation := Calc_IsGreaterOrEqual;
    Token_LowerOrEqual:   Operation := Calc_IsLowerOrEqual;
    Token_IN:             Operation := Calc_IN;
    else begin
  If Operation = Calc_IN then GenerateCodeForOperator_IN (Self, RightSimpleExpression)
    else CalculateOperation (Operation, Self, RightSimpleExpression);
Turbo Pascal compatible compiler presented at TurboPascal.org is written in Turbo Pascal, compiles Turbo Pascal 7 source code and generates the same executable files as the original Borland compiler. It also uses exactly the same data structures and algorithms as the original Turbo Pascal 7 compiler.
Procedure ProcessDeclarations;
    StatementCompilerSwitches := ModuleCompilerSwitches;
    Case Token of
      Token_CONST:     ProcessConstantDeclarations;
      Token_TYPE:      ProcessTypeDeclarations;
      Token_VAR:       ProcessVariableDeclarations;
      Token_PROCEDURE: ProcessProcedureDeclaration;
      Token_FUNCTION:  ProcessProcedureDeclaration;
      else If SourceType <> stUnitInterface then
        Case Token of
          Token_LABEL:       ProcessLabelDeclarations;
          Token_CONSTRUCTOR: ProcessProcedureDeclaration;
          Token_DESTRUCTOR:  ProcessProcedureDeclaration;
          else Exit;
        end else Exit;
  until False;
Before you decide if this is what you are looking for you can
check the code on the Turbo Pascal Compiler Internals website.
Of course, the original compiler was not written in Pascal but having all algorithms written in Pascal makes them easy to read and understand. You can get two versions of Turbo Pacal compiler:

  • TPC16
    This is the original Turbo Pacal compiler written in Turbo Pascal. It represents the original Borland command line compiler but has one legacy property: it relies heavily on the segment:offset addressing. If you are interested in the compiler internals then this is it.
  • TPC32
    This is the original Turbo Pacal compiler written in Delphi. It represents the original Borland command line compiler but doesn't rely on the segment:offset addressing. TPC32 is a Win32 command line application but still generates DOS executable code. If you are interested in compiler design or in making your own compiler then this is it.

Turbo Pascal Compiler

The best book on compiler design