source: tags/1.3.0/Target/TargetRust.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: 3.2 KB
Line 
1unit TargetRust;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Target, BFTarget, Process, Dialogs,
7 LazFileUtils;
8
9type
10
11 { TTargetRust }
12
13 TTargetRust = 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{ TTargetRust }
30
31constructor TTargetRust.Create;
32begin
33 inherited;
34 FName := 'Rust';
35 FSourceExtension := '.rs';
36 FImageIndex := 30;
37 FCapabilities := [tcCompile, tcRun];
38 {$IFDEF WINDOWS}
39 CompilerPath := 'c:\Program Files\Rust\rustc.exe';
40 FCompiledExtension := '.exe';
41 {$ENDIF}
42 {$IFDEF UNIX}
43 CompilerPath := '/usr/bin/rustc';
44 FCompiledExtension := '';
45 {$ENDIF}
46end;
47
48function TTargetRust.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 TTargetRust.GetCompileParams: TStringArray;
59begin
60 Result := [GetSourceFileName, '-o', GetCompileFileName];
61end;
62
63procedure TTargetRust.Compile;
64var
65 Op: Char;
66 MulValue: Integer;
67begin
68 inherited;
69 Indent := 0;
70 FTargetCode := '';
71
72 AddLine('fn main()');
73 AddLine('{');
74 Inc(Indent);
75 AddLine('let mut memory: [u8; ' + IntToStr(MemorySize) + '] = [0; ' + IntToStr(MemorySize) + '];');
76 AddLine('let mut pos = 0;');
77 AddLine('');
78 FProgramIndex := 0;
79 while FProgramIndex < FProgram.Count do begin
80 case FProgram[FProgramIndex].Command of
81 cmPointerInc: AddLine('pos += ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
82 cmPointerDec: AddLine('pos -= ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
83 cmInc: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
84 cmDec: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
85 cmOutput: AddLine('print!("{}", char::from(' + GetMemoryCell + '));');
86 cmInput: AddLine(GetMemoryCell + ' = getchar().to_u8();');
87 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
88 cmMultiply: begin
89 if FProgram[FProgramIndex].Parameter >= 0 then begin
90 Op := '+';
91 MulValue := FProgram[FProgramIndex].Parameter;
92 end else begin
93 Op := '-';
94 MulValue := -FProgram[FProgramIndex].Parameter;
95 end;
96 if MulValue = 1 then
97 AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' ' + Op + ' memory[pos];')
98 else AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' ' + Op + ' memory[pos] * ' + IntToStr(MulValue) + ';');
99 end;
100 cmLoopStart: begin
101 AddLine('while ' + GetMemoryCell + ' != 0');
102 AddLine('{');
103 Inc(Indent);
104 end;
105 cmLoopEnd: begin
106 Dec(Indent);
107 AddLine('}');
108 end;
109 end;
110 Inc(FProgramIndex);
111 end;
112 Dec(Indent);
113 AddLine('}');
114
115 CompileToFile;
116end;
117
118procedure TTargetRust.Run;
119begin
120 inherited;
121 RunFromFile;
122end;
123
124end.
125
Note: See TracBrowser for help on using the repository browser.