1 | unit UTargetPython;
|
---|
2 |
|
---|
3 | {$mode Delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, UTarget, UBFTarget;
|
---|
9 |
|
---|
10 | type
|
---|
11 |
|
---|
12 | { TTargetPython }
|
---|
13 |
|
---|
14 | TTargetPython = class(TBFTarget)
|
---|
15 | private
|
---|
16 | function GetMemoryCell: string;
|
---|
17 | public
|
---|
18 | constructor Create; override;
|
---|
19 | procedure Compile; override;
|
---|
20 | procedure Run; override;
|
---|
21 | end;
|
---|
22 |
|
---|
23 |
|
---|
24 | implementation
|
---|
25 |
|
---|
26 | { TTargetPython }
|
---|
27 |
|
---|
28 | constructor TTargetPython.Create;
|
---|
29 | begin
|
---|
30 | inherited;
|
---|
31 | FName := 'Python';
|
---|
32 | FSourceExtension := '.py';
|
---|
33 | FRunExtension := '.py';
|
---|
34 | FCompiledExtension := '.py';
|
---|
35 | FImageIndex := 26;
|
---|
36 | FCapabilities := [tcCompile, tcRun];
|
---|
37 | {$IFDEF WINDOWS}
|
---|
38 | CompilerPath := '';
|
---|
39 | ExecutorPath := 'c:\Program Files\Python\python.exe';
|
---|
40 | {$ENDIF}
|
---|
41 | {$IFDEF UNIX}
|
---|
42 | CompilerPath := '';
|
---|
43 | ExecutorPath := '/usr/bin/python';
|
---|
44 | {$ENDIF}
|
---|
45 | end;
|
---|
46 |
|
---|
47 | function TTargetPython.GetMemoryCell: string;
|
---|
48 | begin
|
---|
49 | Result := 'memory[position';
|
---|
50 | if FProgram[FProgramIndex].RelIndex > 0 then
|
---|
51 | Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
|
---|
52 | else if FProgram[FProgramIndex].RelIndex < 0 then
|
---|
53 | Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
|
---|
54 | Result := Result + ']';
|
---|
55 | end;
|
---|
56 |
|
---|
57 | procedure TTargetPython.Compile;
|
---|
58 | begin
|
---|
59 | inherited;
|
---|
60 | Indent := 0;
|
---|
61 | FTargetCode := '';
|
---|
62 |
|
---|
63 | AddLine('# ' + ProgramName);
|
---|
64 | AddLine('');
|
---|
65 | AddLine('class _Getch:');
|
---|
66 | AddLine(' """Gets a single character from standard input. Does not echo to the screen."""');
|
---|
67 | AddLine(' def __init__(self):');
|
---|
68 | AddLine(' try:');
|
---|
69 | AddLine(' self.impl = _GetchWindows()');
|
---|
70 | AddLine(' except ImportError:');
|
---|
71 | AddLine(' self.impl = _GetchUnix()');
|
---|
72 | AddLine('');
|
---|
73 | AddLine(' def __call__(self): return self.impl()');
|
---|
74 | AddLine('');
|
---|
75 | AddLine('');
|
---|
76 | AddLine('class _GetchUnix:');
|
---|
77 | AddLine(' def __init__(self):');
|
---|
78 | AddLine(' import tty, sys');
|
---|
79 | AddLine('');
|
---|
80 | AddLine(' def __call__(self):');
|
---|
81 | AddLine(' import sys, tty, termios');
|
---|
82 | AddLine(' fd = sys.stdin.fileno()');
|
---|
83 | AddLine(' old_settings = termios.tcgetattr(fd)');
|
---|
84 | AddLine(' try:');
|
---|
85 | AddLine(' tty.setraw(sys.stdin.fileno())');
|
---|
86 | AddLine(' ch = sys.stdin.read(1)');
|
---|
87 | AddLine(' finally:');
|
---|
88 | AddLine(' termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)');
|
---|
89 | AddLine(' return ch');
|
---|
90 | AddLine('');
|
---|
91 | AddLine('');
|
---|
92 | AddLine('class _GetchWindows:');
|
---|
93 | AddLine(' def __init__(self):');
|
---|
94 | AddLine(' import msvcrt');
|
---|
95 | AddLine('');
|
---|
96 | AddLine(' def __call__(self):');
|
---|
97 | AddLine(' import msvcrt');
|
---|
98 | AddLine(' return msvcrt.getch()');
|
---|
99 | AddLine('');
|
---|
100 | AddLine('getchar = _Getch()');
|
---|
101 | AddLine('');
|
---|
102 | AddLine('import sys');
|
---|
103 | AddLine('memory = [0] * ' + IntToStr(MemorySize));
|
---|
104 | AddLine('position = 0');
|
---|
105 | FProgramIndex := 0;
|
---|
106 | while FProgramIndex < FProgram.Count do begin
|
---|
107 | case FProgram[FProgramIndex].Command of
|
---|
108 | cmPointerInc: AddLine('position += ' + IntToStr(FProgram[FProgramIndex].Parameter));
|
---|
109 | cmPointerDec: AddLine('position -= ' + IntToStr(FProgram[FProgramIndex].Parameter));
|
---|
110 | cmInc: AddLine(GetMemoryCell + ' += ' + IntToStr(FProgram[FProgramIndex].Parameter));
|
---|
111 | cmDec: AddLine(GetMemoryCell + ' -= ' + IntToStr(FProgram[FProgramIndex].Parameter));
|
---|
112 | cmOutput: begin
|
---|
113 | AddLine('sys.stdout.write(chr(' + GetMemoryCell + '))');
|
---|
114 | AddLine('sys.stdout.flush()');
|
---|
115 | end;
|
---|
116 | cmInput: AddLine(GetMemoryCell + ' = ord(getchar())');
|
---|
117 | cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter));
|
---|
118 | cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + memory[position] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
|
---|
119 | cmLoopStart: begin
|
---|
120 | AddLine('while(' + GetMemoryCell + ' != 0):');
|
---|
121 | Inc(Indent);
|
---|
122 | end;
|
---|
123 | cmLoopEnd: begin
|
---|
124 | Dec(Indent);
|
---|
125 | end;
|
---|
126 | end;
|
---|
127 | Inc(FProgramIndex);
|
---|
128 | end;
|
---|
129 |
|
---|
130 | CompileToFile;
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure TTargetPython.Run;
|
---|
134 | begin
|
---|
135 | inherited;
|
---|
136 | RunFromFile;
|
---|
137 | end;
|
---|
138 |
|
---|
139 | end.
|
---|
140 |
|
---|