2
from pypy.rlib.parsing.parsing import ParseError
3
from pypy.rlib.parsing.deterministic import LexerError
4
from pypy.lang.prolog.interpreter.interactive import helptext
5
from pypy.lang.prolog.interpreter.parsing import parse_file, get_query_and_vars
6
from pypy.lang.prolog.interpreter.parsing import get_engine
7
from pypy.lang.prolog.interpreter.engine import Engine
8
from pypy.lang.prolog.interpreter.engine import Continuation
9
from pypy.lang.prolog.interpreter import error, term
10
import pypy.lang.prolog.interpreter.term
11
pypy.lang.prolog.interpreter.term.DEBUG = False
14
class StopItNow(Exception):
17
class ContinueContinuation(Continuation):
18
def __init__(self, var_to_pos, write):
19
self.var_to_pos = var_to_pos
22
def call(self, engine):
24
var_representation(self.var_to_pos, engine, self.write)
32
raise error.UnificationFailed
36
var_representation(self.var_to_pos, engine, self.write)
38
self.write('unknown action. press "h" for help\n')
40
def var_representation(var_to_pos, engine, write):
41
from pypy.lang.prolog.builtin import formatting
42
f = formatting.TermFormatter(engine, quoted=True, max_depth=20)
43
for var, real_var in var_to_pos.iteritems():
44
if var.startswith("_"):
46
val = f.format(real_var.getvalue(engine.frame))
47
write("%s = %s\n" % (var, val))
54
os.write(2, "debug: " + msg + '\n')
56
def printmessage(msg):
70
return "".join(result)
72
def run(goal, var_to_pos, e):
73
from pypy.lang.prolog.interpreter.error import UnificationFailed, CatchableError
74
from pypy.lang.prolog.interpreter.error import UncatchableError, UserError
75
from pypy.lang.prolog.builtin import formatting
76
f = formatting.TermFormatter(e, quoted=True, max_depth=20)
78
e.run(goal, ContinueContinuation(var_to_pos, printmessage))
79
except UnificationFailed:
81
except UncatchableError, e:
82
printmessage("INTERNAL ERROR: %s\n" % (e.message, ))
84
printmessage("ERROR: ")
85
f._make_reverse_op_mapping()
86
printmessage("Unhandled exception: ")
87
printmessage(f.format(e.term))
88
except CatchableError, e:
89
f._make_reverse_op_mapping()
90
printmessage("ERROR: ")
92
if isinstance(t, term.Term):
94
if isinstance(errorterm, term.Callable):
95
if errorterm.name == "instantiation_error":
96
printmessage("arguments not sufficiently instantiated\n")
98
elif errorterm.name == "existence_error":
99
if isinstance(errorterm, term.Term):
100
printmessage("Undefined %s: %s\n" % (
101
f.format(errorterm.args[0]),
102
f.format(errorterm.args[1])))
104
elif errorterm.name == "domain_error":
105
if isinstance(errorterm, term.Term):
107
"Domain error: '%s' expected, found '%s'\n" % (
108
f.format(errorterm.args[0]),
109
f.format(errorterm.args[1])))
111
elif errorterm.name == "type_error":
112
if isinstance(errorterm, term.Term):
114
"Type error: '%s' expected, found '%s'\n" % (
115
f.format(errorterm.args[0]),
116
f.format(errorterm.args[1])))
118
printmessage(" (but I cannot tell you which one)\n")
122
printmessage("yes\n")
125
printmessage("welcome!\n")
129
if line == "halt.\n":
132
goals, var_to_pos = engine.parse(line)
133
except ParseError, exc:
134
printmessage(exc.nice_error_message("<stdin>", line) + "\n")
136
except LexerError, exc:
137
printmessage(exc.nice_error_message("<stdin>") + "\n")
140
run(goal, var_to_pos, engine)
142
def execute(e, filename):
143
e.run(term.Term("consult", [term.Atom(filename)]))
145
if __name__ == '__main__':