​On Thu, May 21, 2015 at 9:58 AM, Edward K. Ream <[email protected]> 
wrote:

> I have created several tools that apply to this problem:
> 1. The string composition functions in leoGlobals.py-->...-->g.List 
composition.
​
I spent several hours playing with this approach yesterday.  It wasn't as 
clean as I imagined.

After several hours of thought a beautiful new design pattern appeared. 
This pattern should lead to the fastest, simplest, clearest and most 
flexible code.  There are several parts to this pattern:

*Descriptive code generation*

Ast visitors will generate "code" that describes the desired result using 
descriptive helpers.  Like this:

# ClassDef(identifier name, expr* bases, stmt* body, expr* decorator_list)

def do_ClassDef (self,node):

    self.blank_lines(2)
    if node.expr:
        self.visit_list(node.expr,sep='\n')
            # or separate_lines=True
    self.line_start()
    self.word('class')
    self.word(node.name)
    if node.bases:
        self.lt('(')
        self.visit_list(node.bases,sep=', ')
        self.rt(')')
    self.lit(':')
    self.line_end()
    self.blank_lines(2)

*A global code list*

Helpers add results to a single, global, flat *code list*.  This eliminates 
a later flattening phase.

Helpers put *descriptions *of the desired results onto this list, not 
characters.

*A peephole*

A peephole "optimizer" will traverse the code list, fulfilling the 
"requests" made by the items on the code list. 

For example, the self.blank_lines(2) *helper *puts a blank_lines(2) *item* 
on the code list. This is a request for two blank lines.  Fulfilling this 
request may entail *lookahead* or *look behind*, that is, moving backwards 
one or more items in the list.

The blank_lines item may generate different results depending on context. 
Withing Leo, the start of a node is equivalent to arbitrarily many blank 
lines.  Outside of Leo, real blank lines will be generated.

The peephole knows nothing about the parse tree that generates it.  This is 
as it must be. It is a vital simplification.

The pass 1 code will deal with about a dozen kinds of requests in a regular 
manner.  This puts all the special cases in one place, where they can be 
handled in the presence of all necessary data.  

Peephole code is surprisingly easy to get right.  I have no doubt that it 
will be straightforward.

*Conversion to characters*

A final pass will convert the requests to actual characters. It will 
generate a results list, and then return ''.join(results).  This will be 
the only string operation in the beautifier!  Nothing could stress the gc 
less.

*Summary*

This organization will result in the fastest, simplest, clearest and most 
flexible code.

A flat code list makes it easy to look ahead an behind, an essential 
operation in the peephole.

A flat code list collapses complexity throughout the program.

This design puts the entire project on the strongest possible footing. The 
goal of a 4x speedup over PythonTidy seems within reach.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to