source: trunk/IDE/Core.pas

Last change on this file was 77, checked in by chronos, 6 months ago
  • Modified: Compiler targets moved into modules.
File size: 9.8 KB
Line 
1unit Core;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, LastOpenedList, Project, ApplicationInfo,
7 Compiler, Registry, RegistryEx, DebugLog, Translator, Target,
8 SourceCodePascal, ModularSystem;
9
10type
11 { TCustomCompiler }
12
13 TCustomCompiler = class(TCompiler)
14 private
15 procedure InitSystemModule;
16 public
17 StartTime: TDateTime;
18 SourceFiles: TSourceFileManager;
19 TargetFiles: TSourceFileManager;
20 procedure Init; override;
21 function ElapsedTime: TDateTime;
22 procedure LoadFromRegistry(Root: HKEY; const Key: string);
23 procedure SaveToRegistry(Root: HKEY; const Key: string);
24 constructor Create; override;
25 destructor Destroy; override;
26 end;
27
28 { TCore }
29
30 TCore = class(TDataModule)
31 ApplicationInfo: TApplicationInfo;
32 Translator1: TTranslator;
33 DebugLog1: TDebugLog;
34 LastOpenedFiles: TLastOpenedList;
35 ModuleManager1: TModuleManager;
36 procedure DataModuleCreate(Sender: TObject);
37 procedure DataModuleDestroy(Sender: TObject);
38 procedure LastOpenedFilesChange(Sender: TObject);
39 private
40 procedure ProjectChange(Sender: TObject);
41 function LoadSourceFile(FileName: string; var Content: string): Boolean;
42 function SaveSourceFile(FileName: string; const Content: string): Boolean;
43 public
44 ReopenLastOpenedFile: Boolean;
45 LogParsing: Boolean;
46 Project: TProject;
47 ProjectTemplates: TProjectTemplates;
48 TargetProject: TProject;
49 Compiler: TCustomCompiler;
50 ModuleManager: TModuleManager;
51 procedure ProjectTemplatesInit;
52 procedure ProjectOpen(FileName: string);
53 procedure ProjectNew;
54 procedure CompilerDebugLog(Text: string);
55 procedure LoadFromRegistry(Root: HKEY; const Key: string);
56 procedure SaveToRegistry(Root: HKEY; const Key: string);
57 procedure RegisterIDEModules;
58 procedure RegisterCompilerModules;
59 end;
60
61var
62 Core: TCore;
63
64
65implementation
66
67{$R *.lfm}
68
69uses
70 FormMain, ProjectTemplates, IDEModulePascal, ModulePascal, ModuleGCC,
71 ModuleInterpretter, ModuleDelphi, ModulePHP, ModuleJava, ModuleASM8051,
72 ModuleXML, ModuleNASM;
73
74{ TCore }
75
76procedure TCore.ProjectOpen(FileName: string);
77begin
78 ProjectNew;
79 Project.LoadFromFile(FileName);
80 LastOpenedFiles.AddItem(FileName);
81 FormMain.FormMain.UpdateInterface;
82end;
83
84procedure TCore.ProjectNew;
85begin
86 if Assigned(Project) then FreeAndNil(Project);
87 Project := TProject.Create;
88 Project.FileName := SNewProject;
89 Project.OnChange := ProjectChange;
90end;
91
92procedure TCore.CompilerDebugLog(Text: string);
93begin
94 DebugLog1.Add('', Text);
95end;
96
97procedure TCore.DataModuleCreate(Sender: TObject);
98begin
99 DeleteFile(DebugLog1.FileName);
100 Compiler := TCustomCompiler.Create;
101 //Compiler.OnLoadSource := LoadSourceFile;
102 //Compiler.OnSaveTarget := SaveSourceFile;
103 Project := TProject.Create;
104 ProjectTemplates := TProjectTemplates.Create;
105 TargetProject := TProject.Create;
106 LastOpenedFiles := TLastOpenedList.Create(nil);
107 LastOpenedFiles.OnChange := LastOpenedFilesChange;
108
109 ReopenLastOpenedFile := True;
110 LogParsing := False;
111
112 ModuleManager := TModuleManager.Create(nil);
113 RegisterIDEModules;
114
115 RegisterCompilerModules;
116end;
117
118procedure TCore.DataModuleDestroy(Sender: TObject);
119begin
120 FreeAndNil(ModuleManager);
121 FreeAndNil(ProjectTemplates);
122 FreeAndNil(LastOpenedFiles);
123 FreeAndNil(TargetProject);
124 FreeAndNil(Project);
125 FreeAndNil(Compiler);
126end;
127
128procedure TCore.ProjectTemplatesInit;
129begin
130 ProjectTemplates.AddTemplate(TProjectTemplateConsoleApp.Create);
131 ProjectTemplates.AddTemplate(TProjectTemplateGUIApp.Create);
132 ProjectTemplates.AddTemplate(TProjectTemplatePackage.Create);
133 ProjectTemplates.AddTemplate(TProjectTemplateUnit.Create);
134end;
135
136procedure TCore.LastOpenedFilesChange(Sender: TObject);
137begin
138 LastOpenedFiles.LoadToMenuItem(FormMain.FormMain.MenuItemOpenRecent,
139 FormMain.FormMain.OpenRecentClick);
140 LastOpenedFiles.LoadToMenuItem(FormMain.FormMain.PopupMenu1.Items,
141 FormMain.FormMain.OpenRecentClick);
142end;
143
144procedure TCore.ProjectChange(Sender: TObject);
145begin
146 FormMain.FormMain.UpdateInterface;
147end;
148
149function TCore.LoadSourceFile(FileName: string; var Content: string): Boolean;
150begin
151end;
152
153function TCore.SaveSourceFile(FileName: string; const Content: string): Boolean;
154begin
155end;
156
157procedure TCore.LoadFromRegistry(Root: HKEY; const Key: string);
158begin
159 with TRegistryEx.Create do
160 try
161 RootKey := Root;
162 OpenKey(Key, True);
163 if ValueExists('ReopenLastOpenedFile') then
164 ReopenLastOpenedFile := ReadBool('ReopenLastOpenedFile')
165 else ReopenLastOpenedFile := True;
166 if ValueExists('TargetName') then
167 Compiler.Target := Compiler.Targets.SearchBySysName(ReadString('TargetName'))
168 else Compiler.Target := nil;
169 if ValueExists('LogParsing') then
170 LogParsing := ReadBool('LogParsing')
171 else LogParsing := False;
172 if ValueExists('LanguageCode') then
173 Translator1.Language := Translator1.Languages.SearchByCode(ReadString('LanguageCode'))
174 else Translator1.Language := Translator1.Languages.SearchByCode('');
175 finally
176 Free;
177 end;
178 LastOpenedFiles.LoadFromRegistry(TRegistryContext.Create(Root, Key + '\LastOpenedFiles')); //Root, Key + '\LastOpenedFiles');
179 Compiler.LoadFromRegistry(Root, Key + '\Compiler');
180 FormMain.FormMain.LoadFromRegistry(Root, Key);
181end;
182
183procedure TCore.SaveToRegistry(Root: HKEY; const Key: string);
184begin
185 with TRegistryEx.Create do
186 try
187 RootKey := Root;
188 OpenKey(Key, True);
189 WriteBool('ReopenLastOpenedFile', ReopenLastOpenedFile);
190 if Assigned(Compiler.Target) then
191 WriteString('TargetName', Compiler.Target.SysName)
192 else WriteString('TargetName', '');
193 WriteBool('LogParsing', LogParsing);
194 if Assigned(Translator1.Language) and (Translator1.Language.Code <> '') then
195 WriteString('LanguageCode', Translator1.Language.Code)
196 else WriteString('LanguageCode', '');
197 finally
198 Free;
199 end;
200 LastOpenedFiles.SaveToRegistry(TRegistryContext.Create(Root, Key + '\LastOpenedFiles'));
201 Compiler.SaveToRegistry(Root, Key + '\Compiler');
202 FormMain.FormMain.SaveToRegistry(Root, Key);
203end;
204
205procedure TCore.RegisterIDEModules;
206begin
207 ModuleManager.RegisterModule(TIDEModulePascal.Create(nil));
208end;
209
210procedure TCore.RegisterCompilerModules;
211begin
212 with Compiler.ModuleManager do begin
213 RegisterModule(TModulePascal.Create(nil));
214 RegisterModule(TModuleGCC.Create(nil));
215 RegisterModule(TModuleInterpretter.Create(nil));
216 RegisterModule(TModuleDelphi.Create(nil));
217 RegisterModule(TModulePHP.Create(nil));
218 RegisterModule(TModuleJava.Create(nil));
219 RegisterModule(TModuleASM8051.Create(nil));
220 RegisterModule(TModuleXML.Create(nil));
221 RegisterModule(TModuleNASM.Create(nil));
222 Modules.Perform(maEnable);
223 Modules.Perform(maStart);
224 end;
225end;
226
227{ TCustomCompiler }
228
229procedure TCustomCompiler.InitSystemModule;
230var
231 SystemModule: TSourceModule;
232begin
233 SystemModule := TSourceModule.Create;
234 with SystemModule do begin
235 Name := 'System';
236 Internal := True;
237 with Body.Types.AddNew do begin
238 Name := 'Byte';
239 Size := 1;
240 Internal := True;
241 end;
242 with Body.Types.AddNew do begin
243 Name := 'ShortInt';
244 Size := 1;
245 Internal := True;
246 end;
247 with Body.Types.AddNew do begin
248 Name := 'Word';
249 Size := 2;
250 Internal := True;
251 end;
252 with Body.Types.AddNew do begin
253 Name := 'SmallInt';
254 Size := 2;
255 Internal := True;
256 end;
257 with Body.Types.AddNew do begin
258 Name := 'Cardinal';
259 Size := 4;
260 Internal := True;
261 end;
262 with Body.Types.AddNew do begin
263 Name := 'Integer';
264 Size := 4;
265 Internal := True;
266 end;
267 with Body.Types.AddNew do begin
268 Name := 'UInt64';
269 Size := 8;
270 Internal := True;
271 end;
272 with Body.Types.AddNew do begin
273 Name := 'Int64';
274 Size := 8;
275 Internal := True;
276 end;
277 with Body.Functions.AddNew do begin
278 Name := 'WriteLn';
279 Internal := True;
280 end;
281 ParentProgram := AbstractCode;
282 end;
283 AbstractCode.Modules.Add(SystemModule);
284end;
285
286procedure TCustomCompiler.Init;
287begin
288 inherited;
289 StartTime := Now;
290 InitSystemModule;
291end;
292
293function TCustomCompiler.ElapsedTime: TDateTime;
294begin
295 Result := Now - StartTime;
296end;
297
298procedure TCustomCompiler.LoadFromRegistry(Root: HKEY; const Key: string);
299var
300 I: Integer;
301begin
302 with TRegistryEx.Create do
303 try
304 RootKey := Root;
305 for I := 0 to Targets.Count - 1 do
306 with TTarget(Targets[I]) do begin
307 OpenKey(Key + '\Targets\' + Name, True);
308 if ValueExists('CompilerPath') then
309 Producer.CompilerPath := ReadString('CompilerPath');
310 end;
311 OpenKey(Key, True);
312 if ValueExists('SelectedTarget') then
313 Target := Targets.SearchBySysName(ReadString('SelectedTarget'))
314 else Target := nil;
315 finally
316 Free;
317 end;
318end;
319
320procedure TCustomCompiler.SaveToRegistry(Root: HKEY; const Key: string);
321var
322 I: Integer;
323begin
324 with TRegistryEx.Create do
325 try
326 RootKey := Root;
327 for I := 0 to Targets.Count - 1 do
328 with TTarget(Targets[I]) do begin
329 OpenKey(Key + '\Targets\' + Name, True);
330 if Assigned(Producer) then begin
331 if Producer.CompilerPath <> '' then WriteString('CompilerPath', Producer.CompilerPath)
332 else WriteString('CompilerPath', '');
333 end;
334 end;
335 OpenKey(Key, True);
336 if Assigned(Target) then WriteString('SelectedTarget', Target.SysName)
337 else WriteString('SelectedTarget', '');
338 finally
339 Free;
340 end;
341end;
342
343constructor TCustomCompiler.Create;
344begin
345 inherited Create;
346 SourceFiles := TSourceFileManager.Create;
347 TargetFiles := TSourceFileManager.Create;
348 OnLoadSource := SourceFiles.GetFileContent;
349 OnSaveTarget := TargetFiles.SetFileContent;
350end;
351
352destructor TCustomCompiler.Destroy;
353begin
354 FreeAndNil(TargetFiles);
355 FreeAndNil(SourceFiles);
356 inherited;
357end;
358
359end.
360
Note: See TracBrowser for help on using the repository browser.