source: tags/1.1.0/Target/UTargetC.pas

Last change on this file was 105, checked in by chronos, 6 years ago
  • Fixed: Resolved compiler warnings and notes.
  • Modified: Update packages.
File size: 3.8 KB
Line 
1unit UTargetC;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, UTarget, UBFTarget, Process, Dialogs,
9 LazFileUtils;
10
11type
12
13 { TTargetC }
14
15 TTargetC = class(TBFTarget)
16 private
17 function GetMemoryCell: string;
18 public
19 constructor Create; override;
20 procedure Compile; override;
21 procedure CompileToFile; override;
22 procedure Run; override;
23 end;
24
25
26implementation
27
28{ TTargetC }
29
30constructor TTargetC.Create;
31begin
32 inherited Create;
33 Name := 'C';
34 SourceExtension := '.c';
35 ImageIndex := 23;
36 Capabilities := [tcCompile, tcRun];
37 {$IFDEF Windows}
38 CompilerPath := 'c:\Program Files\MinGW\bin\gcc.exe';
39 CompiledExtension := '.exe';
40 RunExtension := '';
41 {$ENDIF}
42 {$IFDEF Linux}
43 CompilerPath := '/usr/bin/gcc';
44 CompiledExtension := '';
45 RunExtension := '';
46 {$ENDIF}
47end;
48
49function TTargetC.GetMemoryCell: string;
50begin
51 Result := 'Memory[Pos';
52 if FProgram[FProgramIndex].RelIndex > 0 then
53 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
54 else if FProgram[FProgramIndex].RelIndex < 0 then
55 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
56 Result := Result + ']';
57end;
58
59procedure TTargetC.Compile;
60begin
61 inherited;
62 Indent := 0;
63 FTargetCode := '';
64
65 AddLine('#include <stdio.h>');
66 AddLine('');
67 AddLine('int main()');
68 AddLine('{');
69 Inc(Indent);
70 AddLine('char Memory[' + IntToStr(MemorySize) + '];');
71 AddLine('int Pos;');
72 AddLine('char ReadChar;');
73 AddLine('');
74 AddLine('Pos = 0;');
75 FProgramIndex := 0;
76 while (FProgramIndex < Length(FProgram)) do begin
77 case FProgram[FProgramIndex].Command of
78 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
79 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
80 cmInc: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
81 cmDec: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
82 cmOutput: AddLine('putchar(' + GetMemoryCell + ');');
83 cmInput: AddLine(GetMemoryCell + ' = getchar();');
84 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
85 cmMultipy: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
86 cmLoopStart: begin
87 AddLine('while(' + GetMemoryCell + ' != 0)');
88 AddLine('{');
89 Inc(Indent);
90 end;
91 cmLoopEnd: begin
92 Dec(Indent);
93 AddLine('}');
94 end;
95 end;
96 Inc(FProgramIndex);
97 end;
98 AddLine('return 0;');
99 Dec(Indent);
100 AddLine('}');
101
102 CompileToFile;
103end;
104
105procedure TTargetC.CompileToFile;
106var
107 Process: TProcess;
108 CompiledFile: string;
109begin
110 CompiledFile := ExtractFilePath(ProjectFileName) +
111 'compiled' + DirectorySeparator + Name + DirectorySeparator +
112 ExtractFileNameOnly(ProjectFileName) + SourceExtension;
113 ForceDirectoriesUTF8(ExtractFilePath(CompiledFile));
114 with TStringList.Create do
115 try
116 Text := FTargetCode;
117 SaveToFile(CompiledFile);
118 finally
119 Free;
120 end;
121 if FileExistsUTF8(CompilerPath) then
122 try
123 Process := TProcess.Create(nil);
124 Process.CurrentDirectory := ExtractFilePath(CompilerPath);
125 Process.Executable := LongFileName(CompilerPath);
126 Process.Parameters.Add(LongFileName(CompiledFile));
127 Process.Parameters.Add('-o');
128 Process.Parameters.Add(LongFileName(ExtractFilePath(CompiledFile) + ExtractFileNameOnly(CompiledFile) + CompiledExtension));
129 Process.Options := [poWaitOnExit];
130 Process.Execute;
131 finally
132 Process.Free;
133 end else raise Exception.Create(Format(SCompilerNotFound, [CompilerPath]));
134end;
135
136procedure TTargetC.Run;
137begin
138 inherited;
139 RunFromFile;
140end;
141
142end.
143
Note: See TracBrowser for help on using the repository browser.