From: Tim Vieira Date: Mon, 3 Dec 2012 21:01:23 +0000 (-0500) Subject: cleanup utils. X-Git-Url: https://hydra-www.ietfng.org/gitweb/?a=commitdiff_plain;h=4ee4d1ca27fdb575322ca30cbd23928cdafbb190;p=dyna2 cleanup utils. --- diff --git a/bin/utils.py b/bin/utils.py index 5992f6e..e3fb5ae 100644 --- a/bin/utils.py +++ b/bin/utils.py @@ -4,25 +4,24 @@ black, red, green, yellow, blue, magenta, cyan, white = \ map('\033[3%sm%%s\033[0m'.__mod__, range(8)) -def convert(f): +def toANF(code, f='/tmp/tmp.dyna'): + "Convert to ANF using Haskell implemention via system call." + with file(f, 'wb') as tmp: + tmp.write(code) os.system('rm -f %s.anf' % f) # clean up any existing ANF output - assert 0 == os.system("""ghc -isrc Dyna.Analysis.NormalizeParseSelftest -e 'normalizeFile "%s"' """ % f), \ 'failed to convert file.' - with file('%s.anf' % f) as h: return h.read() -def toANF(code, f='/tmp/tmp.dyna'): - with file(f, 'wb') as tmp: - tmp.write(code) - return convert(tmp.name) - -# by George Sakkis (gsakkis at rutgers.edu) -# http://mail.python.org/pipermail/python-list/2005-March/312004.html def parse_sexpr(e): - "If multiple s-expressions expected as output, set multiple to True." + """ + Parse a string representing an s-expressions into lists-of-lists. + + based on implementation by George Sakkis + http://mail.python.org/pipermail/python-list/2005-March/312004.html + """ es, stack = [], [] for token in re.split(r'([()])|\s+', e): if token == '(': @@ -45,15 +44,8 @@ def parse_sexpr(e): return es -def evalthings(t): - if isinstance(t, str): - return t - else: - return [evalthings(x) for x in t] - - def read_anf(e): - x = evalthings(parse_sexpr(e)) + x = parse_sexpr(e) def g(x): return [(var, val[0], val[1:]) for var, val in x]