Changeset 1249 for proto/pebble


Ignore:
Timestamp:
Jul 31, 2011, 12:09:51 AM (8 years ago)
Author:
shermer
Message:

improvements to lexicalStream package.
The unused position-tracking code in CharClassDescriptor? was removed.
LexicalDescriptor? was not doing much, so its functionality was pulled down and removed.
other minor changes.

Location:
proto/pebble/trunk/src
Files:
1 deleted
6 edited

Legend:

Unmodified
Added
Removed
  • proto/pebble/trunk/src/ast/transforms/ASTtoIncode.java

    r1242 r1249  
    104104                }
    105105                public void visitLeave(ConstantASTB node) {
    106                         argumentString.put(node, "SmallConstant(" + node.getValue()+")");
     106                        argumentString.put(node, "smallConstant(" + node.getValue()+")");
    107107                }
    108108                ///////////////////////////////////////////////////////////////
  • proto/pebble/trunk/src/ast/transforms/readme.txt

    r1242 r1249  
    6161                (no ExecutionBlocks w/macro, expand remain in AST after)
    6262               
    63         MoveLexDefsToBranchStarts (in package lexicalStream)
     63        MoveLexDefsToBranchStarts (in package 'lexicalStream')
    6464                Moves lexical expression (sequence & char-class) to beginnings
    6565                of branches, then replaces them with their definition.
     
    6868        Assumption incorporator (which calls Simplifier)
    6969                (No Assumptions remain in AST after)
     70       
     71        (Semantic analysis) 
     72                Traditional static-type stuff would go here.  There is a start to
     73                this in the package 'semantic'.
    7074               
    7175        ASTtoIncode
  • proto/pebble/trunk/src/lexicalStream/CharClassDescriptor.java

    r1242 r1249  
    11package lexicalStream;
    2 
    3 import incode.BasicBlock;
    4 import incode.Instruction;
    52
    63import java.util.ArrayList;
    74import java.util.List;
    85
    9 import util.PStringBuilder;
    106import util.Position;
    117import util.VariableNamer;
     
    1915
    2016
    21 public class CharClassDescriptor extends LexicalDescriptor {
     17public class CharClassDescriptor {
    2218        private static final String SHIFT_STRUCT_NAME = "shift";
    2319        private static final String LEXICAL_STRUCT_NAME = "lex";
    2420        private static final Position zero = new Position(0);
     21       
    2522        private List<Range> ranges;
    26         private List<Position> positions;
    27 
    28         // ccStreamDescriptor does _not_ get complementation information.
    29         public CharClassDescriptor(LexicalCollector collector,
    30                         String specifier, List<Range> ranges, Position position) {
    31                 super(collector, specifier);
     23        private String specifier;
     24       
     25        //////////////////////////////////////////////////////////
     26        // constructor and accessor
     27        //////////////////////////////////////////////////////////
     28        public CharClassDescriptor(String specifier, List<Range> ranges) {
     29                this.specifier = specifier;
    3230                this.ranges = ranges;
    33                 positions = new ArrayList<Position>();
    34                 positions.add(position);
    3531        }
    36        
    37         public void addPosition(Position position) {
    38                 if(!positions.contains(position)) {
    39                         positions.add(position);
    40                 }
    41         }
    42         public List<Position> getPositions() {
    43                 return positions;
     32        public String getSpecifier() {
     33                return specifier;
    4434        }
    4535
    46         private void makeShiftedVersions(BasicBlock block) {
    47                 VariableNamer namer = VariableNamer.getInstance();     
    48                 String previousShift = variableName(); 
    49                 int max = maxPosition();
    50                 String thisShift;
    51                
    52                 for(int p = 1; p <= max; p++) {
    53                         Position pos = new Position(p);
    54                         boolean storedInStruct = positions.contains(pos);
    55                         if(storedInStruct) {
    56                                 thisShift = variableName(pos);
    57                         }
    58                         else {
    59                                 thisShift = namer.newTemporary();
    60                         }
    61                         Instruction shiftInstr = Instruction.makeShift(thisShift, previousShift, ""+1);
    62                         block.add(shiftInstr);
    63                         previousShift = thisShift;
    64                        
    65                         if(storedInStruct) {
    66                                 shiftInstr.markAsLiveOut();
    67                         }
    68                 }
     36        //////////////////////////////////////////////////////////
     37        // asAssignment(), asAssignment(position)
     38        //
     39        // make an AST for assigning this charClass to a new
     40        // temporary.
     41        //////////////////////////////////////////////////////////
     42        public AST asAssignment() {
     43                AST var = VariableASTB.make(variableName());
     44                AST expr = asAST();
     45                return new AssignmentASTN(var, expr);
    6946        }
    70 
    71         private int maxPosition() {
    72                 assert !positions.isEmpty();
    73                
    74                 int max = Integer.MIN_VALUE;
    75                 for(Position position: positions) {
    76                         if(position.get() > max)
    77                                 max = position.get();
    78                 }
    79                 return max;
     47        public AST asAssignment(Position p) {
     48                AST varAtP = VariableASTB.make(variableName(p));
     49                AST var = VariableASTB.make(variableName());
     50                AST expr = ShiftASTB.make(var, p.get());
     51                return new AssignmentASTN(varAtP, expr);
    8052        }
    81 
    82         private void verifyPositions() {
    83                 assert !positions.isEmpty();
    84         }
    85 
    8653        private AST asAST() {
    87                 verifyPositions();
    8854                if(ranges.isEmpty()) {
    8955                        return FalseLiteralASTB.make();
     
    9763                return Utils.orTogether(rangeExprs);
    9864        }
    99         public AST asAssignment() {
    100                 AST var = VariableASTB.make(variableName());
    101                 AST expr = asAST();
    102                 return new AssignmentASTN(var, expr);
     65
     66
     67        //////////////////////////////////////////////////////////
     68        // retrieving the variable name for this charClass.
     69        //////////////////////////////////////////////////////////
     70        public String variableName() {
     71                return variableName(zero);
    10372        }
    104 
    105         public AST asAssignment(Position p) {
    106                 AST varAtP = VariableASTB.make(variableName(p));
    107                 AST var = VariableASTB.make(variableName());
    108                 AST expr = ShiftASTB.make(var, p.get());
    109                 return new AssignmentASTN(varAtP, expr);
     73        public String variableName(Position position) {
     74                return structurePrefix(position) + noPrefixVariableName(position);
    11075        }
    111        
    112         public void addShiftCodeTo(BasicBlock block) {
    113                 makeShiftedVersions(block);
     76        public String noPrefixVariableName() {
     77                return noPrefixVariableName(zero);
    11478        }
    115         public String toString() {
    116                 String name = variableName();
    117                 return name + ": " + getSpecifier() + " @" + positionsString();
     79        public String noPrefixVariableName(Position position) {
     80                VariableNamer namer = VariableNamer.getInstance();
     81                return namer.charClassName(getSpecifier()) + positionalSuffix(position);
    11882        }
    119         private String positionsString() {
    120                 PStringBuilder result = new PStringBuilder();
    121                 for(Position position: positions) {
    122                         result.append(position + ",");
    123                 }
    124                 result.trimTail();                      // remove trailing comma
    125                
    126                 return result.toString();
    127         }
    128        
    129        
    130         public String structurePrefix(Position position) {
     83        private String structurePrefix(Position position) {
    13184                if(position.get()==0) {
    13285                        return LEXICAL_STRUCT_NAME + ".";
     
    13689                }
    13790        }
    138         public String positionalSuffix(Position p) {
    139                 return p.get()==0 ? "" : ("_" + p);
     91        private String positionalSuffix(Position position) {
     92                return position.get()==0 ? "" : ("_" + position);
    14093        }
    141         public String variableName() {
    142                 return variableName(zero);
    143         }
    144         public String noPrefixVariableName() {
    145                 return noPrefixVariableName(zero);
    146         }
    147         public String variableName(Position position) {
    148                 return structurePrefix(position) + noPrefixVariableName(position);
    149         }
    150         public String noPrefixVariableName(Position position) {
    151                 VariableNamer namer = VariableNamer.getInstance();
    152                 return namer.charClassName(getSpecifier()) + positionalSuffix(position);
     94
     95
     96        //////////////////////////////////////////////////////////
     97        // toString
     98        //////////////////////////////////////////////////////////
     99        public String toString() {
     100                return variableName() + ": " + getSpecifier();
    153101        }
    154102}
  • proto/pebble/trunk/src/lexicalStream/LexicalCollector.java

    r1242 r1249  
    140140                return pair.descriptor;
    141141        }       
    142        
     142        //TODO: investigate previous method.  It does not search up the parent link.
    143143       
    144144        ///////////////////////////////////////////////////////////
     
    185185                        result.append(pair.descriptor.toString() + "\n");       //TODO: add positions
    186186                }               
    187                 for(LexicalDescriptor descriptor : sqStreams.values()) {
     187                for(SequenceDescriptor descriptor : sqStreams.values()) {
    188188                        result.append(descriptor.toString() + "\n");
    189189                }
     
    206206        // structs()
    207207        //
    208         // returns a string that consists of the C++ struct
    209         // (Python class) definitions for the data blocks needed
     208        // returns a string that consists of the Python class
     209        // (C++ struct) definitions for the data blocks needed
    210210        // by the lexical streams in this collector and all its
    211211        // descendants.
     
    431431                                        String specifier, List<Range> ranges,
    432432                                        LexicalCollector requestor) {
    433                         return new CharClassDescriptor(requestor, specifier, ranges, new Position(0));
     433                        return new CharClassDescriptor(specifier, ranges);
    434434                }
    435435                @Override
  • proto/pebble/trunk/src/lexicalStream/SequenceDescriptor.java

    r1242 r1249  
    1515
    1616
    17 public class SequenceDescriptor extends LexicalDescriptor {
     17public class SequenceDescriptor {
    1818        private static final String SEQUENCE_STRUCT_NAME = "seq";
    1919        List<SeqElement> elements;
     20        private String specifier;
     21        protected LexicalCollector collector;
     22       
     23       
     24        //////////////////////////////////////////////////////////
     25        // constructor and accessor
     26        //////////////////////////////////////////////////////////
    2027        public SequenceDescriptor(LexicalCollector collector,
    2128                        String specifier,
    2229                        List<SeqElement> elements) {
    23                 super(collector, specifier);
     30                this.collector = collector;
     31                this.specifier = specifier;
    2432                this.elements = elements;
    2533        }
    26 
     34        public String getSpecifier() {
     35                return specifier;
     36        }
     37       
     38        //////////////////////////////////////////////////////////
     39        // asAssignment()
     40        //
     41        // make an AST for assigning this sequence to a new
     42        // temporary.  Assumes the necessary component shifted
     43        // streams are already computed.
     44        //////////////////////////////////////////////////////////
     45        public AST asAssignment() {
     46                AST var = VariableASTB.make(variableName());
     47                AST expr = asAST();
     48                return new AssignmentASTN(var, expr);
     49        }
    2750        private AST asAST() {
    2851                int length = elements.size();
     
    4467                return sequence;
    4568        }
    46         public AST asAssignment() {
    47                 AST var = VariableASTB.make(variableName());
    48                 AST expr = asAST();
    49                 return new AssignmentASTN(var, expr);
    50         }
    51         public String toString() {
    52                 String name = variableName();
    53                 return name + ": " + getSpecifier();
    54         }
     69
     70
     71        //////////////////////////////////////////////////////////
     72        // retrieving the variable name for this sequence.
     73        //////////////////////////////////////////////////////////
    5574        public String variableName() {
    5675                return SEQUENCE_STRUCT_NAME + "." + noPrefixVariableName();
     
    6079                return namer.sequenceName(getSpecifier());
    6180        }
     81
     82
     83       
     84        //////////////////////////////////////////////////////////
     85        // toString
     86        //////////////////////////////////////////////////////////
     87        public String toString() {
     88                return variableName() + ": " + getSpecifier();
     89        }
    6290}
  • proto/pebble/trunk/src/lexicalStream/readme.txt

    r1242 r1249  
    3434                                placing streams in the correct order on a branch.  These
    3535                                structures get linked into a tree by MoveLexdefs...
    36        
    37         LexicalDescriptor
    38                                 An abstract base class for the other stream descriptors.
    3936       
    4037        CharClassDescriptor
     
    110107
    111108After the visitor-driven processing, the LexicalCollector may
    112 be asked to generate the lexical structs (for the C++ backend) which are
    113 required to store the lexical computation.  This should only be invoked for the
    114 top LexicalCollector in a tree.
     109be asked to generate the lexical struct classes (for the pablo/C++ backend)
     110which are required to store the lexical computation.  This should only be
     111invoked for the top LexicalCollector in a tree.
    115112
    116113
     
    127124stream.  It is be formed from the specifier by util.VariableNamer.
    128125
    129 The term "descriptor" is used for an instance of some subclass of
    130 LexicalDescriptor, and these instances (I hope) contain and
    131 coordinate all of the information about the lexical stream.  I note an
    132 exception in that whether a character class is complemented is not stored
    133 in the CharClassDescriptor.
     126The term "descriptor" is used for an instance of CharClassDescriptor or
     127SequenceDescriptor, and these instances sort-of contain and coordinate
     128all of the information about the lexical stream.  I note exceptions in
     129that (1) whether a character class is complemented is not stored in the
     130CharClassDescriptor, and (2) positions at which a character class are
     131required are stored in a LexicalCollector.CharClassPositionsPair, not in
     132the CharClass Descriptor.
    134133
    135134The term "canonical" means a unique description of a lexical stream; I
     
    164163descriptors are meant to be attached to canonized objects rather than the
    165164rough parsed objects. There should be one descriptor per lexical stream
    166 needed, even if it occurs in many places.  There should also be only one
    167 descriptor for the collection of any stream and its shifted versions.
    168 Anyhow, a clean distinction between the two types of classes, or a merging
    169 of them, would be helpful.
     165needed, even if it occurs in many places.  This is currently not the case,
     166in that (say) a character class needed in one sub-branch of the AST does not
     167have the same descriptor as the same character class needed in an unrelated
     168sub-branch of the AST.   
    170169
    171 This is an old comment: currently the collector knows the difference between
    172 the two types of LexicalDescriptors, with methods for each; thus it is
    173 coupled to that hierarchy.  It may be better to have a class that is
    174 parameterized by the LexicalDescriptor concrete type, in case more
    175 types are added.
     170Anyhow, a clean distinction between the two types of classes(TokenData and
     171Descriptors), or a merging of them, might be helpful.
    176172
    177173The collector now uses a (descriptor, positionList) pair for character classes.
    178174This adds complicating logic to the character-class half with no corresponding
    179 complication for the sequence half, so parameterization would be difficult.
    180 The collector is getting fairly complicated and probably should be split into
    181 smaller objects.
     175complication for the sequence half.  The collector is getting fairly complicated
     176and probably should be split into smaller objects.
    182177
    183178
Note: See TracChangeset for help on using the changeset viewer.