1 | unit UTargetC;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, FileUtil, UTarget, UBFTarget, Process, Dialogs,
|
---|
9 | LazFileUtils;
|
---|
10 |
|
---|
11 | type
|
---|
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 |
|
---|
26 | implementation
|
---|
27 |
|
---|
28 | { TTargetC }
|
---|
29 |
|
---|
30 | constructor TTargetC.Create;
|
---|
31 | begin
|
---|
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}
|
---|
47 | end;
|
---|
48 |
|
---|
49 | function TTargetC.GetMemoryCell: string;
|
---|
50 | begin
|
---|
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 + ']';
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure TTargetC.Compile;
|
---|
60 | begin
|
---|
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;
|
---|
103 | end;
|
---|
104 |
|
---|
105 | procedure TTargetC.CompileToFile;
|
---|
106 | var
|
---|
107 | Process: TProcess;
|
---|
108 | CompiledFile: string;
|
---|
109 | begin
|
---|
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.CommandLine := LongFileName(CompilerPath) + ' ' + LongFileName(CompiledFile) + ' -o ' +
|
---|
126 | LongFileName(ExtractFilePath(CompiledFile) + ExtractFileNameOnly(CompiledFile) + CompiledExtension);
|
---|
127 | Process.Options := [poWaitOnExit];
|
---|
128 | Process.Execute;
|
---|
129 | finally
|
---|
130 | Process.Free;
|
---|
131 | end else raise Exception.Create(Format(SCompilerNotFound, [CompilerPath]));
|
---|
132 | end;
|
---|
133 |
|
---|
134 | procedure TTargetC.Run;
|
---|
135 | begin
|
---|
136 | inherited;
|
---|
137 | RunFromFile;
|
---|
138 | end;
|
---|
139 |
|
---|
140 | end.
|
---|
141 |
|
---|