From: Tim Vieira Date: Sat, 6 Jul 2013 02:46:30 +0000 (-0400) Subject: tweaks. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=ae4ceba999df616a6a7bf176edb64b1bfc4372dd;p=dyna2 tweaks. --- diff --git a/src/Dyna/Backend/Python/repl.py b/src/Dyna/Backend/Python/repl.py index ad65ae3..8df3a3a 100644 --- a/src/Dyna/Backend/Python/repl.py +++ b/src/Dyna/Backend/Python/repl.py @@ -11,7 +11,7 @@ to help. import os, cmd, readline from utils import dynac, ip, lexer, subst, drepr, _repr, get_module -from stdlib import topython +from stdlib import topython, todyna from errors import DynaCompilerError, DynaInitializerException from config import dotdynadir from errors import show_traceback @@ -250,7 +250,7 @@ class REPL(cmd.Cmd, object): return print for term, result in sorted((subst(q, result), result) for result in results): - print '%s = %s.' % (term, _repr(result['$val'])) + print '%s = %s.' % (term, _repr(todyna(result['$val']))) print def default(self, line, show_changed=True): diff --git a/src/Dyna/Backend/Python/stdlib.py b/src/Dyna/Backend/Python/stdlib.py index 21c4a76..8e47293 100644 --- a/src/Dyna/Backend/Python/stdlib.py +++ b/src/Dyna/Backend/Python/stdlib.py @@ -27,7 +27,10 @@ def pycall(name, *args): x = eval(name)(*args) return todyna(x) + def topython(x): + #if isinstance(x, AList) or x is Nil: + # return {topython(k): topython(v) for k,v in x.aslist} if isinstance(x, Cons) or x is Nil: return [topython(y) for y in x.aslist] return x @@ -35,6 +38,15 @@ def topython(x): def todynalist(x): # TODO: get rid of this. return todyna(x) + +def getkey(m, k): + return m[k] + +def setkey(m, k, v): + m[k] = v + return m + + def todyna(x): if isinstance(x, (set, Counter)): @@ -43,6 +55,10 @@ def todyna(x): return todyna(x) elif isinstance(x, dict): + #c = Nil + #for k,v in x.items(): + # c = AList(todyna([k,v]), c) + #return c return todyna(x.items()) elif isinstance(x, (list, tuple)): @@ -60,7 +76,7 @@ def get(x, i): def iter_cons(x): if not (isinstance(x, Cons) or x is Nil): raise TypeError("Attemping to iterate something which isn't a list.") - return x + return x.like_chart() def in_list(x, a): if not (isinstance(a, Cons) or a is Nil): diff --git a/src/Dyna/Backend/Python/term.py b/src/Dyna/Backend/Python/term.py index 8b36489..dae2ad7 100644 --- a/src/Dyna/Backend/Python/term.py +++ b/src/Dyna/Backend/Python/term.py @@ -71,13 +71,16 @@ class Cons(Term): def __contains__(self, x): return x in self.aslist - def __iter__(self): + def like_chart(self): for a in self.aslist: if not isinstance(a, Term): yield a, (None,), a else: yield a, (None,), a + def __iter__(self): + return iter(self.aslist) + def __eq__(self, other): try: return self.aslist == other.aslist @@ -111,3 +114,19 @@ class _Nil(Term): return False Nil = _Nil() + + +#class AList(Cons): +# +# def __init__(self, head, tail): +# +# if not (isinstance(tail, AList) or tail is Nil): +# raise TypeError('Malformed alist: tail is not an alist') +# +# if not (isinstance(head, Cons) and len(head.aslist) == 2): +# raise TypeError('Malformed alist: head is not a pair.') +# +# Cons.__init__(self, head, tail) +# +# def __repr__(self): +# return '[%s]' % (', '.join('%s -> %s' % (_repr(k), _repr(v)) for k,v in self.aslist)) diff --git a/test/repl/load.dynadoc b/test/repl/load.dynadoc index aa00c51..dce812a 100644 --- a/test/repl/load.dynadoc +++ b/test/repl/load.dynadoc @@ -54,7 +54,7 @@ > query tree(0) -tree(0) = ['ROOT', ['S', ['NP', 'George'], ['VP', ['V', ['V', 'love'], '-s'], ['NP', 'Laura']]], '.']. +tree(0) = ["ROOT", ["S", ["NP", "George"], ["VP", ["V", ["V", "love"], "-s"], ["NP", "Laura"]]], "."]. > query sentence_length(10)