From: Tim Vieira Date: Fri, 31 May 2013 21:42:28 +0000 (-0400) Subject: added dynac_code -- toward a REPL! X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=6abdc3770124b27b5fa5f3c783ca8e94fc9844a7;p=dyna2 added dynac_code -- toward a REPL! --- diff --git a/src/Dyna/Backend/Python/debug.py b/src/Dyna/Backend/Python/debug.py index 6bcd5cc..63a51c3 100644 --- a/src/Dyna/Backend/Python/debug.py +++ b/src/Dyna/Backend/Python/debug.py @@ -205,7 +205,7 @@ def graph_styles(g): return sty -def main(dynafile): +def main(dynafile, browser=True): if not os.path.exists(cssfile) or not os.path.exists(jsfile): print("Debug must be run from the root of the Dyna source tree") @@ -390,7 +390,7 @@ Initializer: print >> html, '' - if argv.browser: + if browser: os.system('gnome-open %s 2>/dev/null >/dev/null' % html.name) @@ -403,4 +403,4 @@ if __name__ == '__main__': p.add_argument('-x', dest='browser', action='store_false') argv = p.parse_args() - main(argv.input) + main(argv.input, browser=argv.browser) diff --git a/src/Dyna/Backend/Python/defn.py b/src/Dyna/Backend/Python/defn.py index ab16217..1f830a6 100644 --- a/src/Dyna/Backend/Python/defn.py +++ b/src/Dyna/Backend/Python/defn.py @@ -1,24 +1,6 @@ """ Misc doctests ------------- - -Call indirection -================ - - >>> call['*/2'](3,4) - 12 - - >>> call['*/2']('a',4) # string*int - 'aaaa' - - >>> call['+/2']('a','b') # string+string - 'ab' - - >>> call['//2'](3,4) # integer division - 0 - - >>> call['//2'](3.0,4) - 0.75 """ import math, operator @@ -83,7 +65,7 @@ class SetEquals(Aggregator): def inc(self, val): self.set.add(val) def dec(self, val): - self.set.pop(val) + self.set.remove(val) def fold(self): return self.set def clear(self): diff --git a/src/Dyna/Backend/Python/interpreter.py b/src/Dyna/Backend/Python/interpreter.py index a4af8f1..770125d 100644 --- a/src/Dyna/Backend/Python/interpreter.py +++ b/src/Dyna/Backend/Python/interpreter.py @@ -18,11 +18,40 @@ - XXX: we should probably fuse update handlers instead of dispatching to each one independently. - - TODO: deletion of a rule should be running the initializer for the rule in - deletion mode. + - TODO: deleting a rule: (1) remove update handlers (2) run initializers in + delete mode (3) remove initializers. - TODO: hooks from introspection, eval, and prioritization. + +REPL +==== + + - TODO: aggregator map conflicts + + +INTERPRETER +=========== + + - Error values (with provenance ideally) + + Consider the following program: + | c += 0 + | b += 1 + | a += b / c + | d += a + | e += d + + Results in the fixed point: + | c = 0 + | b = 1 + | a = error("divison by zero in rule 'a += b / c'") + | d = error("because a = error in 'rule d += a.'") # because error annihilate aggregators + | e = error("because of d ...") + + Should errors have linear provenance? The error could have come from more than + one parent. + """ #from debug import ultraTB2; ultraTB2.enable() @@ -234,11 +263,11 @@ def peel(fn, item): """ if fn == "true/0" : - assert (item is True) - return + assert item is True + return if fn == "false/0" : - assert (item is False) - return + assert item is False + return assert isinstance(item, tuple) (fa, idx) = item @@ -256,7 +285,7 @@ def build(fn, *args): return (fn, idx) -def emit(item, val): +def emit(item, val, ruleix=None, variables=None): print >> sys.stderr, (red if _delete else green) \ % 'emit %s (val %s; curr: %s)' % (pretty(item), val, lookup(item)) @@ -324,11 +353,36 @@ def go(): else: dump_charts() -def dynac(f): - out = "%s.plan.py" % f +def dynac(f, out): cmd = '%s/dist/build/dyna/dyna -B python -o "%s" "%s"' % (dynahome, out, f) - assert 0 == os.system(cmd), 'command failed:\n\t' + cmd - return out + if os.system(cmd): + print 'command failed:\n\t' + cmd + return True + + +def dynac_code(code, debug=0): + "skip the file." + + dyna = '/tmp/tmp.dyna' + + out = '%s.plan.py' % dyna + + with file(dyna, 'wb') as f: + f.write(code) + + if dynac(dyna, out): # stop if the compiler failed. + return + + if debug: + import debug + debug.main(dyna) + + with file(out) as f: + new_code = f.read() + + print new_code + + do(out) def load(f, verbose=True): @@ -365,7 +419,7 @@ def do(filename): parser = ArgumentParser(description="The dyna interpreter!") parser.add_argument('source', help='Path to Dyna source file (or plan if --plan=true).') -parser.add_argument('--plan', action='store_true', default=False, +parser.add_argument('--plan', action='store_true', default=False, help='`source` specifies output of the compiler instead of dyna source code.') parser.add_argument('-i', dest='interactive', action='store_true', help='Fire-up an IPython shell.') @@ -376,7 +430,8 @@ argv = parser.parse_args() if argv.plan: plan = argv.source else: - plan = dynac(argv.source) + plan = "%s.plan.py" % argv.source + dynac(argv.source, plan) do(plan)