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