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;
|
---|
33 | FName := 'C';
|
---|
34 | FSourceExtension := '.c';
|
---|
35 | FImageIndex := 23;
|
---|
36 | FCapabilities := [tcCompile, tcRun];
|
---|
37 | {$IFDEF WINDOWS}
|
---|
38 | CompilerPath := 'c:\Program Files\MinGW\bin\gcc.exe';
|
---|
39 | FCompiledExtension := '.exe';
|
---|
40 | FRunExtension := '';
|
---|
41 | {$ENDIF}
|
---|
42 | {$IFDEF UNIX}
|
---|
43 | CompilerPath := '/usr/bin/gcc';
|
---|
44 | FCompiledExtension := '';
|
---|
45 | FRunExtension := '';
|
---|
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 < FProgram.Count 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 | cmMultiply: 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 | CompiledDir + 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]));
|
---|
134 | end;
|
---|
135 |
|
---|
136 | procedure TTargetC.Run;
|
---|
137 | begin
|
---|
138 | inherited;
|
---|
139 | RunFromFile;
|
---|
140 | end;
|
---|
141 |
|
---|
142 | end.
|
---|
143 |
|
---|