-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl.py
72 lines (53 loc) · 1.64 KB
/
repl.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
import rdfscript.rdfscriptparser as parser
from rdfscript.env import Env
import sys
class REPL:
def __init__(self,
serializer='nt',
out=None,
optpaths=[],
optextensions=[],
debug_lvl=1):
self.env = Env(repl=True,
serializer=serializer,
paths=optpaths,
extensions=optextensions)
self.parser = parser.RDFScriptParser(debug_lvl=debug_lvl)
self.reader = self.parser.scanner
self.out_file = out
def start(self):
try:
while True:
self.read()
self.evaluate()
except EOFError:
self.finish()
def read(self):
prompt = self.get_prompt_string()
if sys.version_info >= (3, 0):
s = input(prompt + ' > ')
else:
s = raw_input(prompt + ' > ')
if not s:
self.read()
self.reader.input(s)
def evaluate(self):
form = self.parser.parser.parse(lexer=self.reader)
result = self.env.interpret(form)
self.pprint(result)
def pprint(self, value):
if value is not None:
string = format("%s" % value)
print(string)
def finish(self):
if not self.out_file:
self.pprint(self.env)
else:
with open(self.out_file, 'w') as out:
out.write(str(self.env))
def get_prompt_string(self):
if self.env.prefix is not None:
prompt = self.env.prefix
else:
prompt = 'RDF'
return prompt