Turbo Pascal


Borland Turbo Pascal is definitely the most successful and popular pascal compiler ever. Powerful syntax, fast compilation, simple IDE and effective code contributed to the popularity of Pascal programming language and Turbo Pascal Compiler.

On this website you will find the source code of a complete Turbo pascal compiler - a Turbo pascal compiler written in Turbo Pascal. Of course this is not the original source code of the compiler since it was not written in Pascal, but a totally compatible compiler that will be able to compile Turbo Pascal source code and generate the same executable files as the original Borland compiler.

You will be able to see the internal functions, data structures, algorithms, tips and tricks used by Turbo Pascal. Every compiler is a symphony of data structures and algorithms and Turbo pascal is no exception.

Below you can find one example function from the Turbo Pascal compiler that will be published here.
Procedure CompileUnit;
Var CurrentUnitInstance: PUnitHeader;
begin
  SourceType := stUnitInterface;
  ExpectTokenAndGetNext (Token_UNIT);
  ExpectIdentifier;
  CreateUnitIdentiferSymbolTableAndStoreMainUnitName;
  GetNextToken;
  ExpectTokenAndGetNext (Token_Semicolon);
  ExpectTokenAndGetNext (Token_INTERFACE);
  SetModuleFlagsAndProcessUsedUnits;
  If not (cmoCompileOnlyInterface in CompilerModeOptions) or not IsLastUnitAlreadyLoaded (CurrentUnitInstance) then
    begin
      ProcessDeclarations;
      CalculateIdentifiersChecksum;
      SourceType := stUnitImplementation;
      ExpectTokenAndGetNext (Token_IMPLEMENTATION);
      ProcessUsedUnits;
      ProcessDeclarations;
      If Token = Token_BEGIN then ProcessMainProgramBlock         { Initialization part }
        else ExpectTokenAndGetNext (Token_END);
      CheckForPeriodAndModuleEnd;
      ImportObjectFiles;
      CheckForUndefined_FORWARD_Or_EXTERNAL (Ptr (SymbolTable [stProcedures].Segment, 8));
      RemovePrivateIdentifiersFromUnit;
      JoinSymbolTablesAndCreateUnit;
      CreateUnitFile;
    end else JoinSymbolTablesAndCreateUnit;
  CopySegmentsOfSymbolTablesToUnitHeader;
end;