source: trunk/Target/TargetPython.pas

Last change on this file was 145, checked in by chronos, 11 months ago
  • Modified: Remove U prefix from unit names.
  • Modified: Updated Common package.
File size: 3.9 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 FRunExtension := '.py';
32 FCompiledExtension := '.py';
33 FImageIndex := 26;
34 FCapabilities := [tcCompile, tcRun];
35 {$IFDEF WINDOWS}
36 CompilerPath := '';
37 ExecutorPath := 'c:\Program Files\Python\python.exe';
38 {$ENDIF}
39 {$IFDEF UNIX}
40 CompilerPath := '';
41 ExecutorPath := '/usr/bin/python';
42 {$ENDIF}
43end;
44
45function TTargetPython.GetMemoryCell: string;
46begin
47 Result := 'memory[position';
48 if FProgram[FProgramIndex].RelIndex > 0 then
49 Result := Result + ' + ' + IntToStr(FProgram[FProgramIndex].RelIndex)
50 else if FProgram[FProgramIndex].RelIndex < 0 then
51 Result := Result + ' - ' + IntToStr(Abs(FProgram[FProgramIndex].RelIndex));
52 Result := Result + ']';
53end;
54
55procedure TTargetPython.Compile;
56begin
57 inherited;
58 Indent := 0;
59 FTargetCode := '';
60
61 AddLine('# ' + ProgramName);
62 AddLine('');
63 AddLine('class _Getch:');
64 AddLine(' """Gets a single character from standard input. Does not echo to the screen."""');
65 AddLine(' def __init__(self):');
66 AddLine(' try:');
67 AddLine(' self.impl = _GetchWindows()');
68 AddLine(' except ImportError:');
69 AddLine(' self.impl = _GetchUnix()');
70 AddLine('');
71 AddLine(' def __call__(self): return self.impl()');
72 AddLine('');
73 AddLine('');
74 AddLine('class _GetchUnix:');
75 AddLine(' def __init__(self):');
76 AddLine(' import tty, sys');
77 AddLine('');
78 AddLine(' def __call__(self):');
79 AddLine(' import sys, tty, termios');
80 AddLine(' fd = sys.stdin.fileno()');
81 AddLine(' old_settings = termios.tcgetattr(fd)');
82 AddLine(' try:');
83 AddLine(' tty.setraw(sys.stdin.fileno())');
84 AddLine(' ch = sys.stdin.read(1)');
85 AddLine(' finally:');
86 AddLine(' termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)');
87 AddLine(' return ch');
88 AddLine('');
89 AddLine('');
90 AddLine('class _GetchWindows:');
91 AddLine(' def __init__(self):');
92 AddLine(' import msvcrt');
93 AddLine('');
94 AddLine(' def __call__(self):');
95 AddLine(' import msvcrt');
96 AddLine(' return msvcrt.getch()');
97 AddLine('');
98 AddLine('getchar = _Getch()');
99 AddLine('');
100 AddLine('import sys');
101 AddLine('memory = [0] * ' + IntToStr(MemorySize));
102 AddLine('position = 0');
103 FProgramIndex := 0;
104 while FProgramIndex < FProgram.Count do begin
105 case FProgram[FProgramIndex].Command of
106 cmPointerInc: AddLine('position += ' + IntToStr(FProgram[FProgramIndex].Parameter));
107 cmPointerDec: AddLine('position -= ' + IntToStr(FProgram[FProgramIndex].Parameter));
108 cmInc: AddLine(GetMemoryCell + ' += ' + IntToStr(FProgram[FProgramIndex].Parameter));
109 cmDec: AddLine(GetMemoryCell + ' -= ' + IntToStr(FProgram[FProgramIndex].Parameter));
110 cmOutput: begin
111 AddLine('sys.stdout.write(chr(' + GetMemoryCell + '))');
112 AddLine('sys.stdout.flush()');
113 end;
114 cmInput: AddLine(GetMemoryCell + ' = ord(getchar())');
115 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter));
116 cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + memory[position] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');
117 cmLoopStart: begin
118 AddLine('while(' + GetMemoryCell + ' != 0):');
119 Inc(Indent);
120 end;
121 cmLoopEnd: begin
122 Dec(Indent);
123 end;
124 end;
125 Inc(FProgramIndex);
126 end;
127
128 CompileToFile;
129end;
130
131procedure TTargetPython.Run;
132begin
133 inherited;
134 RunFromFile;
135end;
136
137end.
138
Note: See TracBrowser for help on using the repository browser.