From eb9dc56f5c8df4f6572cf249899894ba594ea1ad Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 28 Oct 2012 21:03:02 +0100 Subject: simplifing the tree --- auto-auto-complete.py | 42 +++++++++++++++++++++++++++++++++++++++++- example | 4 ++-- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/auto-auto-complete.py b/auto-auto-complete.py index 53b455a..0865bca 100755 --- a/auto-auto-complete.py +++ b/auto-auto-complete.py @@ -98,6 +98,9 @@ class Parser: else: stack[stackptr] = [] elif (c == ')') and (quote is None): + if buf is not None: + stack[stackptr].append(buf) + buf = None if stackptr == 0: return stack[0] stackptr -= 1 @@ -117,7 +120,43 @@ class Parser: buf += c raise Exception('premature end of file') - + + + ''' + Simplifies a tree + + @param tree:list<↑|str> The tree + ''' + @staticmethod + def simplify(tree): + program = tree[0] + stack = [tree] + while len(stack) > 0: + node = stack.pop() + new = [] + edited = False + for item in node: + if isinstance(item, list): + if item[0] == 'multiple': + master = item[1] + for slave in item[2:]: + new.append([master] + slave) + edited = True + elif item[0] == 'case': + for alt in item[1:]: + if alt[0] == program: + new.append(alt[1]) + break + edited = True + else: + new.append(item) + else: + new.append(item) + if edited: + node[:] = new + for item in node: + if isinstance(item, list): + stack.append(item) ''' @@ -166,6 +205,7 @@ if __name__ == '__main__': with open(source, 'rb') as file: source = file.read().decode('utf8', 'replace') source = Parser.parse(source) + Parser.simplify(source) print(source) diff --git a/example b/example index 63915de..b825641 100644 --- a/example +++ b/example @@ -49,8 +49,8 @@ (no-exec ls "'/usr/share/ponysay/balloons'" (case (ponysay .say) (ponythink .think))) ) (suggestion wrap (verbatim none inhertit 100 60) - (calc (pipe (stty size) - (cut -d ' ' -f 2) + (calc (pipe (exec stty size) + (exec cut -d ' ' -f 2) ) - 10 ) ) -- cgit v1.2.3-70-g09d2