Changeset 2937


Ignore:
Timestamp:
Mar 19, 2013, 1:59:40 PM (5 years ago)
Author:
ksherdy
Message:

Code generator code clean up and comments.

Location:
proto/pabloj/trunk/src/compiler/codeGenerator
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • proto/pabloj/trunk/src/compiler/codeGenerator/helpers/CodeFragments.java

    r2834 r2937  
     1/*
     2 * Deprecated - reference only.
     3 *
     4 * Helper methods implemented as private methods of code generation modules.
     5 *
     6 * @author <ksherdy at sfu dot ca>
     7 */
     8
    19package compiler.codeGenerator.helpers;
    210
     
    614
    715public class CodeFragments {
    8 
    9         //////////////////////////////////////////////////////////////
    10         // static interface             
    1116
    1217        /*
     
    2126                return builder.toString();
    2227        }       
    23         */
    24 
    25         /*
    26         public static String makeStrmFuncDecl(FuncDefNode node, Unparser unparser, int indent) {
    27                
    28                 String funcName = Accessors.funcName(node);             
    29                 String parameters = Accessors.
    30                 return unparser.getCode(node, indent);
    31         }
    32         */
    33                  
    34         /*
    35         //////////////////////////////////////////////////////////////
    36         // TODO - Remove hack
    37         //        Replace capitalised struct type name and lower case instance with
    38         //        language support               
     28       
    3929        public static String makeStrmFuncDefs(ASTNode root, Unparser unparser) {
    4030       
     
    4434       
    4535        }
    46         */             
    47        
    48         // Decls
    49         /*
     36         
    5037        public static String makeStrmStructDecls(ProgramNode node, CodeXFormer unparser, int indent) {
    5138                StringBuilder builder = new StringBuilder();
     
    6047                return unparser.getCode(node, indent);
    6148        }
    62         */
    6349       
    64         // Defs
    65        
    66         //////////////////////////////////////////////////////////////
    67         // TODO - Remove hack
    68         //        Replace capitalised struct type name and lower case instance with
    69         //        language support     
    70         /*
    7150        public static String makeStructDefs(ProgramNode node, int indent) {
    7251                StringBuilder builder = new StringBuilder();
     
    9170                return builder.toString();
    9271        }
    93         */
    9472       
    95         /*
    96         ////////////////////////////////////////////////////////////// 
    97         //  External to CPPXFormer
    98         //     
    9973        public static String makeFuncDefs(ProgramNode node, int indent) {
    10074                StringBuilder builder = new StringBuilder();
     
    11286                String name = Accessors.funcName(node);
    11387                CodeStore.addIndentation(builder, indent);
    114                 builder.append(Generators.capitalize(name));    // TODO - hack
     88                builder.append(Generators.capitalize(name));   
    11589                builder.append(" ");
    11690                builder.append(Generators.unCapitalize(name)); 
  • proto/pabloj/trunk/src/compiler/codeGenerator/helpers/CodeStore.java

    r2834 r2937  
     1/*
     2 *
     3 * CodeStore abstraction - a list of CodeLines and 'result variable'
     4 * CodeLine abstract - a code string with relative indent
     5 *
     6 * @author Tom Shermer - original
     7 * @author Ken Herdy <ksherdy at sfu dot ca> - minor additions only
     8 *
     9 */
     10
    111package compiler.codeGenerator.helpers;
    212
     
    818        private class CodeLine {
    919                public String line;
    10                 public final int relativeIndent;         // compared to previous line.
     20                public final int relativeIndent;         
    1121                public CodeLine(String line, int relativeIndent) {
    1222                        super();
     
    1828        public static final char NEWLINE = '\n';
    1929        public static final char INDENT  = '\t';
    20         List<CodeLine> code = new ArrayList<CodeLine>();
     30        private List<CodeLine> code = new ArrayList<CodeLine>();
    2131        private String resultVarName = "codeStoreNoVar";
    2232
     
    104114        //////////////////////////////////////////////////////////////
    105115        // static interface
    106         /*
    107         public static void addIndentation(int indent, StringBuilder builder) {
    108                
    109         }
    110         */
    111116        public static void addIndentation(StringBuilder builder, int indent) {
    112117                for(int i=0; i<indent; i++) {
     
    117122        public void betailLastLine(int amount) {
    118123                int lastIndex = code.size() - 1;
    119                 CodeLine last = code.remove(lastIndex);;
     124                CodeLine last = code.remove(lastIndex);
    120125               
    121126                String line = last.line;
  • proto/pabloj/trunk/src/compiler/codeGenerator/helpers/Labeller.java

    r2834 r2937  
     1/*
     2 * Helper to generate unique labels.
     3 *
     4 *
     5 * @author Tom Shermer
     6 */
     7
    18package compiler.codeGenerator.helpers;
    29
  • proto/pabloj/trunk/src/compiler/codeGenerator/helpers/UnparserUtil.java

    r2834 r2937  
     1/*
     2 * Unparser (code generation) utility methods.
     3 *
     4 * @author Ken Herdy <ksherdy at sfu dot ca>
     5 */
     6
    17package compiler.codeGenerator.helpers;
    2 
    3 import pabloB.ast.*;
    48
    59import java.util.Iterator;
    610import java.util.List;
    711
    8 public abstract class UnparserUtil extends ASTVisitor.Default<CodeStore> {
     12public class UnparserUtil {
    913                       
    1014        public static CodeStore concatenatedChildrenCode(List<CodeStore> childResults) {
  • proto/pabloj/trunk/src/compiler/codeGenerator/visitors/CPPUnparser.java

    r2917 r2937  
    1 //
    2 // Transforms PabloB AST to C++ code.
    3 //
    4  
    5 package compiler.codeGenerator.visitors;
    6 
    7 import pabloB.ast.*;
    8 
    9 import java.util.Iterator;
    10 import java.util.List;
    11 
    12 import compiler.ast.pabloB.Accessors;
    13 import compiler.codeGenerator.helpers.CodeStore;
    14 import compiler.codeGenerator.helpers.UnparserUtil;
    15 import compiler.lang.carry.CarrySetBuiltins2CPP;
    16 import compiler.symbols.CarryInfoSetSymbol;
    17 import compiler.symbols.SymbolTable;
    18 
    19 // CPP stream struct target
     1/*
     2 * PabloB to C++ translation.
     3 * 
     4 * @author Ken Herdy <ksherdy at sfu dot ca>
     5 */
     6
     7// C++ stream structure translation target
    208/*
    219struct $name
     
    2513*/
    2614 
    27 // CPP stream function target
     15// C++ stream function translation target
    2816/*             
    2917struct $name
     
    4533*/
    4634
     35package compiler.codeGenerator.visitors;
     36
     37import pabloB.ast.*;
     38
     39import java.util.Iterator;
     40import java.util.List;
     41
     42import compiler.ast.pabloB.Accessors;
     43import compiler.codeGenerator.helpers.CodeStore;
     44import compiler.codeGenerator.helpers.UnparserUtil;
     45import compiler.lang.carry.CarrySetBuiltins2CPP;
     46import compiler.symbolTable.CarryInfoSetSymbol;
     47import compiler.symbolTable.SymbolTable;
    4748
    4849public class CPPUnparser extends Unparser {
    4950
    5051        private CarrySetBuiltins2CPP carrySetBuiltins2CPP;
    51        
    52         // synthetic attributes
    5352        private String stateIdentifier = null;
    5453       
     
    6968                return codeStore.toStringIndented(indent);
    7069        }
    71        
    72         public String getFileExt() {
    73                 return ".hpp";
    74         }
    75        
     70               
    7671        //////////////////////////////////////////////////////////////
    7772        // visitors
     
    8580       
    8681        @SuppressWarnings("static-access")
    87        
    8882        public CodeStore visitLeave(KernelDefNode node, List<CodeStore> childResults) {
    8983                CodeStore code = new CodeStore();
     
    218212        }       
    219213       
    220         ////////////////////////////////////////////////////////////////////////////
    221         // Stream Functions - User defined stream function (extension point)
    222         ////////////////////////////////////////////////////////////////////////////
    223214        public CodeStore visitLeave(FuncDefNode node, List<CodeStore> childResults) {
    224215                       
     
    265256               
    266257        ////////////////////////////////////////////////////////////////////////////
    267         // Stmts
     258        // Statements
    268259        ////////////////////////////////////////////////////////////////////////////   
    269260       
    270         // Assign Stmt
     261        // Assignment statement
    271262        public CodeStore visitLeave(AssignNode node, List<CodeStore> childResults) {
    272263                CodeStore code = new CodeStore();
     
    280271        }
    281272
    282         // FuncCall Stm
     273        // Function call statemen
    283274        public CodeStore visitLeave(FuncCallNode node, List<CodeStore> childResults) {
    284275                CodeStore code = new CodeStore();
     
    305296        }
    306297               
    307         // if Stmt
     298        // if statement
    308299        public CodeStore visitLeave(IfStmtNode node, List<CodeStore> childResults) {
    309300                CodeStore code = new CodeStore();
     
    324315        }
    325316       
    326         // while Stmt
     317        // while statement
    327318        public CodeStore visitLeave(WhileStmtNode node, List<CodeStore> childResults) {
    328319                CodeStore code = new CodeStore();
     
    336327        }       
    337328       
    338         // return Stmt
     329        // return statement
    339330        public CodeStore visitLeave(ReturnStmtNode node, List<CodeStore> childResults) {
    340331                CodeStore code = new CodeStore();
     
    344335        }
    345336                               
    346         // Var Decl
     337        // variable declarations
    347338        public CodeStore visitLeave(LocalVarDeclNode node, List<CodeStore> childResults) {
    348339       
     
    488479        // Stream Structures
    489480        ////////////////////////////////////////////////////////////////////////////
    490         // Struct Type - used both as a variable and as sequence of lines
     481        // Struct Type - used both as a variable and as sequence of code lines
    491482        //
    492483        public CodeStore visitLeave(StructTypeNode node, List<CodeStore> childResults) {
  • proto/pabloj/trunk/src/compiler/codeGenerator/visitors/Unparser.java

    r2917 r2937  
    1 //
    2 // Description:
    3 //
    4 //- Objects -
    5 //
    6 // CodeStore            - a list of 'CodeLine' objects, a 'resultVariable'
    7 // ResultVariable       - a code value (String), not a complete code line, rather a code line fragment
    8 // CodeLine             - a code line (String), a complete line and with relative indent information
    9 //
    10 // Unparser<CodeStore> subclasses Default<CodeStore> and Default<T> implements Visitor<T>
    11 //
    12 //        For each ASTNode type, Unparser<CodeStore> overrides,
    13 //
    14 //        CodeStore visitLeave(ASTNode node, List<CodeStore> childResults);
    15 //
    16 //        i.e. each node's call to visitLeave takes as input the CodeStore from each of each child
    17 //        as a list and returns a single CodeStore object based on visitLeave method of that node
    18 //
    19 //
    20 // Unparser<CodeStore> provides 'getCode(ASTNode node)';
    21 // 'getCode(ASTNode node)' returns the string that is translation of substree 'node'
    22 //
    23 //
    24 // - Bottom-Up AST to Target Language String Translation -
    25 //
    26 // (a) Each Unparser<CodeStore> visitLeave override creates a CodeStore object 'code' and returns that object.
    27 //
    28 // (b) AST nodes that translate to code line fragments call 'code.setResultVarName("some-value")' to pass
    29 //  information up the tree, e.g. AST terminal nodes
    30 //
    31 // (c) AST nodes that construct code line(s) assemble child line fragments via 'childResults.get(i).getResultVarName()'
    32 //  wherein 'i' references to the CodeStore of the 'ith' child
    33 //
    34 //  or
    35 //
    36 //  append the code lines of the 'ith' child via code.addAll(childResults.get(i))
    37 //
     1
     2/*
     3 * - Abstractions
     4 *
     5 * CodeStore            - a list of 'CodeLine' objects, a 'resultVariable'
     6 * ResultVariable       - a code value (String), not newline terminated, no relative indentation, ie. a code fragment
     7 * CodeLine             - a code line (String), newline terminated, relative indentation
     8 *
     9 *   
     10 * - AST to Target Language Translation Implementation
     11 *
     12 * Unparser<CodeStore> subclasses Default<CodeStore>, Default<T> implements Visitor<T>   
     13 *   
     14 * For each ASTNode type, Unparser<CodeStore> overrides a method
     15 *   CodeStore visitLeave(ASTNode node, List<CodeStore> childResults);
     16 *
     17 * Unparser<CodeStore> 'getCode(ASTNode node)';
     18 * 'getCode(ASTNode node)' triggers an AST traversal that populates
     19 * a CodeStore object and returns the target code string to the caller.
     20 *
     21 * In general,
     22 *
     23 * (a) Each call to Unparser<CodeStore> visitLeave(...) creates a new CodeStore object,
     24 *     appends the code lines of all child CodeStore objects, and returns the new CodeStore
     25 *     to its parent.
     26 * 
     27 * (b) AST nodes that translate to code fragments (non-complete lines of code)
     28 *     call 'code.setResultVarName("some-value")'. Parent nodes
     29 *     call getResultVarName() on the child to retrieve code fragments.
     30 *     
     31 * (c) AST nodes that generate complete lines of code by either
     32 *
     33 *    (i) assembly of child line fragments via 'childResults.get(i).getResultVarName()',
     34 *        where 'i' references to the CodeStore of the 'ith' child
     35 *       
     36 *        or
     37 *         
     38 *    (ii) appending code line(s) via a call to code.addAll(childResults.get(i)).       
     39 *
     40 *         
     41 *  @author Ken Herdy <ksherdy at sfu dot ca>       
     42*/
    3843
    3944package compiler.codeGenerator.visitors;
     
    4247import pabloB.ast.ASTVisitor.Default;
    4348import compiler.codeGenerator.helpers.CodeStore;
    44 import compiler.symbols.SymbolTable;
    45 
     49import compiler.symbolTable.SymbolTable;
    4650
    4751abstract public class Unparser extends Default<CodeStore> {
    4852       
    4953        abstract public String getCode(ASTNode node);           
    50         abstract public String getCode(ASTNode node, int indent);
    51         abstract public String getFileExt();
    52        
     54        abstract public String getCode(ASTNode node, int indent);       
    5355       
    5456        protected SymbolTable symbolTable; // TODO - refactor / remove
Note: See TracChangeset for help on using the changeset viewer.