source: tags/1.0.0/Target/UTargetPython.pas

Last change on this file was 87, checked in by chronos, 7 years ago
  • Added: New CSharp target.
  • Fixed: Use configured memory size in targets.
File size: 3.9 KB
Line 
1unit UTargetPython;
2
3{$mode Delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, UTarget, UBFTarget;
9
10type
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
24implementation
25
26{ TTargetPython }
27
28constructor TTargetPython.Create;
29begin
30 inherited Create;
31 Name := 'Python';
32 SourceExtension := '.py';
33 RunExtension := '.py';
34 CompiledExtension := '.py';
35 ImageIndex := 26;
36 Capabilities := [tcCompile, tcRun];
37 {$IFDEF Windows}
38 CompilerPath := '';
39 ExecutorPath := 'c:\Program Files\Python\python.exe';
40 {$ENDIF}
41 {$IFDEF Linux}
42 CompilerPath := '';
43 ExecutorPath := '/usr/bin/python';
44 {$ENDIF}
45end;
46
47function TTargetPython.GetMemoryCell: string;
48begin
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 + ']';
55end;
56
57procedure TTargetPython.Compile;
58begin
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 < Length(FProgram)) 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 cmMultipy: 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;
131end;
132
133procedure TTargetPython.Run;
134begin
135 inherited Run;
136 RunFromFile;
137end;
138
139end.
140
Note: See TracBrowser for help on using the repository browser.