pascalabcnet/pabcnetc/ConsoleCompiler.cs
AlexanderZemlyak 0c29ac92b2
Refactoring of Compiler.cs (#2984)
* Add first comments

* Finish commenting for Compile and CompileUnit

* Write TODO sections

* Add a few clarifications

* splitted some functions from compile

* Written some methods from Compile to functions

* Update variable names

* Refactor - stage 1

Refactor GetUsesSection and IsPossibleNameSpace

* Refactor - stage 2

Rename a few functions and variables

* Correct an inaccuracy

* Added comments, look through CompileUnit

* Rename a few functions and add new comments

* Split CompileUnit to Subfunctions

Add IsUnitCompiled, IsUnitInPCU, InitializeNewUnit, GetSourceCode, GenSyntaxTree, GenUnitDocumentation, CheckDLLDirectiveOnlyForLibraries, MatchErrorsToBadNodes, CheckIfUnitModule, SetUseDLLForSystemUnits,
CompileInterfaceDependencies, CompileCurrentUnitInterface, GetImplementationUsesSection, CompileImplementationDependencies, CompileCurrentUnitImplementation

* Added some TODOs

* Return uses_unit_in original name

Renaming of syntax tree nodes leads to internal compiler errors

* Extract GenerateILCode method

* Add checking if recompilation needed method

Needs to be discussed and revised

* Add functions for catch blocks in Compile

* Extract building semantic tree method

Creating main function to be moved to another class

* Rename UnitsSortedList

* Edit CompileUnitsFromDelayedList method

* Change a few variable names, make CreateRCFile function and add comments

* Make code more "user-friendly"

* Add TODOs 31.11.23

* Create PrebuildSemanticTreeActionsMethod

* Refactor semantic checks section in initialize new unit method

* Refactor Adding standard units to uses method

* Return file_name and compiler_directives original names to avoid internal compilation errors

* Refactor GetReferences Method

* Initial refactoring of IncludeNamespaces function

* Create three more methods and wrap important code in regions

* Add TODO's

* Resolve merge conflicts

* Add returned value to ConstructSyntaxTree method

* Fix UnitsSortedList NotFoundError in PCUWriter

* Workaround commit

* Update PABCSystem after tests' changes

* Rename syntax trees in some methods

* Delete CurrentSyntaxUnit variable

* Revert unnecessary project files changes

* Squashed commit of the following:

commit f4d7599f1a39252feac97bd5391f46dfdc25f6f4
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:29 2023 +0300

    added links to identarranger

commit 3bd5d33e2b2969884e61a3279ff9c353013b57fe
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:03 2023 +0300

    Change extension for verybasic to yavb

commit 61294c6e7d405e1c68516cc81d3a109e8a1ee295
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:33:57 2023 +0300

    Change Program Example

commit 86971488341ed6bf13c39e13a513d7ac0c51c285
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:28:59 2023 +0300

    Add IndentArranger to Compile

commit 2ce50bbbf9db22c4243b247b870f6935ce206d26
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 17 10:00:31 2023 +0300

    Add Semicolon After Each Statement

commit 796309d340e8d8ba730ff9418fa376f34fb7fd36
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Wed Nov 15 18:49:11 2023 +0300

    Add Alpha Version of Python-style If-statement

commit 5eb88f946fe8254b4f5c5a56ed0a567b3be51227
Merge: ab4ce5b0 0162b637
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:54:26 2023 +0300

    Merge branch 'IndentArranger' into VeryBasicLanguage

commit ab4ce5b0e32d42004726e3dc45cc0d8a65e53f56
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:53:51 2023 +0300

    Fixes from seminar

commit af74012289d0d08517c13499a2a66cb543fc06d2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 18:57:29 2023 +0300

    finally working!

    fully implemented compatibility

commit 58a39c313324b468d1eec207ba3f5f6eddf74ef2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 11:58:16 2023 +0300

    more compatibility with pabc

    Now verybasic statements translate to pascal compiler
    added .bat script for autobuilding verybasic

commit 0162b6376b8fe970704f26213bf9f0f670dfb12e
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:06:14 2023 +0300

    Update test.txt

commit 1b759ab1cd7ee2ffeb14afff0418ddf0f30b0399
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:05:36 2023 +0300

    Add Indent and Unindent Keywords to Generated File

commit cabda7f3985651cff2a8740f1a5bdea8fe7ac892
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 10 21:25:52 2023 +0300

    Add Generation of Output File

commit 306f033d0176ab559e3622b8505427dbe2e0e203
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:42:18 2023 +0300

    Update IndentArranger.sln

commit 60c0ceced1aa3a7d2e33de0facb1126e295f43b5
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:41:22 2023 +0300

    Add IndentArranger

commit 1f4556c4573dae154fcc167b41ff6ab9e8122136
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Mon Nov 6 22:48:36 2023 +0300

    Made my own VeryBasicParser project

    Copied some code from SaushkinParser
    Tried to compile it and implement into Pascal
    Doesn't work due to grammatik issue

* Squashed commit of the following:

commit 4e73d9ac3ffef68312f06a74dc83afffcf3ccbee
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 15:08:10 2023 +0300

    Fixed GPPG (i think so at least)

commit e5cfc220828b37fb2f35e565683019716ec3f0ce
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:44:27 2023 +0300

    Update Compiler.cs

commit 4698e2e75a5f3b4f523a8bc7733a50e8abb4fca1
Merge: 22aaf2b2 f4d7599f
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:54 2023 +0300

    Merge branch 'IndentArrangerTemp' into VeryBasicLanguage

commit f4d7599f1a39252feac97bd5391f46dfdc25f6f4
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:29 2023 +0300

    added links to identarranger

commit 3bd5d33e2b2969884e61a3279ff9c353013b57fe
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:43:03 2023 +0300

    Change extension for verybasic to yavb

commit 22aaf2b2508278a9ffce525ffed52419bf72c23f
Merge: c326174f 61294c6e
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 22 11:26:36 2023 +0300

    Merge branch 'IndentArrangerTemp' into VeryBasicLanguage

commit 61294c6e7d405e1c68516cc81d3a109e8a1ee295
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:33:57 2023 +0300

    Change Program Example

commit 86971488341ed6bf13c39e13a513d7ac0c51c285
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Tue Nov 21 09:28:59 2023 +0300

    Add IndentArranger to Compile

commit c326174ff5ecccf9c4f76d58aea4653f047af049
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 19 15:03:19 2023 +0300

    Added UniversalParserHelper and GPPG

    ShiftReduceParser moved to another project
    UniversalParserHelper project added, most of it copied from SaushkinParser

commit 2ce50bbbf9db22c4243b247b870f6935ce206d26
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 17 10:00:31 2023 +0300

    Add Semicolon After Each Statement

commit 796309d340e8d8ba730ff9418fa376f34fb7fd36
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Wed Nov 15 18:49:11 2023 +0300

    Add Alpha Version of Python-style If-statement

commit 5eb88f946fe8254b4f5c5a56ed0a567b3be51227
Merge: ab4ce5b0 0162b637
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:54:26 2023 +0300

    Merge branch 'IndentArranger' into VeryBasicLanguage

commit ab4ce5b0e32d42004726e3dc45cc0d8a65e53f56
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Wed Nov 15 16:53:51 2023 +0300

    Fixes from seminar

commit af74012289d0d08517c13499a2a66cb543fc06d2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 18:57:29 2023 +0300

    finally working!

    fully implemented compatibility

commit 58a39c313324b468d1eec207ba3f5f6eddf74ef2
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Sun Nov 12 11:58:16 2023 +0300

    more compatibility with pabc

    Now verybasic statements translate to pascal compiler
    added .bat script for autobuilding verybasic

commit 0162b6376b8fe970704f26213bf9f0f670dfb12e
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:06:14 2023 +0300

    Update test.txt

commit 1b759ab1cd7ee2ffeb14afff0418ddf0f30b0399
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Sun Nov 12 10:05:36 2023 +0300

    Add Indent and Unindent Keywords to Generated File

commit cabda7f3985651cff2a8740f1a5bdea8fe7ac892
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Fri Nov 10 21:25:52 2023 +0300

    Add Generation of Output File

commit 306f033d0176ab559e3622b8505427dbe2e0e203
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:42:18 2023 +0300

    Update IndentArranger.sln

commit 60c0ceced1aa3a7d2e33de0facb1126e295f43b5
Author: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Date:   Thu Nov 9 20:41:22 2023 +0300

    Add IndentArranger

commit 1f4556c4573dae154fcc167b41ff6ab9e8122136
Author: Владислав Крылов <krylov@sfedu.ru>
Date:   Mon Nov 6 22:48:36 2023 +0300

    Made my own VeryBasicParser project

    Copied some code from SaushkinParser
    Tried to compile it and implement into Pascal
    Doesn't work due to grammatik issue

* Changed GPPG project NET Framework version, added .dll to gitignore

* Adding UniversalParserHelper to project, trying to include VeryBasic

* Managed dependencies and got VeryBasicLanguage to work

* Change extension of a test program

* Rebuild changes

What should be added to .gitignore?

* Fix bug related to err0524_res_unit.pas

* Rebuild Parser

* Change Indent and Unindent tokens

* Add Symbol Table to ParserABC.y

* Change Indent Space Number to 2

* Add While Loop

* Add Some Operations to Parser

* Make Initialization Node at the Beginning of a program

* Create Grammar.txt

* Test program added

* Add ELIF and SyntaxHighlight

* Fix TableSymbol

* Add Division

* Add Method Call

* Add For Loop

* Rename SPython Parser Folder

* Add documented comments for CompileUnit method

* Added Errors to SPython

Added Errors.cs
Removed link to PABCSaushkinParser
Minor fixes

* Create default constructor for SourceContext

* Move null check of currentUnit to upper level in CompileUnit

* Move CreateMainFunction method from Compiler to TreeConverter class

* Add gppg and parserhelper to visualpascalabcnet dependencies

* Updated installer files to include GPPG and UniversalParserHelper

* Rename GPPG to ShiftReduceParser

* Fix ShiftReduceParser project dependencies

* Fix ShiftReduceParserDependencies second iteration

* Workaround commit

* Update PABCSystem after tests' changes

* Refactor ConvertDirectives method

* Get rid of legacy standard modules code

* Return varBeginOffset and beginOffset calculation to Compiler class

Размещение метода в SyntaxTreeToSemanticTreeConverter не целесообразно. В комментарии видимо имелось в виду что-то другое.

* Workaround commit

* Update PABCSystem after tests' changes

* Revert SPython changes

Оставляем только изменения связанные с рефакторингом.

* Update .gitignore

Co-authored-by: Sun Serega <sunserega2@gmail.com>

* Resolve a few Sun Serega treds

* Delete comments in ParsersController.cs

* Replace specific path with path variable in Studio.bat

* Add comment in Studio.bat file and return FileName in CompilerError.cs

* Fix TreeSubsidiary.cs encoding and sectCore.nsh indents

* Return old version of TestRunner.exe

* Rename some variables and polish a few methods

* Uncomment accidentally commented code

* Replace spaces with tabs

* Changed dll name from GPPG

* Revert "Changed dll name from GPPG"

This reverts commit c485cc8cb787809b7e9dfa8a361e75f17ed39893.

* Update .gitignore

* Delete Libraries/ShiftReduceParser.dll

* Delete bin\ShiftReduceParser.dll

* Replace tabs with spaces

* Update encoding in Studio.bat

* Fix bug with PABCrtl excluded files

* Refactor StandardModule class

* Add null checks to make debuging easier

* Delete unnecessary null checks in SymTable.cs

---------

Co-authored-by: Владислав Крылов <krylov@sfedu.ru>
Co-authored-by: MovchanGitHub <92666028+MovchanGitHub@users.noreply.github.com>
Co-authored-by: Sun Serega <sunserega2@gmail.com>
2023-12-18 22:33:27 +03:00

526 lines
21 KiB
C#

// Copyright (©) Ivan Bondarev, Stanislav Mikhalkovich (for details please see \doc\copyright.txt)
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
using System;
using PascalABCCompiler.Errors;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
namespace PascalABCCompiler
{
/*class mc<T>
{
public static int meth()
{
return 3;
}
}*/
class ConsoleCompiler
{
private const int DefaultConsoleBufferWidth = 80;
private static string StringsPrefix = "PABCNETC_";
static bool short_output = true;
static string BlankString = "";
private static string StringResourcesGet(string Key)
{
return StringResources.Get(StringsPrefix + Key);
}
public static bool IgnoreNotSupportedError = false;
public static PascalABCCompiler.Compiler Compiler=null;
public static string FileName = "";
public static string TagertFileName = "";
public static string OutputDirectory = "";
public static bool RestartOnNewCompile = false;
public static bool DetailOut = false;
public static bool Rebuild=false;
public static bool NoConsole = false;
public static CompilerOptions.OutputType outputType;
public static bool Debug = true;
public static bool UseDll = false;
public static List<Error> GlobalErrorsList = new List<Error>();
public static DateTime StartTime;
public static uint AllLinesCompiled;
public static bool ScanSubdirs = false;
public static bool ExecuteCommand(string command)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
Console.ForegroundColor = ConsoleColor.Gray;
if (command == "" || command == null) return false;
if (command.ToLower().IndexOf("cd ") == 0)
{
try
{
Environment.CurrentDirectory = (new DirectoryInfo(command.Remove(0, 3))).FullName;
}
catch (Exception)
{
WriteErrorText(StringResourcesGet("ERROR_INVALID_DIRECTORY") + Environment.NewLine);
}
//Console.WriteLine("Current directory change to " + Environment.CurrentDirectory);
return true;
}
if (command.ToLower() == "reset")
{
//Console.Clear();
Reset();
return true;
}
if (command.ToLower().IndexOf("resetoncompile=")==0)
{
RestartOnNewCompile = command.IndexOf("1")>0;
Console.WriteLine("ResetOnCompile==" + RestartOnNewCompile);
return true;
}
if (command.ToLower().IndexOf("ignorense=") == 0)
{
IgnoreNotSupportedError = command.IndexOf("1") > 0;
Console.WriteLine("IgnoreNotSupportedError==" + IgnoreNotSupportedError);
return true;
}
if (command.ToLower().IndexOf("scansubdirs=") == 0)
{
ScanSubdirs = command.IndexOf("1") > 0;
Console.WriteLine("ScanSubdirs==" + ScanSubdirs);
return true;
}
if (command.ToLower().IndexOf("debug=") == 0)
{
Debug = command.IndexOf("1") > 0;
Console.WriteLine("Debug==" + Debug);
return true;
}
if (command.ToLower().IndexOf("usedll=") == 0)
{
UseDll = command.IndexOf("1") > 0;
Console.WriteLine("UseDll==" + UseDll);
return true;
}
if (command.ToLower().IndexOf("showallmessages=") == 0)
{
DetailOut = command.IndexOf("1") > 0;
Console.WriteLine("ShowAllMessages==" + DetailOut);
return true;
} if (command.ToLower().IndexOf("outdir=") == 0)
{
OutputDirectory = command.Remove(0, 7);
Console.WriteLine("OutDir==" + OutputDirectory);
return true;
}
if (command.ToLower().IndexOf("outtype=") == 0)
{
SetOutType(command.Remove(0, 8));
Console.WriteLine("OutType==" + outputType);
return true;
}
if (command.ToLower()=="clrscr")
{
Console.Clear();
return true;
}
if (command.ToLower() == "collect")
{
GC.Collect();
return true;
}
if (command.ToLower().IndexOf("rebuild=") == 0)
{
Rebuild = command.IndexOf("1") > 0;
Console.WriteLine("Rebuild==" + Rebuild);
return true;
}
if (command.ToLower().IndexOf("language=") == 0)
{
int i=command.IndexOf("=");
int n = Convert.ToInt32(command.Substring(i + 1, command.Length-i-1));
if (n < StringResourcesLanguage.AccessibleLanguages.Count && n >= 0)
{
StringResourcesLanguage.CurrentLanguageName = StringResourcesLanguage.AccessibleLanguages[n];
ExecuteCommand("ClrScr"); ExecuteCommand("?");
}
else
Console.WriteLine("Language=" + StringResourcesLanguage.CurrentLanguageName);
return true;
}
if (command == "?")
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(StringResourcesGet("HELP"));
for (int i = 0; i < StringResourcesLanguage.AccessibleLanguages.Count;i++)
Console.WriteLine(string.Format("\t{0}-{1}", i, StringResourcesLanguage.AccessibleLanguages[i]));
//int jjj = 5;
//bool bbb= jjj < mc<int>.meth();
return true;
}
if (ScanSubdirs)
StartScanDir(command, Environment.CurrentDirectory);
else
CompileAllFilesInDirectory(command, Environment.CurrentDirectory);
return true;
}
static int AllFilesCount;
static int SuccessFilesCount;
static void StartScanDir(string mask, string directory)
{
AllFilesCount = SuccessFilesCount = 0;
ScanDir(mask, directory);
Console.WriteLine(string.Format(StringResourcesGet("TOTAL{0}_SUCCESS{1}_PROCENT{2}"), AllFilesCount, SuccessFilesCount, Convert.ToInt32((double)SuccessFilesCount / (double)AllFilesCount * 100.0)));
}
static void ScanDir(string mask,string directory)
{
CompileAllFilesInDirectory(mask, directory);
DirectoryInfo[] dirs = (new DirectoryInfo(directory)).GetDirectories();
foreach (DirectoryInfo dinf in dirs)
if (directory.ToLower() != dinf.FullName)
ScanDir(mask, dinf.FullName);
}
static void CompileAllFilesInDirectory(string mask,string directory)
{
DirectoryInfo di = new DirectoryInfo(directory);
//Console.WriteLine("["+directory+"]");
FileInfo[] files;
try
{
files = di.GetFiles(mask);
}
catch (DirectoryNotFoundException)
{
WriteErrorText(StringResourcesGet("ERROR_DIRECTORY_NOT_FOUND") + Environment.NewLine);
return;
}
catch (ArgumentException)
{
WriteErrorText(StringResourcesGet("ERROR_INVALID_DIRECTORY") + Environment.NewLine);
return;
}
DateTime dt = DateTime.Now;
long length = 0;
GlobalErrorsList.Clear();
AllLinesCompiled = 0;
foreach (FileInfo fi in files)
{
CompileAssembly(fi.FullName, outputType, false);
AllLinesCompiled += Compiler.LinesCompiled;
length += fi.Length;
}
if (!short_output)
if (GlobalErrorsList.Count > 0 && files.Length > 1)
{
WriteErrorText(StringResourcesGet("FULL_ERROR_LIST") + Environment.NewLine);
for (int i = 0; i < GlobalErrorsList.Count; i++)
{
if (GlobalErrorsList[i] is ProgramModuleExpected || GlobalErrorsList[i] is ParserBadFileExtension)
WriteColorText("[" + i + "]", ConsoleColor.Green);
else
if (GlobalErrorsList[i] is SemanticError)
WriteColorText("[" + i + "]", ConsoleColor.Red);
else
WriteErrorText("[" + i + "]");
Console.ForegroundColor = ConsoleColor.Gray;
if (GlobalErrorsList[i] is LocatedError)
{
SourceLocation sl;
if ((sl = (GlobalErrorsList[i] as LocatedError).SourceLocation) != null)
Console.WriteLine(string.Format("[{0},{1}] {2}: {3}", sl.BeginPosition.Line, sl.BeginPosition.Column, Path.GetFileName(sl.FileName), GlobalErrorsList[i].Message));
else
Console.WriteLine(GlobalErrorsList[i]);
}
else
{
if(GlobalErrorsList[i] is CompilerInternalError)
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(GlobalErrorsList[i]);
}
}
}
Console.ForegroundColor = ConsoleColor.White;
if (short_output)
ClearLine();
Console.WriteLine(string.Format(StringResourcesGet("TOTAL{0}_FILES{1}_LINES_{2}_TIME{3}"), files.Length, (length / 1024), AllLinesCompiled, (DateTime.Now - dt).TotalMilliseconds));
}
public static void ClearLine()
{
if (NoConsole)
{
return;
}
Console.CursorLeft = 0;
Console.Write(BlankString);
Console.CursorLeft = 0;
}
public static int CompileAssembly(string FileName, CompilerOptions.OutputType outputType, bool PauseIfError)
{
if (RestartOnNewCompile)
Reset();
StartTime = DateTime.Now;
Console.ForegroundColor = ConsoleColor.White;
string msg = string.Format(StringResourcesGet("COMPILING_ASSEMBLY{0}"), System.IO.Path.GetFileName(FileName));
if (short_output)
{
ClearLine();
Console.Write(msg);
}
else
Console.WriteLine(msg);
DateTime ldt = DateTime.Now;
CompilerOptions co = new CompilerOptions(FileName, outputType);
if (FileName.ToLower().EndsWith(".pabcproj"))
co.ProjectCompiled = true;
if (OutputDirectory != "")
co.OutputDirectory=OutputDirectory;
co.Rebuild = Rebuild;
co.Debug = Debug;
co.UseDllForSystemUnits = UseDll;
AllFilesCount++;
if (Compiler.Compile(co) != null)
SuccessFilesCount++;
if (IgnoreNotSupportedError)
if ((Compiler.ErrorsList.Count == 1) && (Compiler.ErrorsList[0] is Errors.SemanticNonSupportedError))
{
Compiler.ErrorsList.Clear();
SuccessFilesCount++;
}
if (Compiler.ErrorsList.Count > 0)
{
//Console.Beep();
if (short_output) Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Gray;
for (int i = 0; i < Compiler.ErrorsList.Count; i++)
{
WriteErrorText("[" + i + "]");
if (Compiler.ErrorsList[i] is LocatedError)
{
SourceLocation sl;
if ((sl = (Compiler.ErrorsList[i] as LocatedError).SourceLocation) != null)
Console.WriteLine(string.Format("[{0},{1}] {2}: {3}", sl.BeginPosition.Line, sl.BeginPosition.Column, Path.GetFileName(sl.FileName), Compiler.ErrorsList[i].Message));
else
Console.WriteLine(Compiler.ErrorsList[i]);
}
else
{
if (Compiler.ErrorsList[i] is CompilerInternalError)
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(Compiler.ErrorsList[i]);
}
}
GlobalErrorsList.AddRange(Compiler.ErrorsList);
if (PauseIfError)
Console.ReadKey();
}
else
{
if (!short_output)
{
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(string.Format(StringResourcesGet("OK_{0}MS_{1}LINES"), (DateTime.Now - ldt).TotalMilliseconds, Compiler.LinesCompiled));
}
}
Console.ForegroundColor = ConsoleColor.Gray;
return Compiler.ErrorsList.Count;
}
private static void ChangeCompilerState(ICompiler sender, PascalABCCompiler.CompilerState State, string FileName)
{
switch (State)
{
case CompilerState.ParserConnected:
Console.WriteLine(string.Format(StringResourcesGet("CONNECTED_PARSER{0}"),sender.ParsersController.LastParser));
return;
}
if (DetailOut)
{
Console.ForegroundColor = ConsoleColor.Gray;
if (FileName != null)
{
FileName = string.Format("[{0}]{1} {2}...", Math.Round((DateTime.Now - StartTime).TotalMilliseconds), State, System.IO.Path.GetFileName(FileName));
StartTime = DateTime.Now;
Console.WriteLine(FileName);
//Console.Title = file_name;
}
else
{
Console.WriteLine(string.Format("{0}.", State));
//Console.Title = State.ToString();
}
}
}
public static void WriteColorText(string Text, ConsoleColor FGColor)
{
Console.ForegroundColor = FGColor;
Console.Write(Text);
Console.ResetColor();
}
public static void WriteErrorText(string Text)
{
WriteColorText(Text, ConsoleColor.Red);
}
public static void Reset()
{
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine(StringResourcesGet("RESTARTING_COMPILER"));
//StringResourcesLanguage.CurrentLanguageName = "–усский";
DateTime ldt = DateTime.Now;
Compiler = new PascalABCCompiler.Compiler(null,ChangeCompilerState);
//GC.Collect();
WriteColorText(Compiler.Banner + "\nCopyright (c) 2005-2023 by Ivan Bondarev, Stanislav Mikhalkovich\n", ConsoleColor.Green);
Console.WriteLine("OK {0}ms", (DateTime.Now - ldt).TotalMilliseconds);
if (Compiler.SupportedSourceFiles.Length == 0)
WriteColorText(StringResourcesGet("ERROR_PARSERS_NOT_FOUND")+Environment.NewLine, ConsoleColor.Red);
Compiler.InternalDebug.SkipPCUErrors = false;
}
public static void ShowConnectedParsers()
{
if (Compiler.SupportedSourceFiles.Length > 0)
{
Console.Write(StringResourcesGet("CONNECTED_PARSERS"));
foreach (PascalABCCompiler.SupportedSourceFile ssf in Compiler.SupportedSourceFiles)
Console.Write(ssf+"; ");
Console.WriteLine();
}
}
public static void ShowConnectedConversions()
{
if (Compiler.SemanticTreeConvertersController.SemanticTreeConverters.Count > 0)
{
Console.Write(StringResourcesGet("CONNECTED_CONVERSIONS"));
foreach (PascalABCCompiler.SemanticTreeConverters.ISemanticTreeConverter sc in Compiler.SemanticTreeConvertersController.SemanticTreeConverters)
Console.Write(sc.Name + "; ");
Console.WriteLine();
}
}
public static void SetOutType(string type)
{
type = type.ToLower();
if (type == "dll")
outputType = CompilerOptions.OutputType.ClassLibrary;
if (type == "exe")
outputType = CompilerOptions.OutputType.ConsoleApplicaton;
if (type == "winexe")
outputType = CompilerOptions.OutputType.WindowsApplication;
}
public static int Main(string[] initialArgs)
{
var args = initialArgs.ToList();
if (args.Remove("/noconsole"))
{
NoConsole = true;
}
PascalABCCompiler.StringResourcesLanguage.LoadDefaultConfig();
if (args.Count == 1 && args[0] == "commandmode")
{
return (new CommandConsoleCompiler()).Run();
}
else
{
CultureInfo ci = CultureInfo.InstalledUICulture;
if (StringResourcesLanguage.CurrentTwoLetterISO == "ru" && ci.TwoLetterISOLanguageName != "ru")
StringResourcesLanguage.CurrentLanguageName = StringResourcesLanguage.AccessibleLanguages[StringResourcesLanguage.TwoLetterISOLanguages.IndexOf("en")];
else
StringResourcesLanguage.CurrentLanguageName = StringResourcesLanguage.AccessibleLanguages[StringResourcesLanguage.TwoLetterISOLanguages.IndexOf(StringResourcesLanguage.CurrentTwoLetterISO)];
}
for (int i = 0; i < ConsoleBufferWidth-1; i++)
BlankString += " ";
Console.ForegroundColor = ConsoleColor.White;
OutputDirectory="";
Console.Title = StringResourcesGet("STARTING");
Reset();
Console.Title = Compiler.Banner;
//Console.CursorLeft = (Console.BufferWidth - Compiler.Banner.Length) / 2;
//Console.WriteLine(Compiler.Banner);
short_output = args.Count != 1;
if (args.Count >= 1)
{
FileName = args[0];
}
if (args.Count >= 2)
if (args[1] == "/rebuildnodebug")
{
Rebuild = true;
Compiler.InternalDebug.IncludeDebugInfoInPCU = false;
short_output = false;
DetailOut = true;
}
else
if (args[1] == "/rebuild")
{
Rebuild = true;
short_output = false;
DetailOut = true;
}
else
TagertFileName = args[1];
outputType = CompilerOptions.OutputType.ConsoleApplicaton;
if (args.Count >= 3)
SetOutType(args[2]);
ShowConnectedParsers();
ShowConnectedConversions();
if (args.Count >= 1)
{
return CompileAssembly(FileName, outputType, true);
}
ExecuteCommand("?");
while (true)
{
Console.WriteLine();
string Dir = Environment.CurrentDirectory; int dleng = 25;
Console.ForegroundColor = ConsoleColor.Gray;
if (Dir.Length > dleng)
Dir = string.Format("...{0}>", Dir.Substring(Dir.Length - dleng));
else
Dir = string.Format("{0}>", Dir);
Console.Write(Dir);
Console.ForegroundColor = ConsoleColor.Green;
if (!ExecuteCommand(Console.ReadLine()))
{
Console.ForegroundColor = ConsoleColor.Gray;
return 0;
}
}
}
private static int ConsoleBufferWidth
{
get { return NoConsole ? DefaultConsoleBufferWidth : Console.BufferWidth; }
}
/* public class MainClass
{
public static void Main(string[] args)
{
}
}*/
}
}