1 | unit TargetRuby;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, FileUtil, Target, BFTarget, Process, Dialogs,
|
---|
7 | LazFileUtils;
|
---|
8 |
|
---|
9 | type
|
---|
10 |
|
---|
11 | { TTargetRuby }
|
---|
12 |
|
---|
13 | TTargetRuby = 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 | { TTargetRuby }
|
---|
30 |
|
---|
31 | constructor TTargetRuby.Create;
|
---|
32 | begin
|
---|
33 | inherited;
|
---|
34 | FName := 'Ruby';
|
---|
35 | FSourceExtension := '.rb';
|
---|
36 | FCompiledExtension := '.rb';
|
---|
37 | FImageIndex := 33;
|
---|
38 | FCapabilities := [tcCompile, tcRun];
|
---|
39 | {$IFDEF WINDOWS}
|
---|
40 | CompilerPath := '';
|
---|
41 | ExecutorPath := 'c:\Program Files\Ruby\ruby.exe';
|
---|
42 | {$ENDIF}
|
---|
43 | {$IFDEF UNIX}
|
---|
44 | CompilerPath := '';
|
---|
45 | ExecutorPath := '/usr/bin/ruby';
|
---|
46 | {$ENDIF}
|
---|
47 | end;
|
---|
48 |
|
---|
49 | function TTargetRuby.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 | function TTargetRuby.GetCompileParams: TStringArray;
|
---|
60 | begin
|
---|
61 | Result := [GetSourceFileName, '-o', GetCompileFileName];
|
---|
62 | end;
|
---|
63 |
|
---|
64 | procedure TTargetRuby.Compile;
|
---|
65 | var
|
---|
66 | Op: Char;
|
---|
67 | MulValue: Integer;
|
---|
68 | begin
|
---|
69 | inherited;
|
---|
70 | Indent := 0;
|
---|
71 | FTargetCode := '';
|
---|
72 |
|
---|
73 | AddLine('require ''io/console''');
|
---|
74 | AddLine;
|
---|
75 | AddLine('memory = Array.new(' + IntToStr(MemorySize) + ', 0)');
|
---|
76 | AddLine('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 ' + GetMemoryCell + '.chr');
|
---|
86 | cmInput: AddLine(GetMemoryCell + ' = STDIN.getch');
|
---|
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 | Inc(Indent);
|
---|
103 | end;
|
---|
104 | cmLoopEnd: begin
|
---|
105 | Dec(Indent);
|
---|
106 | AddLine('end');
|
---|
107 | end;
|
---|
108 | end;
|
---|
109 | Inc(FProgramIndex);
|
---|
110 | end;
|
---|
111 |
|
---|
112 | CompileToFile;
|
---|
113 | end;
|
---|
114 |
|
---|
115 | procedure TTargetRuby.Run;
|
---|
116 | begin
|
---|
117 | inherited;
|
---|
118 | RunFromFile;
|
---|
119 | end;
|
---|
120 |
|
---|
121 | end.
|
---|
122 |
|
---|