//==============================================================================
//  (textmacros, libraries, scopes, script importing)
//==============================================================================
unit Preprocessor;

//==============================================================================
//  INTERFACE
//==============================================================================
interface

function Run(sinput:string):string;

//==============================================================================
//  IMPLEMENTATION
//==============================================================================
implementation
uses Windows, SysUtils, TextOnly, JassLib, Structs, StringHash, vxFilesys,
     PseudoRandom, JASSerException, Zinc, ScopeStack, JStruct,
     Constants, ExternalUsage, TextMacro, JHInterface, JHInput, JHParams,
     ImportPaths, TextCommon, PreprocessorImport, TempUnit, InitializerFunctions,
     LibrariesAndScopes;


//==============================================================================
procedure LoadBlizzardJAndCommonJ;
begin
    JassLib.Reset();
    // process common.j and blizzard.j
    if( GetCommonJ() <> '') then begin
        ProStatus('Loading ('+GetCommonJ()+'): ');
        JassLib.parseFile(GetCommonJ());
        ProEnd();
    end;
    if( GetBlizzardJ <> '') then begin
        ProStatus('Loading ('+GetBlizzardJ()+'): ');
        JassLib.parseFile(GetBlizzardJ());
        ProEnd();
    end;
end;
                                             

//==============================================================================
//  RUN - main function for this stage
//==============================================================================
function Run(sinput:string):string;
var i: integer;
begin
    ProStatus('PREPROCESSING');
    LoadBlizzardJAndCommonJ();
    ExternalUsage.ResetExternalUsage();
    JHInput.Reset2();

    PreprocessorImport.Run(sinput);

    if (inputN<2) then raise Exception.Create('Input file seems too small / unclosed string issues');

    if(WARCITY_MODE) then begin
         RESULT:=PreprocessorImport.GetOutput();
         exit;
    end;

    ProStatus('  Textmacros: ');
    TextMacro.processTextMacros();
    ProEnd();

    if(MACRO_MODE) then begin
         RESULT:='';
         for i := 0 to inputN-1 do SWriteLn(RESULT, JHInput.Get(i));
         exit;
    end;

    ProStatus('  Zinc: ');
    Zinc.ProcessZinc();
    ProEnd();

    if(ZINC_MODE) then begin
         RESULT:='';
         for i := 0 to inputN-1 do SWriteLn(RESULT, JHInput.Get(i));
         exit;
    end;

    ProStatus('  Libraries & Scopes: ');
    // uses only input array
    LibrariesAndScopes.parse();
    LibrariesAndScopes.writeInOrder(Result);

    PreprocessorImport.ResetFlag();

    ProEnd();
end;

//==============================================================================
//  END
//==============================================================================
end.


