source: tags/1.3.0/Target/TargetPython.pas

Last change on this file was 162, checked in by chronos, 3 months ago

Merged revision(s) 161 from trunk:

  • Fixed: All targets compilation and run.
File size: 3.8 KB
Line 
1unit TargetPython;
2
3interface
4
5uses
6 Classes, SysUtils, Target, BFTarget;
7
8type
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
22implementation
23
24{ TTargetPython }
25
26constructor TTargetPython.Create;
27begin
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}
42end;
43
44function TTargetPython.GetMemoryCell: string;
45begin
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 + ']';
52end;
53
54procedure TTargetPython.Compile;
55begin
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;
128end;
129
130procedure TTargetPython.Run;
131begin
132 inherited;
133 RunFromFile;
134end;
135
136end.
137
Note: See TracBrowser for help on using the repository browser.