source: trunk/Target/TargetLua.pas

Last change on this file was 181, checked in by chronos, 3 months ago
  • Added: Lua target.
File size: 3.1 KB
Line 
1unit TargetLua;
2
3interface
4
5uses
6 Classes, SysUtils, FileUtil, Target, BFTarget, Process, Dialogs,
7 LazFileUtils;
8
9type
10
11 { TTargetLua }
12
13 TTargetLua = 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{ TTargetLua }
30
31constructor TTargetLua.Create;
32begin
33 inherited;
34 FName := 'Lua';
35 FSourceExtension := '.lua';
36 FCompiledExtension := '.lua';
37 FImageIndex := 34;
38 FCapabilities := [tcCompile, tcRun];
39 {$IFDEF WINDOWS}
40 CompilerPath := '';
41 ExecutorPath := 'c:\Program Files\Lua\lua.exe';
42 {$ENDIF}
43 {$IFDEF UNIX}
44 CompilerPath := '';
45 ExecutorPath := '/usr/bin/lua';
46 {$ENDIF}
47end;
48
49function TTargetLua.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 TTargetLua.GetCompileParams: TStringArray;
60begin
61 Result := [GetSourceFileName, '-o', GetCompileFileName];
62end;
63
64procedure TTargetLua.Compile;
65var
66 Op: Char;
67 MulValue: Integer;
68begin
69 inherited;
70 Indent := 0;
71 FTargetCode := '';
72
73 AddLine('memory = {}');
74 AddLine('for i = 0, ' + IntToStr(MemorySize - 1) + ' do');
75 AddLine(' memory[i] = 0');
76 AddLine('end');
77 AddLine('pos = 0');
78 AddLine;
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('io.write(string.char(' + GetMemoryCell + '))');
87 cmInput: AddLine(GetMemoryCell + ' = io.read(1)');
88 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter));
89 cmMultiply: begin
90 if FProgram[FProgramIndex].Parameter >= 0 then begin
91 Op := '+';
92 MulValue := FProgram[FProgramIndex].Parameter;
93 end else begin
94 Op := '-';
95 MulValue := -FProgram[FProgramIndex].Parameter;
96 end;
97 if MulValue = 1 then
98 AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' ' + Op + ' memory[pos]')
99 else AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' ' + Op + ' memory[pos] * ' + IntToStr(MulValue));
100 end;
101 cmLoopStart: begin
102 AddLine('while(' + GetMemoryCell + ' ~= 0)');
103 AddLine('do');
104 Inc(Indent);
105 end;
106 cmLoopEnd: begin
107 Dec(Indent);
108 AddLine('end');
109 end;
110 end;
111 Inc(FProgramIndex);
112 end;
113
114 CompileToFile;
115end;
116
117procedure TTargetLua.Run;
118begin
119 inherited;
120 RunFromFile;
121end;
122
123end.
124
Note: See TracBrowser for help on using the repository browser.