1 | unit TargetC;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Target, BFTarget, Process, Dialogs,
|
---|
7 | LazFileUtils;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
24 | implementation
|
---|
25 |
|
---|
26 | uses
|
---|
27 | Common;
|
---|
28 |
|
---|
29 | { TTargetC }
|
---|
30 |
|
---|
31 | constructor TTargetC.Create;
|
---|
32 | begin
|
---|
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}
|
---|
46 | end;
|
---|
47 |
|
---|
48 | function TTargetC.GetMemoryCell: string;
|
---|
49 | begin
|
---|
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 + ']';
|
---|
56 | end;
|
---|
57 |
|
---|
58 | function TTargetC.GetCompileParams: TStringArray;
|
---|
59 | begin
|
---|
60 | Result := [GetSourceFileName, '-o', GetCompileFileName];
|
---|
61 | end;
|
---|
62 |
|
---|
63 | procedure TTargetC.Compile;
|
---|
64 | begin
|
---|
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;
|
---|
107 | end;
|
---|
108 |
|
---|
109 | procedure TTargetC.Run;
|
---|
110 | begin
|
---|
111 | inherited;
|
---|
112 | RunFromFile;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | end.
|
---|
116 |
|
---|