From 7cb4a177a5dcf7fcc6d2a68b9fb0eb89cb637d5c Mon Sep 17 00:00:00 2001 From: Tim Vieira Date: Thu, 27 Jun 2013 21:09:23 -0400 Subject: [PATCH] Backchained items are no longer displayed in solution. Nicer error messages when post/load commands are not understood. --- examples/trace.dyna | 3 --- src/Dyna/Backend/Python/interpreter.py | 7 +++++++ src/Dyna/Backend/Python/load/__init__.py | 16 ++++++++++++++-- src/Dyna/Backend/Python/post/__init__.py | 17 +++++++++++++++-- src/Dyna/Backend/Python/post/trace.py | 7 +++---- test/repl/load.bash | 2 +- 6 files changed, 40 insertions(+), 12 deletions(-) delete mode 100644 examples/trace.dyna diff --git a/examples/trace.dyna b/examples/trace.dyna deleted file mode 100644 index e935f60..0000000 --- a/examples/trace.dyna +++ /dev/null @@ -1,3 +0,0 @@ - -% trace of a matrix named "a" -trace("A") += matrix("A", X, X). diff --git a/src/Dyna/Backend/Python/interpreter.py b/src/Dyna/Backend/Python/interpreter.py index 1a0fb53..f3998b5 100644 --- a/src/Dyna/Backend/Python/interpreter.py +++ b/src/Dyna/Backend/Python/interpreter.py @@ -281,6 +281,9 @@ class Interpreter(object): self.rules = ddict(Rule) self.error = {} + # not essential, available in parser_state + self.backchained = set() + def __getstate__(self): return ((self.chart, self.agenda, @@ -324,6 +327,7 @@ class Interpreter(object): print >> out, '========' fns = self.chart.keys() fns.sort() + fns = [x for x in fns if x not in self.backchained] # don't show backchained items nullary = [x for x in fns if x.endswith('/0')] others = [x for x in fns if not x.endswith('/0')] # show nullary charts first @@ -614,6 +618,9 @@ class Interpreter(object): # accept the new parser state self.parser_state = env.parser_state + + self.backchained = {f + '/' + a for f, a in re.findall(":-backchain '([^']+)'/(\d+).", env.parser_state)} + # process emits for e in emits: self.emit(*e, delete=False) diff --git a/src/Dyna/Backend/Python/load/__init__.py b/src/Dyna/Backend/Python/load/__init__.py index dea5dff..79da4ba 100644 --- a/src/Dyna/Backend/Python/load/__init__.py +++ b/src/Dyna/Backend/Python/load/__init__.py @@ -6,7 +6,19 @@ from pickled import pickled import re as _re def run(interp, line): - [(name, module, args)] = _re.findall('^([a-z][a-zA-Z_0-9]*) = ([a-z][a-zA-Z_0-9]*)\((.*)\)', line) - m = getattr(__import__('load'), module)(interp, name) + try: + [(name, module, args)] = _re.findall('^([a-z][a-zA-Z_0-9]*) = ([a-z][a-zA-Z_0-9]*)\((.*)\)', line) + except ValueError: + print 'Error: failed to parse post command.' + print ' %s' % line + print + return + + try: + m = getattr(__import__('load'), module)(interp, name) + except KeyError: + print 'did not recognize post-processor %r' % name + return + exec 'm.main(%s)' % args interp.go() diff --git a/src/Dyna/Backend/Python/post/__init__.py b/src/Dyna/Backend/Python/post/__init__.py index 77c9085..1484307 100644 --- a/src/Dyna/Backend/Python/post/__init__.py +++ b/src/Dyna/Backend/Python/post/__init__.py @@ -6,6 +6,19 @@ from dump_solution import dump_solution from trace import trace def run(interp, line): - [(name, args)] = _re.findall('([a-z][a-zA-Z_0-9]*)\((.*)\)$', line.strip()) - m = globals()[name](interp) + + try: + [(name, args)] = _re.findall('([a-z][a-zA-Z_0-9]*)\((.*)\)$', line.strip()) + except ValueError: + print 'Error: failed to parse post command.' + print ' %s' % line + print + return + + try: + m = globals()[name](interp) + except KeyError: + print 'did not recognize post-processor %r' % name + return + eval('m.main(%s)' % args) diff --git a/src/Dyna/Backend/Python/post/trace.py b/src/Dyna/Backend/Python/post/trace.py index 0fe076b..ba54ca2 100644 --- a/src/Dyna/Backend/Python/post/trace.py +++ b/src/Dyna/Backend/Python/post/trace.py @@ -1,6 +1,8 @@ # -*- coding: utf-8 -*- """ Examine solution as an outline of computation. + +TODO: backchained stuff """ import re @@ -8,9 +10,6 @@ from utils import yellow, green, red from defn import drepr from term import _repr import debug, defn - -import webbrowser -from debug import Hypergraph from cStringIO import StringIO from utils import lexer, subst @@ -20,7 +19,7 @@ from collections import defaultdict class trace(object): """ - Crude visualization of circuit pertaining to state of the interpreter. + Examine solution as an outline of computation. """ def __init__(self, interp): diff --git a/test/repl/load.bash b/test/repl/load.bash index 293fade..b6021f4 100755 --- a/test/repl/load.bash +++ b/test/repl/load.bash @@ -6,5 +6,5 @@ --load 'rules_tsv = tsv("test/repl/english.gr")' \ 'token = matrix("test/repl/english.sen", astype=str)' \ 'tree = sexpr("test/repl/english.par")' \ - --post 'dump_chart()' \ + --post 'dump_solution()' \ "$@" -- 2.50.1