source: tags/1.3.0/Target/TargetC.pas

Last change on this file was 162, checked in by chronos, 3 months ago

Merged revision(s) 161 from trunk:

  • Fixed: All targets compilation and run.
File size: 2.8 KB
Line 
1unit TargetC;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Target, BFTarget, Process, Dialogs,
7 LazFileUtils;
8
9type
10
11 { TTargetC }
12
13 TTargetC = class(TBFTarget)
14 private
15 function GetMemoryCell: string;
16 public
17 function GetCompileParams: TStringArray; override;
18 constructor Create; override;
19 procedure Compile; override;
20 procedure Run; override;
21 end;
22
23
24implementation
25
26uses
27 Common;
28
29{ TTargetC }
30
31constructor TTargetC.Create;
32begin
33 inherited;
34 FName := 'C';
35 FSourceExtension := '.c';
36 FImageIndex := 23;
37 FCapabilities := [tcCompile, tcRun];
38 {$IFDEF WINDOWS}
39 CompilerPath := 'c:\Program Files\MinGW\bin\gcc.exe';
40 FCompiledExtension := '.exe';
41 {$ENDIF}
42 {$IFDEF UNIX}
43 CompilerPath := '/usr/bin/gcc';
44 FCompiledExtension := '';
45 {$ENDIF}
46end;
47
48function TTargetC.GetMemoryCell: string;
49begin
50 Result := 'Memory[Pos';
51 if FProgram[FProgramIndex].RelIndex > 0 then
52 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
53 else if FProgram[FProgramIndex].RelIndex < 0 then
54 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
55 Result := Result + ']';
56end;
57
58function TTargetC.GetCompileParams: TStringArray;
59begin
60 Result := [GetSourceFileName, '-o', GetCompileFileName];
61end;
62
63procedure TTargetC.Compile;
64begin
65 inherited;
66 Indent := 0;
67 FTargetCode := '';
68
69 AddLine('#include <stdio.h>');
70 AddLine('');
71 AddLine('int main()');
72 AddLine('{');
73 Inc(Indent);
74 AddLine('char Memory[' + IntToStr(MemorySize) + '];');
75 AddLine('int Pos;');
76 AddLine('char ReadChar;');
77 AddLine('');
78 AddLine('Pos = 0;');
79 FProgramIndex := 0;
80 while FProgramIndex < FProgram.Count do begin
81 case FProgram[FProgramIndex].Command of
82 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
83 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
84 cmInc: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
85 cmDec: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
86 cmOutput: AddLine('putchar(' + GetMemoryCell + ');');
87 cmInput: AddLine(GetMemoryCell + ' = getchar();');
88 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
89 cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
90 cmLoopStart: begin
91 AddLine('while(' + GetMemoryCell + ' != 0)');
92 AddLine('{');
93 Inc(Indent);
94 end;
95 cmLoopEnd: begin
96 Dec(Indent);
97 AddLine('}');
98 end;
99 end;
100 Inc(FProgramIndex);
101 end;
102 AddLine('return 0;');
103 Dec(Indent);
104 AddLine('}');
105
106 CompileToFile;
107end;
108
109procedure TTargetC.Run;
110begin
111 inherited;
112 RunFromFile;
113end;
114
115end.
116
Note: See TracBrowser for help on using the repository browser.