1 | unit TargetRust;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Target, BFTarget, Process, Dialogs,
|
---|
7 | LazFileUtils;
|
---|
8 |
|
---|
9 | type
|
---|
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 |
|
---|
24 | implementation
|
---|
25 |
|
---|
26 | uses
|
---|
27 | Common;
|
---|
28 |
|
---|
29 | { TTargetRust }
|
---|
30 |
|
---|
31 | constructor TTargetRust.Create;
|
---|
32 | begin
|
---|
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}
|
---|
46 | end;
|
---|
47 |
|
---|
48 | function TTargetRust.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 TTargetRust.GetCompileParams: TStringArray;
|
---|
59 | begin
|
---|
60 | Result := [GetSourceFileName, '-o', GetCompileFileName];
|
---|
61 | end;
|
---|
62 |
|
---|
63 | procedure TTargetRust.Compile;
|
---|
64 | var
|
---|
65 | Op: Char;
|
---|
66 | MulValue: Integer;
|
---|
67 | begin
|
---|
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;
|
---|
116 | end;
|
---|
117 |
|
---|
118 | procedure TTargetRust.Run;
|
---|
119 | begin
|
---|
120 | inherited;
|
---|
121 | RunFromFile;
|
---|
122 | end;
|
---|
123 |
|
---|
124 | end.
|
---|
125 |
|
---|