diff options
| author | Mattias Andrée <maandree@operamail.com> | 2012-10-28 21:03:02 +0100 | 
|---|---|---|
| committer | Mattias Andrée <maandree@operamail.com> | 2012-10-28 21:03:02 +0100 | 
| commit | eb9dc56f5c8df4f6572cf249899894ba594ea1ad (patch) | |
| tree | 4f41fc4244dbfe93dc2b8283f08761d733148b16 | |
| parent | whoops (diff) | |
| download | auto-auto-complete-eb9dc56f5c8df4f6572cf249899894ba594ea1ad.tar.gz auto-auto-complete-eb9dc56f5c8df4f6572cf249899894ba594ea1ad.tar.bz2 auto-auto-complete-eb9dc56f5c8df4f6572cf249899894ba594ea1ad.tar.xz | |
simplifing the tree
| -rwxr-xr-x | auto-auto-complete.py | 42 | ||||
| -rw-r--r-- | 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) @@ -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  			    )  	) | 
