source: trunk/Target/TargetRuby.pas

Last change on this file was 180, checked in by chronos, 3 months ago
  • Added: Ruby target.
File size: 3.1 KB
Line 
1unit TargetRuby;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Target, BFTarget, Process, Dialogs,
7 LazFileUtils;
8
9type
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
24implementation
25
26uses
27 Common;
28
29{ TTargetRuby }
30
31constructor TTargetRuby.Create;
32begin
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}
47end;
48
49function TTargetRuby.GetMemoryCell: string;
50begin
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 + ']';
57end;
58
59function TTargetRuby.GetCompileParams: TStringArray;
60begin
61 Result := [GetSourceFileName, '-o', GetCompileFileName];
62end;
63
64procedure TTargetRuby.Compile;
65var
66 Op: Char;
67 MulValue: Integer;
68begin
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;
113end;
114
115procedure TTargetRuby.Run;
116begin
117 inherited;
118 RunFromFile;
119end;
120
121end.
122
Note: See TracBrowser for help on using the repository browser.