for them (e.g. matplotlib) and that ./dyna starts-up quicker.
*.swp
*.pyc
+*.d # debugger's output directory.
+
dist/
examples/*.d
examples/*.hist
import operator
from collections import Counter
-
+from utils import drepr, _repr
class Aggregator(object):
def fold(self):
if name.startswith('u') and not name.startswith('u_'))
-from term import _repr
-def drepr(vs):
- return '{%s}' % ', '.join('%s=%s' % (k, _repr(v)) for k,v in vs.iteritems())
-
from collections import namedtuple
class Result(namedtuple('Result', 'value variables')):
def __repr__(self):
for k, v in env.agg_decl.items():
self.new_fn(k, v)
- new_rules = set()
+ new_rules = set()
for _, r, _ in env.queries:
new_rules.add(r)
for r, _ in env.initializers:
-from sexpr import sexpr
-from tsv import tsv
-from matrix import matrix
-from pickled import pickled
+#from sexpr import sexpr
+#from tsv import tsv
+#from matrix import matrix
+#from pickled import pickled
import re as _re
+from utils import get_module
+
+available = 'sexpr', 'tsv', 'matrix'
def run(interp, line):
try:
print
return
- try:
- m = getattr(__import__('load'), module)(interp, name)
- except KeyError:
- print 'did not recognize post-processor %r' % name
+ if module not in available:
+ print 'did not recognize loader %r' % name
return
+ m = get_module('load', module)(interp, name)
exec 'm.main(%s)' % args
interp.go()
import re as _re
-from save import save
-from graph import graph
-from draw_circuit import draw_circuit
-from dump_solution import dump_solution
-from trace import trace
+from utils import get_module
+
+available = 'trace', 'dump_solution', 'draw_circuit', 'graph', 'save'
def run(interp, line):
try:
- [(name, args)] = _re.findall('([a-z][a-zA-Z_0-9]*)\((.*)\)$', line.strip())
+ [(module, 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
+ if module not in available:
+ print 'did not recognize post-processor %r' % module
return
+ m = get_module('post', module)(interp)
eval('m.main(%s)' % args)
"""
import re
-from utils import yellow, green, red
-from defn import drepr
-from term import _repr
+from utils import yellow, green, red, _repr, drepr
import debug, defn
from cStringIO import StringIO
from utils import lexer, subst
TODO: $include load rules from a file.
"""
-import re, os, cmd, readline
-
-import debug, interpreter
-from utils import dynac, ip, lexer, subst
+import os, cmd, readline
+from utils import dynac, ip, lexer, subst, drepr, _repr
from errors import DynaCompilerError, DynaInitializerException
-from chart import _repr
from config import dotdynadir
-
from errors import show_traceback
-import load, post
-
from interpreter import Interpreter, foo, none
-
-from term import _repr
-from defn import drepr
+import load, post
class REPL(cmd.Cmd, object):
"""
Development tool. Used for view Dyna's intermediate representations.
"""
+ import debug
with file(dotdynadir / 'repl-debug-line.dyna', 'wb') as f:
f.write(line)
debug.main(f.name)
- `query` shows variable bindings applied to query
:- query f(X)
- 1 ← f(1)
- 4 ← f(2)
+ 1 =* f(1)
+ 4 =* f(2)
"""
results = self._query(q)
[cmd, sub] = mod
if cmd in ('load', 'post'):
try:
- print getattr(globals()[cmd], sub).__doc__
- except (KeyError, AttributeError):
+ exec 'from %s.%s import %s as m' % (cmd, sub, sub)
+ print m.__doc__
+ except (ImportError, KeyError, AttributeError):
print 'No help available for "%s %s"' % (cmd, sub)
return
else:
Available loaders:
- {loaders}
+ {load}
For more information about a particular loader type the following (in
this case we get help for the `tsv` loader):
show_traceback()
readline.write_history_file(self.hist)
- do_load.__doc__ = do_load.__doc__.format(loaders=', '.join(x for x in dir(load) if not x.startswith('_')))
-
def do_post(self, line):
"""
Execute post-processor.
show_traceback()
readline.write_history_file(self.hist)
- do_post.__doc__ = do_post.__doc__.format(post=', '.join(x for x in dir(post) if not x.startswith('_')))
+
+ do_load.__doc__ = do_load.__doc__.format(load=', '.join(load.available))
+ do_post.__doc__ = do_post.__doc__.format(post=', '.join(post.available))
from contextlib import contextmanager
from collections import namedtuple
+
def _repr(x):
if x is True:
return 'true'
return repr(x)
+def drepr(vs):
+ return '{%s}' % ', '.join('%s=%s' % (k, _repr(v)) for k,v in vs.iteritems())
+
+
# interactive IPython shell
ip = InteractiveShellEmbed(banner1 = 'Dropping into IPython\n')
+def get_module(cmd, sub):
+ try:
+ exec 'from %s.%s import %s as m' % (cmd, sub, sub)
+ except (ImportError, SyntaxError):
+ return
+ else:
+ return m
+
+
black, red, green, yellow, blue, magenta, cyan, white = \
map('\033[3%sm%%s\033[0m'.__mod__, range(8))
class ANF(namedtuple('ANF', 'lines ruleix agg head evals unifs result')):
pass
+
def read_anf(e):
def _g(x):
-# return [(var, val[0], val[1:]) for var, val in x]
for var, val in x:
if isinstance(val, list):
yield (var, val[0], val[1:])
% binary rules
rewrite(X, Y, Z) :=
rules_tsv(Linenum, Cost, X, R),
- cons(Y, &cons(Z, &nil)) is pycall("split", R, "\\s+"),
+ [Y, Z] is pycall("split", R, "\\s+"),
pycall("float", Cost).
% load unary rules
rewrite(X, Y) :=
rules_tsv(Linenum, Cost, X, R),
- cons(Y, &nil) is pycall("split", R, "\\s+"),
+ [Y] is pycall("split", R, "\\s+"),
pycall("float", Cost).
phrase(S,X,I,K) += phrase(S,Y,I,K) + rewrite(X,Y).