-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinteractive.py
164 lines (137 loc) · 4.94 KB
/
interactive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#
# Copyright 2009 Benjamin Mellor
#
# This file is part of Fundy.
#
# Fundy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import sys
from rpython.rlib.parsing.parsing import ParseError
from rpython.rlib.parsing.deterministic import LexerError
from rpython.rlib.streamio import open_file_as_stream, fdopen_as_stream
from asteval import Eval
from fundyparse import parse
from version import version_numbers
# Use __stdin__ etc rather than stdin so it works in IDLE too, although you
# have to interact with Fundy through the terminal that started IDLE, rather
# than IDLE's gui window.
stdin_stream = fdopen_as_stream(sys.__stdin__.fileno(), "rU", buffering=1)
stdout_stream = fdopen_as_stream(sys.__stdout__.fileno(), "wU", buffering=1)
stderr_stream = fdopen_as_stream(sys.__stderr__.fileno(), "wU", buffering=0)
class FundyConsole(object):
def __init__(self, filename="<console>"):
self.filename = filename
self.asteval = Eval()
self.stdin = stdin_stream
self.stdout = stdout_stream
self.stderr = stderr_stream
self.resetbuffer()
def resetbuffer(self):
self.buffer= []
def interact(self, banner=None, prompt='|> ', continue_prompt='.. '):
if banner is None:
self.write('Fundy %d.%d.%d\n' % version_numbers)
else:
self.write(banner + '\n')
more = 0
while True:
try:
if more:
line = self.raw_input(continue_prompt)
else:
line = self.raw_input(prompt)
except EOFError:
self.write("\n")
break
else:
more = self.push(line)
# status code
# TODO: make more informative
return 0
def push(self, line):
self.buffer.append(line)
source = "\n".join(self.buffer)
more = self.runsource(source)
if not more:
self.resetbuffer()
return more
def raw_input(self, prompt=""):
#try:
self.stdout.do_write(prompt)
line = self.stdin.readline()
if not line:
raise EOFError
elif line.endswith('\n'):
line = line[:-1] # remove trailing newline
return line
#except KeyboardInterrupt:
# raise
def write(self, s):
self.stderr.write(s)
def compile(self, source):
try:
tree = parse(source + '\n')
except ParseError, e:
# HACK: If there are more def's than returns, or more open
# parentheses than closed, then presume the parse error is because
# of that and return None signalling that more input is required.
# This allows such constructs to naturally be entered over several
# lines interactively.
if source.count('def') > source.count('return') or \
source.count('(') > source.count(')'):
return None
else:
raise
# no exception
return tree
def runsource(self, source):
try:
tree = self.compile(source)
except LexerError, e:
self.write(e.nice_error_message(filename=self.filename) + '\n')
except ParseError, e:
self.write(e.nice_error_message(filename=self.filename,
source=source) + '\n')
else:
if tree is None:
return True # incomplete input
else:
self.runcode(tree)
# compile error or successfully ran
return False
def runcode(self, tree):
self.asteval.dispatch(tree)
def main(argv):
if len(argv) > 1:
# argv[0] is the executable name
scriptname = argv[1]
try:
stream = open_file_as_stream(scriptname, mode="rU")
try:
source = stream.readall()
finally:
stream.close()
except OSError, e:
stderr_stream.write('Error reading file "%s" (errno: %d)\n'
% (scriptname, e.errno))
return 1
interp = FundyConsole(scriptname)
interp.runsource(source)
return 0
else:
interp = FundyConsole()
return interp.interact()
if __name__ == '__main__':
import sys
main(sys.argv)