-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGinCompiler.pas
220 lines (181 loc) · 7.49 KB
/
GinCompiler.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
program GinCompiler;
{$apptype windows}
{$mainresource Resources\IconRes.res}
//TODO: Ñäåëàòü íîðìàëüíî try..except, try..finally
//TODO: Ñäåëàòü çíàê + äëÿ char è string
//TODO: Ðåàëèçîâàòü ïåðå÷èñëèìûé òèï
// type Months = (January,February,March,April,May,June,July,August,September,October,November,December);
uses
PABCSystem,
System,
System.IO,
System.Collections,
System.Collections.Generic,
CompilerIDE,
CompilerUnit,
CommonUnit;
const
LibSource = 'LibSource';
// %ERRORLEVEL%
ERROR_SUCCESS = 0;
ERROR_BAD_PARAMETERS = 2;
ERROR_OTHER = 4;
function WINAPI_AllocConsole: longword; external 'kernel32.dll' name 'AllocConsole';
procedure ColorWriteln(s: string; cl: ConsoleColor);
begin
// if COLOR_CMD then
// begin
var clRestore := Console.ForegroundColor;
Console.ForegroundColor := cl;
writeln(s);
Console.ForegroundColor := clRestore;
// end
// else writeln(s);
end;
procedure PrintBanner;
begin
ColorWriteln(Format('GinCompiler {0} (compiled with PABC.NET)', Version), ConsoleColor.Green);
end;
procedure PrintHelp;
begin
PrintBanner;
writeln;
ColorWriteln('Èñïîëüçîâàíèå:', ConsoleColor.White);
writelnFormat('{0} <ïàðàìåòðû_êîìïèëÿòîðà> [äèñê:][ïóòü]èìÿ_ôàéëà', Reflection.Assembly.GetEntryAssembly.ManifestModule.Name);
writeln;
ColorWriteln(' ÏÀÐÀÌÅÒÐÛ ÊÎÌÏÈËßÒÎÐÀ', ConsoleColor.White);
writeln('/help èëè /? : Âûâîäèò äàííóþ ïîìîùü è çàâåðøàåò ïðîãðàììó');
writeln;
writeln('/nologo : Çàïðåùàåò âûâîä ñîîáùåíèÿ î ïðîãðàììå');
writeln;
writeln('/codepage:<êîäîâàÿ_ñòðàíèöà>: Óêàçûâàåò, êàêóþ êîäîâóþ ñòðàíèöó ñëåäóåò.');
writeln(' èñïîëüçîâàòü ïðè îòêðûòèè èñõîäíûõ ôàéëîâ.');
writeln(' <êîäîâàÿ_ñòðàíèöà> = utf8|unicode');
writeln(' |ansi|<íîìåð_êîäèðîâêè>');
writeln;
writeln('/apptype:<windows|console> : Óêàçûâàåò ôîðìàò âûõîäíîãî ôàéëà');
writeln;
writeln('/out:<âûõîäíîé_ôàéë> : Îïðåäåëÿåò èìÿ âûõîäíîãî ôàéëà.');
writeln;
writeln('/define:<ñïèñîê_èìåí> : Îïðåäåëÿåò ñèìâîë(èìÿ) óñëîâíîé êîìïèëÿöèè.');
writeln(' Îêàçûâàåò òàêîå æå âëèÿíèå, ÷òî è èñïîëüçîâàíèå');
writeln(' äèðåêòèâû ïðåïðîöåññîðà {define <èìÿ>}, çà');
writeln(' èñêëþ÷åíèåì òîãî, ÷òî ïàðàìåòð êîìïèëÿòîðà');
writeln(' âëèÿåò íà âñå ôàéëû ïðîåêòà.');
writeln(' Ïåðå÷èñëåíèå ÷åðåç ","');
writeln;
writeln('/reference:<ñïèñîê_ñáîðîê> : Ññûëàåòñÿ íà ìåòàäàííûå èç óêàçàííûõ ');
writeln(' ôàéëîâ ñáîðêè. Ïåðå÷èñëåíèå ÷åðåç ","');
writeln(' Àíàëîã äèðåêòèâû êîìïèëÿòîðà $reference');
writeln;
writeln('/win32res:<win32_ðåñóðñû> : Óêàçûâàåò ðåñóðñû Win32(.res), êîòîðûå ñëåäóåò');
writeln(' âñòàâèòü â âûõîäíîé ôàéë. Ïåðå÷èñëåíèå ÷åðåç ","');
writeln(' Íóæåí ê ïðèìåðó åñëè ïðîãðàììå íóæíà èêîíêà');
writeln(' Àíàëîã äèðåêòèâû êîìïèëÿòîðà $win32res');
writeln;
writeln('/res:<ñïèñîê_ôàéëîâ> : Âêëþ÷àåò ýòè ôàéëû, êàê ðåñóðñû .NET Framework â');
writeln(' âûõîäíîé ôàéë. Ïåðå÷èñëåíèå ÷åðåç ",".');
writeln(' Àíàëîã äèðåêòèâû êîìïèëÿòîðà $res');
writeln;
writeln('Gardens Point LEX/optimize : Âêëþ÷àåò îïòèìèçàöèþ êîäà.');
writeln('/debug : Êîìïèëÿòîð áóäåò ãåíåðèðîâàòü .pdb ôàéëû');
writeln;
ColorWriteln('Ïðèìåðû:', ConsoleColor.White);
writelnFormat('{0} /nologo /codepage:ansi /define:"debug,win32" "C:\d i r\file.pas"', Path.GetFileNameWithoutExtension(Reflection.Assembly.GetEntryAssembly.ManifestModule.Name));
writelnFormat('{0} /apptype:windows /reference:"System.Windows.Forms.dll" file.pas', Reflection.Assembly.GetEntryAssembly.ManifestModule.Name);
readln;
end;
var
CmdLineDict: Dictionary<string, string>;
procedure WriteCompilerMessage(Message: MessageInfo);
begin
writeln(Message.Text);
end;
begin
System.Environment.ExitCode := ERROR_OTHER;
// ˸ãêèé ïàðñèíã êîìàíäíîé ñòðîêè
CmdLineDict := new Dictionary<string, string>;
foreach s: string in CommandLineArgs do
begin
var arr := s.Split(':');
if arr.Length = 1 then
CmdLineDict.Add(arr[0].ToLower, '')
else if arr.Length = 2 then
CmdLineDict.Add(arr[0].ToLower, arr[1]);
end;
if (CommandLineArgs.Length = 0) or (CmdLineDict.Count > CommandLineArgs.Length)
or CmdLineDict.ContainsKey('/?') or CmdLineDict.ContainsKey('/help')
or CmdLineDict.ContainsKey('help') then
begin
PrintHelp;
Environment.&Exit(ERROR_BAD_PARAMETERS);
end;
if IO.&File.Exists(CommandLineArgs[CommandLineArgs.Length - 1]) then Options.SourceFile := Path.GetFullPath(CommandLineArgs[CommandLineArgs.Length - 1]);
Options.Optimize := CmdLineDict.ContainsKey('/optimize');
Options.Debug := CmdLineDict.ContainsKey('/debug');
if CmdLineDict.ContainsKey('/codepage') then
begin
var CodepageStr := CmdLineDict['/codepage'].ToLower;
if CodepageStr = 'utf8' then
Options.Codepage := Text.Encoding.UTF8
else if CodepageStr = 'ansi' then
Options.Codepage := Text.Encoding.GetEncoding(1251)
else if CodepageStr = 'unicode' then
Options.Codepage := Text.Encoding.Unicode
else
begin
var CodepageID: integer;
if TryStrToInt(CodepageStr, CodepageID) then
begin
Options.Codepage := Text.Encoding.GetEncoding(CodepageID);
end
end;
if Options.Codepage = nil then raise new Exception(String.Format('êîäèðîâêà, ñâÿçàííàÿ ñ èäåíòèôèêàòîðîì "{0}" íå ñóùåñòâóåò', CodepageStr));
end;
if CmdLineDict.ContainsKey('/out') then
Options.OutFile := Path.GetFullPath(CmdLineDict['/out'])
else
Options.OutFile := Path.ChangeExtension(Options.SourceFile, '.exe');
if CmdLineDict.ContainsKey('/apptype') then
begin
Options.CompilerDirectives.Add(new CompilerDirective('apptype', CmdLineDict['/apptype'].ToLower, new Location));
end;
if CmdLineDict.ContainsKey('/define') then
begin
var arr := CmdLineDict['/define'].Split(',');
foreach s: string in arr do
Options.DefineList.Add(s.ToLower);
end;
if CmdLineDict.ContainsKey('/reference') then
begin
var arr := CmdLineDict['/reference'].Split(',');
foreach s: string in arr do
Options.CompilerDirectives.Add(new CompilerDirective('reference', s.ToLower, new Location));
end;
if CmdLineDict.ContainsKey('/win32res') then
begin
var arr := CmdLineDict['/win32res'].Split(',');
foreach s: string in arr do
Options.CompilerDirectives.Add(new CompilerDirective('win32res', s.ToLower, new Location));
end;
if CmdLineDict.ContainsKey('/res') then
begin
var arr := CmdLineDict['/res'].Split(',');
foreach s: string in arr do
Options.CompilerDirectives.Add(new CompilerDirective('res', s.ToLower, new Location));
end;
if CmdLineDict.ContainsKey('/gui') then
begin
// Âêëþ÷àåì òåìó WinXP
System.Windows.Forms.Application.EnableVisualStyles;
// Çàïóñêàåì ïðèëîæåíèå
System.Windows.Forms.Application.Run(new EditorForm);
end else
begin
if not CmdLineDict.ContainsKey('/nologo') then PrintBanner;
var Cmp := new Compiler;
Cmp.Compile(WriteCompilerMessage);
WINAPI_AllocConsole;
end;
Environment.&Exit(ERROR_SUCCESS);
end.