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